Add API app_register_av and app_install function test cases. Change tests that drops...
authorJanusz Kozerski <j.kozerski@samsung.com>
Mon, 8 Apr 2013 15:44:13 +0000 (17:44 +0200)
committerMarcin Niesluchowski <m.niesluchow@samsung.com>
Thu, 23 Jan 2014 13:19:16 +0000 (14:19 +0100)
[Issue#]       SSDWSSP-176
[Feature]      2 new testcases.
[Cause]        N/A
[Solution]     N/A
[Verification] Needs commit: http://slp-info.sec.samsung.net/gerrit/#/c/178794/

Change-Id: Ie727f0ce172aeced6cde1d473220452e3c5b1975

tests/libprivilege-control-tests/test_cases.cpp

index ee94927..ac0b609 100644 (file)
@@ -42,6 +42,7 @@
 
 
 #define SMACK_RULES_DIR  "/etc/smack/accesses.d/"
+#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"
 
 const char *PRIVS[] = { "WRT", "test_privilege_control_rules", NULL };
 
+#define APP_TEST_APP_1 "test-application1"
+#define APP_TEST_APP_2 "test-application_2"
+#define APP_TEST_APP_3 "test-app-3"
+#define APP_TEST_AV_1 "test-antivirus1"
+#define APP_TEST_AV_2 "test-antivirus_2"
+#define APP_TEST_AV_3 "test-av-3"
+#define SMACK_APPS_LABELS_DATABASE "/opt/dbspace/.privilege_control_all_apps_id.db"
+#define SMACK_AVS_LABELS_DATABASE "/opt/dbspace/.privilege_control_all_avs_id.db"
+
 #define APP_GID 5000
 #define APP_UID 5000
 #define APP_USER_NAME "app"
@@ -280,6 +290,68 @@ void remove_smack_files()
     unlink(OTHER_BLAHBLAH);
 }
 
+int smack_file_name(const char* app_id, char** path)
+{
+    if (asprintf(path, SMACK_RULES_DIR "/%s", app_id) == -1) {
+        RUNNER_ASSERT_MSG(false, "asprint failed");
+        *path = NULL;
+    }
+
+    return 0;
+}
+
+int cleaning_smack_app_files (void)
+{
+    char *path = NULL;
+    int fd = -1;
+
+    smack_file_name(APP_TEST_APP_1, &path);
+    unlink(path);
+    free(path);
+
+    smack_file_name(APP_TEST_APP_2, &path);
+    unlink(path);
+    free(path);
+
+    smack_file_name(APP_TEST_APP_3, &path);
+    unlink(path);
+    free(path);
+
+    smack_file_name(APP_TEST_AV_1, &path);
+    unlink(path);
+    free(path);
+
+    smack_file_name(APP_TEST_AV_2, &path);
+    unlink(path);
+    free(path);
+
+    smack_file_name(APP_TEST_AV_3, &path);
+    unlink(path);
+    free(path);
+
+    return 0;
+}
+
+int cleaning_smack_database_files (void)
+{
+    int fd = -1;
+
+    //clean app database
+    unlink(SMACK_APPS_LABELS_DATABASE);
+    fd = open(SMACK_APPS_LABELS_DATABASE, O_RDWR | O_EXCL | O_CREAT, 0644);
+    if (fd == -1) {
+        return -1;
+    }
+
+    //clean av database
+    unlink(SMACK_AVS_LABELS_DATABASE);
+    fd = open(SMACK_AVS_LABELS_DATABASE, O_RDWR | O_EXCL | O_CREAT, 0644);
+    if (fd == -1) {
+        return -1;
+    }
+
+    return 0;
+}
 } // namespace
 
 /**
@@ -363,7 +435,7 @@ RUNNER_TEST(privilege_control03_add_permissions)
 /**
  * Revoke permissions from the list. Should be executed as privileged user.
  */
-RUNNER_TEST(privilege_control04_revoke_permissions)
+RUNNER_CHILD_TEST(privilege_control04_revoke_permissions)
 {
     int result;
 
@@ -601,3 +673,116 @@ RUNNER_TEST(privilege_control07_add_api_feature)
 
     // TODO database group ids
 }
+
+/*
+ * Check app_install function
+ */
+RUNNER_TEST(privilege_control08_app_install)
+{
+    int result;
+    char *path = NULL;
+    int fd = -1;
+
+    // cleaning
+    cleaning_smack_app_files();
+
+    result = app_install(APP_TEST_APP_1);
+    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);
+    RUNNER_ASSERT_MSG(result == -1, "app_install returned " << result <<". Errno: " << strerror(errno));
+
+    // cleaning
+    cleaning_smack_app_files();
+}
+
+/*
+ * 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)
+{
+    int result;
+    //FILE* file_av = NULL;
+    //FILE* file_app = NULL;
+    //int fd_app = -1;
+    int fd  = -1;
+    char *path = NULL;
+    char *buff;
+    int len;
+    int i;
+    //char label1[SMACK_LABEL_LEN +1];
+    //char label2[SMACK_LABEL_LEN +1];
+    //char acces_rights[6 +1];
+    //char row[2 * SMACK_LABEL_LEN + 20] //
+    const char* correct_antivirus1_rules = "test-antivirus1 test-application1 rwx--\n"
+                                            "test-antivirus1 test-application_2 rwx--\n"
+                                            "test-antivirus1 test-app-3 rwx--";
+    const char* correct_antivirus2_rules = "test-antivirus_2 test-application1 rwx--\n"
+                                           "test-antivirus_2 test-application_2 rwx--\n"
+                                           "test-antivirus_2 test-app-3 rwx--";
+
+    // cleaning
+    cleaning_smack_app_files();
+    cleaning_smack_database_files();
+
+    result = app_install(APP_TEST_APP_1);
+    RUNNER_ASSERT_MSG(result == 0, "app_install returned " << result <<". Errno: " << strerror(errno));
+
+    result = app_install(APP_TEST_APP_2);
+    RUNNER_ASSERT_MSG(result == 0, "app_install returned " << result <<". Errno: " << strerror(errno));
+
+    result = app_register_av(APP_TEST_AV_1);
+    RUNNER_ASSERT_MSG(result == 0, "app_register_av returned " << result <<". Errno: " << strerror(errno));
+
+    result = app_install(APP_TEST_APP_3);
+    RUNNER_ASSERT_MSG(result == 0, "app_install returned " << result <<". Errno: " << strerror(errno));
+
+    result = app_register_av(APP_TEST_AV_2);
+    RUNNER_ASSERT_MSG(result == 0, "app_register_av returned " << result <<". Errno: " << strerror(errno));
+
+    // checking rules for anti virus 1
+    // compare between file in /etc/smack/access.d/ and correct value (correct_antivirus1_rules).
+    len = strlen(correct_antivirus1_rules);
+    buff = (char *) malloc((len+1) * sizeof(char));
+    smack_file_name(APP_TEST_AV_1, &path);
+    fd = open(path, O_RDONLY);
+    free(path);
+    RUNNER_ASSERT_MSG(fd > -1, "file open failed " << result <<". Errno: " << strerror(errno));
+    result = read(fd, buff, len);
+    close(fd);
+    fd = -1;
+    buff[len] = '\0';
+    RUNNER_ASSERT_MSG(result > -1, "read from file descriptor failed. Errno: " << strerror(errno));
+    result = strncmp(buff, correct_antivirus1_rules, len);
+    RUNNER_ASSERT_MSG(result == 0, "Rules do not match: " << result << "\n\"" << buff << "\"\n\"" << correct_antivirus1_rules << "\"\n" << len);
+    free(buff);
+
+    // checking rules for anti virus 2
+    len = strlen(correct_antivirus2_rules);
+    buff = (char *) malloc((len+1) * sizeof(char));
+    smack_file_name(APP_TEST_AV_2, &path);
+    fd = open(path, O_RDONLY);
+    free (path);
+    RUNNER_ASSERT_MSG(fd > -1, "file open failed " << result <<". Errno: " << strerror(errno));
+    result = read(fd, buff, len);
+    close (fd);
+    fd = -1;
+    buff[len] = '\0';
+    RUNNER_ASSERT_MSG(result > -1, "read from file descriptor failed. Errno: " << strerror(errno));
+    result = strncmp(buff, correct_antivirus2_rules, len);
+    RUNNER_ASSERT_MSG(result == 0, "Rules do not match: " << result << "\n\"" << buff << "\"\n\"" << correct_antivirus1_rules << "\"\n" << len);
+    free(buff);
+
+    // cleaning
+    cleaning_smack_app_files();
+    cleaning_smack_database_files();
+}