0da0bee28b231c868b0fda8c2652a9d9bc9e304b
[platform/core/test/security-tests.git] / src / ckm / capi-access_control.cpp
1 #include <sys/types.h>
2 #include <sys/wait.h>
3
4 #include <dpl/test/test_runner.h>
5 #include <dpl/test/test_runner_child.h>
6
7 #include <tests_common.h>
8 #include <ckm-common.h>
9 #include <access_provider2.h>
10
11 #include <ckmc/ckmc-manager.h>
12 #include <ckmc/ckmc-control.h>
13 #include <ckmc/ckmc-type.h>
14 #include <ckmc/ckmc-error.h>
15
16 #include <ckm/ckm-type.h>
17
18 namespace {
19 const int USER_ROOT             = 0;
20 const int APP_1                 = 6000;
21 const int GROUP_1               = 6000;
22 const int APP_2                 = 6200;
23 const int GROUP_2               = 6200;
24 const char * const APP_PASS_1   = "app-pass-1";
25 const char * const APP_PASS_2   = "app-pass-2";
26 const char* APP_LABEL_1         = "APP_LABEL_1";
27 const char* APP_LABEL_2         = "APP_LABEL_2";
28 const char* APP_LABEL_3         = "APP_LABEL_3";
29 const char* APP_LABEL_4         = "APP_LABEL_4";
30
31
32 const char* NO_ALIAS = "definitely-non-existent-alias";
33 const char* NO_OWNER = "definitely-non-existent-owner";
34
35 const char* TEST_ALIAS = "test-alias";
36 const char* TEST_ALIAS2 = "test-alias2";
37 const char* TEST_ALIAS3 = "test-alias3";
38
39 const char* TEST_DATA = "dsflsdkghkslhglrtghierhgilrehgidsafasdffsgfdgdgfdgfdgfdgfdggf";
40
41 void allow_access_deprecated(const char* alias, const char* accessor, ckmc_access_right_e accessRights)
42 {
43     int ret = ckmc_allow_access(alias, accessor, accessRights);
44     RUNNER_ASSERT_MSG(CKMC_ERROR_NONE == ret, "Trying to allow access returned: " << CKMCErrorToString(ret));
45 }
46
47 void allow_access_deprecated_by_adm(uid_t uid, const char *label, const char* alias, const char* accessor, ckmc_access_right_e accessRights)
48 {
49     // data removal should revoke this access
50     int ret = ckmc_allow_access_by_adm(uid, label, alias, accessor, accessRights);
51     RUNNER_ASSERT_MSG(CKMC_ERROR_NONE == ret, "Trying to allow access returned: " << CKMCErrorToString(ret));
52 }
53
54 void allow_access_by_adm(uid_t uid, const char *label, const char* alias, const char* accessor, int permissionMask)
55 {
56     // data removal should revoke this access
57     int ret = ckmc_set_permission_by_adm(uid, aliasWithLabel(label, alias).c_str(), accessor, permissionMask);
58     RUNNER_ASSERT_MSG(CKMC_ERROR_NONE == ret, "Trying to allow access returned: " << CKMCErrorToString(ret));
59 }
60
61 void deny_access_by_adm(uid_t uid, const char *label, const char* alias, const char* accessor)
62 {
63     int ret = ckmc_set_permission_by_adm(uid, aliasWithLabel(label, alias).c_str(), accessor, CKMC_PERMISSION_NONE);
64     RUNNER_ASSERT_MSG(CKMC_ERROR_NONE == ret, "Denying access failed. " << CKMCErrorToString(ret));
65 }
66
67 void check_alias_count(size_t expected)
68 {
69     size_t count = count_aliases(ALIAS_DATA);
70     RUNNER_ASSERT_MSG(count == expected, "Expected " << expected << " aliases, got " << count);
71 }
72
73 } // namespace anonymous
74
75 RUNNER_TEST_GROUP_INIT (T300_CKMC_ACCESS_CONTROL_USER_C_API);
76
77
78 /////////////////////////////////////////////////////////////////////////////
79 // Manager
80 RUNNER_TEST(T3000_init)
81 {
82     reset_user_data(APP_1, APP_PASS_1);
83     reset_user_data(APP_2, APP_PASS_2);
84 }
85
86 // invalid arguments check
87 RUNNER_TEST(T3001_manager_allow_access_invalid)
88 {
89     ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
90
91     RUNNER_ASSERT(
92             CKMC_ERROR_INVALID_PARAMETER == ckmc_set_permission(NULL, "accessor", CKMC_PERMISSION_READ));
93     RUNNER_ASSERT(
94             CKMC_ERROR_INVALID_PARAMETER == ckmc_set_permission("alias", NULL, CKMC_PERMISSION_READ));
95 }
96
97 // invalid arguments check
98 RUNNER_TEST(T3002_manager_deny_access_invalid)
99 {
100     ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
101
102     RUNNER_ASSERT(CKMC_ERROR_INVALID_PARAMETER == ckmc_set_permission(NULL, "accessor", CKMC_PERMISSION_NONE));
103     RUNNER_ASSERT(CKMC_ERROR_INVALID_PARAMETER == ckmc_set_permission("alias", NULL, CKMC_PERMISSION_NONE));
104 }
105
106 // tries to allow access for non existing alias
107 RUNNER_CHILD_TEST(T3003_manager_allow_access_non_existing)
108 {
109     ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
110
111     int ret = ckmc_set_permission(NO_ALIAS, "label", CKMC_PERMISSION_READ);
112     RUNNER_ASSERT_MSG(CKMC_ERROR_DB_ALIAS_UNKNOWN == ret,
113                          "Allowing access for non existing alias returned " << CKMCErrorToString(ret));
114 }
115
116 // tries to deny access for non existing alias
117 RUNNER_CHILD_TEST(T3004_manager_deny_access_non_existing)
118 {
119     ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
120
121     int ret = ckmc_set_permission(NO_ALIAS, "label", CKMC_PERMISSION_NONE);
122     RUNNER_ASSERT_MSG(CKMC_ERROR_DB_ALIAS_UNKNOWN == ret,
123                          "Denying access for non existing alias returned " << CKMCErrorToString(ret));
124 }
125
126 // tries to deny access that does not exist in database
127 RUNNER_CHILD_TEST(T3005_manager_deny_access_non_existing_access)
128 {
129     ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
130
131     ScopedSaveData ssd(TEST_ALIAS, TEST_DATA);
132
133     // deny non existing access to existing alias
134     int ret = ckmc_set_permission(TEST_ALIAS, "label", CKMC_PERMISSION_NONE);
135     RUNNER_ASSERT_MSG(CKMC_ERROR_INVALID_PARAMETER == ret,
136                          "Denying non existing access returned: " << CKMCErrorToString(ret));
137 }
138
139 // tries to allow access to application own data
140 RUNNER_CHILD_TEST(T3006_manager_allow_access_to_myself)
141 {
142     ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
143
144     ScopedSaveData ssd(TEST_ALIAS, TEST_DATA);
145
146     CharPtr label = get_label();
147     int ret = ckmc_set_permission(TEST_ALIAS, label.get(), CKMC_PERMISSION_READ);
148     RUNNER_ASSERT_MSG(CKMC_ERROR_INVALID_PARAMETER == ret,
149                          "Trying to allow myself returned: " << CKMCErrorToString(ret));
150 }
151
152 // verifies that alias can not contain forbidden characters
153 RUNNER_CHILD_TEST(T3007_manager_check_alias_valid)
154 {
155     ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
156
157     ScopedSaveData ssd(TEST_ALIAS, TEST_DATA);
158
159     std::string test_alias_playground = std::string("AAA BBB CCC");
160     check_read(test_alias_playground.c_str(), 0, TEST_DATA, CKMC_ERROR_INVALID_PARAMETER);
161
162     // control: expect success
163     check_read(TEST_ALIAS, 0, TEST_DATA);
164     check_read(TEST_ALIAS, APP_LABEL_1, TEST_DATA);
165 }
166
167 // verifies that label can not contain forbidden characters
168 RUNNER_CHILD_TEST(T3008_manager_check_label_valid)
169 {
170     ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
171
172     ScopedSaveData ssd(TEST_ALIAS, TEST_DATA);
173
174     // basic test
175     std::string APP_LABEL_1_playground = std::string("AAA BBB CCC");
176     check_read(TEST_ALIAS, APP_LABEL_1_playground.c_str(), TEST_DATA, CKMC_ERROR_INVALID_PARAMETER);
177
178     // insert part of the separator in the middle
179     APP_LABEL_1_playground = std::string(APP_LABEL_1);
180     APP_LABEL_1_playground.insert(APP_LABEL_1_playground.size()/2, ckmc_label_name_separator);
181     check_read(TEST_ALIAS, APP_LABEL_1_playground.c_str(), TEST_DATA, CKMC_ERROR_INVALID_PARAMETER);
182
183     // prepend separator
184     APP_LABEL_1_playground = std::string(APP_LABEL_1);
185     APP_LABEL_1_playground.insert(0, ckmc_label_name_separator);
186     check_read(TEST_ALIAS, APP_LABEL_1_playground.c_str(), TEST_DATA, CKMC_ERROR_INVALID_PARAMETER);
187
188     // append separator
189     APP_LABEL_1_playground = std::string(APP_LABEL_1);
190     APP_LABEL_1_playground.append(ckmc_label_name_separator);
191     check_read(TEST_ALIAS, APP_LABEL_1_playground.c_str(), TEST_DATA, CKMC_ERROR_INVALID_PARAMETER);
192
193     // control: expect success
194     check_read(TEST_ALIAS, APP_LABEL_1, TEST_DATA);
195 }
196
197
198 // tries to access other application data without permission
199 RUNNER_TEST(T3020_manager_access_not_allowed)
200 {
201     // prepare: add data
202     GarbageCollector gc;
203     {
204         ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
205         gc.save(TEST_ALIAS, TEST_DATA);
206     }
207
208     // test accessibility from another label
209     {
210         ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
211
212         std::string TEST_ALIAS_adr = aliasWithLabel(APP_LABEL_1, TEST_ALIAS);
213         check_read_not_visible(TEST_ALIAS_adr.c_str());
214         check_remove_not_visible(TEST_ALIAS_adr.c_str());
215     }
216 }
217
218 // tries to access other application data with permission
219 RUNNER_TEST(T3021_manager_access_allowed)
220 {
221     // prepare: add data
222     GarbageCollector gc;
223     {
224         ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
225         gc.save(TEST_ALIAS, TEST_DATA);
226         allow_access(TEST_ALIAS, APP_LABEL_2, CKMC_PERMISSION_READ);
227     }
228
229     // test accessibility from another label
230     {
231         ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
232         check_read_allowed(aliasWithLabel(APP_LABEL_1, TEST_ALIAS).c_str(), TEST_DATA);
233     }
234 }
235
236 // tries to read other application data with permission for read/remove
237 RUNNER_TEST(T3022_manager_access_allowed_with_remove)
238 {
239     // prepare: add data
240     GarbageCollector gc;
241     {
242         ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
243         gc.save(TEST_ALIAS, TEST_DATA);
244         allow_access(TEST_ALIAS, APP_LABEL_2, CKMC_PERMISSION_READ | CKMC_PERMISSION_REMOVE);
245     }
246
247     // test accessibility from another label
248     {
249         ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
250         check_read_allowed(aliasWithLabel(APP_LABEL_1, TEST_ALIAS).c_str(), TEST_DATA);
251     }
252 }
253
254 // tries to remove other application data with permission for reading only
255 RUNNER_TEST(T3023_manager_access_allowed_remove_denied)
256 {
257     // prepare: add data
258     GarbageCollector gc;
259     {
260         ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
261         gc.save(TEST_ALIAS, TEST_DATA);
262         allow_access(TEST_ALIAS, APP_LABEL_2, CKMC_PERMISSION_READ);
263     }
264
265     // test accessibility from another label
266     {
267         ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
268         std::string TEST_ALIAS_adr = aliasWithLabel(APP_LABEL_1, TEST_ALIAS);
269         check_remove_denied(TEST_ALIAS_adr.c_str());
270         check_read_allowed(TEST_ALIAS_adr.c_str(), TEST_DATA);
271     }
272 }
273
274 // tries to remove other application data with permission
275 RUNNER_TEST(T3025_manager_remove_allowed)
276 {
277     // prepare: add data
278     GarbageCollector gc;
279     {
280         ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
281         gc.save(TEST_ALIAS, TEST_DATA);
282         allow_access(TEST_ALIAS, APP_LABEL_2, CKMC_PERMISSION_READ | CKMC_PERMISSION_REMOVE);
283     }
284
285     // test accessibility from another label
286     {
287         ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
288         check_remove_allowed(aliasWithLabel(APP_LABEL_1, TEST_ALIAS).c_str());
289     }
290 }
291
292 // tries to access other application data after allow function was called twice with different
293 // rights
294 RUNNER_TEST(T3026_manager_double_allow)
295 {
296     // prepare: add data
297     GarbageCollector gc;
298     {
299         ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
300         gc.save(TEST_ALIAS, TEST_DATA);
301
302         // access should be overwritten
303         allow_access(TEST_ALIAS, APP_LABEL_2, CKMC_PERMISSION_READ | CKMC_PERMISSION_REMOVE);
304         allow_access(TEST_ALIAS, APP_LABEL_2, CKMC_PERMISSION_READ);
305     }
306
307     // test accessibility from another label
308     {
309         ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
310
311         std::string TEST_ALIAS_adr = aliasWithLabel(APP_LABEL_1, TEST_ALIAS);
312         check_remove_denied(TEST_ALIAS_adr.c_str());
313         check_read_allowed(TEST_ALIAS_adr.c_str(), TEST_DATA);
314     }
315 }
316
317 // tries to access application data with permission and after permission has been revoked
318 RUNNER_TEST(T3027_manager_allow_deny)
319 {
320     // prepare: add data
321     GarbageCollector gc;
322     std::string TEST_ALIAS_adr = aliasWithLabel(APP_LABEL_1, TEST_ALIAS);
323     {
324         ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
325         gc.save(TEST_ALIAS, TEST_DATA);
326
327         allow_access(TEST_ALIAS, APP_LABEL_2, CKMC_PERMISSION_READ);
328     }
329
330     // test accessibility from another label
331     {
332         ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
333
334         check_remove_denied(TEST_ALIAS_adr.c_str());
335         check_read_allowed(TEST_ALIAS_adr.c_str(), TEST_DATA);
336     }
337
338     // remove permission
339     {
340         ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
341
342         deny_access(TEST_ALIAS, APP_LABEL_2);
343     }
344
345     // test accessibility from another label
346     {
347         ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
348
349         check_remove_not_visible(TEST_ALIAS_adr.c_str());
350         check_read_not_visible(TEST_ALIAS_adr.c_str());
351     }
352 }
353
354 RUNNER_TEST(T3028_manager_access_by_label)
355 {
356     // prepare: add data
357     GarbageCollector gc;
358     const char *additional_data = "label-2-data";
359     {
360         ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
361         gc.save(TEST_ALIAS, TEST_DATA);
362
363         allow_access(TEST_ALIAS, APP_LABEL_2, CKMC_PERMISSION_READ);
364     }
365
366     // add data as app 2
367     {
368         ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
369         save_data(TEST_ALIAS, additional_data);
370
371         allow_access(TEST_ALIAS, APP_LABEL_1, CKMC_PERMISSION_READ);
372
373         // test if accessing valid alias (of label2 domain)
374         check_read_allowed(TEST_ALIAS, additional_data);
375     }
376
377     // test accessibility to app 2 from app 1
378     {
379         ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
380
381         // test if can access label2 alias from label1 domain - should succeed
382         check_read_allowed(aliasWithLabel(APP_LABEL_2, TEST_ALIAS).c_str(), additional_data);
383     }
384 }
385
386 // tries to modify another label's permission
387 RUNNER_TEST(T3029_manager_access_modification_by_foreign_label)
388 {
389     // prepare: add data
390     GarbageCollector gc;
391     {
392         ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
393         gc.save(TEST_ALIAS, TEST_DATA);
394
395         allow_access(TEST_ALIAS, APP_LABEL_3, CKMC_PERMISSION_READ | CKMC_PERMISSION_REMOVE);
396     }
397
398     // test accessibility from another label
399     {
400         ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
401
402         allow_access_negative(aliasWithLabel(APP_LABEL_1, TEST_ALIAS).c_str(), APP_LABEL_4, CKMC_PERMISSION_READ | CKMC_PERMISSION_REMOVE, CKMC_ERROR_PERMISSION_DENIED);
403         deny_access_negative (aliasWithLabel(APP_LABEL_1, TEST_ALIAS).c_str(), APP_LABEL_4, CKMC_ERROR_PERMISSION_DENIED);
404     }
405 }
406
407 // checks if only aliases readable by given app are returned
408 RUNNER_TEST(T3030_manager_get_all_aliases)
409 {
410     // prepare: add data
411     GarbageCollector gc;
412     size_t count;
413     {
414         ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
415         gc.save(TEST_ALIAS, TEST_DATA);
416         gc.save(TEST_ALIAS2, TEST_DATA);
417
418         count = count_aliases(ALIAS_DATA);
419         allow_access(TEST_ALIAS, APP_LABEL_2, CKMC_PERMISSION_READ);
420     }
421
422     // test accessibility from another label
423     {
424         ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
425
426         // check that app can access other aliases when it has permission
427         check_alias_count(count - 1);
428
429         ScopedSaveData ssd3(TEST_ALIAS3, TEST_DATA);
430
431         // check that app can access its own aliases
432         check_alias_count(count - 1 + 1);
433     }
434
435     // remove permission
436     {
437         ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
438         deny_access(TEST_ALIAS, APP_LABEL_2);
439     }
440
441     // test accessibility from another label
442     {
443         ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
444
445         // check that app can't access other aliases for which permission has been revoked
446         check_alias_count(count - 2);
447     }
448 }
449
450 // tries to access other application data with permission
451 RUNNER_TEST(T3031_manager_deprecated_access_allowed)
452 {
453     // prepare: add data
454     GarbageCollector gc;
455     {
456         ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
457         gc.save(TEST_ALIAS, TEST_DATA);
458
459         allow_access_deprecated(TEST_ALIAS, APP_LABEL_2, CKMC_AR_READ);
460     }
461
462     // test accessibility from another label
463     {
464         ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
465
466         check_read_allowed(aliasWithLabel(APP_LABEL_1, TEST_ALIAS).c_str(), TEST_DATA);
467     }
468 }
469
470 // tries to read other application data with permission for read/remove
471 RUNNER_TEST(T3032_manager_deprecated_access_allowed_with_remove)
472 {
473     // prepare: add data
474     GarbageCollector gc;
475     {
476         ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
477         gc.save(TEST_ALIAS, TEST_DATA);
478
479         allow_access_deprecated(TEST_ALIAS, APP_LABEL_2, CKMC_AR_READ_REMOVE);
480     }
481
482     // test accessibility from another label
483     {
484         ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
485
486         check_read_allowed(aliasWithLabel(APP_LABEL_1, TEST_ALIAS).c_str(), TEST_DATA);
487     }
488 }
489
490 // tries to remove other application data with permission for reading only
491 RUNNER_TEST(T3033_manager_deprecated_access_allowed_remove_denied)
492 {
493     // prepare: add data
494     GarbageCollector gc;
495     {
496         ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
497         gc.save(TEST_ALIAS, TEST_DATA);
498
499         allow_access_deprecated(TEST_ALIAS, APP_LABEL_2, CKMC_AR_READ);
500     }
501
502     // test accessibility from another label
503     {
504         ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
505
506         std::string TEST_ALIAS_adr = aliasWithLabel(APP_LABEL_1, TEST_ALIAS);
507         check_remove_denied(TEST_ALIAS_adr.c_str());
508         check_read_allowed(TEST_ALIAS_adr.c_str(), TEST_DATA);
509     }
510 }
511
512 // tries to remove other application data with permission
513 RUNNER_TEST(T3034_manager_deprecated_remove_allowed)
514 {
515     // prepare: add data
516     GarbageCollector gc;
517     {
518         ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
519         gc.save(TEST_ALIAS, TEST_DATA);
520
521         allow_access_deprecated(TEST_ALIAS, APP_LABEL_2, CKMC_AR_READ_REMOVE);
522     }
523
524     // test accessibility from another label
525     {
526         ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
527
528         check_remove_allowed(aliasWithLabel(APP_LABEL_1, TEST_ALIAS).c_str());
529     }
530 }
531
532 /////////////////////////////////////////////////////////////////////////////
533 // Control
534
535 RUNNER_TEST_GROUP_INIT (T310_CKMC_ACCESS_CONTROL_ROOT_C_API);
536
537 RUNNER_TEST(T3100_init)
538 {
539     reset_user_data(APP_1, APP_PASS_1);
540     reset_user_data(APP_2, APP_PASS_2);
541 }
542
543 // invalid argument check
544 RUNNER_TEST(T3101_control_allow_access_invalid)
545 {
546     // prepare: add data
547     GarbageCollector gc;
548     {
549         ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
550         gc.save(TEST_ALIAS, TEST_DATA);
551     }
552
553     int ret;
554     ret = ckmc_set_permission_by_adm(APP_1, TEST_ALIAS, "accessor", CKMC_PERMISSION_READ);
555     RUNNER_ASSERT(CKMC_ERROR_INVALID_PARAMETER == ret);
556     ret = ckmc_set_permission_by_adm(APP_1, "owner alias", NULL, CKMC_PERMISSION_READ);
557     RUNNER_ASSERT(CKMC_ERROR_INVALID_PARAMETER == ret);
558
559     // double owner
560     std::string aliasLabel = aliasWithLabel(get_label().get(), TEST_ALIAS);
561     ret = ckmc_set_permission_by_adm(APP_1, aliasWithLabel("another-owner", aliasLabel.c_str()).c_str(), APP_LABEL_1, CKMC_PERMISSION_READ);
562     RUNNER_ASSERT(CKMC_ERROR_INVALID_PARAMETER == ret);
563 }
564
565 // invalid argument check
566 RUNNER_TEST(T3102_control_deny_access_invalid)
567 {
568     // prepare: add data
569     GarbageCollector gc;
570     {
571         ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
572         gc.save(TEST_ALIAS, TEST_DATA);
573     }
574
575     RUNNER_ASSERT(CKMC_ERROR_INVALID_PARAMETER ==
576             ckmc_set_permission_by_adm(APP_1, aliasWithLabel(NULL, TEST_ALIAS).c_str(), "accessor", CKMC_PERMISSION_NONE));
577     RUNNER_ASSERT(CKMC_ERROR_INVALID_PARAMETER ==
578             ckmc_set_permission_by_adm(APP_1, aliasWithLabel("owner", TEST_ALIAS).c_str(), NULL, CKMC_PERMISSION_NONE));
579
580     // double owner
581     std::string aliasLabel = aliasWithLabel(get_label().get(), TEST_ALIAS);
582     RUNNER_ASSERT(CKMC_ERROR_INVALID_PARAMETER ==
583             ckmc_set_permission_by_adm(APP_1, aliasWithLabel("another-owner", aliasLabel.c_str()).c_str(), APP_LABEL_1, CKMC_PERMISSION_NONE));
584 }
585
586 // tries to allow access for non existing alias
587 RUNNER_TEST(T3103_control_allow_access_non_existing)
588 {
589     int ret = ckmc_set_permission_by_adm(APP_1, aliasWithLabel(NO_OWNER, NO_ALIAS).c_str(), "label", CKMC_PERMISSION_READ);
590     RUNNER_ASSERT_MSG(CKMC_ERROR_DB_ALIAS_UNKNOWN == ret,
591                          "Allowing access for non existing alias returned " << CKMCErrorToString(ret));
592 }
593
594 // tries to deny access for non existing alias
595 RUNNER_TEST(T3104_control_deny_access_non_existing)
596 {
597     int ret = ckmc_set_permission_by_adm(APP_1, aliasWithLabel(NO_OWNER, NO_ALIAS).c_str(), "label", CKMC_PERMISSION_NONE);
598     RUNNER_ASSERT_MSG(CKMC_ERROR_DB_ALIAS_UNKNOWN == ret,
599                          "Denying access for non existing alias returned " << CKMCErrorToString(ret));
600 }
601
602 // tries to deny non existing access
603 RUNNER_TEST(T3105_control_deny_access_non_existing_access)
604 {
605     // prepare: add data
606     GarbageCollector gc;
607     {
608         ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
609         gc.save(TEST_ALIAS, TEST_DATA);
610     }
611
612     int ret = ckmc_set_permission_by_adm(APP_1, aliasWithLabel(APP_LABEL_1, TEST_ALIAS).c_str(), APP_LABEL_2, CKMC_PERMISSION_NONE);
613     RUNNER_ASSERT_MSG(CKMC_ERROR_INVALID_PARAMETER == ret,
614                       "Denying non existing access returned: " << CKMCErrorToString(ret));
615 }
616
617 // tries to allow application to access its own data
618 RUNNER_TEST(T3106_control_allow_access_to_myself)
619 {
620     // prepare: add data
621     GarbageCollector gc;
622     ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
623     gc.save(TEST_ALIAS, TEST_DATA);
624
625     // test
626     int ret = ckmc_set_permission(TEST_ALIAS, APP_LABEL_1, CKMC_PERMISSION_READ);
627     RUNNER_ASSERT_MSG(CKMC_ERROR_INVALID_PARAMETER == ret,
628                       "Trying to allow myself returned: " << CKMCErrorToString(ret));
629 }
630
631 // tries to use admin API as a user
632 RUNNER_CHILD_TEST(T3110_control_allow_access_as_user)
633 {
634     RUNNER_IGNORED_MSG("Disabled until labeled sockets not available");
635
636     // prepare: add data
637     GarbageCollector gc;
638     ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
639     gc.save(TEST_ALIAS, TEST_DATA);
640
641     // test
642     int ret = ckmc_set_permission_by_adm(APP_1, aliasWithLabel(APP_LABEL_1, TEST_ALIAS).c_str(), APP_LABEL_2, CKMC_PERMISSION_READ);
643     RUNNER_ASSERT_MSG(CKMC_ERROR_PERMISSION_DENIED == ret,
644                       "Ordinary user should not be able to use control API. Error " << CKMCErrorToString(ret));
645 }
646
647 // tries to use admin API as a user
648 RUNNER_CHILD_TEST(T3111_control_deny_access_as_user)
649 {
650     RUNNER_IGNORED_MSG("Disabled until labeled sockets not available");
651
652     // prepare: add data
653     GarbageCollector gc;
654     ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
655     gc.save(TEST_ALIAS, TEST_DATA);
656
657     // test
658     int ret = ckmc_set_permission_by_adm(APP_1, aliasWithLabel(APP_LABEL_1, TEST_ALIAS).c_str(), APP_LABEL_2, CKMC_PERMISSION_NONE);
659     RUNNER_ASSERT_MSG(CKMC_ERROR_PERMISSION_DENIED == ret,
660                       "Ordinary user should not be able to use control API. Error " << CKMCErrorToString(ret));
661 }
662
663 // tries to read other application data with permission
664 RUNNER_TEST(T3121_control_access_allowed)
665 {
666     // prepare: add data
667     GarbageCollector gc;
668     {
669         ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
670         gc.save(TEST_ALIAS, TEST_DATA);
671     }
672
673     allow_access_by_adm(APP_1, APP_LABEL_1, TEST_ALIAS, APP_LABEL_2, CKMC_PERMISSION_READ);
674     {
675         ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
676
677         check_read_allowed(aliasWithLabel(APP_LABEL_1, TEST_ALIAS).c_str(), TEST_DATA);
678     }
679 }
680
681 // tries to read other application data with permission to read/remove
682 RUNNER_TEST(T3122_control_access_allowed_with_remove)
683 {
684     // prepare: add data
685     GarbageCollector gc;
686     {
687         ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
688         gc.save(TEST_ALIAS, TEST_DATA);
689     }
690
691     allow_access_by_adm(APP_1, APP_LABEL_1, TEST_ALIAS, APP_LABEL_2, CKMC_PERMISSION_READ | CKMC_PERMISSION_REMOVE);
692     {
693         ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
694
695         check_read_allowed(aliasWithLabel(APP_LABEL_1, TEST_ALIAS).c_str(), TEST_DATA);
696     }
697 }
698
699 // tries to remove other application data with permission to read
700 RUNNER_TEST(T3122_control_access_allowed_remove_denied)
701 {
702     // prepare: add data
703     GarbageCollector gc;
704     {
705         ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
706         gc.save(TEST_ALIAS, TEST_DATA);
707     }
708
709     allow_access_by_adm(APP_1, APP_LABEL_1, TEST_ALIAS, APP_LABEL_2, CKMC_PERMISSION_READ);
710     {
711         ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
712
713         check_remove_denied(aliasWithLabel(APP_LABEL_1, TEST_ALIAS).c_str());
714     }
715 }
716
717 // tries to remove other application data with permission
718 RUNNER_TEST(T3125_control_remove_allowed)
719 {
720     // prepare: add data
721     GarbageCollector gc;
722     {
723         ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
724         gc.save(TEST_ALIAS, TEST_DATA);
725     }
726
727     allow_access_by_adm(APP_1, APP_LABEL_1, TEST_ALIAS, APP_LABEL_2, CKMC_PERMISSION_READ | CKMC_PERMISSION_REMOVE);
728     {
729         ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
730
731         check_remove_allowed(aliasWithLabel(APP_LABEL_1, TEST_ALIAS).c_str());
732     }
733 }
734
735 // tries to access other application data after allow function has been called twice with different
736 // rights
737 RUNNER_TEST(T3126_control_double_allow)
738 {
739     // prepare: add data
740     GarbageCollector gc;
741     {
742         ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
743         gc.save(TEST_ALIAS, TEST_DATA);
744     }
745
746     // access should be overwritten
747     allow_access_by_adm(APP_1, APP_LABEL_1, TEST_ALIAS, APP_LABEL_2, CKMC_PERMISSION_READ | CKMC_PERMISSION_REMOVE);
748     allow_access_by_adm(APP_1, APP_LABEL_1, TEST_ALIAS, APP_LABEL_2, CKMC_PERMISSION_READ);
749     {
750         ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
751
752         std::string TEST_ALIAS_adr = aliasWithLabel(APP_LABEL_1, TEST_ALIAS);
753         check_remove_denied(TEST_ALIAS_adr.c_str());
754         check_read_allowed(TEST_ALIAS_adr.c_str(), TEST_DATA);
755     }
756 }
757
758 // tries to access other application data with permission and after permission has been revoked
759 RUNNER_TEST(T3127_control_allow_deny)
760 {
761     // prepare: add data
762     GarbageCollector gc;
763     {
764         ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
765         gc.save(TEST_ALIAS, TEST_DATA);
766     }
767
768     std::string TEST_ALIAS_adr = aliasWithLabel(APP_LABEL_1, TEST_ALIAS);
769     allow_access_by_adm(APP_1, APP_LABEL_1, TEST_ALIAS, APP_LABEL_2, CKMC_PERMISSION_READ);
770     {
771         ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
772
773         check_remove_denied(TEST_ALIAS_adr.c_str());
774         check_read_allowed(TEST_ALIAS_adr.c_str(), TEST_DATA);
775     }
776
777     deny_access_by_adm(APP_1, APP_LABEL_1, TEST_ALIAS, APP_LABEL_2);
778     {
779         ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
780
781         check_remove_not_visible(TEST_ALIAS_adr.c_str());
782         check_read_not_visible(TEST_ALIAS_adr.c_str());
783     }
784 }
785
786 // checks if only aliases readable by given app are returned
787 RUNNER_TEST(T3130_control_get_all_aliases)
788 {
789     // prepare: add data
790     GarbageCollector gc;
791     size_t count;
792     {
793         ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
794         gc.save(TEST_ALIAS, TEST_DATA);
795         gc.save(TEST_ALIAS2, TEST_DATA);
796
797         count = count_aliases(ALIAS_DATA);
798     }
799
800     allow_access_by_adm(APP_1, APP_LABEL_1, TEST_ALIAS, APP_LABEL_2, CKMC_PERMISSION_READ);
801     {
802         ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
803
804         // check that app can access other aliases when it has permission
805         check_alias_count(count - 1);
806
807         ScopedSaveData ssd(TEST_ALIAS3, TEST_DATA);
808
809         // check that app can access its own aliases
810         check_alias_count(count - 1 + 1);
811     }
812
813     deny_access_by_adm(APP_1, APP_LABEL_1, TEST_ALIAS, APP_LABEL_2);
814     {
815         ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
816
817         // check that app can't access other aliases for which permission has been revoked
818         check_alias_count(count - 2);
819     }
820 }
821
822 // tries to add access to data in a database of invalid user
823 RUNNER_TEST(T3140_control_allow_invalid_user)
824 {
825     // prepare: add data
826     GarbageCollector gc;
827     {
828         ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
829         gc.save(TEST_ALIAS, TEST_DATA);
830     }
831
832     int ret = ckmc_set_permission_by_adm(APP_2, aliasWithLabel(APP_LABEL_1, TEST_ALIAS).c_str(), APP_LABEL_2, CKMC_PERMISSION_READ | CKMC_PERMISSION_REMOVE);
833     RUNNER_ASSERT_MSG(CKMC_ERROR_DB_ALIAS_UNKNOWN == ret,
834                       "Trying to allow access to invalid user returned: " << CKMCErrorToString(ret));
835 }
836
837 // tries to revoke access to data in a database of invalid user
838 RUNNER_TEST(T3141_control_deny_invalid_user)
839 {
840     // prepare: add data
841     GarbageCollector gc;
842     {
843         ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
844         gc.save(TEST_ALIAS, TEST_DATA);
845     }
846
847     int ret = ckmc_set_permission_by_adm(APP_2, aliasWithLabel(APP_LABEL_1, TEST_ALIAS).c_str(), APP_LABEL_2, CKMC_PERMISSION_NONE);
848     RUNNER_ASSERT_MSG(CKMC_ERROR_DB_ALIAS_UNKNOWN == ret,
849                       "Trying to deny access to invalid user returned: " << CKMCErrorToString(ret));
850 }
851
852 // tries to read other application data with permission
853 RUNNER_TEST(T3142_control_deprecated_access_allowed)
854 {
855     // prepare: add data
856     GarbageCollector gc;
857     {
858         ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
859         gc.save(TEST_ALIAS, TEST_DATA);
860     }
861
862     allow_access_deprecated_by_adm(APP_1, APP_LABEL_1, TEST_ALIAS, APP_LABEL_2, CKMC_AR_READ);
863     {
864         ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
865
866         check_read_allowed(aliasWithLabel(APP_LABEL_1, TEST_ALIAS).c_str(), TEST_DATA);
867     }
868 }
869
870 // tries to read other application data with permission to read/remove
871 RUNNER_TEST(T3143_control_deprecated_access_allowed_with_remove)
872 {
873     // prepare: add data
874     GarbageCollector gc;
875     {
876         ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
877         gc.save(TEST_ALIAS, TEST_DATA);
878     }
879
880     allow_access_deprecated_by_adm(APP_1, APP_LABEL_1, TEST_ALIAS, APP_LABEL_2, CKMC_AR_READ_REMOVE);
881     {
882         ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
883
884         check_read_allowed(aliasWithLabel(APP_LABEL_1, TEST_ALIAS).c_str(), TEST_DATA);
885     }
886 }
887
888 // tries to remove other application data with permission to read
889 RUNNER_TEST(T3144_control_deprecated_access_allowed_remove_denied)
890 {
891     // prepare: add data
892     GarbageCollector gc;
893     {
894         ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
895         gc.save(TEST_ALIAS, TEST_DATA);
896     }
897
898     allow_access_deprecated_by_adm(APP_1, APP_LABEL_1, TEST_ALIAS, APP_LABEL_2, CKMC_AR_READ);
899     {
900         ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
901
902         check_remove_denied(aliasWithLabel(APP_LABEL_1, TEST_ALIAS).c_str());
903     }
904 }
905
906 // tries to remove other application data with permission
907 RUNNER_TEST(T3145_control_deprecated_remove_allowed)
908 {
909     // prepare: add data
910     GarbageCollector gc;
911     {
912         ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
913         gc.save(TEST_ALIAS, TEST_DATA);
914     }
915
916     allow_access_deprecated_by_adm(APP_1, APP_LABEL_1, TEST_ALIAS, APP_LABEL_2, CKMC_AR_READ_REMOVE);
917     {
918         ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
919
920         check_remove_allowed(aliasWithLabel(APP_LABEL_1, TEST_ALIAS).c_str());
921     }
922 }