2 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * @file test_cases.cpp
19 * @author Jan Olszak (j.olszak@samsung.com)
20 * @author Rafal Krypa (r.krypa@samsung.com)
22 * @brief libprivilege-control test runer
33 #include <dpl/test/test_runner.h>
34 #include <dpl/test/test_runner_child.h>
35 #include <dpl/log/log.h>
36 #include <sys/types.h>
39 #include <sys/xattr.h>
40 #include <sys/smack.h>
41 #include <privilege-control.h>
45 #define SMACK_RULES_DIR "/etc/smack/accesses.d/"
46 #define SMACK_LOAD2 "/smack/load2"
47 #define TEST_APP_DIR "/etc/smack/test_privilege_control_DIR/app_dir"
48 #define TEST_NON_APP_DIR "/etc/smack/test_privilege_control_DIR/non_app_dir"
49 #define APPID_DIR "test_APP_ID_dir"
50 #define APPID_SHARED_DIR "test_APP_ID_shared_dir"
51 #define CANARY_LABEL "tiny_yellow_canary"
53 #define APP_ID "test_APP"
54 #define APP_SET_PRIV_PATH "/etc/smack/test_privilege_control_DIR/test_set_app_privilege/test_APP"
55 #define APP_SET_PRIV_PATH_REAL "/etc/smack/test_privilege_control_DIR/test_set_app_privilege/test_APP_REAL"
57 const char *PRIVS[] = { "WRT", "test_privilege_control_rules", NULL };
59 #define LIBPRIVILEGE_APP_GROUP_LIST "/usr/share/privilege-control/app_group_list"
60 #define LIBPRIVILEGE_TEST_DAC_FILE "/usr/share/privilege-control/test_privilege_control_rules.dac"
62 #define APP_TEST_APP_1 "test-application1"
63 #define APP_TEST_APP_2 "test-application_2"
64 #define APP_TEST_APP_3 "test-app-3"
65 #define APP_TEST_AV_1 "test-antivirus1"
66 #define APP_TEST_AV_2 "test-antivirus_2"
67 #define APP_TEST_AV_3 "test-av-3"
68 #define SMACK_APPS_LABELS_DATABASE "/opt/dbspace/.privilege_control_all_apps_id.db"
69 #define SMACK_AVS_LABELS_DATABASE "/opt/dbspace/.privilege_control_all_avs_id.db"
73 #define APP_USER_NAME "app"
74 #define APP_HOME_DIR "/opt/home/app"
76 // How many open file descriptors should ftw() function use?
77 #define FTW_MAX_FDS 16
79 // Rules from test_privilege_control_rules.smack
80 const std::vector< std::vector<std::string> > rules = {
81 { APP_ID, "test_book_1", "r" },
82 { APP_ID, "test_book_2", "w" },
83 { APP_ID, "test_book_3", "x" },
84 { APP_ID, "test_book_4", "rw" },
85 { APP_ID, "test_book_5", "rx" },
86 { APP_ID, "test_book_6", "wx" },
87 { APP_ID, "test_book_7", "rwx" },
88 { "test_subject_1", APP_ID, "r" },
89 { "test_subject_2", APP_ID, "w" },
90 { "test_subject_3", APP_ID, "x" },
91 { "test_subject_4", APP_ID, "rw" },
92 { "test_subject_5", APP_ID, "rx" },
93 { "test_subject_6", APP_ID, "wx" },
94 { "test_subject_7", APP_ID, "rwx" },
95 { APP_ID, APPID_SHARED_DIR, "rwxat"}};
100 const char* OSP_BLAHBLAH = "/usr/share/privilege-control/OSP_blahblah.smack";
101 const char* WRT_BLAHBLAH = "/usr/share/privilege-control/WGT_blahblah.smack";
102 const char* OTHER_BLAHBLAH = "/usr/share/privilege-control/blahblah.smack";
103 const char* BLAHBLAH_FEATURE = "http://feature/blah/blahblah";
106 * Check if every rule is true.
107 * @return 1 if ALL rules in SMACK, 0 if ANY rule isn't
109 int test_have_all_accesses(const std::vector< std::vector<std::string> >& rules){
111 for(uint i =0; i<rules.size();++i ){
112 result = smack_have_access(rules[i][0].c_str(),rules[i][1].c_str(),rules[i][2].c_str());
120 * Check if every rule is true.
121 * @return 1 if ANY rule in SMACK, 0 if
123 int test_have_any_accesses(const std::vector< std::vector<std::string> >& rules){
125 for(uint i =0; i<rules.size();++i ){
126 result = smack_have_access(rules[i][0].c_str(),rules[i][1].c_str(),rules[i][2].c_str());
133 RUNNER_TEST_GROUP_INIT(libprivilegecontrol)
135 int nftw_remove_labels(const char *fpath, const struct stat *sb,
136 int typeflag, struct FTW *ftwbuf)
138 smack_lsetlabel(fpath, NULL, SMACK_LABEL_ACCESS);
139 smack_lsetlabel(fpath, NULL, SMACK_LABEL_EXEC);
140 smack_lsetlabel(fpath, NULL, SMACK_LABEL_TRANSMUTE);
145 int nftw_set_labels_non_app_dir(const char *fpath, const struct stat *sb,
146 int typeflag, struct FTW *ftwbuf)
148 smack_lsetlabel(fpath, CANARY_LABEL, SMACK_LABEL_ACCESS);
149 smack_lsetlabel(fpath, CANARY_LABEL, SMACK_LABEL_EXEC);
150 smack_lsetlabel(fpath, NULL, SMACK_LABEL_TRANSMUTE);
155 int nftw_check_labels_non_app_dir(const char *fpath, const struct stat *sb,
156 int typeflag, struct FTW *ftwbuf)
162 result = smack_lgetlabel(fpath, &label, SMACK_LABEL_ACCESS);
163 RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
164 result = strcmp(CANARY_LABEL, label);
165 RUNNER_ASSERT_MSG(result == 0, "ACCESS label on " << fpath << " is overwritten");
168 result = smack_lgetlabel(fpath, &label, SMACK_LABEL_EXEC);
169 RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
170 result = strcmp(CANARY_LABEL, label);
171 RUNNER_ASSERT_MSG(result == 0, "EXEC label on " << fpath << " is overwritten");
174 result = smack_lgetlabel(fpath, &label, SMACK_LABEL_TRANSMUTE);
175 RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
176 RUNNER_ASSERT_MSG(label == NULL, "TRANSMUTE label on " << fpath << " is set");
181 int nftw_check_labels_app_dir(const char *fpath, const struct stat *sb,
182 int typeflag, struct FTW *ftwbuf)
188 result = smack_lgetlabel(fpath, &label, SMACK_LABEL_ACCESS);
189 RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
190 RUNNER_ASSERT_MSG(label != NULL, "ACCESS label on " << fpath << " is not set");
191 result = strcmp(APPID_DIR, label);
192 RUNNER_ASSERT_MSG(result == 0, "ACCESS label on " << fpath << " is incorrect");
195 result = smack_lgetlabel(fpath, &label, SMACK_LABEL_EXEC);
196 RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
197 if (S_ISREG(sb->st_mode) && (sb->st_mode & S_IXUSR)) {
198 RUNNER_ASSERT_MSG(label != NULL, "EXEC label on " << fpath << " is not set");
199 result = strcmp(APPID_DIR, label);
200 RUNNER_ASSERT_MSG(result == 0, "EXEC label on executable file " << fpath << " is incorrect");
202 RUNNER_ASSERT_MSG(label == NULL, "EXEC label on " << fpath << " is set");
205 result = smack_lgetlabel(fpath, &label, SMACK_LABEL_TRANSMUTE);
206 RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
207 RUNNER_ASSERT_MSG(label == NULL, "TRANSMUTE label on " << fpath << " is set");
212 int nftw_check_labels_app_shared_dir(const char *fpath, const struct stat *sb,
213 int typeflag, struct FTW *ftwbuf)
219 result = smack_lgetlabel(fpath, &label, SMACK_LABEL_ACCESS);
220 RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
221 RUNNER_ASSERT_MSG(label != NULL, "ACCESS label on " << fpath << " is not set");
222 result = strcmp(APPID_SHARED_DIR, label);
223 RUNNER_ASSERT_MSG(result == 0, "ACCESS label on " << fpath << " is incorrect");
226 result = smack_lgetlabel(fpath, &label, SMACK_LABEL_EXEC);
227 RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
228 RUNNER_ASSERT_MSG(label == NULL, "EXEC label on " << fpath << " is set");
231 result = smack_lgetlabel(fpath, &label, SMACK_LABEL_TRANSMUTE);
232 RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
233 if (S_ISDIR(sb->st_mode)) {
234 RUNNER_ASSERT_MSG(label != NULL, "TRANSMUTE label on " << fpath << " is not set");
235 result = strcmp("TRUE", label);
236 RUNNER_ASSERT_MSG(result == 0, "TRANSMUTE label on " << fpath << " is not set");
238 RUNNER_ASSERT_MSG(label == NULL, "TRANSMUTE label on " << fpath << " is set");
243 int file_exists(const char* path)
245 FILE* file = fopen(path, "r");
253 void osp_blahblah_check(int line_no, const std::vector<std::string>& rules)
255 std::ifstream smack_file(OSP_BLAHBLAH);
256 RUNNER_ASSERT_MSG(smack_file, "Line: " << line_no << " Failed to create " << OSP_BLAHBLAH);
258 auto it = rules.begin();
260 while(std::getline(smack_file,line)) {
261 RUNNER_ASSERT_MSG(it != rules.end(), "Line: " << line_no << "Additional line in file: " << line);
262 RUNNER_ASSERT_MSG(*it == line, "Line: " << line_no << " " << *it << "!=" << line);
266 RUNNER_ASSERT_MSG(it == rules.end(), "Line: " << line_no << " Missing line in file: " << *it);
271 void remove_smack_files()
273 unlink(OSP_BLAHBLAH);
274 unlink(WRT_BLAHBLAH);
275 unlink(OTHER_BLAHBLAH);
278 int smack_file_name(const char* app_id, char** path)
280 if (asprintf(path, SMACK_RULES_DIR "/%s", app_id) == -1) {
281 RUNNER_ASSERT_MSG(false, "asprint failed");
288 int cleaning_smack_app_files (void)
293 smack_file_name(APP_TEST_APP_1, &path);
297 smack_file_name(APP_TEST_APP_2, &path);
301 smack_file_name(APP_TEST_APP_3, &path);
305 smack_file_name(APP_TEST_AV_1, &path);
309 smack_file_name(APP_TEST_AV_2, &path);
313 smack_file_name(APP_TEST_AV_3, &path);
320 int cleaning_smack_database_files (void)
325 unlink(SMACK_APPS_LABELS_DATABASE);
326 fd = open(SMACK_APPS_LABELS_DATABASE, O_RDWR | O_EXCL | O_CREAT, 0644);
332 unlink(SMACK_AVS_LABELS_DATABASE);
333 fd = open(SMACK_AVS_LABELS_DATABASE, O_RDWR | O_EXCL | O_CREAT, 0644);
343 * Test setting labels for all files and folders in given path.
345 RUNNER_TEST(privilege_control02_app_label_dir)
349 result = nftw(TEST_APP_DIR, &nftw_remove_labels, FTW_MAX_FDS, FTW_PHYS);
350 RUNNER_ASSERT_MSG(result == 0, "Unable to clean up Smack labels in " << TEST_APP_DIR);
352 result = nftw(TEST_NON_APP_DIR, &nftw_set_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
353 RUNNER_ASSERT_MSG(result == 0, "Unable to clean up Smack labels in " << TEST_NON_APP_DIR);
355 result = app_label_dir(APPID_DIR, TEST_APP_DIR);
356 RUNNER_ASSERT_MSG(result == 0, "app_label_dir() failed");
358 result = nftw(TEST_APP_DIR, &nftw_check_labels_app_dir, FTW_MAX_FDS, FTW_PHYS);
359 RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for app dir");
361 result = nftw(TEST_NON_APP_DIR, &nftw_check_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
362 RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for non-app dir");
365 RUNNER_TEST(privilege_control03_app_label_shared_dir)
369 result = nftw(TEST_APP_DIR, &nftw_remove_labels, FTW_MAX_FDS, FTW_PHYS);
370 RUNNER_ASSERT_MSG(result == 0, "Unable to clean up Smack labels in " << TEST_APP_DIR);
372 result = nftw(TEST_NON_APP_DIR, &nftw_set_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
373 RUNNER_ASSERT_MSG(result == 0, "Unable to clean up Smack labels in " << TEST_NON_APP_DIR);
375 result = app_label_shared_dir(APP_ID, APPID_SHARED_DIR, TEST_APP_DIR);
376 RUNNER_ASSERT_MSG(result == 0, "app_label_shared_dir() failed");
378 result = nftw(TEST_APP_DIR, &nftw_check_labels_app_shared_dir, FTW_MAX_FDS, FTW_PHYS);
379 RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for shared app dir");
381 result = nftw(TEST_NON_APP_DIR, &nftw_check_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
382 RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for non-app dir");
387 * Add permisions from test_privilege_control_rules template
389 RUNNER_TEST(privilege_control04_add_permissions)
391 int result = app_add_permissions(APP_ID, PRIVS);
392 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
393 " Error adding app permissions. Errno: " << result);
395 // Check if the accesses are realy applied..
396 result = test_have_all_accesses(rules);
397 RUNNER_ASSERT_MSG(result==1, "Permissions not added.");
400 FILE *pFile = fopen(SMACK_RULES_DIR APP_ID, "rb");
401 RUNNER_ASSERT_MSG(pFile != NULL,
402 "SMACK file NOT created!. Errno: " << errno);
405 fseek(pFile, 0L, SEEK_END);
406 int smack_file_length = ftell(pFile);
407 RUNNER_ASSERT_MSG(smack_file_length>0,
408 "SMACK file empty, but privileges list was not empty.. Errno: " << errno);
416 * Revoke permissions from the list. Should be executed as privileged user.
418 RUNNER_CHILD_TEST(privilege_control06_revoke_permissions)
424 // Revoke permissions
425 result = app_revoke_permissions(APP_ID);
426 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
427 "Error revoking app permissions. Errno: " << result);
429 // Are all the permissions revoked?
430 result = test_have_all_accesses(rules);
431 RUNNER_ASSERT_MSG(result!=1, "Not all permisions revoked.");
433 smack_file_name(APP_ID, &path);
434 fd = open(path, O_RDONLY);
435 RUNNER_ASSERT_MSG(fd >= 0, "SMACK file deleted after app_revoke_permissions");
436 RUNNER_ASSERT_MSG(lseek(fd, 0, SEEK_END) == 0, "SMACK file not empty after app_revoke_permissions");
441 static void read_gids(std::set<unsigned> &set, const char* file_path)
443 FILE *f = fopen(file_path, "r");
444 RUNNER_ASSERT_MSG(f != NULL, "Unable to open file " << file_path);
446 while (fscanf(f, "%u\n", &gid) == 1) {
451 RUNNER_TEST(privilege_control05_add_shared_dir_readers)
454 #define TEST_OBJ "TEST_OBJECT"
455 #define TEST_OBJ_SOME_OTHER "TEST_OBJA"
456 #define test_string_01 "TEST_raz TEST_OBJECT r-x-- -----"
457 #define test_string_21 "TEST_trzy TEST_OBJA -wx--\n"
458 #define test_string_22 "TEST_trzy TEST_OBJECT r-x-- -----\n"
464 const char *app_labels[] = {"TEST_raz", "TEST_dwa", "TEST_trzy", ""};
465 const int READ_BUF_SIZE = 1000;
466 char buf[READ_BUF_SIZE];
468 struct smack_accesses * rules = NULL;
470 result = smack_accesses_new(&rules);
471 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "Error in smack_accesses_new. Error: " << result);
473 for (i = 0; i < 3; i++) {
475 (void)app_uninstall(app_labels[i]);
476 result = app_install(app_labels[i]);
477 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "Error in app_install.");
479 asprintf(&path, SMACK_RULES_DIR "/%s", app_labels[i]);
480 fd = open(path, O_WRONLY, 0644);
481 RUNNER_ASSERT_MSG(fd != -1, "Error in opening file " << path);
484 result = smack_accesses_add(rules,app_labels[i],TEST_OBJ,"wt");
485 RUNNER_ASSERT_MSG(result == 0, "smack_accesses_add failed");
488 smack_accesses_free(rules);
489 result = smack_accesses_new(&rules);
490 result = smack_accesses_add(rules,app_labels[i],TEST_OBJ_SOME_OTHER,"wx");
491 RUNNER_ASSERT_MSG(result == 0, "smack_accesses_add failed");
493 result = smack_accesses_apply(rules);
494 RUNNER_ASSERT_MSG(fd != -1, "smack_accesses_apply failed");
496 result = smack_accesses_save(rules, fd);
497 RUNNER_ASSERT_MSG(fd != -1, "smack_accesses_apply failed");
504 smack_accesses_free(rules);
506 // THE TEST - accesses
508 result = add_shared_dir_readers(TEST_OBJ,app_labels);
509 RUNNER_ASSERT_MSG(result == 0, "add_shared_dir_readers failed");
511 result = smack_have_access(app_labels[0],TEST_OBJ,"rx");
512 RUNNER_ASSERT_MSG(result == 1, "add_shared_dir_readers ERROR");
514 result = smack_have_access(app_labels[1],TEST_OBJ,"rx");
515 RUNNER_ASSERT_MSG(result == 1, "add_shared_dir_readers ERROR");
517 result = smack_have_access(app_labels[2],TEST_OBJ,"rx");
518 RUNNER_ASSERT_MSG(result == 1, "add_shared_dir_readers ERROR");
520 result = smack_have_access(app_labels[1],TEST_OBJ,"rwxt");
521 RUNNER_ASSERT_MSG(result == 1, "add_shared_dir_readers ERROR");
523 result = smack_have_access(app_labels[2],TEST_OBJ_SOME_OTHER,"wx");
524 RUNNER_ASSERT_MSG(result == 1, "add_shared_dir_readers ERROR");
527 //TEST the operations on empty files
529 asprintf(&path, SMACK_RULES_DIR "/%s", app_labels[0]);
530 file = fopen(path, "r");
532 RUNNER_ASSERT_MSG(file, "fopen failed, errno:" << errno);
534 fgets(buf, READ_BUF_SIZE, file);
535 result = strcmp(buf, test_string_01);
536 RUNNER_ASSERT_MSG( result!=0, "add_shared_dir_readers ERROR, file not formatted" << path );
541 //TEST the operations on non empty files
542 asprintf(&path, SMACK_RULES_DIR "/%s", app_labels[2]);
544 file = fopen(path, "r");
545 RUNNER_ASSERT_MSG(file, "fopen failed, errno:" << errno);
547 fgets(buf, READ_BUF_SIZE, file);
548 result = strcmp(buf, test_string_21);
549 RUNNER_ASSERT_MSG( result==0, "add_shared_dir_readers ERROR, file not formatted" );
551 fgets(buf, READ_BUF_SIZE, file);
552 result = strcmp(buf, test_string_22);
553 RUNNER_ASSERT_MSG( result==0, "add_shared_dir_readers ERROR, file not formatted" );
561 * Set APP privileges.
563 RUNNER_CHILD_TEST(privilege_control05_set_app_privilege)
568 smack_lsetlabel(APP_SET_PRIV_PATH_REAL, APP_ID, SMACK_LABEL_EXEC);
569 smack_lsetlabel(APP_SET_PRIV_PATH, APP_ID "_symlink", SMACK_LABEL_EXEC);
571 // Set APP privileges
572 result = set_app_privilege(APP_ID, NULL, APP_SET_PRIV_PATH);
573 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "Error in set_app_privilege. Error: " << result);
575 // Check if SMACK label really set
577 result = smack_new_label_from_self(&label);
578 RUNNER_ASSERT_MSG(result == 0, "Error getting current process label");
579 RUNNER_ASSERT_MSG(label != NULL, "Process label is not set");
580 result = strcmp(APP_ID, label);
581 RUNNER_ASSERT_MSG(result == 0, "Process label " << label << " is incorrect");
583 // Check if DAC privileges really set
584 RUNNER_ASSERT_MSG(getuid() == APP_UID, "Wrong UID");
585 RUNNER_ASSERT_MSG(getgid() == APP_GID, "Wrong GID");
587 result = strcmp(getenv("HOME"), APP_HOME_DIR);
588 RUNNER_ASSERT_MSG(result == 0, "Wrong HOME DIR");
590 result = strcmp(getenv("USER"), APP_USER_NAME);
591 RUNNER_ASSERT_MSG(result == 0, "Wrong user USER NAME");
593 std::set<unsigned> groups_check;
594 read_gids(groups_check, LIBPRIVILEGE_APP_GROUP_LIST);
595 read_gids(groups_check, LIBPRIVILEGE_TEST_DAC_FILE);
597 int groups_cnt = getgroups(0, NULL);
598 RUNNER_ASSERT_MSG(groups_cnt > 0, "Wrong number of supplementary groupsCnt");
599 gid_t *groups_list = (gid_t *) calloc(groups_cnt, sizeof(gid_t));
600 RUNNER_ASSERT_MSG(groups_list != NULL, "Memory allocation failed");
601 getgroups(groups_cnt, groups_list);
603 for (int i = 0; i < groups_cnt; ++i) {
604 if (groups_check.erase(groups_list[i]) == 0) {
605 // getgroups() may also return process' main group
606 if (groups_list[i] == getgid())
607 RUNNER_ASSERT_MSG(false, "Application belongs to unknown group (GID=" << groups_list[i] << ")");
610 std::string groups_left;
611 for (std::set<unsigned>::iterator it = groups_check.begin(); it != groups_check.end(); it++) {
612 groups_left.append(std::to_string(*it)).append(" ");
614 RUNNER_ASSERT_MSG(groups_check.empty(), "Application doesn't belong to some required groups: " << groups_left);
617 RUNNER_TEST(privilege_control08_app_give_access)
619 const char *subject = "lkjq345v34sfa";
620 const char *object = "lk9290f92lkjz";
621 smack_accesses *tmp = NULL;
623 RUNNER_ASSERT(0 == smack_accesses_new(&tmp));
625 std::unique_ptr<smack_accesses,std::function<void(smack_accesses*)>>
626 smack(tmp, smack_accesses_free);
628 RUNNER_ASSERT(0 == smack_accesses_add(smack.get(), subject, object, "r--a-"));
629 RUNNER_ASSERT(0 == smack_accesses_apply(smack.get()));
631 app_give_access(subject, object, "wt");
633 RUNNER_ASSERT(1 == smack_have_access(subject, object, "rwat"));
634 RUNNER_ASSERT(0 == smack_have_access(subject, object, "x"));
636 app_revoke_access(subject, object);
638 RUNNER_ASSERT(1 == smack_have_access(subject, object, "ra"));
639 RUNNER_ASSERT(0 == smack_have_access(subject, object, "w"));
640 RUNNER_ASSERT(0 == smack_have_access(subject, object, "x"));
641 RUNNER_ASSERT(0 == smack_have_access(subject, object, "t"));
643 RUNNER_ASSERT(0 == smack_accesses_add(smack.get(), subject, object, "-"));
644 RUNNER_ASSERT(0 == smack_accesses_apply(smack.get()));
648 * Add new API feature
650 RUNNER_TEST(privilege_control09_add_api_feature)
654 remove_smack_files();
657 // argument validation
658 result = add_api_feature(APP_TYPE_OSP, NULL, NULL, NULL);
659 RUNNER_ASSERT(result == PC_ERR_INVALID_PARAM);
661 result = add_api_feature(APP_TYPE_OSP,"" , NULL, NULL);
662 RUNNER_ASSERT(result == PC_ERR_INVALID_PARAM);
665 // already existing features
666 result = add_api_feature(APP_TYPE_OSP,"messaging" , NULL, NULL);
667 RUNNER_ASSERT(result == PC_ERR_INVALID_PARAM);
669 result = add_api_feature(APP_TYPE_OSP,"blahblah/messaging" , NULL, NULL);
670 RUNNER_ASSERT(result == PC_ERR_INVALID_PARAM);
672 result = add_api_feature(APP_TYPE_WGT,"blahblahblah/messaging" , NULL, NULL);
673 RUNNER_ASSERT(result == PC_ERR_INVALID_PARAM);
675 result = add_api_feature(APP_TYPE_OTHER,"blah/messaging" , NULL, NULL);
676 RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
680 result = add_api_feature(APP_TYPE_OSP,"blahblah" , NULL, NULL);
681 RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
683 result = add_api_feature(APP_TYPE_WGT,"blahblah" , NULL, NULL);
684 RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
686 result = add_api_feature(APP_TYPE_OTHER,"blahblah" , NULL, NULL);
687 RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
690 // smack files existence
691 result = file_exists(OSP_BLAHBLAH);
692 RUNNER_ASSERT(result == -1);
694 result = file_exists(WRT_BLAHBLAH);
695 RUNNER_ASSERT(result == -1);
697 result = file_exists(OTHER_BLAHBLAH);
698 RUNNER_ASSERT(result == -1);
702 result = add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE , { NULL }, NULL);
703 RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
704 result = file_exists(OSP_BLAHBLAH);
705 RUNNER_ASSERT(result == -1);
707 result = add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE , (const char*[]){ "", NULL }, NULL);
708 RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
709 result = file_exists(OSP_BLAHBLAH);
710 RUNNER_ASSERT(result == 0);
711 remove_smack_files();
713 result = add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE , (const char*[]){ " \t\n", "\t \n", "\n\t ", NULL }, NULL);
714 RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
715 result = file_exists(OSP_BLAHBLAH);
716 RUNNER_ASSERT(result == 0);
717 remove_smack_files();
721 result = add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE , (const char*[]){ "malformed", NULL }, NULL);
722 RUNNER_ASSERT(result == PC_ERR_INVALID_PARAM);
723 result = file_exists(OSP_BLAHBLAH);
724 RUNNER_ASSERT(result == -1);
726 result = add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE , (const char*[]){ "malformed malformed", NULL }, NULL);
727 RUNNER_ASSERT(result == PC_ERR_INVALID_PARAM);
728 result = file_exists(OSP_BLAHBLAH);
729 RUNNER_ASSERT(result == -1);
731 result = add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE , (const char*[]){ "-malformed malformed rwxat", NULL }, NULL);
732 RUNNER_ASSERT(result == PC_ERR_INVALID_PARAM);
733 result = file_exists(OSP_BLAHBLAH);
734 RUNNER_ASSERT(result == -1);
736 result = add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE , (const char*[]){ "~/\"\\\ malformed rwxat", NULL }, NULL);
737 RUNNER_ASSERT(result == PC_ERR_INVALID_PARAM);
738 result = file_exists(OSP_BLAHBLAH);
739 RUNNER_ASSERT(result == -1);
741 result = add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE , (const char*[]){ "subject object rwxat something else", NULL }, NULL);
742 RUNNER_ASSERT(result == PC_ERR_INVALID_PARAM);
743 result = file_exists(OSP_BLAHBLAH);
744 RUNNER_ASSERT(result == -1);
748 result = add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE , (const char*[]){ "malformed malformed maaaaaalformed", NULL }, NULL);
749 RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
750 osp_blahblah_check(__LINE__, { "malformed malformed r--a-" });
751 remove_smack_files();
753 result = add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE , (const char*[]){ "subject object foo", NULL }, NULL);
754 RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
755 osp_blahblah_check(__LINE__, { "subject object -----" });
756 remove_smack_files();
758 result = add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE , (const char*[]){
759 "subject object\t rwxat",
761 "subject2\tobject2 txarw",
764 RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
765 osp_blahblah_check(__LINE__, { "subject object rwxat", "subject2 object2 rwxat"});
766 remove_smack_files();
768 result = add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE , (const char*[]){
769 "Sub::jE,ct object a-RwX",
771 RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
772 osp_blahblah_check(__LINE__, { "Sub::jE,ct object rwxa-"});
773 remove_smack_files();
775 // TODO For now identical/complementary rules are not merged.
776 result = add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE , (const char*[]){
777 "subject object rwxat",
779 "subject object txarw",
782 RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
783 osp_blahblah_check(__LINE__, { "subject object rwxat", "subject object rwxat"});
784 remove_smack_files();
787 // TODO database group ids
791 * Check app_install function
793 RUNNER_TEST(privilege_control01_app_install)
799 smack_file_name(APP_ID, &path);
802 result = app_install(APP_ID);
803 RUNNER_ASSERT_MSG(result == 0, "app_install returned " << result <<". Errno: " << strerror(errno));
805 // checking if file really exists
806 fd = open(path, O_RDONLY);
807 RUNNER_ASSERT_MSG(fd >= 0, "File open failed: " << path << " : " << result << ". Errno: " << strerror(errno));
811 // try install second time app with the same ID - it should failed with -1 (Errno: File exists).
812 result = app_install(APP_ID);
813 RUNNER_ASSERT_MSG(result == -1, "app_install returned " << result <<". Errno: " << strerror(errno));
817 * Check app_install function
819 RUNNER_TEST(privilege_control07_app_uninstall)
825 smack_file_name(APP_ID, &path);
826 result = app_uninstall(APP_ID);
827 RUNNER_ASSERT_MSG(result == 0, "app_uninstall returned " << result <<". Errno: " << strerror(errno));
829 // checking if file really exists
830 smack_file_name(APP_ID, &path);
831 fd = open(path, O_RDONLY);
832 RUNNER_ASSERT_MSG(fd == -1, "SMACK file NOT deleted after app_uninstall");
838 * Check app_register_av function
839 * Notice that this test case may have no sense if previous would fail (privilege_control06_app_install)
841 RUNNER_TEST(privilege_control10_app_register_av)
844 //FILE* file_av = NULL;
845 //FILE* file_app = NULL;
852 //char label1[SMACK_LABEL_LEN +1];
853 //char label2[SMACK_LABEL_LEN +1];
854 //char acces_rights[6 +1];
855 //char row[2 * SMACK_LABEL_LEN + 20] //
856 const char* correct_antivirus1_rules = "test-antivirus1 test-application1 rwx--\n"
857 "test-antivirus1 test-application_2 rwx--\n"
858 "test-antivirus1 test-app-3 rwx--";
859 const char* correct_antivirus2_rules = "test-antivirus_2 test-application1 rwx--\n"
860 "test-antivirus_2 test-application_2 rwx--\n"
861 "test-antivirus_2 test-app-3 rwx--";
864 cleaning_smack_app_files();
865 cleaning_smack_database_files();
867 result = app_install(APP_TEST_APP_1);
868 RUNNER_ASSERT_MSG(result == 0, "app_install returned " << result <<". Errno: " << strerror(errno));
870 result = app_install(APP_TEST_APP_2);
871 RUNNER_ASSERT_MSG(result == 0, "app_install returned " << result <<". Errno: " << strerror(errno));
873 result = app_register_av(APP_TEST_AV_1);
874 RUNNER_ASSERT_MSG(result == 0, "app_register_av returned " << result <<". Errno: " << strerror(errno));
876 result = app_install(APP_TEST_APP_3);
877 RUNNER_ASSERT_MSG(result == 0, "app_install returned " << result <<". Errno: " << strerror(errno));
879 result = app_register_av(APP_TEST_AV_2);
880 RUNNER_ASSERT_MSG(result == 0, "app_register_av returned " << result <<". Errno: " << strerror(errno));
882 // checking rules for anti virus 1
883 // compare between file in /etc/smack/access.d/ and correct value (correct_antivirus1_rules).
884 len = strlen(correct_antivirus1_rules);
885 buff = (char *) malloc((len+1) * sizeof(char));
886 smack_file_name(APP_TEST_AV_1, &path);
887 fd = open(path, O_RDONLY);
889 RUNNER_ASSERT_MSG(fd > -1, "file open failed " << result <<". Errno: " << strerror(errno));
890 result = read(fd, buff, len);
894 RUNNER_ASSERT_MSG(result > -1, "read from file descriptor failed. Errno: " << strerror(errno));
895 result = strncmp(buff, correct_antivirus1_rules, len);
896 RUNNER_ASSERT_MSG(result == 0, "Rules do not match: " << result << "\n\"" << buff << "\"\n\"" << correct_antivirus1_rules << "\"\n" << len);
899 // checking rules for anti virus 2
900 len = strlen(correct_antivirus2_rules);
901 buff = (char *) malloc((len+1) * sizeof(char));
902 smack_file_name(APP_TEST_AV_2, &path);
903 fd = open(path, O_RDONLY);
905 RUNNER_ASSERT_MSG(fd > -1, "file open failed " << result <<". Errno: " << strerror(errno));
906 result = read(fd, buff, len);
910 RUNNER_ASSERT_MSG(result > -1, "read from file descriptor failed. Errno: " << strerror(errno));
911 result = strncmp(buff, correct_antivirus2_rules, len);
912 RUNNER_ASSERT_MSG(result == 0, "Rules do not match: " << result << "\n\"" << buff << "\"\n\"" << correct_antivirus1_rules << "\"\n" << len);
916 cleaning_smack_app_files();
917 cleaning_smack_database_files();