2323#include "log.h"
2424#include "macro.h"
2525#include "nulstr-util.h"
26+ #include "os-util.h"
2627#include "process-util.h"
2728#include "selinux-util.h"
2829#include "set.h"
@@ -890,6 +891,150 @@ static void test_condition_test_environment(void) {
890891 test_condition_test_environment_one ("EXISTINGENVVAR=" , false);
891892}
892893
894+ static void test_condition_test_os_release (void ) {
895+ _cleanup_strv_free_ char * * os_release_pairs = NULL ;
896+ _cleanup_free_ char * version_id = NULL ;
897+ const char * key_value_pair ;
898+ Condition * condition ;
899+
900+ /* Should not happen, but it's a test so we don't know the environment. */
901+ if (load_os_release_pairs (NULL , & os_release_pairs ) < 0 )
902+ return ;
903+ if (strv_length (os_release_pairs ) < 2 )
904+ return ;
905+
906+ condition = condition_new (CONDITION_OS_RELEASE , "_THISHOPEFULLYWONTEXIST=01234 56789" , false, false);
907+ assert_se (condition );
908+ assert_se (condition_test (condition , environ ) == 0 );
909+ condition_free (condition );
910+
911+ condition = condition_new (CONDITION_OS_RELEASE , "WRONG FORMAT" , false, false);
912+ assert_se (condition );
913+ assert_se (condition_test (condition , environ ) == - EINVAL );
914+ condition_free (condition );
915+
916+ condition = condition_new (CONDITION_OS_RELEASE , "WRONG!<>=FORMAT" , false, false);
917+ assert_se (condition );
918+ assert_se (condition_test (condition , environ ) == - EINVAL );
919+ condition_free (condition );
920+
921+ condition = condition_new (CONDITION_OS_RELEASE , "WRONG FORMAT=" , false, false);
922+ assert_se (condition );
923+ assert_se (condition_test (condition , environ ) == - EINVAL );
924+ condition_free (condition );
925+
926+ condition = condition_new (CONDITION_OS_RELEASE , "WRONG =FORMAT" , false, false);
927+ assert_se (condition );
928+ assert_se (condition_test (condition , environ ) == - EINVAL );
929+ condition_free (condition );
930+
931+ condition = condition_new (CONDITION_OS_RELEASE , "WRONG = FORMAT" , false, false);
932+ assert_se (condition );
933+ assert_se (condition_test (condition , environ ) == - EINVAL );
934+ condition_free (condition );
935+
936+ condition = condition_new (CONDITION_OS_RELEASE , "WRONGFORMAT= " , false, false);
937+ assert_se (condition );
938+ assert_se (condition_test (condition , environ ) == - EINVAL );
939+ condition_free (condition );
940+
941+ condition = condition_new (CONDITION_OS_RELEASE , "WRO NG=FORMAT" , false, false);
942+ assert_se (condition );
943+ assert_se (condition_test (condition , environ ) == - EINVAL );
944+ condition_free (condition );
945+
946+ condition = condition_new (CONDITION_OS_RELEASE , "" , false, false);
947+ assert_se (condition );
948+ assert_se (condition_test (condition , environ ));
949+ condition_free (condition );
950+
951+ /* load_os_release_pairs() removes quotes, we have to add them back,
952+ * otherwise we get a string: "PRETTY_NAME=Debian GNU/Linux 10 (buster)"
953+ * which is wrong, as the value is not quoted anymore. */
954+ const char * quote = strchr (os_release_pairs [1 ], ' ' ) ? "\"" : "" ;
955+ key_value_pair = strjoina (os_release_pairs [0 ], "=" , quote , os_release_pairs [1 ], quote );
956+ condition = condition_new (CONDITION_OS_RELEASE , key_value_pair , false, false);
957+ assert_se (condition );
958+ assert_se (condition_test (condition , environ ));
959+ condition_free (condition );
960+
961+ key_value_pair = strjoina (os_release_pairs [0 ], "!=" , quote , os_release_pairs [1 ], quote );
962+ condition = condition_new (CONDITION_OS_RELEASE , key_value_pair , false, false);
963+ assert_se (condition );
964+ assert_se (condition_test (condition , environ ) == 0 );
965+ condition_free (condition );
966+
967+ /* Some distros (eg: Arch) do not set VERSION_ID */
968+ if (parse_os_release (NULL , "VERSION_ID" , & version_id ) <= 0 )
969+ return ;
970+
971+ key_value_pair = strjoina ("VERSION_ID" , "=" , version_id );
972+ condition = condition_new (CONDITION_OS_RELEASE , key_value_pair , false, false);
973+ assert_se (condition );
974+ assert_se (condition_test (condition , environ ));
975+ condition_free (condition );
976+
977+ key_value_pair = strjoina ("VERSION_ID" , "!=" , version_id );
978+ condition = condition_new (CONDITION_OS_RELEASE , key_value_pair , false, false);
979+ assert_se (condition );
980+ assert_se (condition_test (condition , environ ) == 0 );
981+ condition_free (condition );
982+
983+ key_value_pair = strjoina ("VERSION_ID" , "<=" , version_id );
984+ condition = condition_new (CONDITION_OS_RELEASE , key_value_pair , false, false);
985+ assert_se (condition );
986+ assert_se (condition_test (condition , environ ));
987+ condition_free (condition );
988+
989+ key_value_pair = strjoina ("VERSION_ID" , ">=" , version_id );
990+ condition = condition_new (CONDITION_OS_RELEASE , key_value_pair , false, false);
991+ assert_se (condition );
992+ assert_se (condition_test (condition , environ ));
993+ condition_free (condition );
994+
995+ key_value_pair = strjoina ("VERSION_ID" , "<" , version_id , ".1" );
996+ condition = condition_new (CONDITION_OS_RELEASE , key_value_pair , false, false);
997+ assert_se (condition );
998+ assert_se (condition_test (condition , environ ));
999+ condition_free (condition );
1000+
1001+ key_value_pair = strjoina ("VERSION_ID" , ">" , version_id , ".1" );
1002+ condition = condition_new (CONDITION_OS_RELEASE , key_value_pair , false, false);
1003+ assert_se (condition );
1004+ assert_se (condition_test (condition , environ ) == 0 );
1005+ condition_free (condition );
1006+
1007+ key_value_pair = strjoina ("VERSION_ID" , "=" , version_id , " " , os_release_pairs [0 ], "=" , quote , os_release_pairs [1 ], quote );
1008+ condition = condition_new (CONDITION_OS_RELEASE , key_value_pair , false, false);
1009+ assert_se (condition );
1010+ assert_se (condition_test (condition , environ ));
1011+ condition_free (condition );
1012+
1013+ key_value_pair = strjoina ("VERSION_ID" , "!=" , version_id , " " , os_release_pairs [0 ], "=" , quote , os_release_pairs [1 ], quote );
1014+ condition = condition_new (CONDITION_OS_RELEASE , key_value_pair , false, false);
1015+ assert_se (condition );
1016+ assert_se (condition_test (condition , environ ) == 0 );
1017+ condition_free (condition );
1018+
1019+ key_value_pair = strjoina ("VERSION_ID" , "=" , version_id , " " , os_release_pairs [0 ], "!=" , quote , os_release_pairs [1 ], quote );
1020+ condition = condition_new (CONDITION_OS_RELEASE , key_value_pair , false, false);
1021+ assert_se (condition );
1022+ assert_se (condition_test (condition , environ ) == 0 );
1023+ condition_free (condition );
1024+
1025+ key_value_pair = strjoina ("VERSION_ID" , "!=" , version_id , " " , os_release_pairs [0 ], "!=" , quote , os_release_pairs [1 ], quote );
1026+ condition = condition_new (CONDITION_OS_RELEASE , key_value_pair , false, false);
1027+ assert_se (condition );
1028+ assert_se (condition_test (condition , environ ) == 0 );
1029+ condition_free (condition );
1030+
1031+ key_value_pair = strjoina ("VERSION_ID" , "<" , version_id , ".1" , " " , os_release_pairs [0 ], "=" , quote , os_release_pairs [1 ], quote );
1032+ condition = condition_new (CONDITION_OS_RELEASE , key_value_pair , false, false);
1033+ assert_se (condition );
1034+ assert_se (condition_test (condition , environ ));
1035+ condition_free (condition );
1036+ }
1037+
8931038int main (int argc , char * argv []) {
8941039 test_setup_logging (LOG_DEBUG );
8951040
@@ -912,6 +1057,7 @@ int main(int argc, char *argv[]) {
9121057#if defined(__i386__ ) || defined(__x86_64__ )
9131058 test_condition_test_cpufeature ();
9141059#endif
1060+ test_condition_test_os_release ();
9151061
9161062 return 0 ;
9171063}
0 commit comments