Modify, systematize and reorder tests related the following libprivilege-control...
authorRafal Krypa <r.krypa@samsung.com>
Wed, 17 Apr 2013 15:59:36 +0000 (17:59 +0200)
committerMarcin Niesluchowski <m.niesluchow@samsung.com>
Thu, 23 Jan 2014 13:19:16 +0000 (14:19 +0100)
- app_install
- app_add_permissions
- app_revoke_permissions
- set_app_privilege
- app_uninstall

They now depend on each other and use the same app_id. Current tests conform
to latest libprivilege-control requirements and design.

Change-Id: I13f675b8d8840d9cb6d0ee538e33409b025c7648

packaging/security-tests.spec
tests/libprivilege-control-tests/CMakeLists.txt
tests/libprivilege-control-tests/test_cases.cpp
tests/libprivilege-control-tests/test_privilege_control_rules.dac [new file with mode: 0644]

index fb9024c..cfc613c 100644 (file)
@@ -57,5 +57,5 @@ echo "security-tests postinst done ..."
 /usr/bin/security-server-tests-password
 /etc/smack/test_smack_rules
 /etc/smack/test_smack_rules_lnk
-/usr/share/privilege-control/test_privilege_control_rules.smack
+/usr/share/privilege-control/*
 /etc/smack/test_privilege_control_DIR/*
index 565037b..a6507fc 100644 (file)
@@ -67,13 +67,11 @@ INSTALL(TARGETS ${LPC_TARGET_TEST}
 INSTALL(FILES
     ${PROJECT_SOURCE_DIR}/tests/libprivilege-control-tests/test_privilege_control_rules.smack
     DESTINATION /usr/share/privilege-control/
-    PERMISSIONS
-    OWNER_READ
-    OWNER_EXECUTE
-    GROUP_READ
-    GROUP_EXECUTE
-    WORLD_READ
-    WORLD_EXECUTE
+  )
+
+INSTALL(FILES
+    ${PROJECT_SOURCE_DIR}/tests/libprivilege-control-tests/test_privilege_control_rules.dac
+    DESTINATION /usr/share/privilege-control/
   )
 
 INSTALL(DIRECTORY
index ac0b609..996c369 100644 (file)
 #define SMACK_LOAD2 "/smack/load2"
 #define TEST_APP_DIR "/etc/smack/test_privilege_control_DIR/app_dir"
 #define TEST_NON_APP_DIR "/etc/smack/test_privilege_control_DIR/non_app_dir"
-#define APPID_ADD  "test_APP_ID_add"
-#define APPID_REVOKE  "test_APP_ID_revoke"
 #define APPID_DIR  "test_APP_ID_dir"
 #define APPID_SHARED_DIR  "test_APP_ID_shared_dir"
 #define CANARY_LABEL "tiny_yellow_canary"
 
-#define APP_SET_PRIV  "test_APP"
+#define APP_ID  "test_APP"
 #define APP_SET_PRIV_PATH "/etc/smack/test_privilege_control_DIR/test_set_app_privilege/test_APP"
 #define APP_SET_PRIV_PATH_REAL "/etc/smack/test_privilege_control_DIR/test_set_app_privilege/test_APP_REAL"
 
 const char *PRIVS[] = { "WRT", "test_privilege_control_rules", NULL };
 
+#define LIBPRIVILEGE_APP_GROUP_LIST "/usr/share/privilege-control/app_group_list"
+#define LIBPRIVILEGE_TEST_DAC_FILE "/usr/share/privilege-control/test_privilege_control_rules.dac"
+
 #define APP_TEST_APP_1 "test-application1"
 #define APP_TEST_APP_2 "test-application_2"
 #define APP_TEST_APP_3 "test-app-3"
@@ -75,40 +76,23 @@ const char *PRIVS[] = { "WRT", "test_privilege_control_rules", NULL };
 #define FTW_MAX_FDS 16
 
 // Rules from test_privilege_control_rules.smack
-const std::vector< std::vector<std::string> > rulesAdd = {
-        { APPID_ADD, "test_book_1", "r" },
-        { APPID_ADD, "test_book_2", "w" },
-        { APPID_ADD, "test_book_3", "x" },
-        { APPID_ADD, "test_book_4", "rw" },
-        { APPID_ADD, "test_book_5", "rx" },
-        { APPID_ADD, "test_book_6", "wx" },
-        { APPID_ADD, "test_book_7", "rwx" },
-        { "test_subject_1", APPID_ADD, "r" },
-        { "test_subject_2", APPID_ADD, "w" },
-        { "test_subject_3", APPID_ADD, "x" },
-        { "test_subject_4", APPID_ADD, "rw" },
-        { "test_subject_5", APPID_ADD, "rx" },
-        { "test_subject_6", APPID_ADD, "wx" },
-        { "test_subject_7", APPID_ADD, "rwx" },
-        { APPID_ADD, APPID_SHARED_DIR, "rwxat"}};
-
+const std::vector< std::vector<std::string> > rules = {
+        { APP_ID, "test_book_1", "r" },
+        { APP_ID, "test_book_2", "w" },
+        { APP_ID, "test_book_3", "x" },
+        { APP_ID, "test_book_4", "rw" },
+        { APP_ID, "test_book_5", "rx" },
+        { APP_ID, "test_book_6", "wx" },
+        { APP_ID, "test_book_7", "rwx" },
+        { "test_subject_1", APP_ID, "r" },
+        { "test_subject_2", APP_ID, "w" },
+        { "test_subject_3", APP_ID, "x" },
+        { "test_subject_4", APP_ID, "rw" },
+        { "test_subject_5", APP_ID, "rx" },
+        { "test_subject_6", APP_ID, "wx" },
+        { "test_subject_7", APP_ID, "rwx" },
+        { APP_ID, APPID_SHARED_DIR, "rwxat"}};
 
-// Rules from test_privilege_control_rules.smack
-const std::vector< std::vector<std::string> > rulesRevoke = {
-        { APPID_REVOKE, "test_book_1", "r" },
-        { APPID_REVOKE, "test_book_2", "w" },
-        { APPID_REVOKE, "test_book_3", "x" },
-        { APPID_REVOKE, "test_book_4", "rw" },
-        { APPID_REVOKE, "test_book_5", "rx" },
-        { APPID_REVOKE, "test_book_6", "wx" },
-        { APPID_REVOKE, "test_book_7", "rwx" },
-        { "test_subject_1", APPID_REVOKE, "r" },
-        { "test_subject_2", APPID_REVOKE, "w" },
-        { "test_subject_3", APPID_REVOKE, "x" },
-        { "test_subject_4", APPID_REVOKE, "rw" },
-        { "test_subject_5", APPID_REVOKE, "rx" },
-        { "test_subject_6", APPID_REVOKE, "wx" },
-        { "test_subject_7", APPID_REVOKE, "rwx" }};
 
 namespace {
 
@@ -357,7 +341,7 @@ int cleaning_smack_database_files (void)
 /**
  * Test setting labels for all files and folders in given path.
  */
-RUNNER_TEST(privilege_control01_app_label_dir)
+RUNNER_TEST(privilege_control02_app_label_dir)
 {
     int result;
 
@@ -377,7 +361,7 @@ RUNNER_TEST(privilege_control01_app_label_dir)
     RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for non-app dir");
 }
 
-RUNNER_TEST(privilege_control02_app_label_shared_dir)
+RUNNER_TEST(privilege_control03_app_label_shared_dir)
 {
     int result;
 
@@ -387,7 +371,7 @@ RUNNER_TEST(privilege_control02_app_label_shared_dir)
     result = nftw(TEST_NON_APP_DIR, &nftw_set_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
     RUNNER_ASSERT_MSG(result == 0, "Unable to clean up Smack labels in " << TEST_NON_APP_DIR);
 
-    result = app_label_shared_dir(APPID_ADD, APPID_SHARED_DIR, TEST_APP_DIR);
+    result = app_label_shared_dir(APP_ID, APPID_SHARED_DIR, TEST_APP_DIR);
     RUNNER_ASSERT_MSG(result == 0, "app_label_shared_dir() failed");
 
     result = nftw(TEST_APP_DIR, &nftw_check_labels_app_shared_dir, FTW_MAX_FDS, FTW_PHYS);
@@ -401,18 +385,18 @@ RUNNER_TEST(privilege_control02_app_label_shared_dir)
 /**
  * Add permisions from  test_privilege_control_rules template
  */
-RUNNER_TEST(privilege_control03_add_permissions)
+RUNNER_TEST(privilege_control04_add_permissions)
 {
-    int result = app_add_permissions(APPID_ADD, PRIVS);
+    int result = app_add_permissions(APP_ID, PRIVS);
     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
             " Error adding app permissions. Errno: " << result);
 
     // Check if the accesses are realy applied..
-    result = test_have_all_accesses(rulesAdd);
+    result = test_have_all_accesses(rules);
     RUNNER_ASSERT_MSG(result==1, "Permissions not added.");
 
     //// File exists?
-    FILE *pFile = fopen(SMACK_RULES_DIR APPID_ADD, "rb");
+    FILE *pFile = fopen(SMACK_RULES_DIR APP_ID, "rb");
     RUNNER_ASSERT_MSG(pFile != NULL,
             "SMACK file NOT created!. Errno: " << errno);
 
@@ -422,11 +406,6 @@ RUNNER_TEST(privilege_control03_add_permissions)
     RUNNER_ASSERT_MSG(smack_file_length>0,
             "SMACK file empty, but privileges list was not empty.. Errno: " << errno);
 
-    // Clean up
-    result = app_revoke_permissions(APPID_ADD);
-    RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
-            "Error revoking app permissions. Errno: " << result);
-
     if (pFile != NULL)
         fclose(pFile);
 
@@ -435,34 +414,37 @@ RUNNER_TEST(privilege_control03_add_permissions)
 /**
  * Revoke permissions from the list. Should be executed as privileged user.
  */
-RUNNER_CHILD_TEST(privilege_control04_revoke_permissions)
+RUNNER_CHILD_TEST(privilege_control06_revoke_permissions)
 {
     int result;
-
-    // Prepare permissions that we want to revoke
-    result = app_add_permissions(APPID_REVOKE, PRIVS);
-    RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
-            " Error adding app permissions. Errno: " << result);
+    char* path = NULL;
+    int fd;
 
     // Revoke permissions
-    result = app_revoke_permissions(APPID_REVOKE);
+    result = app_revoke_permissions(APP_ID);
     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
             "Error revoking app permissions. Errno: " << result);
 
     // Are all the permissions revoked?
-    result = test_have_all_accesses(rulesRevoke);
+    result = test_have_all_accesses(rules);
     RUNNER_ASSERT_MSG(result!=1, "Not all permisions revoked.");
 
-    //// Is the smack file deleted?
-    FILE *pFile = fopen(SMACK_RULES_DIR APPID_REVOKE, "rb");
-    RUNNER_ASSERT_MSG(pFile == NULL,
-            "SMACK file NOT deleted after revoking ALL privileges. Errno: " << errno);
-
-    RUNNER_ASSERT_MSG(errno == ENOENT,
-            "SMACK file NOT deleted after revoking ALL privileges. Errno: " << errno);
+    smack_file_name(APP_ID, &path);
+    fd = open(path, O_RDONLY);
+    RUNNER_ASSERT_MSG(fd >= 0, "SMACK file deleted after app_revoke_permissions");
+    RUNNER_ASSERT_MSG(lseek(fd, 0, SEEK_END) == 0, "SMACK file not empty after app_revoke_permissions");
+    free(path);
+    close(fd);
+}
 
-    if (pFile != NULL)
-        fclose(pFile);
+static void read_gids(std::set<unsigned> &set, const char* file_path)
+{
+       FILE *f = fopen(file_path, "r");
+       RUNNER_ASSERT_MSG(f != NULL, "Unable to open file " << file_path);
+       unsigned gid;
+       while (fscanf(f, "%u\n", &gid) == 1) {
+               set.insert(gid);
+       }
 }
 
 /**
@@ -471,15 +453,13 @@ RUNNER_CHILD_TEST(privilege_control04_revoke_permissions)
 RUNNER_CHILD_TEST(privilege_control05_set_app_privilege)
 {
     int result;
-    char* labelApp = "test_pc_label";
-    char* labelAppSymlink = "test_pc_label_symlink";
 
     // Preset exec label
-    smack_lsetlabel(APP_SET_PRIV_PATH_REAL, labelApp, SMACK_LABEL_EXEC);
-    smack_lsetlabel(APP_SET_PRIV_PATH, labelAppSymlink, SMACK_LABEL_EXEC);
+    smack_lsetlabel(APP_SET_PRIV_PATH_REAL, APP_ID, SMACK_LABEL_EXEC);
+    smack_lsetlabel(APP_SET_PRIV_PATH, APP_ID "_symlink", SMACK_LABEL_EXEC);
 
     // Set APP privileges
-    result = set_app_privilege(APP_SET_PRIV, NULL, APP_SET_PRIV_PATH);
+    result = set_app_privilege(APP_ID, NULL, APP_SET_PRIV_PATH);
     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "Error in set_app_privilege. Error: " << result);
 
     // Check if SMACK label really set
@@ -487,7 +467,7 @@ RUNNER_CHILD_TEST(privilege_control05_set_app_privilege)
     result = smack_new_label_from_self(&label);
     RUNNER_ASSERT_MSG(result == 0, "Error getting current process label");
     RUNNER_ASSERT_MSG(label != NULL, "Process label is not set");
-    result = strcmp(labelApp, label);
+    result = strcmp(APP_ID, label);
     RUNNER_ASSERT_MSG(result == 0, "Process label " << label << " is incorrect");
 
     // Check if DAC privileges really set
@@ -499,9 +479,32 @@ RUNNER_CHILD_TEST(privilege_control05_set_app_privilege)
 
     result = strcmp(getenv("USER"), APP_USER_NAME);
     RUNNER_ASSERT_MSG(result == 0, "Wrong user USER NAME");
+
+    std::set<unsigned> groups_check;
+    read_gids(groups_check, LIBPRIVILEGE_APP_GROUP_LIST);
+    read_gids(groups_check, LIBPRIVILEGE_TEST_DAC_FILE);
+
+    int groups_cnt = getgroups(0, NULL);
+    RUNNER_ASSERT_MSG(groups_cnt > 0, "Wrong number of supplementary groupsCnt");
+    gid_t *groups_list = (gid_t *) calloc(groups_cnt, sizeof(gid_t));
+    RUNNER_ASSERT_MSG(groups_list != NULL, "Memory allocation failed");
+    getgroups(groups_cnt, groups_list);
+
+    for (int i = 0; i < groups_cnt; ++i) {
+        if (groups_check.erase(groups_list[i]) == 0) {
+            // getgroups() may also return process' main group
+            if (groups_list[i] == getgid())
+                RUNNER_ASSERT_MSG(false, "Application belongs to unknown group (GID=" << groups_list[i] << ")");
+        }
+    }
+    std::string groups_left;
+    for (std::set<unsigned>::iterator it = groups_check.begin(); it != groups_check.end(); it++) {
+        groups_left.append(std::to_string(*it)).append(" ");
+    }
+    RUNNER_ASSERT_MSG(groups_check.empty(), "Application doesn't belong to some required groups: " << groups_left);
 }
 
-RUNNER_TEST(privilege_control06_app_give_access)
+RUNNER_TEST(privilege_control08_app_give_access)
 {
     const char *subject = "lkjq345v34sfa";
     const char *object = "lk9290f92lkjz";
@@ -534,7 +537,7 @@ RUNNER_TEST(privilege_control06_app_give_access)
 /**
  * Add new API feature
  */
-RUNNER_TEST(privilege_control07_add_api_feature)
+RUNNER_TEST(privilege_control09_add_api_feature)
 {
     int result;
 
@@ -677,38 +680,55 @@ RUNNER_TEST(privilege_control07_add_api_feature)
 /*
  * Check app_install function
  */
-RUNNER_TEST(privilege_control08_app_install)
+RUNNER_TEST(privilege_control01_app_install)
 {
     int result;
     char *path = NULL;
     int fd = -1;
 
-    // cleaning
-    cleaning_smack_app_files();
+    smack_file_name(APP_ID, &path);
+    unlink(path);
 
-    result = app_install(APP_TEST_APP_1);
+    result = app_install(APP_ID);
     RUNNER_ASSERT_MSG(result == 0, "app_install returned " << result <<". Errno: " << strerror(errno));
 
     // checking if file really exists
-    smack_file_name(APP_TEST_APP_1, &path);
     fd = open(path, O_RDONLY);
     RUNNER_ASSERT_MSG(fd >= 0, "File open failed: " << path << " : " << result << ". Errno: " << strerror(errno));
     close(fd);
     free(path);
 
     // try install second time app with the same ID - it should failed with -1 (Errno: File exists).
-    result = app_install(APP_TEST_APP_1);
+    result = app_install(APP_ID);
     RUNNER_ASSERT_MSG(result == -1, "app_install returned " << result <<". Errno: " << strerror(errno));
+}
 
-    // cleaning
-    cleaning_smack_app_files();
+/*
+ * Check app_install function
+ */
+RUNNER_TEST(privilege_control07_app_uninstall)
+{
+    int result;
+    char *path = NULL;
+    int fd = -1;
+
+    smack_file_name(APP_ID, &path);
+    result = app_uninstall(APP_ID);
+    RUNNER_ASSERT_MSG(result == 0, "app_uninstall returned " << result <<". Errno: " << strerror(errno));
+
+    // checking if file really exists
+    smack_file_name(APP_ID, &path);
+    fd = open(path, O_RDONLY);
+    RUNNER_ASSERT_MSG(fd == -1, "SMACK file NOT deleted after app_uninstall");
+    close(fd);
+    free(path);
 }
 
 /*
  * Check app_register_av function
  * Notice that this test case may have no sense if previous would fail (privilege_control06_app_install)
  */
-RUNNER_TEST(privilege_control09_app_register_av)
+RUNNER_TEST(privilege_control10_app_register_av)
 {
     int result;
     //FILE* file_av = NULL;
diff --git a/tests/libprivilege-control-tests/test_privilege_control_rules.dac b/tests/libprivilege-control-tests/test_privilege_control_rules.dac
new file mode 100644 (file)
index 0000000..eb6ed40
--- /dev/null
@@ -0,0 +1,2 @@
+12345
+23456