From 66f37c81534d122444669d3605a1536562eda1e2 Mon Sep 17 00:00:00 2001 From: Jan Cybulski Date: Wed, 17 Apr 2013 18:38:47 +0200 Subject: [PATCH] Add test privilege_control05_add_shared_dir_readers [Issue#] SSDWSSP-154 [Feature] Tests the functionality of a new API add_shared_dir_readers. [Verification] Successful build, running the libprivilige tests. Change-Id: I25c6c9c0f4ae072477b6177e6dd6d2d7a92e702e --- tests/libprivilege-control-tests/test_cases.cpp | 110 ++++++++++++++++++++++++ 1 file changed, 110 insertions(+) diff --git a/tests/libprivilege-control-tests/test_cases.cpp b/tests/libprivilege-control-tests/test_cases.cpp index 996c369..656b0e4 100644 --- a/tests/libprivilege-control-tests/test_cases.cpp +++ b/tests/libprivilege-control-tests/test_cases.cpp @@ -23,6 +23,7 @@ */ #include +#include #include #include #include @@ -447,6 +448,115 @@ static void read_gids(std::set &set, const char* file_path) } } +RUNNER_TEST(privilege_control05_add_shared_dir_readers) +{ + +#define TEST_OBJ "TEST_OBJECT" +#define TEST_OBJ_SOME_OTHER "TEST_OBJA" +#define test_string_01 "TEST_raz TEST_OBJECT r-x-- -----" +#define test_string_21 "TEST_trzy TEST_OBJA -wx--\n" +#define test_string_22 "TEST_trzy TEST_OBJECT r-x-- -----\n" + + int result; + int i; + int fd = -1; + char *path; + const char *app_labels[] = {"TEST_raz", "TEST_dwa", "TEST_trzy", ""}; + const int READ_BUF_SIZE = 1000; + char buf[READ_BUF_SIZE]; + FILE *file = NULL; + struct smack_accesses * rules = NULL; + + result = smack_accesses_new(&rules); + RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "Error in smack_accesses_new. Error: " << result); + + for (i = 0; i < 3; i++) { + + (void)app_uninstall(app_labels[i]); + result = app_install(app_labels[i]); + RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "Error in app_install."); + + asprintf(&path, SMACK_RULES_DIR "/%s", app_labels[i]); + fd = open(path, O_WRONLY, 0644); + RUNNER_ASSERT_MSG(fd != -1, "Error in opening file " << path); + + if (i == 1) { + result = smack_accesses_add(rules,app_labels[i],TEST_OBJ,"wt"); + RUNNER_ASSERT_MSG(result == 0, "smack_accesses_add failed"); + } + if (i == 2) { + smack_accesses_free(rules); + result = smack_accesses_new(&rules); + result = smack_accesses_add(rules,app_labels[i],TEST_OBJ_SOME_OTHER,"wx"); + RUNNER_ASSERT_MSG(result == 0, "smack_accesses_add failed"); + } + result = smack_accesses_apply(rules); + RUNNER_ASSERT_MSG(fd != -1, "smack_accesses_apply failed"); + + result = smack_accesses_save(rules, fd); + RUNNER_ASSERT_MSG(fd != -1, "smack_accesses_apply failed"); + + free(path); + close(fd); + + } + + smack_accesses_free(rules); + + // THE TEST - accesses + + result = add_shared_dir_readers(TEST_OBJ,app_labels); + RUNNER_ASSERT_MSG(result == 0, "add_shared_dir_readers failed"); + + result = smack_have_access(app_labels[0],TEST_OBJ,"rx"); + RUNNER_ASSERT_MSG(result == 1, "add_shared_dir_readers ERROR"); + + result = smack_have_access(app_labels[1],TEST_OBJ,"rx"); + RUNNER_ASSERT_MSG(result == 1, "add_shared_dir_readers ERROR"); + + result = smack_have_access(app_labels[2],TEST_OBJ,"rx"); + RUNNER_ASSERT_MSG(result == 1, "add_shared_dir_readers ERROR"); + + result = smack_have_access(app_labels[1],TEST_OBJ,"rwxt"); + RUNNER_ASSERT_MSG(result == 1, "add_shared_dir_readers ERROR"); + + result = smack_have_access(app_labels[2],TEST_OBJ_SOME_OTHER,"wx"); + RUNNER_ASSERT_MSG(result == 1, "add_shared_dir_readers ERROR"); + + + //TEST the operations on empty files + + asprintf(&path, SMACK_RULES_DIR "/%s", app_labels[0]); + file = fopen(path, "r"); + + RUNNER_ASSERT_MSG(file, "fopen failed, errno:" << errno); + + fgets(buf, READ_BUF_SIZE, file); + result = strcmp(buf, test_string_01); + RUNNER_ASSERT_MSG( result!=0, "add_shared_dir_readers ERROR, file not formatted" << path ); + + free(path); + fclose(file); + + //TEST the operations on non empty files + asprintf(&path, SMACK_RULES_DIR "/%s", app_labels[2]); + file = NULL; + file = fopen(path, "r"); + RUNNER_ASSERT_MSG(file, "fopen failed, errno:" << errno); + + fgets(buf, READ_BUF_SIZE, file); + result = strcmp(buf, test_string_21); + RUNNER_ASSERT_MSG( result==0, "add_shared_dir_readers ERROR, file not formatted" ); + + fgets(buf, READ_BUF_SIZE, file); + result = strcmp(buf, test_string_22); + RUNNER_ASSERT_MSG( result==0, "add_shared_dir_readers ERROR, file not formatted" ); + + free(path); + fclose(file); +} + + /** * Set APP privileges. */ -- 2.7.4