Clean up libprivilege-control test cases
[platform/core/test/security-tests.git] / tests / libprivilege-control-tests / test_cases.cpp
1 /*
2  * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15 */
16
17 /*
18  * @file        test_cases.cpp
19  * @author      Jan Olszak (j.olszak@samsung.com)
20  * @author      Rafal Krypa (r.krypa@samsung.com)
21  * @version     1.0
22  * @brief       libprivilege-control test runner
23  */
24
25 #include <string>
26 #include <vector>
27 #include <memory>
28 #include <fstream>
29 #include <sstream>
30
31 #include <fcntl.h>
32 #include <errno.h>
33 #include <unistd.h>
34
35 #include <sys/types.h>
36 #include <sys/stat.h>
37
38 #include <sys/socket.h>
39 #include <sys/un.h>
40 #include <sys/smack.h>
41
42 #include <privilege-control.h>
43 #include <dpl/test/test_runner.h>
44 #include <dpl/test/test_runner_child.h>
45 #include <dpl/test/test_runner_multiprocess.h>
46 #include <dpl/log/log.h>
47 #include <tests_common.h>
48 #include <libprivilege-control_test_common.h>
49
50 #include <iostream>
51
52 #define SMACK_STARTUP_RULES_FILE "/opt/etc/smack-app-early/accesses.d/rules"
53
54 #define EFL_APP_ID            "EFL_APP_ID"
55
56 #define EARLY_RULE_SUBJECT    "livebox.web-provider"
57 #define EARLY_RULE_RIGHTS     "rwx---"
58
59 #define SMACK_ACC_LEN            6
60
61 #define APP_1     "app_1"
62 #define APP_1_DIR "/tmp/app_1"
63
64 #define APP_2     "app_2"
65 #define APP_2_DIR "/tmp/app_2"
66
67 #define APP_TEST "app_test"
68
69 namespace {
70
71 const char *PRIVS2_NO_R[] = { "test_privilege_control_rules2_no_r", NULL };
72 const char *PRIVS2_R[] = { "test_privilege_control_rules2_r", NULL };
73 const char *PRIVS2_R_AND_NO_R[] = { "test_privilege_control_rules2_r", "test_privilege_control_rules2_no_r", NULL };
74 const char *PRIVS_EFL[] = { "test_privilege_control_rules_efl", NULL };
75
76 std::vector<std::string> gen_names(std::string prefix, std::string suffix, size_t size)
77 {
78     std::vector<std::string> names;
79     for(size_t i = 0; i < size; ++i) {
80         names.push_back(prefix + "_" + std::to_string(i) + suffix);
81     }
82     return names;
83 }
84
85 const char *OSP_BLAHBLAH = "/usr/share/privilege-control/OSP_feature.blah.blahblah.smack";
86 const char *WRT_BLAHBLAH  ="/usr/share/privilege-control/WGT_blahblah.smack";
87 const char *OTHER_BLAHBLAH  ="/usr/share/privilege-control/blahblah.smack";
88 const std::vector<std::string> OSP_BLAHBLAH_DAC = gen_names("/usr/share/privilege-control/OSP_feature.blah.blahblah", ".dac", 16);
89 const char *WRT_BLAHBLAH_DAC  ="/usr/share/privilege-control/WGT_blahblah.dac";
90 const char *OTHER_BLAHBLAH_DAC = "/usr/share/privilege-control/blahblah.dac";
91 const std::vector<std::string> BLAHBLAH_FEATURE = gen_names("http://feature/blah/blahblah", "", 16);
92
93 int nftw_check_labels_app_shared_dir(const char *fpath, const struct stat *sb,
94                                      int /*typeflag*/, struct FTW* /*ftwbuf*/)
95 {
96     int result;
97     char *label;
98
99     /* ACCESS */
100     result = smack_lgetlabel(fpath, &label, SMACK_LABEL_ACCESS);
101     RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
102     RUNNER_ASSERT_MSG(label != NULL, "ACCESS label on " << fpath << " is not set");
103     result = strcmp(APPID_SHARED_DIR, label);
104     RUNNER_ASSERT_MSG(result == 0, "ACCESS label on " << fpath << " is incorrect");
105
106     result = smack_have_access(APP_ID, APPID_SHARED_DIR, "rwxatl");
107     RUNNER_ASSERT_MSG(result == 1,
108         "Error rwxatl access was not given shared dir. Subject: " <<
109         APP_ID << ". Object: " << APPID_SHARED_DIR << ". Result: " << result);
110     /* EXEC */
111     result = smack_lgetlabel(fpath, &label, SMACK_LABEL_EXEC);
112     RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
113     RUNNER_ASSERT_MSG(label == NULL, "EXEC label on " << fpath << " is set");
114
115     /* TRANSMUTE */
116     result = smack_lgetlabel(fpath, &label, SMACK_LABEL_TRANSMUTE);
117     RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
118     if (S_ISDIR(sb->st_mode)) {
119         RUNNER_ASSERT_MSG(label != NULL, "TRANSMUTE label on " << fpath << " is not set");
120         result = strcmp("TRUE", label);
121         RUNNER_ASSERT_MSG(result == 0, "TRANSMUTE label on " << fpath << " is not set");
122     } else
123         RUNNER_ASSERT_MSG(label == NULL, "TRANSMUTE label on " << fpath << " is set");
124
125     return 0;
126 }
127
128 int check_labels_dir(const char *fpath, const struct stat *sb,
129                      const char *labels_db_path, const char *dir_db_path,
130                      const char *access)
131 {
132     int result;
133     char *label;
134     char *label_gen;
135     char *scanf_label_format;
136     char label_temp[SMACK_LABEL_LEN + 1];
137     FILE *file_db;
138
139     /* ACCESS */
140     result = smack_lgetlabel(fpath, &label_gen, SMACK_LABEL_ACCESS);
141     RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
142     RUNNER_ASSERT_MSG(label_gen != NULL, "ACCESS label on " << fpath << " is not set");
143
144     /* EXEC */
145     result = smack_lgetlabel(fpath, &label, SMACK_LABEL_EXEC);
146     if (result != 0) {
147         free(label_gen);
148         RUNNER_ASSERT_MSG(false, "Could not get label for the path");
149     }
150     if (label != NULL) {
151         free(label_gen);
152         free(label);
153         RUNNER_ASSERT_MSG(false, "EXEC label on " << fpath << " is set.");
154     }
155
156     /* TRANSMUTE */
157     result = smack_lgetlabel(fpath, &label, SMACK_LABEL_TRANSMUTE);
158     if (result != 0) {
159         free(label_gen);
160         free(label);
161         RUNNER_ASSERT_MSG(false, "Could not get label for the path");
162     }
163     if (S_ISDIR(sb->st_mode)) {
164         if (label == NULL) {
165             free(label_gen);
166             free(label);
167             RUNNER_ASSERT_MSG(false, "TRANSMUTE label on " << fpath << " is not set");
168         }
169         result = strcmp("TRUE", label);
170         if (result != 0) {
171             free(label_gen);
172             free(label);
173             RUNNER_ASSERT_MSG(false, "TRANSMUTE label on " << fpath << " is not set to TRUE");
174         }
175     } else if (label != NULL) {
176         free(label_gen);
177         free(label);
178         RUNNER_ASSERT_MSG(false, "TRANSMUTE label on " << fpath << " is set");
179     }
180
181     free(label);
182
183     if (0 > asprintf(&scanf_label_format, "%%%ds\\n", SMACK_LABEL_LEN)) {
184         free(label_gen);
185         RUNNER_ASSERT_MSG(false, "asprintf failed");
186     }
187
188     file_db = fopen(labels_db_path, "r");
189     if (file_db == NULL) {
190         free(label_gen);
191         free(scanf_label_format);
192         RUNNER_ASSERT_MSG(false, "Can not open database for apps");
193     }
194     while (fscanf(file_db, scanf_label_format, label_temp) == 1) {
195         result = smack_have_access(label_temp, label_gen, access);
196         if (result != 1) {
197             fclose(file_db);
198             free(label_gen);
199             free(scanf_label_format);
200             RUNNER_ASSERT_MSG(false,
201                 "Error " << access << " access was not given for subject: "
202                 << label_temp << ". Result: " << result);
203         }
204     }
205     fclose(file_db);
206
207     file_db = fopen(dir_db_path, "r");
208     if (file_db == NULL) {
209         free(label_gen);
210         free(scanf_label_format);
211         RUNNER_ASSERT_MSG(false, "Can not open database for dirs");
212     }
213
214     free(scanf_label_format);
215     free(label_gen);
216     fclose(file_db);
217
218     return 0;
219 }
220
221 int file_exists(const char *path)
222 {
223     FILE *file = fopen(path, "r");
224     if (file) {
225         fclose(file);
226         return 0;
227     }
228     return -1;
229 }
230
231 void osp_blahblah_check(int line_no, const std::vector<std::string> &rules)
232 {
233     std::ifstream smack_file(OSP_BLAHBLAH);
234     RUNNER_ASSERT_MSG(smack_file, "Line: " << line_no << " Failed to create " << OSP_BLAHBLAH);
235
236     auto it = rules.begin();
237     std::string line;
238     while (std::getline(smack_file,line)) {
239         RUNNER_ASSERT_MSG(it != rules.end(), "Line: " << line_no << "Additional line in file: " << line);
240         RUNNER_ASSERT_MSG(*it == line, "Line: " << line_no << " " << *it << "!=" << line);
241         it++;
242     }
243
244     RUNNER_ASSERT_MSG(it == rules.end(), "Line: " << line_no << " Missing line in file: " << *it);
245
246     smack_file.close();
247 }
248
249 void osp_blahblah_dac_check(int line_no, const std::vector<unsigned> &gids, std::string dac_file_path)
250 {
251     std::ifstream dac_file(dac_file_path);
252     RUNNER_ASSERT_MSG(dac_file, "Line: " << line_no << " Failed to create " << dac_file_path);
253
254     auto it = gids.begin();
255     std::string line;
256     while (std::getline(dac_file,line)) {
257         std::istringstream is(line);
258         unsigned gid;
259         is >> gid;
260         RUNNER_ASSERT_MSG(it != gids.end(), "Line: " << line_no << "Additional line in file: " << gid);
261         RUNNER_ASSERT_MSG(*it == gid, "Line: " << line_no << " " << *it << "!=" << gid);
262         it++;
263     }
264
265     RUNNER_ASSERT_MSG(it == gids.end(), "Line: " << line_no << " Missing line in file: " << *it);
266
267     dac_file.close();
268 }
269
270 void remove_smack_files()
271 {
272     // TODO array
273     unlink(OSP_BLAHBLAH);
274     unlink(WRT_BLAHBLAH);
275     unlink(OTHER_BLAHBLAH);
276     unlink(WRT_BLAHBLAH_DAC);
277     unlink(OTHER_BLAHBLAH_DAC);
278
279     for(size_t i=0; i<OSP_BLAHBLAH_DAC.size(); ++i)
280         unlink(OSP_BLAHBLAH_DAC[i].c_str());
281
282     for(size_t i=0; i<OSP_BLAHBLAH_DAC.size(); ++i)
283         unlink(OSP_BLAHBLAH_DAC[i].c_str());
284 }
285
286 } // namespace
287
288 RUNNER_TEST_GROUP_INIT(libprivilegecontrol)
289
290 /**
291  * Test setting labels for all files and folders in given path.
292  */
293 RUNNER_TEST(privilege_control02_app_label_dir)
294 {
295     int result;
296
297     result = nftw(TEST_APP_DIR, &nftw_remove_labels, FTW_MAX_FDS, FTW_PHYS);
298     RUNNER_ASSERT_MSG(result == 0, "Unable to clean up Smack labels in " << TEST_APP_DIR);
299
300     result = nftw(TEST_NON_APP_DIR, &nftw_set_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
301     RUNNER_ASSERT_MSG(result == 0, "Unable to clean up Smack labels in " << TEST_NON_APP_DIR);
302
303     result = perm_app_setup_path(APPID_DIR, TEST_APP_DIR, APP_PATH_PRIVATE);
304     RUNNER_ASSERT_MSG(result == 0, "perm_app_setup_path() failed");
305
306     result = nftw(TEST_APP_DIR, &nftw_check_labels_app_dir, FTW_MAX_FDS, FTW_PHYS);
307     RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for app dir");
308
309     result = nftw(TEST_NON_APP_DIR, &nftw_check_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
310     RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for non-app dir");
311 }
312
313 RUNNER_TEST_SMACK(privilege_control03_app_label_shared_dir)
314 {
315     int result;
316     result = perm_app_install(APP_ID);
317     RUNNER_ASSERT_MSG(result == 0, "perm_app_install returned " << result << ". Errno: " << strerror(errno));
318
319     result = perm_app_setup_path(APP_ID, TEST_APP_DIR, APP_PATH_GROUP_RW, APP_ID);
320     RUNNER_ASSERT_MSG(result != 0, "perm_app_setup_path(APP_ID, APP_ID) didn't fail");
321
322     result = nftw(TEST_APP_DIR, &nftw_remove_labels, FTW_MAX_FDS, FTW_PHYS);
323     RUNNER_ASSERT_MSG(result == 0, "Unable to clean up Smack labels in " << TEST_APP_DIR);
324
325     result = nftw(TEST_NON_APP_DIR, &nftw_set_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
326     RUNNER_ASSERT_MSG(result == 0, "Unable to clean up Smack labels in " << TEST_NON_APP_DIR);
327
328     result = perm_app_setup_path(APP_ID, TEST_APP_DIR, APP_PATH_GROUP_RW, APPID_SHARED_DIR);
329     RUNNER_ASSERT_MSG(result == 0, "perm_app_setup_path() failed");
330
331     result = nftw(TEST_APP_DIR, &nftw_check_labels_app_shared_dir, FTW_MAX_FDS, FTW_PHYS);
332     RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for shared app dir");
333
334     result = nftw(TEST_NON_APP_DIR, &nftw_check_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
335     RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for non-app dir");
336
337     result = perm_app_uninstall(APP_ID);
338     RUNNER_ASSERT_MSG(result == 0, "perm_app_uninstall returned " << result << ". Errno: " << strerror(errno));
339 }
340
341 /**
342  * Simple enabling EFL permissions;.
343  */
344 RUNNER_TEST_SMACK(privilege_control04_add_permissions)
345 {
346     int result = perm_app_uninstall(APP_ID);
347     RUNNER_ASSERT_MSG(result == 0, "perm_app_uninstall returned " << result << ". Errno: " << strerror(errno));
348
349     result = perm_app_install(APP_ID);
350     RUNNER_ASSERT_MSG(result == 0, "perm_app_install returned " << result << ". Errno: " << strerror(errno));
351
352
353     result = perm_app_enable_permissions(APP_ID, APP_TYPE_EFL, PRIVS_EFL, TRUE);
354     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
355         " perm_app_enable_permissions failed with result: " << result);
356
357     // Check if the accesses are realy applied..
358     result = test_have_all_accesses(rules_efl);
359     RUNNER_ASSERT_MSG(result == 1, "Permissions not added.");
360
361     result = perm_app_uninstall(APP_ID);
362     RUNNER_ASSERT_MSG(result == 0, "perm_app_uninstall returned " << result << ". Errno: " << strerror(errno));
363 }
364
365 /**
366  * Revoke permissions from the list. Should be executed as privileged user.
367  */
368 RUNNER_CHILD_TEST(privilege_control06_revoke_permissions)
369 {
370     int result;
371
372     // Cleanup
373     result = perm_app_uninstall(WGT_APP_ID);
374     RUNNER_ASSERT_MSG(result == 0, "perm_app_uninstall returned " << result << ". Errno: " << strerror(errno));
375     result = perm_app_uninstall(WGT_PARTNER_APP_ID);
376     RUNNER_ASSERT_MSG(result == 0, "perm_app_uninstall returned " << result << ". Errno: " << strerror(errno));
377     result = perm_app_uninstall(WGT_PLATFORM_APP_ID);
378     RUNNER_ASSERT_MSG(result == 0, "perm_app_uninstall returned " << result << ". Errno: " << strerror(errno));
379     result = perm_app_uninstall(OSP_APP_ID);
380     RUNNER_ASSERT_MSG(result == 0, "perm_app_uninstall returned " << result << ". Errno: " << strerror(errno));
381     result = perm_app_uninstall(OSP_PARTNER_APP_ID);
382     RUNNER_ASSERT_MSG(result == 0, "perm_app_uninstall returned " << result << ". Errno: " << strerror(errno));
383     result = perm_app_uninstall(OSP_PLATFORM_APP_ID);
384     RUNNER_ASSERT_MSG(result == 0, "perm_app_uninstall returned " << result << ". Errno: " << strerror(errno));
385
386     // Install test apps
387     result = perm_app_install(WGT_APP_ID);
388     RUNNER_ASSERT_MSG(result == 0, "perm_app_install returned " << result << ". Errno: " << strerror(errno));
389     result = perm_app_install(WGT_PARTNER_APP_ID);
390     RUNNER_ASSERT_MSG(result == 0, "perm_app_install returned " << result << ". Errno: " << strerror(errno));
391     result = perm_app_install(WGT_PLATFORM_APP_ID);
392     RUNNER_ASSERT_MSG(result == 0, "perm_app_install returned " << result << ". Errno: " << strerror(errno));
393     result = perm_app_install(OSP_APP_ID);
394     RUNNER_ASSERT_MSG(result == 0, "perm_app_install returned " << result << ". Errno: " << strerror(errno));
395     result = perm_app_install(OSP_PARTNER_APP_ID);
396     RUNNER_ASSERT_MSG(result == 0, "perm_app_install returned " << result << ". Errno: " << strerror(errno));
397     result = perm_app_install(OSP_PLATFORM_APP_ID);
398     RUNNER_ASSERT_MSG(result == 0, "perm_app_install returned " << result << ". Errno: " << strerror(errno));
399
400
401     // TEST:
402     // Revoke permissions
403     result = perm_app_revoke_permissions(WGT_APP_ID);
404     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
405         "Error revoking app permissions. Result: " << result);
406     result = perm_app_revoke_permissions(WGT_PARTNER_APP_ID);
407     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
408         "Error revoking app permissions. Result: " << result);
409     result = perm_app_revoke_permissions(WGT_PLATFORM_APP_ID);
410     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
411         "Error revoking app permissions. Result: " << result);
412     result = perm_app_revoke_permissions(OSP_APP_ID);
413     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
414         "Error revoking app permissions. Result: " << result);
415     result = perm_app_revoke_permissions(OSP_PARTNER_APP_ID);
416     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
417         "Error revoking app permissions. Result: " << result);
418     result = perm_app_revoke_permissions(OSP_PLATFORM_APP_ID);
419     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
420         "Error revoking app permissions. Result: " << result);
421
422     // Are all the permissions revoked?
423     result = test_have_any_accesses(rules_wgt);
424     RUNNER_ASSERT_MSG(result == 0, "Not all permisions revoked.");
425     result = test_have_any_accesses(rules_wgt_partner);
426     RUNNER_ASSERT_MSG(result == 0, "Not all permisions revoked.");
427     result = test_have_any_accesses(rules_wgt_platform);
428     RUNNER_ASSERT_MSG(result == 0, "Not all permisions revoked.");
429     result = test_have_any_accesses(rules_osp);
430     RUNNER_ASSERT_MSG(result == 0, "Not all permisions revoked.");
431     result = test_have_any_accesses(rules_osp_partner);
432     RUNNER_ASSERT_MSG(result == 0, "Not all permisions revoked.");
433     result = test_have_any_accesses(rules_osp_platform);
434     RUNNER_ASSERT_MSG(result == 0, "Not all permisions revoked.");
435
436     // Cleanup - uninstall test apps
437     result = perm_app_uninstall(WGT_APP_ID);
438     RUNNER_ASSERT_MSG(result == 0, "perm_app_uninstall returned " << result << ". Errno: " << strerror(errno));
439     result = perm_app_uninstall(WGT_PARTNER_APP_ID);
440     RUNNER_ASSERT_MSG(result == 0, "perm_app_uninstall returned " << result << ". Errno: " << strerror(errno));
441     result = perm_app_uninstall(WGT_PLATFORM_APP_ID);
442     RUNNER_ASSERT_MSG(result == 0, "perm_app_uninstall returned " << result << ". Errno: " << strerror(errno));
443     result = perm_app_uninstall(OSP_APP_ID);
444     RUNNER_ASSERT_MSG(result == 0, "perm_app_uninstall returned " << result << ". Errno: " << strerror(errno));
445     result = perm_app_uninstall(OSP_PARTNER_APP_ID);
446     RUNNER_ASSERT_MSG(result == 0, "perm_app_uninstall returned " << result << ". Errno: " << strerror(errno));
447     result = perm_app_uninstall(OSP_PLATFORM_APP_ID);
448     RUNNER_ASSERT_MSG(result == 0, "perm_app_uninstall returned " << result << ". Errno: " << strerror(errno));
449 }
450
451
452 void set_app_privilege(int line_no,
453                        const char* app_id, app_type_t APP_TYPE,
454                        const char** privileges, const char* type,
455                        const char* app_path, const char* dac_file,
456                        const std::vector< std::vector<std::string> > &rules) {
457     int result = perm_app_uninstall(app_id);
458     RUNNER_ASSERT_MSG(result == 0, "Line: " << line_no <<
459             " perm_app_uninstall returned " << result << ". Errno: " << strerror(errno));
460     result = perm_app_install(app_id);
461     RUNNER_ASSERT_MSG(result == 0, "Line: " << line_no <<
462             " perm_app_install returned " << result << ". Errno: " << strerror(errno));
463
464     // TEST:
465     result = perm_app_enable_permissions(app_id, APP_TYPE, privileges, 1);
466     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "Line: " << line_no <<
467         " Error enabling app permissions. Result: " << result);
468
469     result = test_have_all_accesses(rules);
470     RUNNER_ASSERT_MSG(result == 1, "Permissions not added.");
471
472     result = perm_app_set_privilege(app_id, type, app_path);
473     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "Line: " << line_no <<
474             " Error in perm_app_set_privilege. Error: " << result);
475
476     // Check if SMACK label really set
477     char *label;
478     result = smack_new_label_from_self(&label);
479     RUNNER_ASSERT_MSG(result >= 0, "Line: " << line_no <<
480             " Error getting current process label");
481     RUNNER_ASSERT_MSG(label != NULL, "Line: " << line_no <<
482             " Process label is not set");
483     result = strcmp(app_id, label);
484     RUNNER_ASSERT_MSG(result == 0, "Line: " << line_no <<
485             " Process label " << label << " is incorrect");
486
487     check_groups(dac_file);
488 }
489
490 /**
491  * Set APP privileges. wgt.
492  */
493 RUNNER_CHILD_TEST_SMACK(privilege_control05_set_app_privilege_wgt)
494 {
495     set_app_privilege(__LINE__,WGT_APP_ID, APP_TYPE_WGT, PRIVS_WGT, "wgt", WGT_APP_PATH,
496             LIBPRIVILEGE_TEST_DAC_FILE_WGT, rules_wgt);
497 }
498
499 /**
500  * Set APP privileges. wgt_partner.
501  */
502 RUNNER_CHILD_TEST_SMACK(privilege_control05_set_app_privilege_wgt_partner)
503 {
504     set_app_privilege(__LINE__, WGT_PARTNER_APP_ID, APP_TYPE_WGT_PARTNER, PRIVS_WGT,
505             "wgt_partner", WGT_PARTNER_APP_PATH,
506             LIBPRIVILEGE_TEST_DAC_FILE_WGT, rules_wgt_partner);
507 }
508
509 /**
510  * Set APP privileges. wgt_platform.
511  */
512 RUNNER_CHILD_TEST_SMACK(privilege_control05_set_app_privilege_wgt_platform)
513 {
514     set_app_privilege(__LINE__, WGT_PLATFORM_APP_ID, APP_TYPE_WGT_PLATFORM, PRIVS_WGT,
515             "wgt_platform", WGT_PLATFORM_APP_PATH,
516             LIBPRIVILEGE_TEST_DAC_FILE_WGT, rules_wgt_platform);
517 }
518
519 /**
520  * Set APP privileges. osp app.
521  */
522 RUNNER_CHILD_TEST_SMACK(privilege_control05_set_app_privilege_osp)
523 {
524     set_app_privilege(__LINE__, OSP_APP_ID, APP_TYPE_OSP, PRIVS_OSP, NULL, OSP_APP_PATH,
525             LIBPRIVILEGE_TEST_DAC_FILE_OSP, rules_osp);
526 }
527
528 /**
529  * Set APP privileges. partner osp app.
530  */
531 RUNNER_CHILD_TEST_SMACK(privilege_control05_set_app_privilege_osp_partner)
532 {
533     set_app_privilege(__LINE__, OSP_PARTNER_APP_ID, APP_TYPE_OSP_PARTNER, PRIVS_OSP,
534             NULL, OSP_PARTNER_APP_PATH, LIBPRIVILEGE_TEST_DAC_FILE_OSP, rules_osp_partner);
535 }
536
537 /**
538  * Set APP privileges. platform osp app.
539  */
540 RUNNER_CHILD_TEST_SMACK(privilege_control05_set_app_privilege_osp_platform)
541 {
542     set_app_privilege(__LINE__, OSP_PLATFORM_APP_ID, APP_TYPE_OSP_PLATFORM, PRIVS_OSP,
543             NULL, OSP_PLATFORM_APP_PATH,
544             LIBPRIVILEGE_TEST_DAC_FILE_OSP, rules_osp_platform);
545 }
546
547 /**
548  * Add new API feature
549  */
550 RUNNER_TEST(privilege_control11_add_api_feature)
551 {
552     int result;
553
554     remove_smack_files();
555
556
557     // argument validation
558     result = perm_add_api_feature(APP_TYPE_OSP, NULL, NULL, NULL, 0);
559     RUNNER_ASSERT(result == PC_ERR_INVALID_PARAM);
560
561     result = perm_add_api_feature(APP_TYPE_OSP,"", NULL, NULL, 0);
562     RUNNER_ASSERT(result == PC_ERR_INVALID_PARAM);
563
564
565     // Already existing feature:
566     // TODO: Database will be malformed. (Rules for these features will be removed.)
567     result = perm_add_api_feature(APP_TYPE_OSP,"http://tizen.org/privilege/messaging.read", NULL, NULL, 0);
568     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "perm_add_api_feature returned: " << result);
569
570     result = perm_add_api_feature(APP_TYPE_WGT,"http://tizen.org/privilege/messaging.sms", NULL, NULL, 0);
571     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "perm_add_api_feature returned: " << result);
572
573     // empty features
574     result = perm_add_api_feature(APP_TYPE_OSP,"blahblah", NULL, NULL, 0);
575     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "perm_add_api_feature returned: " << result);
576
577     result = perm_add_api_feature(APP_TYPE_WGT,"blahblah", NULL, NULL, 0);
578     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "perm_add_api_feature returned: " << result);
579
580
581     // empty rules
582     result = perm_add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE[0].c_str(), { NULL }, NULL, 0);
583     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "perm_add_api_feature returned: " << result);
584
585     result = perm_add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE[1].c_str(), (const char*[]) { "", NULL }, NULL, 0);
586     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "perm_add_api_feature returned: " << result);
587
588     result = perm_add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE[2].c_str(), (const char*[]) { " \t\n", "\t \n", "\n\t  ", NULL }, NULL, 0);
589     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "perm_add_api_feature returned: " << result);
590
591     // malformed rules
592     result = perm_add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE[3].c_str(), (const char*[]) { "malformed", NULL }, NULL, 0);
593     RUNNER_ASSERT_MSG(result == PC_ERR_INVALID_PARAM, "perm_add_api_feature returned: " << result);
594
595     result = perm_add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE[4].c_str(), (const char*[]) { "malformed malformed", NULL }, NULL, 0);
596     RUNNER_ASSERT_MSG(result == PC_ERR_INVALID_PARAM, "perm_add_api_feature returned: " << result);
597
598     result = perm_add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE[5].c_str(), (const char*[]) { "-malformed malformed rwxat", NULL }, NULL, 0);
599     RUNNER_ASSERT_MSG(result == PC_ERR_INVALID_PARAM, "perm_add_api_feature returned: " << result);
600
601     result = perm_add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE[6].c_str(), (const char*[]) { "~/\"\\ malformed rwxat", NULL }, NULL, 0);
602     RUNNER_ASSERT_MSG(result == PC_ERR_INVALID_PARAM, "perm_add_api_feature returned: " << result);
603
604     result = perm_add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE[7].c_str(), (const char*[]) { "subject object rwxat something else", NULL }, NULL, 0);
605     RUNNER_ASSERT_MSG(result == PC_ERR_INVALID_PARAM, "perm_add_api_feature returned: " << result);
606
607
608     // correct rules
609     result = perm_add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE[8].c_str(), (const char*[]) {
610             "~APP~    object\t rwxatl",
611             " \t \n",
612             "subject2\t~APP~ ltxarw",
613             "",
614             NULL
615         }, NULL, 0);
616     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "perm_add_api_feature returned: " << result);
617
618     result = perm_add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE[9].c_str(), (const char*[]) {
619             "Sub::jE,ct ~APP~ a-rwxl",
620             NULL
621         }, NULL, 0);
622     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "perm_add_api_feature returned: " << result);
623
624     result = perm_add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE[10].c_str(), (const char*[]) {
625             "Sub::sjE,ct ~APP~ a-RwXL", // TODO This fails.
626             NULL
627         }, NULL, 0);
628     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "perm_add_api_feature returned: " << result);
629
630
631     // TODO For now identical/complementary rules are not merged.
632     result = perm_add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE[11].c_str(), (const char*[]) {
633             "subject1 ~APP~ rwxatl",
634             " \t \n",
635             "subject2 ~APP~ ltxarw",
636             "",
637             NULL
638         }, NULL, 0);
639     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "perm_add_api_feature returned: " << result);
640
641
642     // empty group ids
643     result = perm_add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE[12].c_str(), (const char*[]) {"~APP~ b a",NULL},(const gid_t[]) {0,1,2},0);
644     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "perm_add_api_feature returned: " << result);
645     result = file_exists(OSP_BLAHBLAH_DAC[12].c_str());
646     RUNNER_ASSERT(result == -1);
647     remove_smack_files();
648
649
650     // valid group ids
651     result = perm_add_api_feature(APP_TYPE_OSP,BLAHBLAH_FEATURE[13].c_str(), (const char*[]) {"~APP~ b a",NULL},(const gid_t[]) {0,1,2},3);
652     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "perm_add_api_feature returned: " << result);
653     osp_blahblah_dac_check(__LINE__, {0,1,2}, OSP_BLAHBLAH_DAC[13]);
654     remove_smack_files();
655
656     result = perm_add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE[14].c_str(), (const char*[]) {"~APP~ b a",NULL},(const gid_t[]) {0,1,2},1);
657     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "perm_add_api_feature returned: " << result);
658     osp_blahblah_dac_check(__LINE__, {0}, OSP_BLAHBLAH_DAC[14]);
659     remove_smack_files();
660
661     result = perm_add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE[15].c_str(), (const char*[]) {"~APP~ b a",NULL},(const gid_t[]) {1,1,1},3);
662     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "perm_add_api_feature returned: " << result);
663     osp_blahblah_dac_check(__LINE__, {1,1,1},OSP_BLAHBLAH_DAC[15]);
664     remove_smack_files();
665 }
666
667 /*
668  * Check perm_app_install function
669  */
670 RUNNER_TEST(privilege_control01_app_install)
671 {
672     int result;
673
674
675     perm_app_uninstall(APP_ID);
676
677     result = perm_app_install(APP_ID);
678     RUNNER_ASSERT_MSG(result == 0, "perm_app_install returned " << result << ". Errno: " << strerror(errno));
679
680     // try install second time app with the same ID - it should pass.
681     result = perm_app_install(APP_ID);
682     RUNNER_ASSERT_MSG(result == 0, "perm_app_install returned " << result << ". Errno: " << strerror(errno));
683 }
684
685 /*
686  * Check perm_app_install function
687  */
688 RUNNER_TEST(privilege_control07_app_uninstall)
689 {
690     int result;
691     int fd = -1;
692
693     result = perm_app_uninstall(APP_ID);
694     RUNNER_ASSERT_MSG(result == 0, "perm_app_uninstall returned " << result << ". Errno: " << strerror(errno));
695
696     // checking if file really exists
697     fd = open(SMACK_RULES_DIR APP_ID, O_RDONLY);
698     RUNNER_ASSERT_MSG(fd == -1, "SMACK file NOT deleted after perm_app_uninstall");
699     close(fd);
700 }
701
702 void checkOnlyAvAccess(const char *av_id, const char *app_id, const char *comment)
703 {
704     int result;
705     result = smack_have_access(av_id, app_id, "rwx");
706     RUNNER_ASSERT_MSG(result == 1,
707         "Error while checking " << av_id << " rwx access to "
708         << app_id << " " << comment << " Result: " << result);
709     result = smack_have_access(av_id, app_id, "a");
710     RUNNER_ASSERT_MSG(result == 0,
711         "Error while checking " << av_id << " a access to "
712         << app_id << " " << comment << " Result: " << result);
713     result = smack_have_access(av_id, app_id, "t");
714     RUNNER_ASSERT_MSG(result == 0,
715         "Error while checking " << av_id << " t access to "
716         << app_id << " " << comment << " Result: " << result);
717 }
718
719 /*
720  * Check app_register_av function
721  * Notice that this test case may have no sense if previous would fail (privilege_control06_app_install)
722  */
723 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
724 RUNNER_TEST_SMACK(privilege_control10_app_register_av)
725 {
726     RUNNER_IGNORED_MSG("app_register_av is not implemented");
727     int result;
728
729     // cleaning
730     smack_revoke_subject(APP_TEST_AV_1);
731     smack_revoke_subject(APP_TEST_AV_2);
732
733     cleaning_smack_app_files();
734
735     // Adding two apps before antivir
736     result = perm_app_install(APP_TEST_APP_1);
737     RUNNER_ASSERT_MSG(result == 0, "perm_app_install returned " << result << ". Errno: " << strerror(errno));
738
739     result = perm_app_install(APP_TEST_APP_2);
740     RUNNER_ASSERT_MSG(result == 0, "perm_app_install returned " << result << ". Errno: " << strerror(errno));
741
742     // Adding antivir
743     result = app_register_av(APP_TEST_AV_1);
744     RUNNER_ASSERT_MSG(result == 0, "app_register_av returned " << result << ". Errno: " << strerror(errno));
745
746     // Checking added apps accesses
747     checkOnlyAvAccess(APP_TEST_AV_1, APP_TEST_APP_1, "app_register_av(APP_TEST_AV_1)");
748     checkOnlyAvAccess(APP_TEST_AV_1, APP_TEST_APP_2, "app_register_av(APP_TEST_AV_1)");
749
750     // Adding third app
751     result = perm_app_install(APP_TEST_APP_3);
752     RUNNER_ASSERT_MSG(result == 0, "perm_app_install returned " << result << ". Errno: " << strerror(errno));
753
754     // Checking app accesses
755     checkOnlyAvAccess(APP_TEST_AV_1, APP_TEST_APP_1, "perm_app_install(APP_TEST_APP_3)");
756     checkOnlyAvAccess(APP_TEST_AV_1, APP_TEST_APP_2, "perm_app_install(APP_TEST_APP_3)");
757     checkOnlyAvAccess(APP_TEST_AV_1, APP_TEST_APP_3, "perm_app_install(APP_TEST_APP_3)");
758
759     // Adding second antivir
760     result = app_register_av(APP_TEST_AV_2);
761     RUNNER_ASSERT_MSG(result == 0, "app_register_av returned " << result << ". Errno: " << strerror(errno));
762
763     // Checking app accesses
764     checkOnlyAvAccess(APP_TEST_AV_1, APP_TEST_APP_1, "app_register_av(APP_TEST_AV_2)");
765     checkOnlyAvAccess(APP_TEST_AV_1, APP_TEST_APP_2, "app_register_av(APP_TEST_AV_2)");
766     checkOnlyAvAccess(APP_TEST_AV_1, APP_TEST_APP_3, "app_register_av(APP_TEST_AV_2)");
767     checkOnlyAvAccess(APP_TEST_AV_2, APP_TEST_APP_1, "app_register_av(APP_TEST_AV_2)");
768     checkOnlyAvAccess(APP_TEST_AV_2, APP_TEST_APP_2, "app_register_av(APP_TEST_AV_2)");
769     checkOnlyAvAccess(APP_TEST_AV_2, APP_TEST_APP_3, "app_register_av(APP_TEST_AV_2)");
770
771     // cleaning
772     smack_revoke_subject(APP_TEST_AV_1);
773     smack_revoke_subject(APP_TEST_AV_2);
774
775     cleaning_smack_app_files();
776 }
777 #pragma GCC diagnostic warning "-Wdeprecated-declarations"
778
779 /**
780  * Grant SMACK permissions based on permissions list.
781  */
782 RUNNER_TEST_SMACK(privilege_control11_app_enable_permissions)
783 {
784     int result;
785
786     // Clean up after test:
787     result = perm_app_uninstall(WGT_APP_ID);
788     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "perm_app_uninstall returned " << result << ". Errno: " << strerror(errno));
789     result = perm_app_install(WGT_APP_ID);
790     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "perm_app_install returned " << result << ". Errno: " << strerror(errno));
791
792 /**
793  * Test - Enabling all permissions with persistant mode enabled
794  */
795     result = perm_app_revoke_permissions(WGT_APP_ID);
796     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
797         "Error revoking app permissions. Result: " << result);
798
799     result = perm_app_enable_permissions(WGT_APP_ID, APP_TYPE_WGT, PRIVS2, 1);
800     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
801         " Error enabling app permissions. Result: " << result);
802
803     // Check if the accesses are realy applied..
804     result = test_have_all_accesses(rules2);
805     RUNNER_ASSERT_MSG(result == 1, "Permissions not added.");
806
807     // Clean up
808     result = perm_app_revoke_permissions(WGT_APP_ID);
809     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
810         "Error revoking app permissions. Result: " << result);
811
812 /**
813  * Test - Enabling all permissions with persistant mode disabled
814  */
815
816     result = perm_app_enable_permissions(WGT_APP_ID, APP_TYPE_WGT, PRIVS2, 0);
817     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
818         " Error enabling app permissions. Result: " << result);
819
820     // Check if the accesses are realy applied..
821     result = test_have_all_accesses(rules2);
822     RUNNER_ASSERT_MSG(result == 1, "Permissions not added.");
823
824     // Clean up
825     result = perm_app_revoke_permissions(WGT_APP_ID);
826     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
827         "Error revoking app permissions. Result: " << result);
828
829 /**
830  * Test - Enabling all permissions in two complementary files
831  */
832
833     result = perm_app_enable_permissions(WGT_APP_ID, APP_TYPE_WGT, PRIVS2_R_AND_NO_R, 1);
834     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
835         " Error enabling app permissions. Result: " << result);
836
837     // Check if the accesses are realy applied..
838     result = test_have_all_accesses(rules2_no_r);
839     RUNNER_ASSERT_MSG(result == 1, "Permissions not added.");
840
841     // Clean up
842     result = perm_app_revoke_permissions(WGT_APP_ID);
843     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
844         "Error revoking app permissions. Result: " << result);
845
846 /**
847  * Test - Enabling some permissions and then enabling complementary permissions
848  */
849
850     // Enable permission for rules 2 no r
851     result = perm_app_enable_permissions(WGT_APP_ID, APP_TYPE_WGT, PRIVS2_NO_R, 1);
852     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
853         " Error enabling app permissions without r. Result: " << result);
854
855     // Check if the accesses are realy applied..
856     result = test_have_all_accesses(rules2_no_r);
857     RUNNER_ASSERT_MSG(result == 1, "Permissions without r not added.");
858
859     // Enable permission for rules 2
860     result = perm_app_enable_permissions(WGT_APP_ID, APP_TYPE_WGT, PRIVS2, 1);
861     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
862         " Error enabling app all permissions. Result: " << result);
863
864     // Check if the accesses are realy applied..
865     result = test_have_all_accesses(rules2);
866     RUNNER_ASSERT_MSG(result == 1, "Permissions all not added.");
867
868     // Clean up
869     result = perm_app_revoke_permissions(WGT_APP_ID);
870     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
871         "Error revoking app permissions. Result: " << result);
872
873 /**
874  * Test - Enabling some permissions and then enabling all permissions
875  */
876
877     // Enable permission for rules 2 no r
878     result = perm_app_enable_permissions(WGT_APP_ID, APP_TYPE_WGT, PRIVS2_NO_R, 1);
879     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
880         " Error enabling app permissions without r. Result: " << result);
881
882     // Check if the accesses are realy applied..
883     result = test_have_all_accesses(rules2_no_r);
884     RUNNER_ASSERT_MSG(result == 1, "Permissions without r not added.");
885
886     // Enable permission for rules 2
887     result = perm_app_enable_permissions(WGT_APP_ID, APP_TYPE_WGT, PRIVS2_R, 1);
888     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
889         " Error enabling app permissions with only r. Result: " << result);
890
891     // Check if the accesses are realy applied..
892     result = test_have_all_accesses(rules2_r);
893     RUNNER_ASSERT_MSG(result == 1, "Permissions with only r not added.");
894
895     // Clean up
896     result = perm_app_revoke_permissions(WGT_APP_ID);
897     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
898         "Error revoking app permissions. Result: " << result);
899
900
901
902     // Clean up after test:
903     result = perm_app_uninstall(WGT_APP_ID);
904     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "perm_app_uninstall returned " << result << ". Errno: " << strerror(errno));
905
906 }
907
908 RUNNER_CHILD_TEST(privilege_control11_app_enable_permissions_efl)
909 {
910     int result;
911
912     // Prepare
913     result = perm_app_uninstall(EFL_APP_ID);
914     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
915             "perm_app_uninstall failed: " << result);
916     result = perm_app_install(EFL_APP_ID);
917     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
918             "perm_app_install failed: " << result);
919
920     // Enable a permission:
921     result = perm_app_enable_permissions(EFL_APP_ID, APP_TYPE_EFL, PRIVS_EFL, 0);
922     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
923         "Error enabling app permissions. Result: " << result);
924
925     RUNNER_ASSERT_MSG(smack_have_access(EFL_APP_ID,"test_book_efl", "r"),
926             "SMACK accesses not granted for EFL_APP");
927
928     // Cleanup
929     result = perm_app_uninstall(EFL_APP_ID);
930     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
931             "perm_app_uninstall failed: " << result);
932 }
933
934 /*
935  * Check perm_app_install function
936  */
937 RUNNER_CHILD_TEST(privilege_control12_app_disable_permissions_efl)
938 {
939     int result;
940
941     // Prepare
942     result = perm_app_uninstall(EFL_APP_ID);
943     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
944             "perm_app_uninstall failed: " << result);
945
946     result = perm_app_install(EFL_APP_ID);
947     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
948             "perm_app_install failed: " << result);
949
950     // Enable a permission
951     result = perm_app_enable_permissions(EFL_APP_ID, APP_TYPE_EFL, PRIVS_EFL, 0);
952     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
953         "Error enabling app permissions. Result: " << result);
954
955     RUNNER_ASSERT_MSG(smack_have_access(EFL_APP_ID,"test_book_efl", "r"),
956             "SMACK accesses not granted for EFL_APP");
957
958     // Disable a permission
959     result = perm_app_disable_permissions(EFL_APP_ID, APP_TYPE_EFL, PRIVS_EFL);
960     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
961         "Error disabling app permissions. Result: " << result);
962
963     RUNNER_ASSERT_MSG(!smack_have_access(EFL_APP_ID,"test_book_efl", "r"),
964             "SMACK accesses not granted for EFL_APP");
965
966     // Cleanup
967     result = perm_app_uninstall(EFL_APP_ID);
968     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
969             "perm_app_uninstall failed: " << result);
970 }
971
972
973 /**
974  * Remove previously granted SMACK permissions based on permissions list.
975  */
976 RUNNER_TEST(privilege_control12_app_disable_permissions)
977 {
978     int result;
979
980     // Prepare
981     result = perm_app_uninstall(WGT_APP_ID);
982     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
983             "perm_app_uninstall failed: " << result);
984
985     result = perm_app_install(WGT_APP_ID);
986     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
987             "perm_app_install failed: " << result);
988 /**
989  * Test - disable all granted permissions.
990  */
991
992     // Prepare permissions that we want to disable
993     result = perm_app_enable_permissions(WGT_APP_ID, APP_TYPE_WGT, PRIVS2, 1);
994     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
995             " Error enabling app permissions. Result: " << result);
996
997     // Are all the permissions enabled?
998     result = test_have_any_accesses(rules2);
999     RUNNER_ASSERT_MSG(result==1, "Not all permisions enabled.");
1000
1001     // Disable permissions
1002     result = perm_app_disable_permissions(WGT_APP_ID, APP_TYPE_WGT, PRIVS2);
1003     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1004             "Error disabling app permissions. Result: " << result);
1005
1006     // Are all the permissions disabled?
1007     result = test_have_any_accesses(rules2);
1008     RUNNER_ASSERT_MSG(result!=1, "Not all permisions disabled.");
1009
1010 /**
1011  * Test - disable some granted permissions leaving non complementary and then disabling those too.
1012  */
1013
1014     // Prepare permissions that will not be disabled
1015     result = perm_app_enable_permissions(WGT_APP_ID, APP_TYPE_WGT, PRIVS, 1);
1016     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1017             " Error adding app first permissions. Result: " << result);
1018
1019     // Prepare permissions that we want to disable
1020     result = perm_app_enable_permissions(WGT_APP_ID, APP_TYPE_WGT, PRIVS2, 1);
1021     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1022             " Error adding app second permissions. Result: " << result);
1023
1024     // Disable second permissions
1025     result = perm_app_disable_permissions(WGT_APP_ID, APP_TYPE_WGT, PRIVS2);
1026     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1027             "Error disabling app second permissions. Result: " << result);
1028
1029     // Are all second permissions disabled?
1030     result = test_have_any_accesses(rules2);
1031     RUNNER_ASSERT_MSG(result!=1, "Not all first permisions disabled.");
1032
1033     // Are all first permissions not disabled?
1034     result = test_have_all_accesses(rules_wgt2);
1035     RUNNER_ASSERT_MSG(result==1, "Some of second permissions disabled.");
1036
1037     // Disable first permissions
1038     result = perm_app_disable_permissions(WGT_APP_ID, APP_TYPE_WGT, PRIVS);
1039     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1040             "Error disabling app first permissions. Result: " << result);
1041
1042     // Are all second permissions disabled?
1043     result = test_have_any_accesses(rules_wgt2);
1044     RUNNER_ASSERT_MSG(result!=1, "Not all second permisions disabled.");
1045
1046 /**
1047  * Test - disable only no r granted permissions.
1048  */
1049
1050     // Prepare permissions
1051     result = perm_app_enable_permissions(WGT_APP_ID, APP_TYPE_WGT, PRIVS2_R, 1);
1052     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1053             " Error adding app permissions. Result: " << result);
1054
1055     // Disable same permissions without r
1056     result = perm_app_disable_permissions(WGT_APP_ID, APP_TYPE_WGT, PRIVS2_NO_R);
1057     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1058             "Error disabling app no r permissions. Result: " << result);
1059
1060     // Is any r permissions disabled?
1061     result = test_have_all_accesses(rules2_r);
1062     RUNNER_ASSERT_MSG(result==1, "Some of r permissions disabled.");
1063     // Are all no r permissions disabled?
1064     result = test_have_any_accesses(rules2_no_r);
1065     RUNNER_ASSERT_MSG(result!=1, "Not all no r permissions disabled.");
1066
1067     // Prepare permissions
1068     result = perm_app_enable_permissions(WGT_APP_ID, APP_TYPE_WGT, PRIVS2_NO_R, 1);
1069     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1070             " Error adding app no r permissions. Result: " << result);
1071     result = test_have_any_accesses(rules2_no_r);
1072     RUNNER_ASSERT_MSG(result=1, "Not all no r permissions enabled.");
1073
1074     // Disable all permissions
1075     result = perm_app_disable_permissions(WGT_APP_ID, APP_TYPE_WGT, PRIVS2_R);
1076     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1077             "Error disabling app permissions. Result: " << result);
1078     result = test_have_any_accesses(rules2_r);
1079     RUNNER_ASSERT_MSG(result!=1, "Not all r permissions disabled.");
1080
1081
1082
1083     // Clean up after test:
1084     result = perm_app_uninstall(WGT_APP_ID);
1085     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "perm_app_uninstall returned " << result << ". Errno: " << strerror(errno));
1086 }
1087
1088 /**
1089  * Reset SMACK permissions for an application by revoking all previously
1090  * granted rules and enabling them again from a rules file from disk.
1091  */
1092 // TODO: This test is incomplete.
1093 RUNNER_TEST_SMACK(privilege_control13_app_reset_permissions)
1094 {
1095     int result;
1096
1097 /**
1098  * Test - doing reset and checking if rules exist again.
1099  */
1100
1101     result = perm_app_install(WGT_APP_ID);
1102     RUNNER_ASSERT_MSG(result == 0, "perm_app_install returned " << result << ". Errno: " << strerror(errno));
1103
1104     // Prepare permissions to reset
1105     result = perm_app_enable_permissions(WGT_APP_ID, APP_TYPE_WGT, PRIVS2, 1);
1106     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1107         " Error adding app permissions. Result: " << result);
1108
1109     // Reset permissions
1110     result = perm_app_reset_permissions(WGT_APP_ID);
1111     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1112         "Error reseting app permissions. Result: " << result);
1113
1114     // Are all second permissions not disabled?
1115     result = test_have_all_accesses(rules2);
1116     RUNNER_ASSERT_MSG(result == 1, "Not all permissions added.");
1117
1118     // Disable permissions
1119     result = perm_app_revoke_permissions(WGT_APP_ID);
1120     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1121         "Error disabling app permissions. Result: " << result);
1122
1123     result = perm_app_uninstall(WGT_APP_ID);
1124     RUNNER_ASSERT_MSG(result == 0, "perm_app_uninstall returned " << result << ". Errno: " << strerror(errno));
1125
1126 }
1127
1128 /**
1129  * Make two applications "friends", by giving them both full permissions on
1130  * each other.
1131  */
1132 RUNNER_TEST_SMACK(privilege_control14_app_add_friend)
1133 {
1134     RUNNER_IGNORED_MSG("perm_app_add_friend is not implemented");
1135
1136     int result;
1137
1138 /**
1139  * Test - making friends with no permissions on each other
1140  */
1141
1142     result = perm_app_revoke_permissions(APP_FRIEND_1);
1143     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1144         "Error revoking app permissions. Result: " << result);
1145     result = perm_app_revoke_permissions(APP_FRIEND_2);
1146     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1147         "Error revoking app permissions. Result: " << result);
1148
1149     perm_app_uninstall(APP_FRIEND_1);
1150     perm_app_uninstall(APP_FRIEND_2);
1151
1152     // Installing friends to be
1153     result = perm_app_install(APP_FRIEND_1);
1154     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1155         " Error installing first app. Result: " << result);
1156     result = perm_app_install(APP_FRIEND_2);
1157     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1158         " Error installing second app. Result: " << result);
1159
1160     // Making friends
1161     result = perm_app_add_friend(APP_FRIEND_1, APP_FRIEND_2);
1162     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1163         " Error making friends. Errno: " << result);
1164
1165     // Checking if friends were made
1166     result = smack_have_access(APP_FRIEND_1, APP_FRIEND_2, "wrxat");
1167     RUNNER_ASSERT_MSG(result == 1,
1168         " Error first one sided friednship failed. Result: " << result);
1169     result = smack_have_access(APP_FRIEND_2, APP_FRIEND_1, "wrxat");
1170     RUNNER_ASSERT_MSG(result == 1,
1171         " Error second one sided friednship failed. Result: " << result);
1172
1173     // Clean up
1174     result = perm_app_revoke_permissions(APP_FRIEND_1);
1175     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1176         "Error revoking app permissions. Result: " << result);
1177     result = perm_app_revoke_permissions(APP_FRIEND_2);
1178     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1179         "Error revoking app permissions. Result: " << result);
1180
1181     perm_app_uninstall(APP_FRIEND_1);
1182     perm_app_uninstall(APP_FRIEND_2);
1183
1184 /**
1185  * Test - making friends with nonexistent friend
1186  */
1187
1188     // Installing one friend
1189     result = perm_app_install(APP_FRIEND_1);
1190     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1191         " Error installing first app. Errno: " << result);
1192
1193     // Adding imaginary friend as second
1194     result = perm_app_add_friend(APP_FRIEND_1, APP_FRIEND_2);
1195     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1196         " Error making friends (first) with imaginairy friend failed. Result: "
1197         << result);
1198     // Adding imaginary friend as first
1199     result = perm_app_add_friend(APP_FRIEND_2, APP_FRIEND_1);
1200     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1201         " Error making friends (second) with imaginairy friend failed. Result: "
1202         << result);
1203     // Clean up
1204     result = perm_app_revoke_permissions(APP_FRIEND_1);
1205     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1206         "Error revoking app permissions. Result: " << result);
1207     result = perm_app_revoke_permissions(APP_FRIEND_2);
1208     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1209         "Error revoking app permissions. Result: " << result);
1210
1211     perm_app_uninstall(APP_FRIEND_1);
1212     perm_app_uninstall(APP_FRIEND_2);
1213
1214 /**
1215  * Test - making friends with some permissions already added
1216  */
1217     unsigned int i;
1218     unsigned int j;
1219
1220     struct smack_accesses *rulesFriend = NULL;
1221
1222     std::vector<std::string> accessesFriend =
1223     { "r", "w", "x", "rw", "rx", "wx", "rwx", "rwxat" };
1224
1225     // Installing friends to be
1226     result = perm_app_install(APP_FRIEND_1);
1227     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1228         " Error installing first app. Result: " << result);
1229     result = perm_app_install(APP_FRIEND_2);
1230     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1231         " Error installing second app. Result: " << result);
1232
1233     for (i = 0; i < accessesFriend.size(); ++i)
1234     {
1235         for (j = 0; j < accessesFriend.size(); ++j)
1236         {
1237             // Adding rules before making friends
1238             result = smack_accesses_new(&rulesFriend);
1239             RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1240                 "Error in smack_accesses_new. Result: " << result);
1241
1242             result = smack_accesses_add(rulesFriend,
1243                 APP_FRIEND_1, APP_FRIEND_2, accessesFriend[i].c_str());
1244             RUNNER_ASSERT_MSG(result == 0,
1245                 "Unable to add modify rulesFirend (first). Result: " << result);
1246             result = smack_accesses_add(rulesFriend, APP_FRIEND_2,
1247                 APP_FRIEND_1, accessesFriend[j].c_str());
1248             RUNNER_ASSERT_MSG(result == 0,
1249                 "Unable to add modify rulesFirend (second). Result: " << result);
1250
1251             result = smack_accesses_apply(rulesFriend);
1252             RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1253                 "Error in smack_accesses_apply. Result: " << result);
1254
1255             // Adding friends
1256             result = perm_app_add_friend(APP_FRIEND_1, APP_FRIEND_2);
1257             RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1258                 " Error making friends. Result: " << result);
1259
1260             // Checking if friends were made
1261             result = smack_have_access(APP_FRIEND_1, APP_FRIEND_2, "wrxat");
1262             RUNNER_ASSERT_MSG(result == 1,
1263                 " Error first one sided friednship failed. Result: " << result);
1264             result = smack_have_access(APP_FRIEND_2, APP_FRIEND_1, "wrxat");
1265             RUNNER_ASSERT_MSG(result == 1,
1266                 " Error second one sided friednship failed. Result: " << result);
1267
1268             // Deleting all rules between friends
1269             smack_accesses_add_modify(rulesFriend,
1270                 APP_FRIEND_1, APP_FRIEND_2,"","rwxat");
1271             smack_accesses_add_modify(rulesFriend,
1272                 APP_FRIEND_2, APP_FRIEND_1,"","rwxat");
1273
1274             result = smack_accesses_apply(rulesFriend);
1275
1276             smack_accesses_free(rulesFriend);
1277             rulesFriend = NULL;
1278         }
1279     }
1280
1281     // Clean up
1282     result = perm_app_revoke_permissions(APP_FRIEND_1);
1283     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1284         "Error revoking app permissions. Result: " << result);
1285     result = perm_app_revoke_permissions(APP_FRIEND_2);
1286     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1287         "Error revoking app permissions. Result: " << result);
1288
1289     perm_app_uninstall(APP_FRIEND_1);
1290     perm_app_uninstall(APP_FRIEND_2);
1291 }
1292
1293 static void smack_set_random_label_based_on_pid_on_self(void)
1294 {
1295     int result;
1296     std::stringstream ss;
1297
1298     ss << "s-" << getpid() << "-" << getppid();
1299     result = smack_set_label_for_self(ss.str().c_str());
1300     RUNNER_ASSERT_MSG(result == 0, "smack_set_label_for_self("
1301         << ss.str().c_str() << ") failed");
1302 }
1303
1304 static void smack_unix_sock_server(int sock)
1305 {
1306     int fd, result;
1307     char *smack_label;
1308
1309     alarm(2);
1310     fd = accept(sock, NULL, NULL);
1311     alarm(0);
1312     if (fd < 0)
1313         return;
1314     result = smack_new_label_from_self(&smack_label);
1315     if (result < 0) {
1316         close(fd);
1317         close(sock);
1318         free(smack_label);
1319         RUNNER_ASSERT_MSG(0, "smack_new_label_from_self() failed");
1320     }
1321     result = write(fd, smack_label, strlen(smack_label));
1322     if (result != (int)strlen(smack_label)) {
1323         close(fd);
1324         close(sock);
1325         free(smack_label);
1326         RUNNER_ASSERT_MSG(0, "write() failed: " << strerror(errno));
1327     }
1328     close(fd);
1329     free(smack_label);
1330 }
1331
1332 RUNNER_MULTIPROCESS_TEST_SMACK(privilege_control15_app_id_from_socket)
1333 {
1334     int pid;
1335     struct sockaddr_un sockaddr = {AF_UNIX, SOCK_PATH};
1336
1337     unlink(SOCK_PATH);
1338     pid = fork();
1339     RUNNER_ASSERT_MSG(pid >= 0, "Fork failed");
1340
1341     smack_set_random_label_based_on_pid_on_self();
1342
1343     if (!pid) { /* child process, server */
1344         int sock, result;
1345
1346         /* Set the process label before creating a socket */
1347         sock = socket(AF_UNIX, SOCK_STREAM, 0);
1348         RUNNER_ASSERT_MSG(sock >= 0, "socket failed: " << strerror(errno));
1349         result = bind(sock,
1350             (struct sockaddr*) &sockaddr, sizeof(struct sockaddr_un));
1351         if (result != 0) {
1352             close(sock);
1353             RUNNER_ASSERT_MSG(0, "bind failed: " << strerror(errno));
1354         }
1355         result = listen(sock, 1);
1356         if (result != 0) {
1357             close(sock);
1358             RUNNER_ASSERT_MSG(0, "listen failed: " << strerror(errno));
1359         }
1360         smack_unix_sock_server(sock);
1361
1362         /* Change the process label with listening socket */
1363         smack_unix_sock_server(sock);
1364
1365         pid = fork();
1366         RUNNER_ASSERT_MSG(pid >= 0, "Fork failed");
1367         /* Now running two concurrent servers.
1368            Test if socket label was unaffected by fork() */
1369         smack_unix_sock_server(sock);
1370         /* Let's give the two servers different labels */
1371         smack_unix_sock_server(sock);
1372         close(sock);
1373
1374         exit(0);
1375     } else { /* parent process, client */
1376         sleep(1); /* Give server some time to setup listening socket */
1377         int i;
1378         for (i = 0; i < 4; ++i) {
1379             int sock;
1380             int result;
1381             char smack_label1[SMACK_LABEL_LEN + 1];
1382             char *smack_label2;
1383
1384             sock = socket(AF_UNIX, SOCK_STREAM, 0);
1385             RUNNER_ASSERT_MSG(sock >= 0,
1386                 "socket failed: " << strerror(errno));
1387             result = connect(sock,
1388                 (struct sockaddr*) &sockaddr, sizeof(struct sockaddr_un));
1389             if (result != 0) {
1390                 close(sock);
1391                 RUNNER_ASSERT_MSG(0, "connect failed: " << strerror(errno));
1392             }
1393
1394             alarm(2);
1395             result = read(sock, smack_label1, SMACK_LABEL_LEN);
1396             alarm(0);
1397             if (result < 0) {
1398                 close(sock);
1399                 RUNNER_ASSERT_MSG(0, "read failed: " << strerror(errno));
1400             }
1401             smack_label1[result] = '\0';
1402             smack_label2 = perm_app_id_from_socket(sock);
1403             if (smack_label2 == NULL) {
1404                 close(sock);
1405                 RUNNER_ASSERT_MSG(0, "perm_app_id_from_socket failed");
1406             }
1407             result = strcmp(smack_label1, smack_label2);
1408             if (result != 0) {
1409                 close(sock);
1410                 RUNNER_ASSERT_MSG(0, "smack labels differ: '" << smack_label1
1411                     << "' != '" << smack_label2 << "-" << random() << "'");
1412             }
1413             close(sock);
1414         }
1415     }
1416 }
1417
1418 RUNNER_TEST(privilege_control16_app_setup_path){
1419     const char *path1 = "/usr/share/privilege-control/app_setup_access_test";
1420     const char *path2 = "/usr/share/privilege-control/app_setup_access_test/directory";
1421     const char *path3 = "/usr/share/privilege-control/app_setup_access_test/one";
1422     const char *path4 = "/usr/share/privilege-control/app_setup_access_test/directory/two";
1423     const char *label1 = "qwert123456za";
1424     const char *label2 = "trewq654123az";
1425
1426     std::unique_ptr<char, std::function<void(void*)> > labelPtr(NULL,free);
1427
1428     mkdir(path1,0);
1429     mkdir(path2,0);
1430
1431     int fd = creat(path3, S_IRWXU);
1432     if (fd >= 0)
1433         close(fd);
1434     fd = creat(path4, S_IRWXU);
1435     if (fd >= 0)
1436         close(fd);
1437
1438     char *label = NULL;
1439
1440     RUNNER_ASSERT(PC_OPERATION_SUCCESS == perm_app_setup_path("somepackageid", path1, APP_PATH_ANY_LABEL, label1));
1441     RUNNER_ASSERT(0 == smack_lgetlabel(path3, &label, SMACK_LABEL_ACCESS));
1442     labelPtr.reset(label);
1443     label = NULL;
1444     RUNNER_ASSERT(0 == strcmp(labelPtr.get(), label1));
1445
1446     RUNNER_ASSERT(PC_OPERATION_SUCCESS == perm_app_setup_path("somepackageid", path1, APP_PATH_ANY_LABEL, label2));
1447     RUNNER_ASSERT(0 == smack_lgetlabel(path4, &label, SMACK_LABEL_EXEC));
1448     labelPtr.reset(label);
1449     label = NULL;
1450     RUNNER_ASSERT(0 == strcmp(labelPtr.get(), label2));
1451
1452     RUNNER_ASSERT(0 == smack_lgetlabel(path1, &label, SMACK_LABEL_EXEC));
1453     labelPtr.reset(label);
1454     label = NULL;
1455     RUNNER_ASSERT(labelPtr.get() == NULL);
1456 }
1457
1458 RUNNER_TEST(privilege_control17_appsettings_privilege)
1459 {
1460     int ret;
1461     char *app1_dir_label;
1462     char *app2_dir_label;
1463     //prepare test
1464
1465     (void)perm_app_uninstall(APP_TEST);
1466     (void)perm_app_uninstall(APP_1);
1467     (void)perm_app_uninstall(APP_2);
1468
1469     //install some app 1
1470     ret = perm_app_install(APP_1);
1471     RUNNER_ASSERT_MSG(ret == PC_OPERATION_SUCCESS, "Error in perm_app_install." << ret);
1472
1473     mkdir(APP_1_DIR, S_IRWXU | S_IRGRP | S_IXGRP);
1474
1475     //register settings folder for app 1
1476     ret = perm_app_setup_path(APP_1, APP_1_DIR, APP_PATH_SETTINGS_RW );
1477     RUNNER_ASSERT_MSG(ret == PC_OPERATION_SUCCESS, "Error in perm_app_setup_path: " << ret);
1478
1479     //install "app_test" and give it appsettings privilege
1480     ret = perm_app_install(APP_TEST);
1481     RUNNER_ASSERT_MSG(ret == PC_OPERATION_SUCCESS, "Error in perm_app_install.");
1482
1483
1484     ret = perm_app_enable_permissions(APP_TEST, APP_TYPE_OSP, PRIV_APPSETTING, true);
1485     RUNNER_ASSERT_MSG(ret == PC_OPERATION_SUCCESS,
1486         " Error enabling app permissions. Result: " << ret);
1487
1488     //check if "app_test" has an RX access to the app "app_1"
1489     ret = smack_have_access(APP_TEST, APP_1, "rx");
1490     RUNNER_ASSERT_MSG(ret,"access denied");
1491
1492     //check if "app_test" has an RWX access to a folder registered by "app_1"
1493     ret = smack_getlabel(APP_1_DIR, &app1_dir_label, SMACK_LABEL_ACCESS );
1494     RUNNER_ASSERT_MSG(ret == PC_OPERATION_SUCCESS,"smack_getlabel failed");
1495     ret = smack_have_access(APP_TEST, app1_dir_label, "rwx");
1496     RUNNER_ASSERT_MSG(ret,"access denied to smack label: " << app1_dir_label);
1497
1498
1499     //intstall another app: "app_2"
1500     ret = perm_app_install(APP_2);
1501     RUNNER_ASSERT_MSG(ret == PC_OPERATION_SUCCESS, "Error in perm_app_install.");
1502
1503     mkdir(APP_2_DIR, S_IRWXU | S_IRGRP | S_IXGRP);
1504     //register settings folder for that "app_2"
1505     ret = perm_app_setup_path(APP_2, APP_2_DIR, APP_PATH_SETTINGS_RW );
1506     RUNNER_ASSERT_MSG(ret == PC_OPERATION_SUCCESS, "Error in perm_app_setup_path: " << ret);
1507
1508     //check if "app_test" has an RX access to the app "app_2"
1509     ret = smack_have_access(APP_TEST, APP_2, "rx");
1510     RUNNER_ASSERT_MSG(ret,"access denies");
1511
1512     //check if "app_test" has an RWX access to a folder registered by "app_2"
1513     ret = smack_getlabel(APP_2_DIR, &app2_dir_label, SMACK_LABEL_ACCESS );
1514     RUNNER_ASSERT_MSG(ret == PC_OPERATION_SUCCESS,"smack_getlabel failed");
1515     ret = smack_have_access(APP_TEST, app2_dir_label, "rwx");
1516     RUNNER_ASSERT_MSG(ret,"access denies");
1517
1518     free (app1_dir_label);
1519     free (app2_dir_label);
1520     rmdir(APP_1_DIR);
1521     rmdir(APP_2_DIR);
1522
1523     (void)perm_app_uninstall(APP_TEST);
1524     (void)perm_app_uninstall(APP_1);
1525     (void)perm_app_uninstall(APP_2);
1526 }
1527
1528 void test_app_setup_path(int line_no, app_path_type_t PATH_TYPE) {
1529     int result;
1530
1531     result = perm_app_uninstall(APP_ID);
1532     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "Line: " << line_no <<
1533             " Error in perm_app_uninstall." << result);
1534
1535     result = perm_app_install(APP_ID);
1536     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "Line: " << line_no <<
1537             " Error in perm_app_install." << result);
1538
1539     result = nftw(TEST_APP_DIR, &nftw_remove_labels, FTW_MAX_FDS, FTW_PHYS);
1540     RUNNER_ASSERT_MSG(result == 0, "Line: " << line_no <<
1541             " Unable to clean up Smack labels in " << TEST_APP_DIR);
1542
1543     result = nftw(TEST_NON_APP_DIR, &nftw_set_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
1544     RUNNER_ASSERT_MSG(result == 0, "Line: " << line_no <<
1545             " Unable to clean up Smack labels in " << TEST_NON_APP_DIR);
1546
1547     result = perm_app_setup_path(APP_ID, TEST_APP_DIR, PATH_TYPE);
1548     RUNNER_ASSERT_MSG(result == 0, "Line: " << line_no <<
1549             " perm_app_setup_path() failed");
1550
1551     result = nftw(TEST_NON_APP_DIR, &nftw_check_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
1552     RUNNER_ASSERT_MSG(result == 0, "Line: " << line_no <<
1553             " Unable to check Smack labels for non-app dir");
1554
1555     result = perm_app_uninstall(APP_ID);
1556     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "Line: " << line_no <<
1557             " Error in perm_app_uninstall." << result);
1558 }
1559
1560 RUNNER_TEST_SMACK(privilege_control18_app_setup_path_public)
1561 {
1562     test_app_setup_path(__LINE__, APP_PATH_PUBLIC_RO);
1563 }
1564
1565 RUNNER_TEST_SMACK(privilege_control19_app_setup_path_settings)
1566 {
1567     test_app_setup_path(__LINE__, APP_PATH_SETTINGS_RW);
1568 }
1569
1570 RUNNER_TEST(privilege_control20_early_rules)
1571 {
1572     RUNNER_IGNORED_MSG("early rules are not implemented");
1573
1574     int result;
1575     int fd = -1;
1576     int pass_1 = 0;
1577     int pass_2 = 0;
1578     char *single_line_format = NULL;
1579     char *perm = NULL;
1580     FILE *file = NULL;
1581
1582     char subject[SMACK_LABEL_LEN + 1] = {0};
1583     char object[SMACK_LABEL_LEN + 1] = {0};
1584     char rule_add[SMACK_ACC_LEN + 1] = {0};
1585     char rule_remove[SMACK_ACC_LEN + 1] = {0};
1586
1587     unlink(SMACK_RULES_DIR APP_ID);
1588
1589     perm_app_uninstall(APP_ID);
1590
1591     result = perm_app_install(APP_ID);
1592     RUNNER_ASSERT_MSG(result == 0, "perm_app_install returned " << result << ". Errno: " << strerror(errno));
1593     result = perm_app_install(APP_TEST_APP_1);
1594     RUNNER_ASSERT_MSG(result == 0, "perm_app_install returned " << result << ". Errno: " << strerror(errno));
1595
1596     // checking if file really exists
1597     fd = open(SMACK_RULES_DIR APP_ID, O_RDONLY);
1598     close(fd);
1599     RUNNER_ASSERT_MSG(fd >= 0, "File open failed: " << SMACK_RULES_DIR << APP_ID << " : " << fd << ". Errno: " << strerror(errno));
1600     fd = -1;
1601
1602     result = perm_app_enable_permissions(APP_ID, APP_TYPE_WGT, (const char**) &perm, 1);
1603     RUNNER_ASSERT_MSG(result == 0, "app_enable_permission failed: " << result);
1604     result = perm_app_enable_permissions(APP_TEST_APP_1, APP_TYPE_WGT, (const char**) &perm, 1);
1605     RUNNER_ASSERT_MSG(result == 0, "app_enable_permission failed: " << result);
1606
1607     file = fopen(SMACK_STARTUP_RULES_FILE, "r");
1608     RUNNER_ASSERT_MSG(file != NULL, "File open failed: " << SMACK_STARTUP_RULES_FILE << " : " << file << ". Errno: " << strerror(errno));
1609
1610     result = asprintf(&single_line_format, "%%%ds %%%ds %%%ds %%%ds\\n", SMACK_LABEL_LEN, SMACK_LABEL_LEN, SMACK_ACC_LEN, SMACK_ACC_LEN);
1611
1612     while(fscanf(file, single_line_format, subject, object, rule_add, rule_remove) == 4) {
1613         if(strncmp(subject, EARLY_RULE_SUBJECT, SMACK_LABEL_LEN) == 0 && strncmp(object, APP_ID, SMACK_LABEL_LEN) == 0) {
1614             pass_1 = 1; // Found rule for APP_ID
1615             continue;
1616         }
1617         if(strncmp(subject, EARLY_RULE_SUBJECT, SMACK_LABEL_LEN) == 0 && strncmp(object, APP_TEST_APP_1, SMACK_LABEL_LEN) == 0) {
1618             pass_2 = 1; // Found rule for APP_TEST_APP_1
1619             continue;
1620         }
1621     }
1622     fclose(file);
1623     file = NULL;
1624
1625     RUNNER_ASSERT_MSG(pass_1 == 1, "Rule " << EARLY_RULE_SUBJECT << " " << APP_ID << " " << EARLY_RULE_RIGHTS << " not found");
1626     RUNNER_ASSERT_MSG(pass_2 == 1, "Rule " << EARLY_RULE_SUBJECT << " " << APP_TEST_APP_1 << " " << EARLY_RULE_RIGHTS << " not found");
1627
1628     // Checking if "early rule" for APP_ID was really removed
1629     // We also should make sure that "early rules" for other apps wasn't removed
1630     result = perm_app_uninstall(APP_ID);
1631     RUNNER_ASSERT_MSG(result == 0, "perm_app_uninstall returned " << result << ". Errno: " << strerror(errno));
1632     pass_1 = 1;
1633     pass_2 = 0;
1634
1635     file = fopen(SMACK_STARTUP_RULES_FILE, "r");
1636         RUNNER_ASSERT_MSG(file != NULL, "File open failed: " << SMACK_STARTUP_RULES_FILE << " : " << file << ". Errno: " << strerror(errno));
1637
1638         while(fscanf(file, single_line_format, subject, object, rule_add, rule_remove) == 4) {
1639                 if(strncmp(subject, EARLY_RULE_SUBJECT, SMACK_LABEL_LEN) == 0 && strncmp(object, APP_ID, SMACK_LABEL_LEN) == 0) {
1640                         pass_1 = 0; // Found rule for APP_ID - it should NOT be here
1641                         continue;
1642                 }
1643                 if(strncmp(subject, EARLY_RULE_SUBJECT, SMACK_LABEL_LEN) == 0 && strncmp(object, APP_TEST_APP_1, SMACK_LABEL_LEN) == 0) {
1644                         pass_2 = 1; // Found rule for APP_TEST_APP_1
1645                         continue;
1646                 }
1647         }
1648         fclose(file);
1649         file = NULL;
1650
1651     RUNNER_ASSERT_MSG(pass_1 == 1, "Rule " << EARLY_RULE_SUBJECT << " " << APP_ID << " " << EARLY_RULE_RIGHTS << " found");
1652     RUNNER_ASSERT_MSG(pass_2 == 1, "Rule " << EARLY_RULE_SUBJECT << " " << APP_TEST_APP_1 << " " << EARLY_RULE_RIGHTS << " not found");
1653
1654     // Removing and checking "early rule" for APP_TEST_APP_1
1655         result = perm_app_uninstall(APP_TEST_APP_1);
1656         RUNNER_ASSERT_MSG(result == 0, "perm_app_uninstall returned " << result << ". Errno: " << strerror(errno));
1657         pass_1 = 1;
1658         pass_2 = 1;
1659
1660         file = fopen(SMACK_STARTUP_RULES_FILE, "r");
1661         RUNNER_ASSERT_MSG(file != NULL, "File open failed: " << SMACK_STARTUP_RULES_FILE << " : " << file << ". Errno: " << strerror(errno));
1662
1663         while(fscanf(file, single_line_format, subject, object, rule_add, rule_remove) == 4) {
1664                 if(strncmp(subject, EARLY_RULE_SUBJECT, SMACK_LABEL_LEN) == 0 && strncmp(object, APP_ID, SMACK_LABEL_LEN) == 0) {
1665                         pass_1 = 0; // Found rule for APP_ID - it should NOT be here
1666                         continue;
1667                 }
1668                 if(strncmp(subject, EARLY_RULE_SUBJECT, SMACK_LABEL_LEN) == 0 && strncmp(object, APP_TEST_APP_1, SMACK_LABEL_LEN) == 0) {
1669                         pass_2 = 0; // Found rule for APP_TEST_APP_1 - it should NOT be here
1670                         continue;
1671                 }
1672         }
1673         free(single_line_format);
1674         fclose(file);
1675
1676         RUNNER_ASSERT_MSG(pass_1 == 1, "Rule " << EARLY_RULE_SUBJECT << " " << APP_ID << " " << EARLY_RULE_RIGHTS << " found");
1677         RUNNER_ASSERT_MSG(pass_2 == 1, "Rule " << EARLY_RULE_SUBJECT << " " << APP_TEST_APP_1 << " " << EARLY_RULE_RIGHTS << " found");
1678 }