204bc3e53421a091b7da965c6dba6f84498fad20
[framework/web/wrt-commons.git] / tests / dao / TestCases_WidgetDAO.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   TestCases_WidgetDAO.cpp
18  * @author  Pawel Sikorski (p.sikorski@samsung.com)
19  * @version 1.0
20  * @brief   This file contains tests for widget dao class.
21  */
22
23 #include <list>
24 #include <cstdlib>
25 #include <cstdio>
26 #include <string>
27 #include <algorithm>
28 #include <dpl/test/test_runner.h>
29 #include <dpl/foreach.h>
30 #include <dpl/exception.h>
31 #include <dpl/wrt-dao-rw/widget_dao.h>
32 #include <dpl/wrt-dao-ro/wrt_db_types.h>
33 #include <dpl/string.h>
34 #include <wrt_plugin_export.h>
35
36 using namespace WrtDB;
37
38 namespace {
39 class WacSecurityMock : public WrtDB::IWacSecurity
40 {
41   public:
42     WacSecurityMock() :
43         mRecognized(false),
44         mDistributorSigned(false),
45         mWacSigned(false)
46     {}
47
48     virtual const WidgetCertificateDataList& getCertificateList() const
49     {
50         return mList;
51     }
52
53     virtual bool isRecognized() const
54     {
55         return mRecognized;
56     }
57     virtual bool isDistributorSigned() const
58     {
59         return mDistributorSigned;
60     }
61     virtual bool isWacSigned() const
62     {
63         return mWacSigned;
64     }
65     virtual void getCertificateChainList(CertificateChainList& /*lst*/) const {}
66     virtual void getCertificateChainList(CertificateChainList& /*lst*/,
67                                          CertificateSource /*source*/) const {}
68
69     WrtDB::WidgetCertificateDataList& getCertificateListRef()
70     {
71         return mList;
72     }
73
74     void setRecognized(bool recognized)
75     {
76         mRecognized = recognized;
77     }
78     void setDistributorSigned(bool distributorSigned)
79     {
80         mDistributorSigned = distributorSigned;
81     }
82     void setWacSigned(bool wacSigned)
83     {
84         mWacSigned = wacSigned;
85     }
86
87   private:
88     WrtDB::WidgetCertificateDataList mList;
89     // author signature verified
90     bool mRecognized;
91     // known distribuor
92     bool mDistributorSigned;
93     // distributor is wac
94     bool mWacSigned;
95 };
96
97 TizenAppId _registerWidget(const WidgetRegisterInfo& regInfo,
98                            const IWacSecurity& sec,
99                            int line)
100 {
101     TizenAppId tizenAppId;
102     Try {
103         auto previous = WidgetDAO::getTizenAppidList();
104
105         // register widget
106         tizenAppId = WidgetDAO::registerWidgetGeneratePkgId(regInfo, sec);
107
108         RUNNER_ASSERT_MSG(!tizenAppId.empty(),
109                           "(called from line " << line << ")");
110
111         auto current = WidgetDAO::getTizenAppidList();
112         RUNNER_ASSERT_MSG(previous.size() + 1 == current.size(),
113                           "(called from line " << line << ")");
114
115         RUNNER_ASSERT_MSG(WidgetDAO::isWidgetInstalled(
116                               tizenAppId),
117                           "(called from line " << line << " tizenAppId: " <<
118                           tizenAppId << ")");
119     }
120     Catch(WidgetDAO::Exception::AlreadyRegistered) {
121         RUNNER_ASSERT_MSG(
122             false,
123             "Unexpected exception (called from line " << line << ")");
124     }
125     return tizenAppId;
126 }
127
128 #define REGISTER_WIDGET(regInfo, sec) _registerWidget((regInfo), \
129                                                       (sec), \
130                                                       __LINE__)
131 } // namespace
132
133 // Widgets used <2300,2500), 2000, 2001, 2002, 2003
134
135 #define RUNNER_ASSERT_WHAT_EQUALS(in, test)                   \
136     { std::string tmp(in);                                     \
137       RUNNER_ASSERT_MSG(tmp == test, "Equals: [" + tmp + "]"); }
138
139 #define RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(in, test)          \
140     {                                                     \
141         if (in.IsNull()) { RUNNER_ASSERT_MSG(false, "NULL"); } \
142         else { RUNNER_ASSERT_WHAT_EQUALS(DPL::ToUTF8String(*in), test); } \
143     }
144
145 #define RUNNER_ASSERT_WHAT_EQUALS_OPTIONALINT(in, test)                   \
146     {                                                                 \
147         if (in.IsNull()) { RUNNER_ASSERT_MSG(false, "NULL"); }             \
148         else { RUNNER_ASSERT_MSG(*in == test, "Equals: [" + *in + "]"); } \
149     }
150
151 RUNNER_TEST_GROUP_INIT(DAO)
152
153 //2300
154 /*
155  * Name: widget_dao_test_register_widget_empty_strings
156  * Description: Tests registeration of new widget with empty values
157  * Expected: widget should be registered in database
158  */
159 RUNNER_TEST(widget_dao_test_register_widget_empty_strings)
160 {
161     WidgetRegisterInfo regInfo;
162
163     //ext info
164     regInfo.shareHref = "";
165
166     //info
167     regInfo.configInfo.widget_id = DPL::FromUTF8String("");
168     regInfo.configInfo.version = DPL::FromUTF8String("");
169     regInfo.configInfo.width = 10;
170     regInfo.configInfo.height = 10;
171     regInfo.configInfo.authorName = DPL::FromUTF8String("");
172     regInfo.configInfo.authorEmail = DPL::FromUTF8String("");
173     regInfo.configInfo.authorHref = DPL::FromUTF8String("");
174     regInfo.baseFolder = "";
175     //TODO authenticated, etc...
176     regInfo.configInfo.flashNeeded = false;
177     regInfo.configInfo.minVersionRequired = DPL::FromUTF8String("1.0");
178     regInfo.configInfo.backSupported = true;
179
180     //loc info
181     ConfigParserData::LocalizedData locData;
182     locData.name = DPL::FromUTF8String("");
183     locData.shortName = DPL::FromUTF8String("");
184     locData.description = DPL::FromUTF8String("");
185     locData.license = DPL::FromUTF8String("");
186     locData.licenseFile = DPL::FromUTF8String("");
187     locData.licenseHref = DPL::FromUTF8String("");
188     regInfo.configInfo.localizedDataSet.insert(
189         std::make_pair(DPL::FromUTF8String("en"), locData));
190
191     //userAgentLoc
192
193     //icons
194     ConfigParserData::Icon icon(DPL::FromUTF8String(""));
195     icon.width = 10;
196     icon.height = 10;
197     LocaleSet locs;
198     locs.insert(DPL::FromUTF8String("en"));
199     WidgetRegisterInfo::LocalizedIcon locIcon(icon, locs);
200     regInfo.localizationData.icons.push_back(locIcon);
201
202     //start file
203     WidgetRegisterInfo::StartFileProperties prop;
204     prop.encoding = DPL::FromUTF8String("");
205     prop.type = DPL::FromUTF8String("");
206     WidgetRegisterInfo::LocalizedStartFile file;
207     file.path = DPL::FromUTF8String("");
208     file.propertiesForLocales.insert(
209         std::make_pair(DPL::FromUTF8String("en"), prop));
210     regInfo.localizationData.startFiles.push_back(file);
211
212     //widget pref
213     ConfigParserData::Preference pref(DPL::FromUTF8String(""), false);
214     pref.value = DPL::FromUTF8String("");
215     regInfo.configInfo.preferencesList.insert(pref);
216
217     //widget feature
218     ConfigParserData::Feature feat(DPL::FromUTF8String(""));
219     regInfo.configInfo.featuresList.insert(feat);
220
221     //win modes
222     regInfo.configInfo.windowModes.insert(DPL::FromUTF8String(""));
223
224     //WARP info
225     ConfigParserData::AccessInfo access(DPL::FromUTF8String(""), true);
226     regInfo.configInfo.accessInfoSet.insert(access);
227
228     //certificates
229     WidgetCertificateData cert;
230     cert.owner = WidgetCertificateData::AUTHOR;
231     cert.type = WidgetCertificateData::ROOT;
232     cert.chainId = 1;
233     cert.strMD5Fingerprint = "";
234     cert.strSHA1Fingerprint = "";
235     cert.strCommonName = DPL::FromUTF8String("");
236
237     WacSecurityMock security;
238     security.getCertificateListRef().push_back(cert);
239
240     REGISTER_WIDGET(regInfo, security);
241 }
242
243 /*
244  * Name: widget_dao_test_register_widget_empty_strings
245  * Description: Tests possiblity of registering twice same content (different
246  * tizenId)
247  * Expected: it should be possible
248  */
249 RUNNER_TEST(widget_dao_test_twice_install_same_widget)
250 {
251     WacSecurityMock sec;
252     {
253         WidgetRegisterInfo regInfo;
254         REGISTER_WIDGET(regInfo, sec);
255     }
256     {
257         WidgetRegisterInfo regInfo;
258         REGISTER_WIDGET(regInfo, sec);
259     }
260 }
261
262 /*
263  * Name: widget_dao_test_register_widget_minimum_info
264  * Description: Tests simplest registeration of new widget
265  * Expected: widget should be registered in database
266  */
267 RUNNER_TEST(widget_dao_test_register_widget_minimum_info)
268 {
269     WacSecurityMock sec;
270     const std::size_t NUMBER_OF_WIDGETS = 5;
271
272     TizenAppId lastTizenAppId;
273
274     for (std::size_t number = 0; number < NUMBER_OF_WIDGETS; ++number) {
275         WidgetRegisterInfo regInfo;
276         TizenAppId tizenAppId = REGISTER_WIDGET(regInfo, sec);
277
278         lastTizenAppId = tizenAppId;
279
280         WidgetDAO dao(tizenAppId);
281         //TODO check nulls
282     }
283 }
284
285 /*
286  * Name: widget_dao_test_register_widget_info
287  * Description: Tests registeration of many widgets
288  * Expected: all widgets should be registered in database
289  */
290 RUNNER_TEST(widget_dao_test_register_widget_info)
291 {
292     WacSecurityMock sec;
293     const std::size_t NUMBER_OF_WIDGETS = 5;
294
295     for (std::size_t number = 0; number < NUMBER_OF_WIDGETS; ++number) {
296         std::ostringstream str;
297         str << "register_info_test_" << number;
298
299         WidgetRegisterInfo regInfo;
300         regInfo.configInfo.widget_id = DPL::FromUTF8String(str.str());
301         regInfo.configInfo.version = DPL::FromUTF8String(str.str());
302         regInfo.configInfo.width = 10;
303         regInfo.configInfo.height = 10;
304         regInfo.configInfo.authorName = DPL::FromUTF8String(str.str());
305         regInfo.configInfo.authorEmail = DPL::FromUTF8String(str.str());
306         regInfo.configInfo.authorHref = DPL::FromUTF8String(str.str());
307         regInfo.baseFolder = str.str(); //base folder at the end has /
308         regInfo.configInfo.flashNeeded = false;
309         //TODO authenticated, etc...
310         //in wrt-installer: TaskWidgetConfig::fillWidgetConfig:
311         //regInfo.minVersion = regInfo.configInfo.minVersionRequired
312         regInfo.minVersion = DPL::FromUTF8String("1.0");
313         regInfo.configInfo.backSupported = true;
314
315         TizenAppId tizenAppId = REGISTER_WIDGET(regInfo, sec);
316
317         WidgetDAO dao(tizenAppId);
318         RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(dao.getGUID(), str.str());
319         RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(dao.getVersion(), str.str());
320         RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(dao.getAuthorName(), str.str());
321         RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(dao.getAuthorEmail(), str.str());
322         RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(dao.getAuthorHref(), str.str());
323         RUNNER_ASSERT_WHAT_EQUALS(dao.getBaseFolder(), str.str() + "/");
324         RUNNER_ASSERT(dao.getWebkitPluginsRequired() == false);
325         //        RUNNER_ASSERT(
326         //            dao.GetWidgetSecurityDomain() == WacSecurity::Trusted);
327         RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(dao.getMinimumWacVersion(), "1.0");
328     }
329 }
330
331 /*
332  * Name: widget_dao_test_register_widget_extended_info
333  * Description: Tests registeration of widget_extended_info
334  * Expected: registeration of extended inforamtion is checked
335  * via existence of backgroudn page value
336  */
337 RUNNER_TEST(widget_dao_test_register_widget_extended_info)
338 {
339     WacSecurityMock sec;
340     const std::size_t NUMBER_OF_WIDGETS = 5;
341
342     for (std::size_t number = 0; number < NUMBER_OF_WIDGETS; ++number) {
343         std::ostringstream str;
344         str << "register_ext_info_test_" << number;
345
346         WidgetRegisterInfo regInfo;
347
348         //        regInfo.shareHref = str.str();
349         regInfo.configInfo.backgroundPage = L"background.html";
350
351         TizenAppId tizenAppId = REGISTER_WIDGET(regInfo, sec);
352
353         WidgetDAO dao(tizenAppId);
354         //        RUNNER_ASSERT_WHAT_EQUALS(dao.GetShareHref(), str.str());
355
356         RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(dao.getBackgroundPage(),
357                                            "background.html");
358     }
359 }
360
361 /*
362  * Name: widget_dao_test_register_widget_localized_info
363  * Description: Tests registeration of localized widgets information
364  * Expected: values received by dao should match those which were registered
365  */
366 RUNNER_TEST(widget_dao_test_register_widget_localized_info)
367 {
368     WacSecurityMock sec;
369     const std::size_t NUMBER_OF_WIDGETS = 5;
370
371     for (std::size_t number = 0; number < NUMBER_OF_WIDGETS; ++number) {
372         WidgetRegisterInfo regInfo;
373         std::ostringstream str_en;
374         std::ostringstream str_pl;
375         str_en << "register_loc_info_test_en_" << number;
376         str_pl << "register_loc_info_test_pl_" << number;
377         { //EN
378             ConfigParserData::LocalizedData locDataEn;
379             locDataEn.name = DPL::FromUTF8String(str_en.str());
380             locDataEn.shortName = DPL::FromUTF8String(str_en.str());
381             locDataEn.description = DPL::FromUTF8String(str_en.str());
382             locDataEn.license = DPL::FromUTF8String(str_en.str());
383             locDataEn.licenseFile = DPL::FromUTF8String(str_en.str());
384             locDataEn.licenseHref = DPL::FromUTF8String(str_en.str());
385             regInfo.configInfo.localizedDataSet.insert(
386                 std::make_pair(DPL::FromUTF8String("en"), locDataEn));
387         }
388
389         { //PL
390             ConfigParserData::LocalizedData locDataPl;
391             locDataPl.name = DPL::FromUTF8String(str_pl.str());
392             locDataPl.shortName = DPL::FromUTF8String(str_pl.str());
393             locDataPl.description = DPL::FromUTF8String(str_pl.str());
394             locDataPl.license = DPL::FromUTF8String(str_pl.str());
395             locDataPl.licenseFile = DPL::FromUTF8String(str_pl.str());
396             locDataPl.licenseHref = DPL::FromUTF8String(str_pl.str());
397             regInfo.configInfo.localizedDataSet.insert(
398                 std::make_pair(DPL::FromUTF8String("pl"), locDataPl));
399         }
400
401         TizenAppId tizenAppId = REGISTER_WIDGET(regInfo, sec);
402
403         WidgetDAO dao(tizenAppId);
404         RUNNER_ASSERT_MSG(dao.getLanguageTags().size() == 2,
405                           "language tags list invalid");
406
407         { //EN
408             WidgetLocalizedInfo locInfo =
409                 dao.getLocalizedInfo(DPL::FromUTF8String("en"));
410
411             RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(locInfo.name,
412                                                str_en.str());
413             RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(locInfo.shortName,
414                                                str_en.str());
415             RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(locInfo.description,
416                                                str_en.str());
417             RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(locInfo.license,
418                                                str_en.str());
419             RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(locInfo.licenseHref,
420                                                str_en.str());
421         }
422
423         { //PL
424             WidgetLocalizedInfo locInfo =
425                 dao.getLocalizedInfo(DPL::FromUTF8String("pl"));
426
427             RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(locInfo.name,
428                                                str_pl.str());
429             RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(locInfo.shortName,
430                                                str_pl.str());
431             RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(locInfo.description,
432                                                str_pl.str());
433             RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(locInfo.license,
434                                                str_pl.str());
435             RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(locInfo.licenseHref,
436                                                str_pl.str());
437         }
438     }
439 }
440
441 /*
442  * Name: widget_dao_test_register_widget_icons
443  * Description: Tests registeration of localized icons information
444  * Expected: values received by dao should match those which were registered
445  * for icon
446  */
447 RUNNER_TEST(widget_dao_test_register_widget_icons)
448 {
449     WacSecurityMock sec;
450     WidgetRegisterInfo regInfo;
451
452     ConfigParserData::Icon icon(L"icon1");
453     icon.width = 10;
454     icon.height = 10;
455     LocaleSet locs;
456     locs.insert(DPL::FromUTF8String("en"));
457     WidgetRegisterInfo::LocalizedIcon locIcon(icon, locs);
458     regInfo.localizationData.icons.push_back(locIcon);
459
460     TizenAppId tizenAppId = REGISTER_WIDGET(regInfo, sec);
461
462     WidgetDAO dao(tizenAppId);
463     WidgetDAOReadOnly::WidgetIconList list = dao.getIconList();
464
465     RUNNER_ASSERT(list.size() == regInfo.localizationData.icons.size());
466     WidgetDAOReadOnly::WidgetIconList::const_iterator it1 = list.begin();
467     WidgetRegisterInfo::LocalizedIconList::const_iterator it2 =
468         regInfo.localizationData.icons.begin();
469     for (; it1 != list.end() && it2 != regInfo.localizationData.icons.end();
470          ++it1, ++it2)
471     {
472         RUNNER_ASSERT(it2->height == it1->iconHeight);
473         RUNNER_ASSERT(it2->width == it1->iconWidth);
474         RUNNER_ASSERT(it2->src == it1->iconSrc);
475         RUNNER_ASSERT(it2->availableLocales == locs);
476     }
477 }
478
479 /*
480  * Name: widget_dao_test_register_widget_start_files
481  * Description: Tests registeration of localized start files
482  * Expected: no expectations as it should be removed
483  */
484 RUNNER_TEST(widget_dao_test_register_widget_start_files)
485 {
486     WacSecurityMock sec;
487
488     WidgetRegisterInfo::LocalizedStartFileList files;
489     WidgetRegisterInfo::StartFilePropertiesForLocalesMap map1;
490     WidgetRegisterInfo::StartFileProperties prop1;
491     prop1.encoding = DPL::FromUTF8String("enc1");
492     prop1.type = DPL::FromUTF8String("type1");
493
494     map1.insert(std::make_pair(DPL::FromUTF8String("en"), prop1));
495     map1.insert(std::make_pair(DPL::FromUTF8String("pl"), prop1));
496
497     WidgetRegisterInfo::LocalizedStartFile file1;
498     WidgetRegisterInfo::LocalizedStartFile file2;
499     file1.path = DPL::FromUTF8String("path1");
500     file1.propertiesForLocales = map1;
501     file2.path = DPL::FromUTF8String("path2");
502     file2.propertiesForLocales = map1;
503
504     files.push_back(file1);
505     files.push_back(file1);
506
507     WidgetRegisterInfo regInfo;
508     regInfo.localizationData.startFiles = files;
509
510     REGISTER_WIDGET(regInfo, sec);
511
512     //TODO no getter in WidgetDAO (getter in LocalizedWidgetDAO,
513     // but it will be removed
514 }
515
516 /*
517  * Name: widget_dao_test_register_widget_features
518  * Description: Tests registeration of features of widget
519  * Expected: number of features should match (for given widget reginfo)
520  */
521 RUNNER_TEST(widget_dao_test_register_widget_features)
522 {
523     WacSecurityMock sec;
524     ConfigParserData::FeaturesList features;
525     features.insert(ConfigParserData::Feature(DPL::FromUTF8String("f1")));
526     features.insert(ConfigParserData::Feature(DPL::FromUTF8String("f2")));
527     features.insert(ConfigParserData::Feature(DPL::FromUTF8String("f3")));
528
529     WidgetRegisterInfo regInfo;
530     FOREACH(it, features)
531     regInfo.configInfo.featuresList.insert(*it);
532
533     TizenAppId tizenAppId = REGISTER_WIDGET(regInfo, sec);
534
535     WidgetDAO dao(tizenAppId);
536     WidgetFeatureSet out = dao.getFeaturesList();
537     RUNNER_ASSERT_MSG(out.size() == features.size(),
538                       "wrong number of features");
539     //    FOREACH(it, out)
540     //        RUNNER_ASSERT(features.find(*it) != features.end());
541 }
542
543 /*
544  * Name: widget_dao_test_register_widget_security_settings
545  * Description: Tests registeration of dafault values of security settings
546  * Expected: widget should be registered in database.
547  * Returned values should match dafault.
548  */
549 RUNNER_TEST(widget_dao_test_register_widget_security_settings)
550 {
551     WacSecurityMock sec;
552     WidgetRegisterInfo regInfo;
553     TizenAppId tizenAppId = REGISTER_WIDGET(regInfo, sec);
554
555     WidgetDAO dao(tizenAppId);
556     RUNNER_ASSERT_MSG(
557         dao.getSecurityPopupUsage() == WrtDB::SETTINGS_TYPE_ON,
558         "SecurityPopupUsage is not deafult on");
559     RUNNER_ASSERT_MSG(
560         dao.getGeolocationUsage() == WrtDB::SETTINGS_TYPE_ON,
561         "GeolocationUsage is not deafult on");
562     RUNNER_ASSERT_MSG(
563         dao.getWebNotificationUsage() == WrtDB::SETTINGS_TYPE_ON,
564         "WebNotificationUsage is not deafult on");
565     RUNNER_ASSERT_MSG(
566         dao.getWebDatabaseUsage() == WrtDB::SETTINGS_TYPE_ON,
567         "WebDatabaseUsage is not deafult on");
568
569     dao.setSecurityPopupUsage(WrtDB::SETTINGS_TYPE_OFF);
570     RUNNER_ASSERT_MSG(
571         dao.getSecurityPopupUsage() == WrtDB::SETTINGS_TYPE_OFF,
572         "SecurityPopupUsage - wrong value");
573     RUNNER_ASSERT_MSG(
574         dao.getGeolocationUsage() == WrtDB::SETTINGS_TYPE_ON,
575         "GeolocationUsage - wrong value");
576     RUNNER_ASSERT_MSG(
577         dao.getWebNotificationUsage() == WrtDB::SETTINGS_TYPE_ON,
578         "WebNotificationUsage - wrong value");
579     RUNNER_ASSERT_MSG(
580         dao.getWebDatabaseUsage() == WrtDB::SETTINGS_TYPE_ON,
581         "WebDatabaseUsage - wrong value");
582
583     dao.setGeolocationUsage(WrtDB::SETTINGS_TYPE_ALWAYS_ASK);
584     RUNNER_ASSERT_MSG(
585         dao.getSecurityPopupUsage() == WrtDB::SETTINGS_TYPE_OFF,
586         "SecurityPopupUsage - wrong value");
587     RUNNER_ASSERT_MSG(
588         dao.getGeolocationUsage() == WrtDB::SETTINGS_TYPE_ALWAYS_ASK,
589         "GeolocationUsage - wrong value");
590     RUNNER_ASSERT_MSG(
591         dao.getWebNotificationUsage() == WrtDB::SETTINGS_TYPE_ON,
592         "WebNotificationUsage - wrong value");
593     RUNNER_ASSERT_MSG(
594         dao.getWebDatabaseUsage() == WrtDB::SETTINGS_TYPE_ON,
595         "WebDatabaseUsage - wrong value");
596
597     dao.setWebNotificationUsage(WrtDB::SETTINGS_TYPE_OFF);
598     RUNNER_ASSERT_MSG(
599         dao.getSecurityPopupUsage() == WrtDB::SETTINGS_TYPE_OFF,
600         "SecurityPopupUsage - wrong value");
601     RUNNER_ASSERT_MSG(
602         dao.getGeolocationUsage() == WrtDB::SETTINGS_TYPE_ALWAYS_ASK,
603         "GeolocationUsage - wrong value");
604     RUNNER_ASSERT_MSG(
605         dao.getWebNotificationUsage() == WrtDB::SETTINGS_TYPE_OFF,
606         "WebNotificationUsage - wrong value");
607     RUNNER_ASSERT_MSG(
608         dao.getWebDatabaseUsage() == WrtDB::SETTINGS_TYPE_ON,
609         "WebDatabaseUsage - wrong value");
610
611     dao.setWebDatabaseUsage(WrtDB::SETTINGS_TYPE_ALWAYS_ASK);
612     RUNNER_ASSERT_MSG(
613         dao.getSecurityPopupUsage() == WrtDB::SETTINGS_TYPE_OFF,
614         "SecurityPopupUsage - wrong value");
615     RUNNER_ASSERT_MSG(
616         dao.getGeolocationUsage() == WrtDB::SETTINGS_TYPE_ALWAYS_ASK,
617         "GeolocationUsage - wrong value");
618     RUNNER_ASSERT_MSG(
619         dao.getWebNotificationUsage() == WrtDB::SETTINGS_TYPE_OFF,
620         "WebNotificationUsage - wrong value");
621     RUNNER_ASSERT_MSG(
622         dao.getWebDatabaseUsage() == WrtDB::SETTINGS_TYPE_ALWAYS_ASK,
623         "WebDatabaseUsage - wrong value");
624 }
625
626 /*
627  * Name: widget_dao_test_register_widget_win_modes
628  * Description: Tests registeration of window modes
629  * Expected: all modes should be returned from dao
630  */
631 RUNNER_TEST(widget_dao_test_register_widget_win_modes)
632 {
633     WacSecurityMock sec;
634     std::set<DPL::String> modes;
635     modes.insert(DPL::FromUTF8String("full"));
636     modes.insert(DPL::FromUTF8String("window"));
637
638     WidgetRegisterInfo regInfo;
639
640     FOREACH(it, modes)
641     regInfo.configInfo.windowModes.insert(*it);
642
643     TizenAppId tizenAppId = REGISTER_WIDGET(regInfo, sec);
644
645     WidgetDAO dao(tizenAppId);
646     std::list<DPL::String> wins = dao.getWindowModes();
647     RUNNER_ASSERT_MSG(modes.size() == wins.size(),
648                       "wrong number of window modes");
649     FOREACH(it, wins)
650     RUNNER_ASSERT(modes.find(*it) != modes.end());
651 }
652
653 /*
654  * Name: widget_dao_test_register_widget_warp_info
655  * Description: Tests registeration of access info iris
656  * Expected: all access info iris should be returned from dao
657  */
658 RUNNER_TEST(widget_dao_test_register_widget_warp_info)
659 {
660     WacSecurityMock sec;
661     ConfigParserData::AccessInfoSet orig;
662     orig.insert(ConfigParserData::AccessInfo(DPL::FromUTF8String("iri1"),
663                                              true));
664     orig.insert(ConfigParserData::AccessInfo(DPL::FromUTF8String("iri2"),
665                                              false));
666     orig.insert(ConfigParserData::AccessInfo(DPL::FromUTF8String("iri3"),
667                                              true));
668
669     WidgetRegisterInfo regInfo;
670     FOREACH(it, orig)
671     regInfo.configInfo.accessInfoSet.insert(*it);
672
673     TizenAppId tizenAppId = REGISTER_WIDGET(regInfo, sec);
674
675     WidgetDAO dao(tizenAppId);
676
677     WidgetAccessInfoList out;
678     dao.getWidgetAccessInfo(out);
679     RUNNER_ASSERT_MSG(out.size() == orig.size(),
680                       "wrong number of access info elem");
681     FOREACH(it, out){
682         ConfigParserData::AccessInfo tmp(it->strIRI, it->bSubDomains);
683         RUNNER_ASSERT(orig.find(tmp) != orig.end());
684     }
685 }
686
687 /*
688  * Name: widget_dao_test_register_widget_certificates
689  * Description: Tests registeration of widget certificates
690  * Expected: all certificates should be returned from dao
691  * and should contain inserted data
692  */
693 RUNNER_TEST(widget_dao_test_register_widget_certificates)
694 {
695     WacSecurityMock sec;
696     WidgetRegisterInfo regInfo;
697
698     WidgetCertificateData cert;
699     cert.owner = WidgetCertificateData::AUTHOR;
700     cert.type = WidgetCertificateData::ROOT;
701     cert.chainId = 1;
702     cert.strMD5Fingerprint = "md5";
703     cert.strSHA1Fingerprint = "sha1";
704     cert.strCommonName = DPL::FromUTF8String("common name");
705
706     WidgetCertificateDataList& certListRef = sec.getCertificateListRef();
707     certListRef.push_back(cert);
708
709     // register widget
710     TizenAppId tizenAppId = REGISTER_WIDGET(regInfo, sec);
711
712     WidgetDAO dao(tizenAppId);
713
714     // certificates
715     WidgetCertificateDataList recList = dao.getCertificateDataList();
716     RUNNER_ASSERT(recList.size() == certListRef.size());
717
718     auto recListIt = recList.begin();
719     auto certListIt = certListRef.begin();
720     for (; recListIt != recList.end() && certListIt != certListRef.end();
721          ++recListIt, ++certListIt)
722     {
723         RUNNER_ASSERT(recListIt->chainId == certListIt->chainId);
724         RUNNER_ASSERT(recListIt->owner == certListIt->owner);
725         RUNNER_ASSERT(recListIt->strCommonName == certListIt->strCommonName);
726         RUNNER_ASSERT(recListIt->strMD5Fingerprint ==
727                       certListIt->strMD5Fingerprint);
728         RUNNER_ASSERT(recListIt->strSHA1Fingerprint ==
729                       certListIt->strSHA1Fingerprint);
730         RUNNER_ASSERT(recListIt->type == certListIt->type);
731     }
732
733     // fingerprints
734     RUNNER_ASSERT(dao.getKeyFingerprints(WidgetCertificateData::DISTRIBUTOR,
735                                          WidgetCertificateData::ENDENTITY).
736                       empty());
737     RUNNER_ASSERT(dao.getKeyFingerprints(WidgetCertificateData::AUTHOR,
738                                          WidgetCertificateData::ENDENTITY).
739                       empty());
740     RUNNER_ASSERT(dao.getKeyFingerprints(WidgetCertificateData::DISTRIBUTOR,
741                                          WidgetCertificateData::ROOT).empty());
742
743     FingerPrintList fingerprints = dao.getKeyFingerprints(
744             WidgetCertificateData::AUTHOR,
745             WidgetCertificateData::ROOT);
746
747     RUNNER_ASSERT(fingerprints.size() == certListRef.size() * 2);
748     FOREACH(it, certListRef)
749     {
750         auto md5 = std::find(fingerprints.begin(),
751                              fingerprints.end(),
752                              it->strMD5Fingerprint);
753         RUNNER_ASSERT(md5 != fingerprints.end());
754
755         auto sha = std::find(fingerprints.begin(),
756                              fingerprints.end(),
757                              it->strSHA1Fingerprint);
758         RUNNER_ASSERT(sha != fingerprints.end());
759     }
760
761     // common names
762     RUNNER_ASSERT(dao.getKeyCommonNameList(WidgetCertificateData::DISTRIBUTOR,
763                                            WidgetCertificateData::ENDENTITY).
764                       empty());
765     RUNNER_ASSERT(dao.getKeyCommonNameList(WidgetCertificateData::AUTHOR,
766                                            WidgetCertificateData::ENDENTITY).
767                       empty());
768     RUNNER_ASSERT(dao.getKeyCommonNameList(WidgetCertificateData::DISTRIBUTOR,
769                                            WidgetCertificateData::ROOT).empty());
770
771     FingerPrintList commonNames = dao.getKeyCommonNameList(
772             WidgetCertificateData::AUTHOR,
773             WidgetCertificateData::ROOT);
774
775     RUNNER_ASSERT(commonNames.size() == certListRef.size());
776     FOREACH(it, certListRef)
777     {
778         auto cn = std::find(commonNames.begin(),
779                             commonNames.end(),
780                             DPL::ToUTF8String(it->strCommonName));
781         RUNNER_ASSERT(cn != commonNames.end());
782     }
783 }
784
785 /*
786  * Name: widget_dao_test_register_widget_privileges
787  * Description: Tests registration of widget privileges
788  */
789 RUNNER_TEST(widget_dao_test_register_widget_privileges)
790 {
791
792     WacSecurityMock sec;
793     WidgetRegisterInfo regInfo;
794
795     ConfigParserData::PrivilegeList& privilegeList =
796             regInfo.configInfo.privilegeList;
797     privilegeList.insert(DPL::FromUTF8String("name"));
798     privilegeList.insert(DPL::FromUTF8String("name2"));
799
800     TizenAppId tizenAppId = REGISTER_WIDGET(regInfo, sec);
801     WidgetDAO dao(tizenAppId);
802
803     WrtDB::PrivilegeList privListFromDB;
804     privListFromDB = dao.getWidgetPrivilege();
805
806     RUNNER_ASSERT(privilegeList.size() == privListFromDB.size());
807
808     auto privListIt = privilegeList.begin();
809     auto privDBIt = privListFromDB.begin();
810     for(; privListIt != privilegeList.end() && privDBIt != privListFromDB.end();
811             ++privListIt, ++privDBIt)
812     {
813         RUNNER_ASSERT(*privDBIt == privListIt->name);
814     }
815 }
816
817 /*
818  * Name: widget_dao_test_register_app_control
819  * Description: Tests app control
820  */
821 RUNNER_TEST(widget_dao_test_register_app_control)
822 {
823     WacSecurityMock sec;
824     WidgetRegisterInfo regInfo;
825
826     ConfigParserData::AppControlInfo appControl(DPL::FromUTF8String("operation"));
827     appControl.m_disposition
828             = ConfigParserData::AppControlInfo::Disposition::WINDOW;
829     appControl.m_mimeList.insert(DPL::FromUTF8String("mime"));
830     appControl.m_src = DPL::FromUTF8String("src");
831     appControl.m_uriList.insert(DPL::FromUTF8String("uri"));
832
833     ConfigParserData::AppControlInfoList& appControlListRef
834             = regInfo.configInfo.appControlList;
835     appControlListRef.push_back(appControl);
836
837     TizenAppId tizenAppId = REGISTER_WIDGET(regInfo, sec);
838
839     WidgetDAO dao(tizenAppId);
840
841     WrtDB::WidgetAppControlList appControlInfoListDB;
842     dao.getAppControlList(appControlInfoListDB);
843     RUNNER_ASSERT(appControlInfoListDB.size() == appControlListRef.size());
844     auto appDBIt = appControlInfoListDB.begin();
845     auto appRefIt = appControlListRef.begin();
846
847     for (;appDBIt != appControlInfoListDB.end()
848             && appRefIt != appControlListRef.end();
849             ++appDBIt, ++appRefIt)
850     {
851         RUNNER_ASSERT((WidgetAppControl::Disposition)
852                 appRefIt->m_disposition == appDBIt->disposition);
853         RUNNER_ASSERT(appRefIt->m_index == appDBIt->index);
854         RUNNER_ASSERT(appRefIt->m_operation == appDBIt->operation);
855         RUNNER_ASSERT(appRefIt->m_src == appDBIt->src);
856         for(auto it = appRefIt->m_mimeList.begin();
857                 it != appRefIt->m_mimeList.end();
858                 ++it)
859         {
860             RUNNER_ASSERT((*it) == appDBIt->mime);
861         }
862         for(auto it = appRefIt->m_uriList.begin();
863                 it != appRefIt->m_uriList.end();
864                 ++it)
865         {
866             RUNNER_ASSERT((*it) == appDBIt->uri);
867         }
868     }
869 }
870
871 /*
872  * Name: widget_dao_test_is_widget_installed
873  * Description: Tests checking if widgets are installed
874  * Expected: installed widgets should be stated as installed
875  */
876 RUNNER_TEST(widget_dao_test_is_widget_installed)
877 {
878     RUNNER_ASSERT(WidgetDAO::isWidgetInstalled(L"tizenid201"));
879
880     WacSecurityMock sec;
881     WidgetRegisterInfo regInfo;
882
883     TizenAppId tizenAppId = REGISTER_WIDGET(regInfo, sec);
884
885     RUNNER_ASSERT(WidgetDAO::isWidgetInstalled(tizenAppId));
886 }
887
888 /*
889  * Name: widget_dao_test_unregister_widget
890  * Description: Tests unregistering widgets
891  * Expected: widget register informations should be successfully removed
892  */
893 RUNNER_TEST(widget_dao_test_unregister_widget)
894 {
895     WacSecurityMock sec;
896     WidgetHandleList handles = WidgetDAO::getHandleList();
897
898     WidgetRegisterInfo regInfo;
899
900     TizenAppId tizenAppId = REGISTER_WIDGET(regInfo, sec);
901
902     WidgetDAO::unregisterWidget(tizenAppId);
903
904     RUNNER_ASSERT_MSG(handles.size() == WidgetDAO::getHandleList().size(),
905                       "Widget unregister failed");
906 }
907
908 /*
909  * Name: widget_dao_test_register_or_update_widget
910  * Description: Tests reregistering widgets
911  * Expected: widget should be successfully replaced
912  */
913 RUNNER_TEST(widget_dao_test_register_or_update_widget)
914 {
915     WacSecurityMock sec;
916
917     WidgetRegisterInfo regInfo;
918     regInfo.configInfo.version = L"1.0";
919     regInfo.configInfo.authorName = L"AAA";
920
921     WidgetRegisterInfo regInfo2;
922     regInfo2.configInfo.version = L"1.1";
923     regInfo2.configInfo.authorName = L"BBB";
924
925     WrtDB::TizenAppId tizenAppId(L"abcdefghij");
926
927     //first installation
928     WidgetDAO::registerWidget(tizenAppId, regInfo, sec);
929     RUNNER_ASSERT_MSG(WidgetDAO::isWidgetInstalled(
930                           tizenAppId), "Widget is not registered");
931
932     //success full update
933     WidgetDAO::registerOrUpdateWidget(tizenAppId, regInfo2, sec);
934     RUNNER_ASSERT_MSG(WidgetDAO::isWidgetInstalled(
935                           tizenAppId), "Widget is not reregistered");
936     WidgetDAO dao(tizenAppId);
937     RUNNER_ASSERT_MSG(
938         *dao.getVersion() == L"1.1", "Data widget was not updated");
939     RUNNER_ASSERT_MSG(
940         *dao.getAuthorName() == L"BBB", "Data widget was not updated");
941
942     WidgetDAO::unregisterWidget(tizenAppId);
943 }
944
945 /*
946  * Name: widget_dao_test_get_widget_tizenAppId_list
947  * Description: Tests getTizenAppidList API for backendlib
948  * Expected: For all position in database should be returned one item in list
949  */
950 RUNNER_TEST(widget_dao_test_get_widget_tizenAppId_list)
951 {
952     TizenAppIdList tizenAppIds = WidgetDAO::getTizenAppidList();
953     RUNNER_ASSERT(tizenAppIds.size() >= 3);
954 }
955
956 /*
957  * Name: widget_dao_test_get_widget_list
958  * Description: Tests getTizenAppidList API for backendlib
959  * Expected: For all position in database should be returned one item in list
960  * Those item should contain valid tizenAppId
961  */
962 RUNNER_TEST(widget_dao_test_get_widget_list)
963 {
964     WidgetDAOReadOnlyList list = WidgetDAOReadOnly::getWidgetList();
965     RUNNER_ASSERT(list.size() >= 3);
966     RUNNER_ASSERT_MSG(!!list.front(), "widget dao exists");
967     WidgetDAOReadOnlyPtr dao = list.front();
968 }
969
970 /*
971  * Name: widget_dao_test_get_widget_attributes
972  * Description: Tests returning basic widget attributes by dao
973  * Expected: Attributes should match values inserted into database
974  */
975 RUNNER_TEST(widget_dao_test_get_widget_attributes)
976 {
977     {
978         TizenAppId tizenAppId = L"tizenid201";
979         WidgetDAO dao(tizenAppId);
980
981         RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(dao.getGUID(), "w_id_2000");
982         RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(dao.getVersion(), "1.0.0");
983         RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(dao.getAuthorName(), "a_name_2000");
984         RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(dao.getAuthorEmail(),
985                                            "a_email_2000");
986         RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(dao.getAuthorHref(), "a_href_2000");
987         RUNNER_ASSERT_WHAT_EQUALS(dao.getBaseFolder(), "basef_2000/");
988         RUNNER_ASSERT(dao.getWebkitPluginsRequired() == true);
989         RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(dao.getMinimumWacVersion(), "1.0");
990     }
991 }
992
993 /*
994  * Name: widget_dao_test_localization
995  * Description: Tests inserting and returning localization info
996  * Expected: Values inserted into database should match values received from
997  * database
998  */
999 RUNNER_TEST(widget_dao_test_localization)
1000 {
1001     WacSecurityMock sec;
1002
1003     // icon
1004     WidgetRegisterInfo regInfo;
1005     ConfigParserData::Icon icon(L"icon1");
1006     icon.width = 10;
1007     icon.height = 10;
1008     LocaleSet locs;
1009     locs.insert(DPL::FromUTF8String("en"));
1010     locs.insert(DPL::FromUTF8String("pl"));
1011     WidgetRegisterInfo::LocalizedIcon locIcon(icon, locs);
1012     regInfo.localizationData.icons.push_back(locIcon);
1013
1014     //start file
1015     WidgetRegisterInfo::StartFileProperties prop_en;
1016     prop_en.encoding = DPL::FromUTF8String("encoding_en");
1017     prop_en.type = DPL::FromUTF8String("type_en");
1018
1019     WidgetRegisterInfo::StartFileProperties prop_pl;
1020     prop_pl.encoding = DPL::FromUTF8String("encoding_pl");
1021     prop_pl.type = DPL::FromUTF8String("type_pl");
1022
1023     WidgetRegisterInfo::LocalizedStartFile file;
1024     file.path = DPL::FromUTF8String("path");
1025     file.propertiesForLocales.insert(
1026         std::make_pair(DPL::FromUTF8String("en"), prop_en));
1027     file.propertiesForLocales.insert(
1028         std::make_pair(DPL::FromUTF8String("pl"), prop_pl));
1029     regInfo.localizationData.startFiles.push_back(file);
1030
1031     // register widget
1032     TizenAppId tizenAppId = REGISTER_WIDGET(regInfo, sec);
1033
1034     WidgetDAO dao(tizenAppId);
1035
1036     // check localized icons
1037     WidgetDAO::WidgetLocalizedIconList locList = dao.getLocalizedIconList();
1038     RUNNER_ASSERT(locList.size() == locs.size());
1039
1040     // non-localized icon
1041     WidgetDAO::WidgetIconList list = dao.getIconList();
1042     int iconId = list.front().iconId;
1043
1044     // compare every icon with the origin
1045     auto locsIt = locs.begin();
1046     auto iconIt = locList.begin();
1047     for (; locsIt != locs.end() && iconIt != locList.end(); ++locsIt,
1048          ++iconIt)
1049     {
1050         RUNNER_ASSERT(iconIt->appId == dao.getHandle());
1051         RUNNER_ASSERT(iconIt->iconId == iconId);
1052         RUNNER_ASSERT(iconIt->widgetLocale == *locsIt);
1053     }
1054
1055     // localized start file list
1056     WidgetDAO::LocalizedStartFileList fList = dao.getLocalizedStartFileList();
1057     RUNNER_ASSERT(fList.size() == file.propertiesForLocales.size());
1058
1059     int startFileId = dao.getStartFileList().front().startFileId;
1060
1061     FOREACH(it, fList)
1062     {
1063         RUNNER_ASSERT(it->appId == dao.getHandle());
1064         auto propIt = file.propertiesForLocales.find(it->widgetLocale);
1065         RUNNER_ASSERT(propIt != file.propertiesForLocales.end());
1066         RUNNER_ASSERT(it->encoding == propIt->second.encoding);
1067         RUNNER_ASSERT(it->type == propIt->second.type);
1068         RUNNER_ASSERT(it->startFileId == startFileId);
1069     }
1070 }
1071
1072 /*
1073  * Name: widget_dao_test_wac_security
1074  * Description: Tests inserting and returning wac security information
1075  * Expected: Values inserted into database should match values received from
1076  * database
1077  */
1078 RUNNER_TEST(widget_dao_test_wac_security)
1079 {
1080     WacSecurityMock sec;
1081     WidgetRegisterInfo regInfo;
1082     {
1083         // register widget
1084         TizenAppId tizenAppId = REGISTER_WIDGET(regInfo, sec);
1085         WidgetDAO dao(tizenAppId);
1086
1087         RUNNER_ASSERT(!dao.isDistributorSigned());
1088         RUNNER_ASSERT(!dao.isRecognized());
1089         RUNNER_ASSERT(!dao.isWacSigned());
1090     }
1091     sec.setDistributorSigned(true);
1092     sec.setRecognized(true);
1093     sec.setWacSigned(true);
1094     {
1095         // register widget
1096         TizenAppId tizenAppId = REGISTER_WIDGET(regInfo, sec);
1097         WidgetDAO dao(tizenAppId);
1098
1099         RUNNER_ASSERT(dao.isDistributorSigned());
1100         RUNNER_ASSERT(dao.isRecognized());
1101         RUNNER_ASSERT(dao.isWacSigned());
1102     }
1103 }
1104
1105 /*
1106  * Name: widget_dao_test_register_scp
1107  * Description: Tests inserting and returning scp policy information
1108  * Expected: Value inserted into database should match values received from
1109  * database
1110  */
1111 RUNNER_TEST(widget_dao_test_register_scp)
1112 {
1113     WacSecurityMock sec;
1114     WidgetRegisterInfo regInfo;
1115     DPL::OptionalString policy = DPL::FromUTF8String("Some awesome csp policy");
1116     regInfo.configInfo.cspPolicy = policy;
1117     {
1118         // register widget
1119         TizenAppId tizenAppId = REGISTER_WIDGET(regInfo, sec);
1120         WidgetDAO dao(tizenAppId);
1121
1122         RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(
1123             dao.getCspPolicy(), DPL::ToUTF8String(*policy));
1124     }
1125 }
1126
1127 /*
1128  * Name: widget_dao_test_register_csp_empty
1129  * Description: Tests inserting and returning empty csp policy
1130  * Expected: Value inserted into database should match values received from
1131  * database
1132  */
1133 RUNNER_TEST(widget_dao_test_register_csp_empty)
1134 {
1135     WacSecurityMock sec;
1136     WidgetRegisterInfo regInfo;
1137     {
1138         // register widget
1139         TizenAppId tizenAppId = REGISTER_WIDGET(regInfo, sec);
1140         WidgetDAO dao(tizenAppId);
1141
1142         RUNNER_ASSERT_MSG(dao.getCspPolicy().IsNull(), "Policy is not null");
1143     }
1144 }
1145 #undef RUNNER_ASSERT_WHAT_EQUALS