tizen 2.3 release
[framework/web/wearable/wrt-security.git] / tests / ace_install / test_cases.cpp
1 /*
2  * Copyright (c) 2011 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  * @file    test_cases.cpp
18  * @author  Tonasz Swierczek (t.swierczek@samsung.com)
19  * @version 1.0
20  * @brief   Implementation file for ACE install test cases.
21  */
22 #include <string.h>
23 #include <cstring>
24
25 #include <dpl/test/test_runner.h>
26 #include <dpl/log/log.h>
27
28 #include "ace_api_install.h"
29 #include "widget_installer.h"
30 #include <ace-dao-ro/AceDAOReadOnly.h>
31
32 RUNNER_TEST_GROUP_INIT(ACE_INSTALL_TEST_SUITE)
33
34 /*
35  * test author: Tomasz Swierczek (t.swierczek@samsung.com)
36  * tested functions: ace_install_initialize(),
37  * ace_install_shutdown()
38  * purpose: New C-API tests. Tests proper initialization and deinitialization
39  * of ace-install library.
40  */
41 RUNNER_TEST(ace_install_test_01_initialization)
42 {
43     RUNNER_ASSERT(ACE_OK == ace_install_initialize());
44     RUNNER_ASSERT(ACE_OK == ace_install_shutdown());
45     RUNNER_ASSERT(ACE_OK == ace_install_initialize());
46 }
47
48 /*
49  * test author: Tomasz Swierczek (t.swierczek@samsung.com)
50  * tested functions: ace_update_policy()
51  * purpose: New C-API tests. Tests proper policy re-parsing many times in a row.
52  */
53 RUNNER_TEST(ace_install_test_02_policy_update)
54 {
55     RUNNER_ASSERT(ACE_OK == ace_update_policy());
56     RUNNER_ASSERT(ACE_OK == ace_update_policy());
57     RUNNER_ASSERT(ACE_OK == ace_update_policy());
58 }
59
60
61 /*
62  * test author: Tomasz Swierczek (t.swierczek@samsung.com)
63  * tested functions: ace_set_requested_dev_caps(),
64  * ace_get_requested_dev_caps(), ace_free_requested_dev_caps()
65  * purpose: New C-API tests. Tests proper setting and getting widget dev caps
66  * upon installation.
67  */
68 RUNNER_TEST(ace_install_test_03_requested_dev_caps)
69 {
70     ace_widget_handle_t handle = InstallerMockup::registerWidget();
71     ace_requested_dev_cap_t reqDevCaps[2];
72     reqDevCaps[0].device_capability = "dev-cap1";
73     reqDevCaps[0].smack_granted = ACE_TRUE;
74     reqDevCaps[1].device_capability = "dev-cap2";
75     reqDevCaps[1].smack_granted = ACE_FALSE;
76     ace_requested_dev_cap_list_t caps;
77     caps.count = 2;
78     caps.items = reqDevCaps;
79     RUNNER_ASSERT(ACE_OK == ace_set_requested_dev_caps(handle, &caps));
80     ace_requested_dev_cap_list_t capsGotFromDB;
81     RUNNER_ASSERT(ACE_OK == ace_get_requested_dev_caps(handle, &capsGotFromDB));
82     RUNNER_ASSERT(capsGotFromDB.count == caps.count);
83     unsigned int i;
84     bool one = false, two = false;
85     for (i = 0; i < capsGotFromDB.count; ++i) {
86         if (0 == strcmp(capsGotFromDB.items[i].device_capability,
87                                  caps.items[0].device_capability)) {
88             RUNNER_ASSERT(capsGotFromDB.items[i].smack_granted ==
89                         caps.items[0].smack_granted);
90             one = true;
91         } else {
92             RUNNER_ASSERT(capsGotFromDB.items[i].smack_granted ==
93                                     caps.items[1].smack_granted);
94             RUNNER_ASSERT(0 == strcmp(capsGotFromDB.items[i].device_capability,
95                                  caps.items[1].device_capability));
96             two = true;
97         }
98     }
99     RUNNER_ASSERT(one && two);
100     RUNNER_ASSERT(ACE_OK == ace_free_requested_dev_caps(&capsGotFromDB));
101 }
102
103 /*
104  * test author: Andrzej Surdej (a.surdej@gmail.com)
105  * tested functions: ace_register_widget(),
106  * ace_unregister_widget()
107  * purpose: New C-API tests. Tests proper setting and getting widget info.
108  */
109 namespace {
110     int widgetHandle = 1000;
111     std::string testAuthor = "someAwesomeProgrammer;)";
112     std::string testVersion = "1.1.1R";
113     std::string widgetGuid = "http://widgets.org/myTestWidget";
114     std::string widgetShareHref = "http://shareWithOtherNiggers.ny.wrong";
115 }
116
117 /*
118  * author:
119  * test: ace_register_widget(), ace_unregister_widget()
120  * description: Try to install and uninstall widget with all correct info.
121  * expect: widget should be installed and uninstalled correctly.
122  */
123 RUNNER_TEST(ace_install_test_04_correct_widget_info)
124 {
125     struct widget_info info;
126     info.author = strdup(testAuthor.c_str());
127     info.id = strdup(widgetGuid.c_str());
128     info.type = WAC20;
129     info.version = strdup(testVersion.c_str());
130     info.shareHerf = strdup(widgetShareHref.c_str());
131
132     ace_return_t retValue = ace_register_widget(widgetHandle, &info, NULL);
133
134     //clean up before asserts
135     free(info.author);
136     free(info.id);
137     free(info.version);
138     free(info.shareHerf);
139
140     RUNNER_ASSERT(ACE_OK == retValue);
141     RUNNER_ASSERT(AceDB::AceDAOReadOnly::isWidgetInstalled(widgetHandle));
142
143     std::string gotVersion = AceDB::AceDAOReadOnly::getVersion(widgetHandle);
144     RUNNER_ASSERT(gotVersion == testVersion);
145
146     std::string gotGuid = AceDB::AceDAOReadOnly::getGUID(widgetHandle);
147     RUNNER_ASSERT(gotGuid == widgetGuid);
148
149     std::string gotShareHref = AceDB::AceDAOReadOnly::getShareHref(widgetHandle);
150     RUNNER_ASSERT(gotShareHref == widgetShareHref);
151
152     RUNNER_ASSERT(AceDB::AceDAOReadOnly::getWidgetType(widgetHandle) == AppTypes::WAC20);
153     RUNNER_ASSERT(AceDB::AceDAOReadOnly::getAuthorName(widgetHandle) == testAuthor);
154
155     RUNNER_ASSERT(ACE_OK == ace_unregister_widget(widgetHandle));
156     RUNNER_ASSERT(!AceDB::AceDAOReadOnly::isWidgetInstalled(widgetHandle));
157 }
158
159 /*
160  * author:
161  * test: ace_register_widget(), ace_unregister_widget()
162  * description: Negative test. NULL widget data passed.
163  * expect: Widget should not be installed.
164  */
165 RUNNER_TEST(ace_install_test_06_wrong_widget_info)
166 {
167     ace_return_t retVal = ace_register_widget(widgetHandle, NULL, NULL);
168     RUNNER_ASSERT(retVal != ACE_OK);
169     RUNNER_ASSERT(!AceDB::AceDAOReadOnly::isWidgetInstalled(widgetHandle));
170     //just to be sure
171     ace_unregister_widget(widgetHandle);
172 }
173
174 /*
175  * author:
176  * test: ace_register_widget(), ace_unregister_widget()
177  * description: Check if the same widget can be installed twice.
178  * expect: First widget should be installed, second widget should NOT be installed.
179  */
180 RUNNER_TEST(ace_install_test_07_twins)
181 {
182     struct widget_info info;
183     info.author = strdup(testAuthor.c_str());
184     info.id = strdup(widgetGuid.c_str());
185     info.type = WAC20;
186     info.version = strdup(testVersion.c_str());
187     info.shareHerf = strdup(widgetShareHref.c_str());
188
189     ace_return_t retValue = ace_register_widget(widgetHandle, &info, NULL);
190
191     //clean up before asserts
192     free(info.author);
193     free(info.id);
194     free(info.version);
195     free(info.shareHerf);
196
197     RUNNER_ASSERT(ACE_OK == retValue);
198     RUNNER_ASSERT(AceDB::AceDAOReadOnly::isWidgetInstalled(widgetHandle));
199
200     retValue = ace_register_widget(widgetHandle, &info, NULL);
201     RUNNER_ASSERT(ACE_OK != retValue);
202
203     RUNNER_ASSERT(AceDB::AceDAOReadOnly::isWidgetInstalled(widgetHandle));
204     ace_unregister_widget(widgetHandle);
205 }
206
207 /*
208  * author:
209  * test: ace_register_widget(), got***.empty(), getWidgetType(), ace_unregister_widget()
210  * description: Installing Widget with NULL info
211  * expect: Widget should be installed, all data about widget should be empty.
212  */
213 RUNNER_TEST(ace_install_test_08_empty_values)
214 {
215     struct widget_info info;
216     info.author = NULL;
217     info.id = NULL;
218     info.type = Tizen;
219     info.version = NULL;
220     info.shareHerf = NULL;
221
222     ace_return_t retValue = ace_register_widget(widgetHandle, &info, NULL);
223
224     //clean up before asserts
225     free(info.author);
226     free(info.id);
227     free(info.version);
228     free(info.shareHerf);
229
230     RUNNER_ASSERT(ACE_OK == retValue);
231     RUNNER_ASSERT(AceDB::AceDAOReadOnly::isWidgetInstalled(widgetHandle));
232
233     std::string gotAuthor = AceDB::AceDAOReadOnly::getAuthorName(widgetHandle);
234     RUNNER_ASSERT(gotAuthor.empty());
235
236     std::string gotVersion = AceDB::AceDAOReadOnly::getVersion(widgetHandle);
237     RUNNER_ASSERT(gotVersion.empty());
238
239     std::string gotGuid = AceDB::AceDAOReadOnly::getGUID(widgetHandle);
240     RUNNER_ASSERT(gotGuid.empty());
241
242     std::string gotShareHref = AceDB::AceDAOReadOnly::getShareHref(widgetHandle);
243     RUNNER_ASSERT(gotShareHref.empty());
244
245     RUNNER_ASSERT(AceDB::AceDAOReadOnly::getWidgetType(widgetHandle) == AppTypes::Tizen);
246
247     RUNNER_ASSERT(ACE_OK == ace_unregister_widget(widgetHandle));
248     RUNNER_ASSERT(!AceDB::AceDAOReadOnly::isWidgetInstalled(widgetHandle));
249 }
250
251 namespace {
252     int chaniId0 = 1;
253     std::string commonName0 = "commonNameForZeroElement";
254     std::string md50 = "md5CertificateForZeroElement";
255     std::string sha0 = "shaCertificateForZeroElement";
256     ace_cert_owner_t owner0 = AUTHOR;
257     ace_cert_type_t type0 = ENDENTITY;
258
259     int chaniId1 = 2;
260     std::string commonName1 = "commonNameForFirstElement";
261     std::string md51 = "md5CertificateForFirstElement";
262     std::string sha1 = "shaCertificateForFirstElement";
263     ace_cert_owner_t owner1 = AUTHOR;
264     ace_cert_type_t type1 = ENDENTITY;
265
266     int chaniId2 = 3;
267     std::string commonName2 = "commonNameForSecondElement";
268     std::string md52 = "md5CertificateForSecondElement";
269     std::string sha2 = "shaCertificateForSecondElement";
270     ace_cert_owner_t owner2 = DISTRIBUTOR;
271     ace_cert_type_t type2 = ROOT;
272 }
273
274 /*
275  * author:
276  * test: ace_register_widget(), getKeyCommonNameList(), getKeyFingerprints()
277  * description: Check if certificate verification works well while widget installing. Also check saved certificate data.
278  * expect: Widget should be installed properly, all data from certificate should match.
279  */
280 RUNNER_TEST(ace_install_test_09_certificate_list)
281 {
282     struct widget_info info;
283     info.author = NULL;
284     info.id = NULL;
285     info.type = Tizen;
286     info.version = NULL;
287     info.shareHerf = NULL;
288
289     ace_certificate_data** certList = new ace_certificate_data*[4];
290     certList[3] = NULL; //last element
291
292     for (int i = 0; i < 3; ++i) {
293         certList[i] = new ace_certificate_data;
294     }
295     certList[0]->chain_id = chaniId0;
296     certList[0]->common_name = strdup(commonName0.c_str());
297     certList[0]->md5_fp = strdup(md50.c_str());
298     certList[0]->sha1_fp = strdup(sha0.c_str());
299     certList[0]->owner = owner0;
300     certList[0]->type = type0;
301
302     certList[1]->chain_id = chaniId1;
303     certList[1]->common_name = strdup(commonName1.c_str());
304     certList[1]->md5_fp = strdup(md51.c_str());
305     certList[1]->sha1_fp = strdup(sha1.c_str());
306     certList[1]->owner = owner1;
307     certList[1]->type = type1;
308
309     certList[2]->chain_id = chaniId2;
310     certList[2]->common_name = strdup(commonName2.c_str());
311     certList[2]->md5_fp = strdup(md52.c_str());
312     certList[2]->sha1_fp = strdup(sha2.c_str());
313     certList[2]->owner = owner2;
314     certList[2]->type = type2;
315
316     ace_return_t retValue = ace_register_widget(widgetHandle, &info, certList);
317
318     //clean up before asserts
319     free(info.author);
320     free(info.id);
321     free(info.version);
322     free(info.shareHerf);
323     for (int i = 0; i < 3; ++i) {
324         free(certList[i]->common_name);
325         free(certList[i]->md5_fp);
326         free(certList[i]->sha1_fp);
327         delete certList[i];
328     }
329     delete[] certList;
330
331     RUNNER_ASSERT(ACE_OK == retValue);
332     RUNNER_ASSERT(AceDB::AceDAOReadOnly::isWidgetInstalled(widgetHandle));
333
334     WidgetCertificateCNList commonNameList =
335         AceDB::AceDAOReadOnly::getKeyCommonNameList(widgetHandle,
336                                                     WidgetCertificateData::AUTHOR,
337                                                     WidgetCertificateData::ENDENTITY);
338     RUNNER_ASSERT(2 == commonNameList.size());
339     RUNNER_ASSERT(commonName0 == commonNameList.front() ||
340                   commonName0 == commonNameList.back());
341     RUNNER_ASSERT(commonName1 == commonNameList.front() ||
342                   commonName1 == commonNameList.back());
343     commonNameList.clear();
344     commonNameList =
345         AceDB::AceDAOReadOnly::getKeyCommonNameList(widgetHandle,
346                                                     WidgetCertificateData::DISTRIBUTOR,
347                                                     WidgetCertificateData::ROOT);
348     RUNNER_ASSERT(1 == commonNameList.size());
349     RUNNER_ASSERT(commonName2 == commonNameList.front());
350
351     FingerPrintList fpList =
352         AceDB::AceDAOReadOnly::getKeyFingerprints(widgetHandle,
353                                                   WidgetCertificateData::AUTHOR,
354                                                   WidgetCertificateData::ENDENTITY);
355     RUNNER_ASSERT(4 == fpList.size());
356
357     fpList.clear();
358     fpList =
359         AceDB::AceDAOReadOnly::getKeyFingerprints(widgetHandle,
360                                                   WidgetCertificateData::DISTRIBUTOR,
361                                                   WidgetCertificateData::ROOT);
362     RUNNER_ASSERT(2 == fpList.size());
363
364     ace_unregister_widget(widgetHandle);
365 }
366
367 /*
368  * test author: Tomasz Swierczek (t.swierczek@samsung.com)
369  * tested functions: ace_get_policy_result()
370  * purpose: New C-API tests. Tests proper policy evaluation by security daemon
371  * and successful passing data to ace-install library. For three calls,
372  * three results are expected: ACE_PERMIT, ACE_DENY and ACE_PROMPT.
373  */
374 RUNNER_TEST(ace_install_test_10_policy_results)
375 {
376     ace_widget_handle_t handle = InstallerMockup::registerWidget();
377     ace_resource_t resourceA = "resourcePermit";
378     ace_resource_t resourceB = "resourceDeny";
379     ace_resource_t resourceC = "resourcePrompt";
380     ace_policy_result_t result = ACE_UNDEFINED;
381     RUNNER_ASSERT(ACE_OK == ace_get_policy_result(resourceA,
382                                                   handle,
383                                                   &result));
384     RUNNER_ASSERT(ACE_PERMIT == result);
385     RUNNER_ASSERT(ACE_OK == ace_get_policy_result(resourceB,
386                                                   handle,
387                                                   &result));
388     RUNNER_ASSERT(ACE_DENY == result);
389     RUNNER_ASSERT(ACE_OK == ace_get_policy_result(resourceC,
390                                                   handle,
391                                                   &result));
392     RUNNER_ASSERT(ACE_PROMPT == result);
393 }