+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);
+}
+
+