14fccff75ec2300df1c0917083b4c0a9b67c7366
[platform/core/test/security-tests.git] / tests / 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
20 const uid_t USER_ROOT = 0;
21 const char* APP_PASS = "user-pass";
22 const char* ROOT_PASS = "test-pass";
23
24 const char* NO_ALIAS = "definitely-non-existent-alias";
25 const char* NO_OWNER = "definitely-non-existent-owner";
26
27 const char* TEST_ALIAS = "test-alias";
28 const char* TEST_ALIAS2 = "test-alias2";
29 const char* TEST_ALIAS3 = "test-alias3";
30
31 const char* TEST_LABEL = "test-label";
32 const char* TEST_LABEL2 = "test-label2";
33 const char* TEST_LABEL3 = "test-label3";
34 const char* TEST_LABEL4 = "test-label4";
35
36 const char* TEST_DATA = "dsflsdkghkslhglrtghierhgilrehgidsafasdffsgfdgdgfdgfdgfdgfdggf";
37
38 void save_data(const char* alias, const char *data)
39 {
40     ckmc_raw_buffer_s buffer;
41     buffer.data = reinterpret_cast<unsigned char*>(const_cast<char*>(data));
42     buffer.size = strlen(data);
43     ckmc_policy_s policy;
44     policy.password = NULL;
45     policy.extractable = true;
46
47     int ret = ckmc_save_data(alias, buffer, policy);
48     RUNNER_ASSERT_MSG(CKMC_ERROR_NONE == ret, "Saving data failed. Error: " << ret);
49 }
50
51 void save_data(const char* alias)
52 {
53     save_data(alias, TEST_DATA);
54 }
55
56 void check_remove_allowed(const char* alias)
57 {
58     int ret = ckmc_remove_alias(alias);
59     // remove, but ignore non existing
60     RUNNER_ASSERT_MSG(CKMC_ERROR_NONE == ret || CKMC_ERROR_DB_ALIAS_UNKNOWN,
61                          "Removing data failed: " << ret);
62 }
63
64 void check_remove_denied(const char* alias)
65 {
66     int ret = ckmc_remove_alias(alias);
67     RUNNER_ASSERT_MSG(
68             CKMC_ERROR_PERMISSION_DENIED == ret,
69             "App with different label shouldn't have rights to remove this data. Error: " << ret);
70 }
71
72 void check_remove_not_visible(const char* alias)
73 {
74     int ret = ckmc_remove_alias(alias);
75     RUNNER_ASSERT_MSG(
76             CKMC_ERROR_DB_ALIAS_UNKNOWN == ret,
77             "App with different label shouldn't have rights to see this data. Error: " << ret);
78 }
79
80 void check_read(const char* alias, const char *label, const char *test_data, int expected_code = CKMC_ERROR_NONE)
81 {
82     ckmc_raw_buffer_s* buffer = NULL;
83     int ret = ckmc_get_data(aliasWithLabel(label, alias).c_str(), NULL, &buffer);
84     RUNNER_ASSERT_MSG(expected_code == ret, "Getting data failed. Expected code: " << expected_code << ", while result code: " << ret);
85
86     if(expected_code == CKMC_ERROR_NONE)
87     {
88         // compare data with expected
89         RUNNER_ASSERT_MSG(
90                 buffer->size == strlen(test_data),
91                 "Extracted data length do not match expected data length (encrypted?).");
92
93         RUNNER_ASSERT_MSG(
94                 memcmp(const_cast<const char*>(reinterpret_cast<char*>(buffer->data)), test_data, buffer->size) == 0,
95                 "Extracted data do not match expected data (encrypted?).");
96
97         ckmc_buffer_free(buffer);
98     }
99 }
100
101 void check_read_allowed(const char* alias, const char *data)
102 {
103     // try to read previously saved data - label taken implicitly
104     check_read(alias, 0, data);
105 }
106 void check_read_allowed(const char* alias)
107 {
108     check_read_allowed(alias, TEST_DATA);
109 }
110
111 void check_read_denied(const char* alias)
112 {
113     // try to read previously saved data - label taken implicitly
114     {
115         ckmc_raw_buffer_s* buffer = NULL;
116         int ret = ckmc_get_data(alias, NULL, &buffer);
117         RUNNER_ASSERT_MSG(CKMC_ERROR_PERMISSION_DENIED == ret,
118                             "App with different label shouldn't have rights to read this data. Error: " << ret);
119         ckmc_buffer_free(buffer);
120     }
121 }
122
123 void check_read_not_visible(const char* alias)
124 {
125     // try to read previously saved data - label taken implicitly
126     {
127         ckmc_raw_buffer_s* buffer = NULL;
128         int ret = ckmc_get_data(alias, NULL, &buffer);
129         RUNNER_ASSERT_MSG(CKMC_ERROR_DB_ALIAS_UNKNOWN == ret,
130                             "App with different label shouldn't have rights to see this data. Error: " << ret);
131         ckmc_buffer_free(buffer);
132     }
133 }
134
135 void allow_access(const char* alias, const char* accessor, int permissionMask)
136 {
137     // data removal should revoke this access
138     int ret = ckmc_set_permission(alias, accessor, permissionMask);
139     RUNNER_ASSERT_MSG(CKMC_ERROR_NONE == ret, "Trying to allow access returned: " << ret);
140 }
141
142 void allow_access_negative(const char* alias, const char* accessor, int permissionMask, int expectedCode)
143 {
144     // data removal should revoke this access
145     int ret = ckmc_set_permission(alias, accessor, permissionMask);
146     RUNNER_ASSERT_MSG(expectedCode == ret, "Trying to allow access returned: " << ret << ", while expected: " << expectedCode);
147 }
148
149 void deny_access(const char* alias, const char* accessor)
150 {
151     int ret = ckmc_deny_access(alias, accessor);
152     RUNNER_ASSERT_MSG(CKMC_ERROR_NONE == ret, "Denying access failed. Error: " << ret);
153 }
154
155 void deny_access_negative(const char* alias, const char* accessor, int expectedCode)
156 {
157     int ret = ckmc_set_permission(alias, accessor, CKMC_PERMISSION_NONE);
158     RUNNER_ASSERT_MSG(expectedCode == ret, "Denying access failed. Error: " << ret << ", while expected: " << expectedCode);
159 }
160
161 void allow_access_by_adm(const char* alias, const char* accessor, int permissionMask)
162 {
163     // data removal should revoke this access
164     int ret = ckmc_set_permission_by_adm(USER_ROOT, aliasWithLabel(get_label().get(), alias).c_str(), accessor, permissionMask);
165     RUNNER_ASSERT_MSG(CKMC_ERROR_NONE == ret, "Trying to allow access returned: " << ret);
166 }
167
168 void deny_access_by_adm(const char* alias, const char* accessor)
169 {
170     int ret = ckmc_set_permission_by_adm(USER_ROOT, aliasWithLabel(get_label().get(), alias).c_str(), accessor, CKMC_PERMISSION_NONE);
171     RUNNER_ASSERT_MSG(CKMC_ERROR_NONE == ret, "Denying access failed. Error: " << ret);
172 }
173
174 int count_aliases()
175 {
176     ckmc_alias_list_s *aliasList = NULL;
177     int ret = ckmc_get_data_alias_list(&aliasList);
178     if (ret == CKMC_ERROR_DB_ALIAS_UNKNOWN)
179         return 0;
180
181     RUNNER_ASSERT_MSG(ret == 0, "Failed to get the list of data aliases. Error: " << ret);
182
183     ckmc_alias_list_s *plist = aliasList;
184     int count = 0;
185     while(plist)
186     {
187         plist = plist->next;
188         count++;
189     }
190     ckmc_alias_list_all_free(aliasList);
191     return count;
192 }
193
194 void check_alias_count(int expected)
195 {
196     int count = count_aliases();
197     RUNNER_ASSERT_MSG(count == expected, "Expected " << expected << " aliases, got " << count);
198 }
199
200 // saves data upon construction and deletes it upon destruction
201 class ScopedSaveData
202 {
203 public:
204     ScopedSaveData(const char* alias) : m_alias(alias)
205     {
206         save_data(alias);
207     }
208     ScopedSaveData(const char* alias, const char *data) : m_alias(alias)
209     {
210         save_data(alias, data);
211     }
212
213     ~ScopedSaveData()
214     {
215         /*
216          * Let it throw. If we can't remove data then remaining tests results will be
217          * unreliable anyway.
218          */
219         check_remove_allowed(m_alias);
220     }
221 private:
222     const char* m_alias;
223 };
224
225 } // namespace anonymous
226
227 RUNNER_TEST_GROUP_INIT (T300_CKMC_ACCESS_CONTROL_C_API);
228
229
230 /////////////////////////////////////////////////////////////////////////////
231 // Manager
232 RUNNER_TEST(T3000_init)
233 {
234     int temp;
235     RUNNER_ASSERT_MSG(CKMC_ERROR_NONE == (temp = ckmc_unlock_user_key(APP_UID, APP_PASS)),
236                          "Error=" << temp);
237     RUNNER_ASSERT_MSG(CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(APP_UID)),
238                          "Error=" << temp);
239     RUNNER_ASSERT_MSG(CKMC_ERROR_NONE == (temp = ckmc_unlock_user_key(USER_ROOT, ROOT_PASS)),
240                          "Error=" << temp);
241     RUNNER_ASSERT_MSG(CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(USER_ROOT)),
242                          "Error=" << temp);
243
244 }
245
246 // invalid arguments check
247 RUNNER_TEST(T3001_manager_allow_access_invalid)
248 {
249     RUNNER_ASSERT(
250             CKMC_ERROR_INVALID_PARAMETER == ckmc_set_permission(NULL, "accessor", CKMC_PERMISSION_READ));
251     RUNNER_ASSERT(
252             CKMC_ERROR_INVALID_PARAMETER == ckmc_set_permission("alias", NULL, CKMC_PERMISSION_READ));
253 }
254
255 // invalid arguments check
256 RUNNER_TEST(T3002_manager_deny_access_invalid)
257 {
258     RUNNER_ASSERT(CKMC_ERROR_INVALID_PARAMETER == ckmc_set_permission(NULL, "accessor", CKMC_PERMISSION_NONE));
259     RUNNER_ASSERT(CKMC_ERROR_INVALID_PARAMETER == ckmc_set_permission("alias", NULL, CKMC_PERMISSION_NONE));
260 }
261
262 // tries to allow access for non existing alias
263 RUNNER_CHILD_TEST(T3003_manager_allow_access_non_existing)
264 {
265     switch_to_storage_user(TEST_LABEL);
266
267     int ret = ckmc_set_permission(NO_ALIAS, "label", CKMC_PERMISSION_READ);
268     RUNNER_ASSERT_MSG(CKMC_ERROR_DB_ALIAS_UNKNOWN == ret,
269                          "Allowing access for non existing alias returned " << ret);
270 }
271
272 // tries to deny access for non existing alias
273 RUNNER_CHILD_TEST(T3004_manager_deny_access_non_existing)
274 {
275     switch_to_storage_user(TEST_LABEL);
276
277     int ret = ckmc_set_permission(NO_ALIAS, "label", CKMC_PERMISSION_NONE);
278     RUNNER_ASSERT_MSG(CKMC_ERROR_DB_ALIAS_UNKNOWN == ret,
279                          "Denying access for non existing alias returned " << ret);
280 }
281
282 // tries to deny access that does not exist in database
283 RUNNER_CHILD_TEST(T3005_manager_deny_access_non_existing_access)
284 {
285     switch_to_storage_user(TEST_LABEL);
286
287     ScopedSaveData ssd(TEST_ALIAS);
288
289     // deny non existing access to existing alias
290     int ret = ckmc_set_permission(TEST_ALIAS, "label", CKMC_PERMISSION_NONE);
291     RUNNER_ASSERT_MSG(CKMC_ERROR_INVALID_PARAMETER == ret,
292                          "Denying non existing access returned: " << ret);
293 }
294
295 // tries to allow access to application own data
296 RUNNER_CHILD_TEST(T3006_manager_allow_access_to_myself)
297 {
298     switch_to_storage_user(TEST_LABEL);
299
300     ScopedSaveData ssd(TEST_ALIAS);
301
302     CharPtr label = get_label();
303     int ret = ckmc_set_permission(TEST_ALIAS, label.get(), CKMC_PERMISSION_READ);
304     RUNNER_ASSERT_MSG(CKMC_ERROR_INVALID_PARAMETER == ret,
305                          "Trying to allow myself returned: " << ret);
306 }
307
308 // verifies that alias can not contain forbidden characters
309 RUNNER_CHILD_TEST(T3007_manager_check_alias_valid)
310 {
311     switch_to_storage_user(TEST_LABEL);
312     ScopedSaveData ssd(TEST_ALIAS);
313
314     std::string test_alias_playground = std::string("AAA BBB CCC");
315     check_read(test_alias_playground.c_str(), 0, TEST_DATA, CKMC_ERROR_INVALID_PARAMETER);
316
317     // control: expect success
318     check_read(TEST_ALIAS, 0, TEST_DATA);
319     check_read(TEST_ALIAS, TEST_LABEL, TEST_DATA);
320 }
321
322 // verifies that label can not contain forbidden characters
323 RUNNER_CHILD_TEST(T3008_manager_check_label_valid)
324 {
325     switch_to_storage_user(TEST_LABEL);
326     ScopedSaveData ssd(TEST_ALIAS);
327
328     // basic test
329     std::string test_label_playground = std::string("AAA BBB CCC");
330     check_read(TEST_ALIAS, test_label_playground.c_str(), TEST_DATA, CKMC_ERROR_INVALID_PARAMETER);
331
332     // insert part of the separator in the middle
333     test_label_playground = std::string(TEST_LABEL);
334     test_label_playground.insert(test_label_playground.size()/2, ckmc_label_name_separator);
335     check_read(TEST_ALIAS, test_label_playground.c_str(), TEST_DATA, CKMC_ERROR_INVALID_PARAMETER);
336
337     // prepend separator
338     test_label_playground = std::string(TEST_LABEL);
339     test_label_playground.insert(0, ckmc_label_name_separator);
340     check_read(TEST_ALIAS, test_label_playground.c_str(), TEST_DATA, CKMC_ERROR_INVALID_PARAMETER);
341
342     // append separator
343     test_label_playground = std::string(TEST_LABEL);
344     test_label_playground.append(ckmc_label_name_separator);
345     check_read(TEST_ALIAS, test_label_playground.c_str(), TEST_DATA, CKMC_ERROR_INVALID_PARAMETER);
346
347     // control: expect success
348     check_read(TEST_ALIAS, TEST_LABEL, TEST_DATA);
349 }
350
351 // tries to access other application data without permission
352 RUNNER_TEST(T3020_manager_access_not_allowed)
353 {
354     CharPtr top_label = get_label();
355
356     ScopedSaveData ssd(TEST_ALIAS);
357     {
358         ScopedLabel sl(TEST_LABEL2);
359
360         std::string TEST_ALIAS_adr = aliasWithLabel(top_label.get(), TEST_ALIAS);
361         check_read_not_visible(TEST_ALIAS_adr.c_str());
362         check_remove_not_visible(TEST_ALIAS_adr.c_str());
363     }
364 }
365
366 // tries to access other application data with permission
367 RUNNER_TEST(T3021_manager_access_allowed)
368 {
369     CharPtr top_label = get_label();
370     ScopedSaveData ssd(TEST_ALIAS);
371
372     allow_access(TEST_ALIAS, TEST_LABEL2, CKMC_PERMISSION_READ);
373     {
374         ScopedLabel sl(TEST_LABEL2);
375
376         check_read_allowed(aliasWithLabel(top_label.get(), TEST_ALIAS).c_str());
377     }
378 }
379
380 // tries to read other application data with permission for read/remove
381 RUNNER_TEST(T3022_manager_access_allowed_with_remove)
382 {
383     CharPtr top_label = get_label();
384     ScopedSaveData ssd(TEST_ALIAS);
385
386     allow_access(TEST_ALIAS, TEST_LABEL2, CKMC_PERMISSION_READ | CKMC_PERMISSION_REMOVE);
387     {
388         ScopedLabel sl(TEST_LABEL2);
389
390         check_read_allowed(aliasWithLabel(top_label.get(), TEST_ALIAS).c_str());
391     }
392 }
393
394 // tries to remove other application data with permission for reading only
395 RUNNER_TEST(T3023_manager_access_allowed_remove_denied)
396 {
397     CharPtr top_label = get_label();
398     ScopedSaveData ssd(TEST_ALIAS);
399
400     allow_access(TEST_ALIAS, TEST_LABEL2, CKMC_PERMISSION_READ);
401     {
402         ScopedLabel sl(TEST_LABEL2);
403
404         std::string TEST_ALIAS_adr = aliasWithLabel(top_label.get(), TEST_ALIAS);
405         check_remove_denied(TEST_ALIAS_adr.c_str());
406         check_read_allowed(TEST_ALIAS_adr.c_str());
407     }
408 }
409
410 // tries to remove other application data with permission
411 RUNNER_TEST(T3025_manager_remove_allowed)
412 {
413     CharPtr top_label = get_label();
414     ScopedSaveData ssd(TEST_ALIAS);
415
416     allow_access(TEST_ALIAS, TEST_LABEL2, CKMC_PERMISSION_READ | CKMC_PERMISSION_REMOVE);
417     {
418         ScopedLabel sl(TEST_LABEL2);
419
420         check_remove_allowed(aliasWithLabel(top_label.get(), TEST_ALIAS).c_str());
421     }
422 }
423
424 // tries to access other application data after allow function was called twice with different
425 // rights
426 RUNNER_TEST(T3026_manager_double_allow)
427 {
428     CharPtr top_label = get_label();
429     ScopedSaveData ssd(TEST_ALIAS);
430
431     // access should be overwritten
432     allow_access(TEST_ALIAS, TEST_LABEL2, CKMC_PERMISSION_READ | CKMC_PERMISSION_REMOVE);
433     allow_access(TEST_ALIAS, TEST_LABEL2, CKMC_PERMISSION_READ);
434     {
435         ScopedLabel sl(TEST_LABEL2);
436
437         std::string TEST_ALIAS_adr = aliasWithLabel(top_label.get(), TEST_ALIAS);
438         check_remove_denied(TEST_ALIAS_adr.c_str());
439         check_read_allowed(TEST_ALIAS_adr.c_str());
440     }
441 }
442
443 // tries to access application data with permission and after permission has been revoked
444 RUNNER_TEST(T3027_manager_allow_deny)
445 {
446     CharPtr top_label = get_label();
447     ScopedSaveData ssd(TEST_ALIAS);
448
449     std::string TEST_ALIAS_adr = aliasWithLabel(top_label.get(), TEST_ALIAS);
450
451     allow_access(TEST_ALIAS, TEST_LABEL2, CKMC_PERMISSION_READ);
452     {
453         ScopedLabel sl(TEST_LABEL2);
454
455         check_remove_denied(TEST_ALIAS_adr.c_str());
456         check_read_allowed(TEST_ALIAS_adr.c_str());
457     }
458
459     deny_access(TEST_ALIAS, TEST_LABEL2);
460     {
461         ScopedLabel sl(TEST_LABEL2);
462
463         check_remove_not_visible(TEST_ALIAS_adr.c_str());
464         check_read_not_visible(TEST_ALIAS_adr.c_str());
465     }
466 }
467
468 RUNNER_TEST(T3028_manager_access_by_label)
469 {
470     CharPtr top_label = get_label();
471     const char *additional_data = "label-2-data";
472     ScopedSaveData ssd(TEST_ALIAS);
473
474     allow_access(TEST_ALIAS, TEST_LABEL2, CKMC_PERMISSION_READ);
475     {
476         ScopedLabel sl(TEST_LABEL2);
477         ScopedSaveData ssd(TEST_ALIAS, additional_data);
478         allow_access(TEST_ALIAS, top_label.get(), CKMC_PERMISSION_READ);
479
480         // test if accessing valid alias (of label2 domain)
481         check_read_allowed(TEST_ALIAS, additional_data);
482
483         // this has to be done here - in the scope, otherwise
484         // scope destructor will remove the TEST_LABEL2::TEST_ALIAS
485         {
486             ScopedLabel sl(top_label.get());
487
488             // test if can access label2 alias from label1 domain - should succeed
489             check_read_allowed(aliasWithLabel(TEST_LABEL2, TEST_ALIAS).c_str(), additional_data);
490         }
491     }
492
493     // test if accessing valid alias (of label1 domain)
494     check_read_allowed(TEST_ALIAS);
495
496     // access should not be possible - already left the LABEL2 scope, object should be removed
497     check_read_not_visible(aliasWithLabel(TEST_LABEL2, TEST_ALIAS).c_str());
498 }
499
500 // tries to modify another label's permission
501 RUNNER_TEST(T3029_manager_access_modification_by_foreign_label)
502 {
503     ScopedLabel sl(TEST_LABEL);
504     ScopedSaveData ssd(TEST_ALIAS);
505     allow_access(TEST_ALIAS, TEST_LABEL3, CKMC_PERMISSION_READ | CKMC_PERMISSION_REMOVE);
506     {
507         ScopedLabel sl(TEST_LABEL2);
508
509         allow_access_negative(aliasWithLabel(TEST_LABEL, TEST_ALIAS).c_str(), TEST_LABEL4, CKMC_PERMISSION_READ | CKMC_PERMISSION_REMOVE, CKMC_ERROR_PERMISSION_DENIED);
510         deny_access_negative (aliasWithLabel(TEST_LABEL, TEST_ALIAS).c_str(), TEST_LABEL4, CKMC_ERROR_PERMISSION_DENIED);
511     }
512 }
513
514 // checks if only aliases readable by given app are returned
515 RUNNER_TEST(T3030_manager_get_all_aliases)
516 {
517     ScopedSaveData ssd1(TEST_ALIAS);
518     ScopedSaveData ssd2(TEST_ALIAS2);
519
520     int count = count_aliases();
521
522     allow_access(TEST_ALIAS, TEST_LABEL2, CKMC_PERMISSION_READ);
523     {
524         ScopedLabel sl(TEST_LABEL2);
525
526         // check that app can access other aliases when it has permission
527         check_alias_count(count - 1);
528
529         ScopedSaveData ssd3(TEST_ALIAS3);
530
531         // check that app can access its own aliases
532         check_alias_count(count - 1 + 1);
533     }
534
535     deny_access(TEST_ALIAS, TEST_LABEL2);
536     {
537         ScopedLabel sl(TEST_LABEL2);
538
539         // check that app can't access other aliases for which permission has been revoked
540         check_alias_count(count - 2);
541     }
542 }
543
544 RUNNER_TEST(T3031_manager_test_decrypt_from_another_label)
545 {
546     int temp;
547     CharPtr top_label = get_label();
548     ScopedSaveData ssd(TEST_ALIAS);
549
550     allow_access(TEST_ALIAS, TEST_LABEL2, CKMC_PERMISSION_READ);
551     {
552         ScopedLabel sl(TEST_LABEL2);
553
554         check_read_allowed(aliasWithLabel(top_label.get(), TEST_ALIAS).c_str());
555
556         // remove the DKEK key - so that on read it must be added again
557         RUNNER_ASSERT_MSG(CKMC_ERROR_NONE == (temp = ckmc_lock_user_key(0)),
558                              "Error=" << temp);
559
560         // on this read, DKEK key will be added again
561         check_read_allowed(aliasWithLabel(top_label.get(), TEST_ALIAS).c_str());
562     }
563 }
564
565
566 /////////////////////////////////////////////////////////////////////////////
567 // Control
568
569 // invalid argument check
570 RUNNER_TEST(T3101_control_allow_access_invalid)
571 {
572     int ret;
573     ret = ckmc_set_permission_by_adm(USER_ROOT, "alias", "accessor", CKMC_PERMISSION_READ);
574     RUNNER_ASSERT(CKMC_ERROR_INVALID_PARAMETER == ret);
575     ret = ckmc_set_permission_by_adm(USER_ROOT, "owner alias", NULL, CKMC_PERMISSION_READ);
576     RUNNER_ASSERT(CKMC_ERROR_INVALID_PARAMETER == ret);
577
578     // double owner
579     std::string aliasLabel = aliasWithLabel(get_label().get(), TEST_ALIAS);
580     ret = ckmc_set_permission_by_adm(USER_ROOT, aliasWithLabel("another-owner", aliasLabel.c_str()).c_str(), TEST_LABEL, CKMC_PERMISSION_READ);
581     RUNNER_ASSERT(CKMC_ERROR_INVALID_PARAMETER == ret);
582 }
583
584 // invalid argument check
585 RUNNER_TEST(T3102_control_deny_access_invalid)
586 {
587     RUNNER_ASSERT(CKMC_ERROR_INVALID_PARAMETER ==
588             ckmc_set_permission_by_adm(USER_ROOT, aliasWithLabel(NULL, "alias").c_str(), "accessor", CKMC_PERMISSION_NONE));
589     RUNNER_ASSERT(CKMC_ERROR_INVALID_PARAMETER ==
590             ckmc_set_permission_by_adm(USER_ROOT, aliasWithLabel("owner", "alias").c_str(), NULL, CKMC_PERMISSION_NONE));
591
592     // double owner
593     std::string aliasLabel = aliasWithLabel(get_label().get(), TEST_ALIAS);
594     RUNNER_ASSERT(CKMC_ERROR_INVALID_PARAMETER ==
595             ckmc_set_permission_by_adm(USER_ROOT, aliasWithLabel("another-owner", aliasLabel.c_str()).c_str(), TEST_LABEL, CKMC_PERMISSION_NONE));
596 }
597
598 // tries to allow access for non existing alias
599 RUNNER_TEST(T3103_control_allow_access_non_existing)
600 {
601     int ret = ckmc_set_permission_by_adm(USER_ROOT, aliasWithLabel(NO_OWNER, NO_ALIAS).c_str(), "label", CKMC_PERMISSION_READ);
602     RUNNER_ASSERT_MSG(CKMC_ERROR_DB_ALIAS_UNKNOWN == ret,
603                          "Allowing access for non existing alias returned " << ret);
604 }
605
606 // tries to deny access for non existing alias
607 RUNNER_TEST(T3104_control_deny_access_non_existing)
608 {
609     int ret = ckmc_set_permission_by_adm(USER_ROOT, aliasWithLabel(NO_OWNER, NO_ALIAS).c_str(), "label", CKMC_PERMISSION_NONE);
610     RUNNER_ASSERT_MSG(CKMC_ERROR_DB_ALIAS_UNKNOWN == ret,
611                          "Denying access for non existing alias returned " << ret);
612 }
613
614 // tries to deny non existing access
615 RUNNER_TEST(T3105_control_deny_access_non_existing_access)
616 {
617     ScopedSaveData ssd(TEST_ALIAS);
618
619     CharPtr label = get_label();
620
621     // deny non existing access to existing alias
622     int ret = ckmc_set_permission_by_adm(USER_ROOT, aliasWithLabel(get_label().get(), TEST_ALIAS).c_str(), "label", CKMC_PERMISSION_NONE);
623     RUNNER_ASSERT_MSG(CKMC_ERROR_INVALID_PARAMETER == ret,
624                          "Denying non existing access returned: " << ret);
625 }
626
627 // tries to allow application to access its own data
628 RUNNER_TEST(T3106_control_allow_access_to_myself)
629 {
630     ScopedSaveData ssd(TEST_ALIAS);
631
632     CharPtr label = get_label();
633     int ret = ckmc_set_permission(TEST_ALIAS, label.get(), CKMC_PERMISSION_READ);
634     RUNNER_ASSERT_MSG(CKMC_ERROR_INVALID_PARAMETER == ret,
635                          "Trying to allow myself returned: " << ret);
636 }
637
638 // tries to use admin API as a user
639 RUNNER_CHILD_TEST(T3110_control_allow_access_as_user)
640 {
641     switch_to_storage_user(TEST_LABEL);
642     int ret = ckmc_set_permission_by_adm(USER_ROOT, aliasWithLabel("owner", "alias").c_str(), "accessor", CKMC_PERMISSION_READ);
643     RUNNER_ASSERT_MSG(CKMC_ERROR_PERMISSION_DENIED == ret,
644                          "Ordinary user should not be able to use control API. Error " << ret);
645 }
646
647 // tries to use admin API as a user
648 RUNNER_CHILD_TEST(T3111_control_allow_access_as_user)
649 {
650     switch_to_storage_user(TEST_LABEL);
651     int ret = ckmc_set_permission_by_adm(USER_ROOT, aliasWithLabel("owner", "alias").c_str(), "accessor", CKMC_PERMISSION_NONE);
652     RUNNER_ASSERT_MSG(CKMC_ERROR_PERMISSION_DENIED == ret,
653                          "Ordinary user should not be able to use control API. Error " << ret);
654 }
655
656 // tries to read other application data with permission
657 RUNNER_TEST(T3121_control_access_allowed)
658 {
659     CharPtr top_label = get_label();
660     ScopedSaveData ssd(TEST_ALIAS);
661
662     allow_access_by_adm(TEST_ALIAS, TEST_LABEL2, CKMC_PERMISSION_READ);
663     {
664         ScopedLabel sl(TEST_LABEL2);
665
666         check_read_allowed(aliasWithLabel(top_label.get(), TEST_ALIAS).c_str());
667     }
668 }
669
670 // tries to read other application data with permission to read/remove
671 RUNNER_TEST(T3122_control_access_allowed_with_remove)
672 {
673     CharPtr top_label = get_label();
674     ScopedSaveData ssd(TEST_ALIAS);
675
676     allow_access_by_adm(TEST_ALIAS, TEST_LABEL2, CKMC_PERMISSION_READ | CKMC_PERMISSION_REMOVE);
677     {
678         ScopedLabel sl(TEST_LABEL2);
679
680         check_read_allowed(aliasWithLabel(top_label.get(), TEST_ALIAS).c_str());
681     }
682 }
683
684 // tries to remove other application data with permission to read
685 RUNNER_TEST(T3122_control_access_allowed_remove_denied)
686 {
687     CharPtr top_label = get_label();
688     ScopedSaveData ssd(TEST_ALIAS);
689
690     allow_access_by_adm(TEST_ALIAS, TEST_LABEL2, CKMC_PERMISSION_READ);
691     {
692         ScopedLabel sl(TEST_LABEL2);
693
694         check_remove_denied(aliasWithLabel(top_label.get(), TEST_ALIAS).c_str());
695     }
696 }
697
698 // tries to remove other application data with permission
699 RUNNER_TEST(T3125_control_remove_allowed)
700 {
701     CharPtr top_label = get_label();
702     ScopedSaveData ssd(TEST_ALIAS);
703
704     allow_access_by_adm(TEST_ALIAS, TEST_LABEL2, CKMC_PERMISSION_READ | CKMC_PERMISSION_REMOVE);
705     {
706         ScopedLabel sl(TEST_LABEL2);
707
708         check_remove_allowed(aliasWithLabel(top_label.get(), TEST_ALIAS).c_str());
709     }
710 }
711
712 // tries to access other application data after allow function has been called twice with different
713 // rights
714 RUNNER_TEST(T3126_control_double_allow)
715 {
716     CharPtr top_label = get_label();
717     ScopedSaveData ssd(TEST_ALIAS);
718
719     // access should be overwritten
720     allow_access_by_adm(TEST_ALIAS, TEST_LABEL2, CKMC_PERMISSION_READ | CKMC_PERMISSION_REMOVE);
721     allow_access_by_adm(TEST_ALIAS, TEST_LABEL2, CKMC_PERMISSION_READ);
722     {
723         ScopedLabel sl(TEST_LABEL2);
724
725         std::string TEST_ALIAS_adr = aliasWithLabel(top_label.get(), TEST_ALIAS);
726         check_remove_denied(TEST_ALIAS_adr.c_str());
727         check_read_allowed(TEST_ALIAS_adr.c_str());
728     }
729 }
730
731 // tries to access other application data with permission and after permission has been revoked
732 RUNNER_TEST(T3127_control_allow_deny)
733 {
734     CharPtr top_label = get_label();
735     ScopedSaveData ssd(TEST_ALIAS);
736
737     std::string TEST_ALIAS_adr = aliasWithLabel(top_label.get(), TEST_ALIAS);
738
739     allow_access_by_adm(TEST_ALIAS, TEST_LABEL2, CKMC_PERMISSION_READ);
740     {
741         ScopedLabel sl(TEST_LABEL2);
742
743         check_remove_denied(TEST_ALIAS_adr.c_str());
744         check_read_allowed(TEST_ALIAS_adr.c_str());
745     }
746     CharPtr label = get_label();
747     deny_access_by_adm(TEST_ALIAS, TEST_LABEL2);
748     {
749         ScopedLabel sl(TEST_LABEL2);
750
751         check_remove_not_visible(TEST_ALIAS_adr.c_str());
752         check_read_not_visible(TEST_ALIAS_adr.c_str());
753     }
754 }
755
756 // checks if only aliases readable by given app are returned
757 RUNNER_TEST(T3130_control_get_all_aliases)
758 {
759     ScopedSaveData ssd1(TEST_ALIAS);
760     ScopedSaveData ssd2(TEST_ALIAS2);
761
762     int count = count_aliases();
763
764     allow_access_by_adm(TEST_ALIAS, TEST_LABEL2, CKMC_PERMISSION_READ);
765     {
766         ScopedLabel sl(TEST_LABEL2);
767
768         // check that app can access other aliases when it has permission
769         check_alias_count(count - 1);
770
771         ScopedSaveData ssd3(TEST_ALIAS3);
772
773         // check that app can access its own aliases
774         check_alias_count(count - 1 + 1);
775     }
776
777     deny_access_by_adm(TEST_ALIAS, TEST_LABEL2);
778     {
779         ScopedLabel sl(TEST_LABEL2);
780
781         // check that app can't access other aliases for which permission has been revoked
782         check_alias_count(count - 2);
783     }
784 }
785
786 // tries to add access to data in a database of invalid user
787 RUNNER_TEST(T3140_control_allow_invalid_user)
788 {
789     ScopedSaveData ssd(TEST_ALIAS);
790
791     int ret = ckmc_set_permission_by_adm(
792             APP_UID, aliasWithLabel(get_label().get(), TEST_ALIAS).c_str(), TEST_LABEL2, CKMC_PERMISSION_READ | CKMC_PERMISSION_REMOVE);
793     RUNNER_ASSERT_MSG(CKMC_ERROR_DB_ALIAS_UNKNOWN == ret,
794                          "Trying to allow access to invalid user returned: " << ret);
795 }
796
797 // tries to revoke access to data in a database of invalid user
798 RUNNER_TEST(T3141_control_deny_invalid_user)
799 {
800     ScopedSaveData ssd(TEST_ALIAS);
801
802     int ret = ckmc_set_permission_by_adm(APP_UID, aliasWithLabel(get_label().get(), TEST_ALIAS).c_str(), TEST_LABEL2, CKMC_PERMISSION_NONE);
803     RUNNER_ASSERT_MSG(CKMC_ERROR_DB_ALIAS_UNKNOWN == ret,
804                          "Trying to deny access to invalid user returned: " << ret);
805 }
806
807
808 RUNNER_TEST(T3999_deinit)
809 {
810     int temp;
811     RUNNER_ASSERT_MSG(CKMC_ERROR_NONE == (temp = ckmc_lock_user_key(APP_UID)),
812                          "Error=" << temp);
813     RUNNER_ASSERT_MSG(CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(APP_UID)),
814                          "Error=" << temp);
815     RUNNER_ASSERT_MSG(CKMC_ERROR_NONE == (temp = ckmc_lock_user_key(USER_ROOT)),
816                          "Error=" << temp);
817     RUNNER_ASSERT_MSG(CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(USER_ROOT)),
818                          "Error=" << temp);
819 }