merge with master
[platform/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(""), false);
219     ConfigParserData::Param par(DPL::FromUTF8String(("")));
220     par.value = DPL::FromUTF8String("");
221     feat.paramsList.insert(par);
222     regInfo.configInfo.featuresList.insert(feat);
223
224     //win modes
225     regInfo.configInfo.windowModes.insert(DPL::FromUTF8String(""));
226
227     //WARP info
228     ConfigParserData::AccessInfo access(DPL::FromUTF8String(""), true);
229     regInfo.configInfo.accessInfoSet.insert(access);
230
231     //certificates
232     WidgetCertificateData cert;
233     cert.owner = WidgetCertificateData::AUTHOR;
234     cert.type = WidgetCertificateData::ROOT;
235     cert.chainId = 1;
236     cert.strMD5Fingerprint = "";
237     cert.strSHA1Fingerprint = "";
238     cert.strCommonName = DPL::FromUTF8String("");
239
240     WacSecurityMock security;
241     security.getCertificateListRef().push_back(cert);
242
243     REGISTER_WIDGET(regInfo, security);
244 }
245
246 /*
247  * Name: widget_dao_test_register_widget_empty_strings
248  * Description: Tests possiblity of registering twice same content (different
249  * tizenId)
250  * Expected: it should be possible
251  */
252 RUNNER_TEST(widget_dao_test_twice_install_same_widget)
253 {
254     WacSecurityMock sec;
255     {
256         WidgetRegisterInfo regInfo;
257         REGISTER_WIDGET(regInfo, sec);
258     }
259     {
260         WidgetRegisterInfo regInfo;
261         REGISTER_WIDGET(regInfo, sec);
262     }
263 }
264
265 /*
266  * Name: widget_dao_test_register_widget_minimum_info
267  * Description: Tests simplest registeration of new widget
268  * Expected: widget should be registered in database
269  */
270 RUNNER_TEST(widget_dao_test_register_widget_minimum_info)
271 {
272     WacSecurityMock sec;
273     const std::size_t NUMBER_OF_WIDGETS = 5;
274
275     TizenAppId lastTizenAppId;
276
277     for (std::size_t number = 0; number < NUMBER_OF_WIDGETS; ++number) {
278         WidgetRegisterInfo regInfo;
279         TizenAppId tizenAppId = REGISTER_WIDGET(regInfo, sec);
280
281         lastTizenAppId = tizenAppId;
282
283         WidgetDAO dao(tizenAppId);
284         //TODO check nulls
285     }
286 }
287
288 /*
289  * Name: widget_dao_test_register_widget_info
290  * Description: Tests registeration of many widgets
291  * Expected: all widgets should be registered in database
292  */
293 RUNNER_TEST(widget_dao_test_register_widget_info)
294 {
295     WacSecurityMock sec;
296     const std::size_t NUMBER_OF_WIDGETS = 5;
297
298     for (std::size_t number = 0; number < NUMBER_OF_WIDGETS; ++number) {
299         std::ostringstream str;
300         str << "register_info_test_" << number;
301
302         WidgetRegisterInfo regInfo;
303         regInfo.configInfo.widget_id = DPL::FromUTF8String(str.str());
304         regInfo.configInfo.version = DPL::FromUTF8String(str.str());
305         regInfo.configInfo.width = 10;
306         regInfo.configInfo.height = 10;
307         regInfo.configInfo.authorName = DPL::FromUTF8String(str.str());
308         regInfo.configInfo.authorEmail = DPL::FromUTF8String(str.str());
309         regInfo.configInfo.authorHref = DPL::FromUTF8String(str.str());
310         regInfo.baseFolder = str.str(); //base folder at the end has /
311         regInfo.configInfo.flashNeeded = false;
312         //TODO authenticated, etc...
313         //in wrt-installer: TaskWidgetConfig::fillWidgetConfig:
314         //regInfo.minVersion = regInfo.configInfo.minVersionRequired
315         regInfo.minVersion = DPL::FromUTF8String("1.0");
316         regInfo.configInfo.backSupported = true;
317
318         TizenAppId tizenAppId = REGISTER_WIDGET(regInfo, sec);
319
320         WidgetDAO dao(tizenAppId);
321         RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(dao.getGUID(), str.str());
322         RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(dao.getVersion(), str.str());
323         RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(dao.getAuthorName(), str.str());
324         RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(dao.getAuthorEmail(), str.str());
325         RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(dao.getAuthorHref(), str.str());
326         RUNNER_ASSERT_WHAT_EQUALS(dao.getBaseFolder(), str.str() + "/");
327         RUNNER_ASSERT(dao.getWebkitPluginsRequired() == false);
328         //        RUNNER_ASSERT(
329         //            dao.GetWidgetSecurityDomain() == WacSecurity::Trusted);
330         RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(dao.getMinimumWacVersion(), "1.0");
331     }
332 }
333
334 /*
335  * Name: widget_dao_test_register_widget_extended_info
336  * Description: Tests registeration of widget_extended_info
337  * Expected: registeration of extended inforamtion is checked
338  * via existence of backgroudn page value
339  */
340 RUNNER_TEST(widget_dao_test_register_widget_extended_info)
341 {
342     WacSecurityMock sec;
343     const std::size_t NUMBER_OF_WIDGETS = 5;
344
345     for (std::size_t number = 0; number < NUMBER_OF_WIDGETS; ++number) {
346         std::ostringstream str;
347         str << "register_ext_info_test_" << number;
348
349         WidgetRegisterInfo regInfo;
350
351         //        regInfo.shareHref = str.str();
352         regInfo.configInfo.backgroundPage = L"background.html";
353
354         TizenAppId tizenAppId = REGISTER_WIDGET(regInfo, sec);
355
356         WidgetDAO dao(tizenAppId);
357         //        RUNNER_ASSERT_WHAT_EQUALS(dao.GetShareHref(), str.str());
358
359         RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(dao.getBackgroundPage(),
360                                            "background.html");
361     }
362 }
363
364 /*
365  * Name: widget_dao_test_register_widget_localized_info
366  * Description: Tests registeration of localized widgets information
367  * Expected: values received by dao should match those which were registered
368  */
369 RUNNER_TEST(widget_dao_test_register_widget_localized_info)
370 {
371     WacSecurityMock sec;
372     const std::size_t NUMBER_OF_WIDGETS = 5;
373
374     for (std::size_t number = 0; number < NUMBER_OF_WIDGETS; ++number) {
375         WidgetRegisterInfo regInfo;
376         std::ostringstream str_en;
377         std::ostringstream str_pl;
378         str_en << "register_loc_info_test_en_" << number;
379         str_pl << "register_loc_info_test_pl_" << number;
380         { //EN
381             ConfigParserData::LocalizedData locDataEn;
382             locDataEn.name = DPL::FromUTF8String(str_en.str());
383             locDataEn.shortName = DPL::FromUTF8String(str_en.str());
384             locDataEn.description = DPL::FromUTF8String(str_en.str());
385             locDataEn.license = DPL::FromUTF8String(str_en.str());
386             locDataEn.licenseFile = DPL::FromUTF8String(str_en.str());
387             locDataEn.licenseHref = DPL::FromUTF8String(str_en.str());
388             regInfo.configInfo.localizedDataSet.insert(
389                 std::make_pair(DPL::FromUTF8String("en"), locDataEn));
390         }
391
392         { //PL
393             ConfigParserData::LocalizedData locDataPl;
394             locDataPl.name = DPL::FromUTF8String(str_pl.str());
395             locDataPl.shortName = DPL::FromUTF8String(str_pl.str());
396             locDataPl.description = DPL::FromUTF8String(str_pl.str());
397             locDataPl.license = DPL::FromUTF8String(str_pl.str());
398             locDataPl.licenseFile = DPL::FromUTF8String(str_pl.str());
399             locDataPl.licenseHref = DPL::FromUTF8String(str_pl.str());
400             regInfo.configInfo.localizedDataSet.insert(
401                 std::make_pair(DPL::FromUTF8String("pl"), locDataPl));
402         }
403
404         TizenAppId tizenAppId = REGISTER_WIDGET(regInfo, sec);
405
406         WidgetDAO dao(tizenAppId);
407         RUNNER_ASSERT_MSG(dao.getLanguageTags().size() == 2,
408                           "language tags list invalid");
409
410         { //EN
411             WidgetLocalizedInfo locInfo =
412                 dao.getLocalizedInfo(DPL::FromUTF8String("en"));
413
414             RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(locInfo.name,
415                                                str_en.str());
416             RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(locInfo.shortName,
417                                                str_en.str());
418             RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(locInfo.description,
419                                                str_en.str());
420             RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(locInfo.license,
421                                                str_en.str());
422             RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(locInfo.licenseHref,
423                                                str_en.str());
424         }
425
426         { //PL
427             WidgetLocalizedInfo locInfo =
428                 dao.getLocalizedInfo(DPL::FromUTF8String("pl"));
429
430             RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(locInfo.name,
431                                                str_pl.str());
432             RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(locInfo.shortName,
433                                                str_pl.str());
434             RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(locInfo.description,
435                                                str_pl.str());
436             RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(locInfo.license,
437                                                str_pl.str());
438             RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(locInfo.licenseHref,
439                                                str_pl.str());
440         }
441     }
442 }
443
444 /*
445  * Name: widget_dao_test_register_widget_icons
446  * Description: Tests registeration of localized icons information
447  * Expected: values received by dao should match those which were registered
448  * for icon
449  */
450 RUNNER_TEST(widget_dao_test_register_widget_icons)
451 {
452     WacSecurityMock sec;
453     WidgetRegisterInfo regInfo;
454
455     ConfigParserData::Icon icon(L"icon1");
456     icon.width = 10;
457     icon.height = 10;
458     LocaleSet locs;
459     locs.insert(DPL::FromUTF8String("en"));
460     WidgetRegisterInfo::LocalizedIcon locIcon(icon, locs);
461     regInfo.localizationData.icons.push_back(locIcon);
462
463     TizenAppId tizenAppId = REGISTER_WIDGET(regInfo, sec);
464
465     WidgetDAO dao(tizenAppId);
466     WidgetDAOReadOnly::WidgetIconList list = dao.getIconList();
467
468     RUNNER_ASSERT(list.size() == regInfo.localizationData.icons.size());
469     WidgetDAOReadOnly::WidgetIconList::const_iterator it1 = list.begin();
470     WidgetRegisterInfo::LocalizedIconList::const_iterator it2 =
471         regInfo.localizationData.icons.begin();
472     for (; it1 != list.end() && it2 != regInfo.localizationData.icons.end();
473          ++it1, ++it2)
474     {
475         RUNNER_ASSERT(it2->height == it1->iconHeight);
476         RUNNER_ASSERT(it2->width == it1->iconWidth);
477         RUNNER_ASSERT(it2->src == it1->iconSrc);
478         RUNNER_ASSERT(it2->availableLocales == locs);
479     }
480 }
481
482 /*
483  * Name: widget_dao_test_register_widget_start_files
484  * Description: Tests registeration of localized start files
485  * Expected: no expectations as it should be removed
486  */
487 RUNNER_TEST(widget_dao_test_register_widget_start_files)
488 {
489     WacSecurityMock sec;
490
491     WidgetRegisterInfo::LocalizedStartFileList files;
492     WidgetRegisterInfo::StartFilePropertiesForLocalesMap map1;
493     WidgetRegisterInfo::StartFileProperties prop1;
494     prop1.encoding = DPL::FromUTF8String("enc1");
495     prop1.type = DPL::FromUTF8String("type1");
496
497     map1.insert(std::make_pair(DPL::FromUTF8String("en"), prop1));
498     map1.insert(std::make_pair(DPL::FromUTF8String("pl"), prop1));
499
500     WidgetRegisterInfo::LocalizedStartFile file1;
501     WidgetRegisterInfo::LocalizedStartFile file2;
502     file1.path = DPL::FromUTF8String("path1");
503     file1.propertiesForLocales = map1;
504     file2.path = DPL::FromUTF8String("path2");
505     file2.propertiesForLocales = map1;
506
507     files.push_back(file1);
508     files.push_back(file1);
509
510     WidgetRegisterInfo regInfo;
511     regInfo.localizationData.startFiles = files;
512
513     REGISTER_WIDGET(regInfo, sec);
514
515     //TODO no getter in WidgetDAO (getter in LocalizedWidgetDAO,
516     // but it will be removed
517 }
518
519 /*
520  * Name: widget_dao_test_register_widget_features
521  * Description: Tests registeration of features of widget
522  * Expected: number of features should match (for given widget reginfo)
523  */
524 RUNNER_TEST(widget_dao_test_register_widget_features)
525 {
526     WacSecurityMock sec;
527     ConfigParserData::FeaturesList features;
528     features.insert(ConfigParserData::Feature(DPL::FromUTF8String("f1"), true));
529     features.insert(ConfigParserData::Feature(DPL::FromUTF8String("f2")));
530     features.insert(ConfigParserData::Feature(DPL::FromUTF8String("f3"),
531                                               false));
532
533     WidgetRegisterInfo regInfo;
534     FOREACH(it, features)
535     regInfo.configInfo.featuresList.insert(*it);
536
537     TizenAppId tizenAppId = REGISTER_WIDGET(regInfo, sec);
538
539     WidgetDAO dao(tizenAppId);
540     WidgetFeatureSet out = dao.getFeaturesList();
541     RUNNER_ASSERT_MSG(out.size() == features.size(),
542                       "wrong number of features");
543     //    FOREACH(it, out)
544     //        RUNNER_ASSERT(features.find(*it) != features.end());
545 }
546
547 /*
548  * Name: widget_dao_test_register_widget_security_settings
549  * Description: Tests registeration of dafault values of security settings
550  * Expected: widget should be registered in database.
551  * Returned values should match dafault.
552  */
553 RUNNER_TEST(widget_dao_test_register_widget_security_settings)
554 {
555     WacSecurityMock sec;
556     WidgetRegisterInfo regInfo;
557     TizenAppId tizenAppId = REGISTER_WIDGET(regInfo, sec);
558
559     WidgetDAO dao(tizenAppId);
560     RUNNER_ASSERT_MSG(
561         dao.getSecurityPopupUsage() == WrtDB::SETTINGS_TYPE_ON,
562         "SecurityPopupUsage is not deafult on");
563     RUNNER_ASSERT_MSG(
564         dao.getGeolocationUsage() == WrtDB::SETTINGS_TYPE_ON,
565         "GeolocationUsage is not deafult on");
566     RUNNER_ASSERT_MSG(
567         dao.getWebNotificationUsage() == WrtDB::SETTINGS_TYPE_ON,
568         "WebNotificationUsage is not deafult on");
569     RUNNER_ASSERT_MSG(
570         dao.getWebDatabaseUsage() == WrtDB::SETTINGS_TYPE_ON,
571         "WebDatabaseUsage is not deafult on");
572     RUNNER_ASSERT_MSG(
573         dao.getFileSystemUsage() == WrtDB::SETTINGS_TYPE_ON,
574         "FileSystemUsage is not deafult on");
575
576     dao.setSecurityPopupUsage(WrtDB::SETTINGS_TYPE_OFF);
577     RUNNER_ASSERT_MSG(
578         dao.getSecurityPopupUsage() == WrtDB::SETTINGS_TYPE_OFF,
579         "SecurityPopupUsage - wrong value");
580     RUNNER_ASSERT_MSG(
581         dao.getGeolocationUsage() == WrtDB::SETTINGS_TYPE_ON,
582         "GeolocationUsage - wrong value");
583     RUNNER_ASSERT_MSG(
584         dao.getWebNotificationUsage() == WrtDB::SETTINGS_TYPE_ON,
585         "WebNotificationUsage - wrong value");
586     RUNNER_ASSERT_MSG(
587         dao.getWebDatabaseUsage() == WrtDB::SETTINGS_TYPE_ON,
588         "WebDatabaseUsage - wrong value");
589     RUNNER_ASSERT_MSG(
590         dao.getFileSystemUsage() == WrtDB::SETTINGS_TYPE_ON,
591         "FileSystemUsage - wrong value");
592
593     dao.setGeolocationUsage(WrtDB::SETTINGS_TYPE_ALWAYS_ASK);
594     RUNNER_ASSERT_MSG(
595         dao.getSecurityPopupUsage() == WrtDB::SETTINGS_TYPE_OFF,
596         "SecurityPopupUsage - wrong value");
597     RUNNER_ASSERT_MSG(
598         dao.getGeolocationUsage() == WrtDB::SETTINGS_TYPE_ALWAYS_ASK,
599         "GeolocationUsage - wrong value");
600     RUNNER_ASSERT_MSG(
601         dao.getWebNotificationUsage() == WrtDB::SETTINGS_TYPE_ON,
602         "WebNotificationUsage - wrong value");
603     RUNNER_ASSERT_MSG(
604         dao.getWebDatabaseUsage() == WrtDB::SETTINGS_TYPE_ON,
605         "WebDatabaseUsage - wrong value");
606     RUNNER_ASSERT_MSG(
607         dao.getFileSystemUsage() == WrtDB::SETTINGS_TYPE_ON,
608         "FileSystemUsage - wrong value");
609
610     dao.setWebNotificationUsage(WrtDB::SETTINGS_TYPE_OFF);
611     RUNNER_ASSERT_MSG(
612         dao.getSecurityPopupUsage() == WrtDB::SETTINGS_TYPE_OFF,
613         "SecurityPopupUsage - wrong value");
614     RUNNER_ASSERT_MSG(
615         dao.getGeolocationUsage() == WrtDB::SETTINGS_TYPE_ALWAYS_ASK,
616         "GeolocationUsage - wrong value");
617     RUNNER_ASSERT_MSG(
618         dao.getWebNotificationUsage() == WrtDB::SETTINGS_TYPE_OFF,
619         "WebNotificationUsage - wrong value");
620     RUNNER_ASSERT_MSG(
621         dao.getWebDatabaseUsage() == WrtDB::SETTINGS_TYPE_ON,
622         "WebDatabaseUsage - wrong value");
623     RUNNER_ASSERT_MSG(
624         dao.getFileSystemUsage() == WrtDB::SETTINGS_TYPE_ON,
625         "FileSystemUsage - wrong value");
626
627     dao.setWebDatabaseUsage(WrtDB::SETTINGS_TYPE_ALWAYS_ASK);
628     RUNNER_ASSERT_MSG(
629         dao.getSecurityPopupUsage() == WrtDB::SETTINGS_TYPE_OFF,
630         "SecurityPopupUsage - wrong value");
631     RUNNER_ASSERT_MSG(
632         dao.getGeolocationUsage() == WrtDB::SETTINGS_TYPE_ALWAYS_ASK,
633         "GeolocationUsage - wrong value");
634     RUNNER_ASSERT_MSG(
635         dao.getWebNotificationUsage() == WrtDB::SETTINGS_TYPE_OFF,
636         "WebNotificationUsage - wrong value");
637     RUNNER_ASSERT_MSG(
638         dao.getWebDatabaseUsage() == WrtDB::SETTINGS_TYPE_ALWAYS_ASK,
639         "WebDatabaseUsage - wrong value");
640     RUNNER_ASSERT_MSG(
641         dao.getFileSystemUsage() == WrtDB::SETTINGS_TYPE_ON,
642         "FileSystemUsage - wrong value");
643
644     dao.setFileSystemUsage(WrtDB::SETTINGS_TYPE_OFF);
645     RUNNER_ASSERT_MSG(
646         dao.getSecurityPopupUsage() == WrtDB::SETTINGS_TYPE_OFF,
647         "SecurityPopupUsage - wrong value");
648     RUNNER_ASSERT_MSG(
649         dao.getGeolocationUsage() == WrtDB::SETTINGS_TYPE_ALWAYS_ASK,
650         "GeolocationUsage - wrong value");
651     RUNNER_ASSERT_MSG(
652         dao.getWebNotificationUsage() == WrtDB::SETTINGS_TYPE_OFF,
653         "WebNotificationUsage - wrong value");
654     RUNNER_ASSERT_MSG(
655         dao.getWebDatabaseUsage() == WrtDB::SETTINGS_TYPE_ALWAYS_ASK,
656         "WebDatabaseUsage - wrong value");
657     RUNNER_ASSERT_MSG(
658         dao.getFileSystemUsage() == WrtDB::SETTINGS_TYPE_OFF,
659         "FileSystemUsage - wrong value");
660
661     dao.setFileSystemUsage(WrtDB::SETTINGS_TYPE_ON);
662     RUNNER_ASSERT_MSG(
663         dao.getSecurityPopupUsage() == WrtDB::SETTINGS_TYPE_OFF,
664         "SecurityPopupUsage - wrong value");
665     RUNNER_ASSERT_MSG(
666         dao.getGeolocationUsage() == WrtDB::SETTINGS_TYPE_ALWAYS_ASK,
667         "GeolocationUsage - wrong value");
668     RUNNER_ASSERT_MSG(
669         dao.getWebNotificationUsage() == WrtDB::SETTINGS_TYPE_OFF,
670         "WebNotificationUsage - wrong value");
671     RUNNER_ASSERT_MSG(
672         dao.getWebDatabaseUsage() == WrtDB::SETTINGS_TYPE_ALWAYS_ASK,
673         "WebDatabaseUsage - wrong value");
674     RUNNER_ASSERT_MSG(
675         dao.getFileSystemUsage() == WrtDB::SETTINGS_TYPE_ON,
676         "FileSystemUsage - wrong value");
677 }
678
679 /*
680  * Name: widget_dao_test_register_widget_win_modes
681  * Description: Tests registeration of window modes
682  * Expected: all modes should be returned from dao
683  */
684 RUNNER_TEST(widget_dao_test_register_widget_win_modes)
685 {
686     WacSecurityMock sec;
687     std::set<DPL::String> modes;
688     modes.insert(DPL::FromUTF8String("full"));
689     modes.insert(DPL::FromUTF8String("window"));
690
691     WidgetRegisterInfo regInfo;
692
693     FOREACH(it, modes)
694     regInfo.configInfo.windowModes.insert(*it);
695
696     TizenAppId tizenAppId = REGISTER_WIDGET(regInfo, sec);
697
698     WidgetDAO dao(tizenAppId);
699     std::list<DPL::String> wins = dao.getWindowModes();
700     RUNNER_ASSERT_MSG(modes.size() == wins.size(),
701                       "wrong number of window modes");
702     FOREACH(it, wins)
703     RUNNER_ASSERT(modes.find(*it) != modes.end());
704 }
705
706 /*
707  * Name: widget_dao_test_register_widget_warp_info
708  * Description: Tests registeration of access info iris
709  * Expected: all access info iris should be returned from dao
710  */
711 RUNNER_TEST(widget_dao_test_register_widget_warp_info)
712 {
713     WacSecurityMock sec;
714     ConfigParserData::AccessInfoSet orig;
715     orig.insert(ConfigParserData::AccessInfo(DPL::FromUTF8String("iri1"),
716                                              true));
717     orig.insert(ConfigParserData::AccessInfo(DPL::FromUTF8String("iri2"),
718                                              false));
719     orig.insert(ConfigParserData::AccessInfo(DPL::FromUTF8String("iri3"),
720                                              true));
721
722     WidgetRegisterInfo regInfo;
723     FOREACH(it, orig)
724     regInfo.configInfo.accessInfoSet.insert(*it);
725
726     TizenAppId tizenAppId = REGISTER_WIDGET(regInfo, sec);
727
728     WidgetDAO dao(tizenAppId);
729
730     WidgetAccessInfoList out;
731     dao.getWidgetAccessInfo(out);
732     RUNNER_ASSERT_MSG(out.size() == orig.size(),
733                       "wrong number of access info elem");
734     FOREACH(it, out){
735         ConfigParserData::AccessInfo tmp(it->strIRI, it->bSubDomains);
736         RUNNER_ASSERT(orig.find(tmp) != orig.end());
737     }
738 }
739
740 /*
741  * Name: widget_dao_test_register_widget_certificates
742  * Description: Tests registeration of widget certificates
743  * Expected: all certificates should be returned from dao
744  * and should contain inserted data
745  */
746 RUNNER_TEST(widget_dao_test_register_widget_certificates)
747 {
748     WacSecurityMock sec;
749     WidgetRegisterInfo regInfo;
750
751     WidgetCertificateData cert;
752     cert.owner = WidgetCertificateData::AUTHOR;
753     cert.type = WidgetCertificateData::ROOT;
754     cert.chainId = 1;
755     cert.strMD5Fingerprint = "md5";
756     cert.strSHA1Fingerprint = "sha1";
757     cert.strCommonName = DPL::FromUTF8String("common name");
758
759     WidgetCertificateDataList& certListRef = sec.getCertificateListRef();
760     certListRef.push_back(cert);
761
762     // register widget
763     TizenAppId tizenAppId = REGISTER_WIDGET(regInfo, sec);
764
765     WidgetDAO dao(tizenAppId);
766
767     // certificates
768     WidgetCertificateDataList recList = dao.getCertificateDataList();
769     RUNNER_ASSERT(recList.size() == certListRef.size());
770
771     auto recListIt = recList.begin();
772     auto certListIt = certListRef.begin();
773     for (; recListIt != recList.end() && certListIt != certListRef.end();
774          ++recListIt, ++certListIt)
775     {
776         RUNNER_ASSERT(recListIt->chainId == certListIt->chainId);
777         RUNNER_ASSERT(recListIt->owner == certListIt->owner);
778         RUNNER_ASSERT(recListIt->strCommonName == certListIt->strCommonName);
779         RUNNER_ASSERT(recListIt->strMD5Fingerprint ==
780                       certListIt->strMD5Fingerprint);
781         RUNNER_ASSERT(recListIt->strSHA1Fingerprint ==
782                       certListIt->strSHA1Fingerprint);
783         RUNNER_ASSERT(recListIt->type == certListIt->type);
784     }
785
786     // fingerprints
787     RUNNER_ASSERT(dao.getKeyFingerprints(WidgetCertificateData::DISTRIBUTOR,
788                                          WidgetCertificateData::ENDENTITY).
789                       empty());
790     RUNNER_ASSERT(dao.getKeyFingerprints(WidgetCertificateData::AUTHOR,
791                                          WidgetCertificateData::ENDENTITY).
792                       empty());
793     RUNNER_ASSERT(dao.getKeyFingerprints(WidgetCertificateData::DISTRIBUTOR,
794                                          WidgetCertificateData::ROOT).empty());
795
796     FingerPrintList fingerprints = dao.getKeyFingerprints(
797             WidgetCertificateData::AUTHOR,
798             WidgetCertificateData::ROOT);
799
800     RUNNER_ASSERT(fingerprints.size() == certListRef.size() * 2);
801     FOREACH(it, certListRef)
802     {
803         auto md5 = std::find(fingerprints.begin(),
804                              fingerprints.end(),
805                              it->strMD5Fingerprint);
806         RUNNER_ASSERT(md5 != fingerprints.end());
807
808         auto sha = std::find(fingerprints.begin(),
809                              fingerprints.end(),
810                              it->strSHA1Fingerprint);
811         RUNNER_ASSERT(sha != fingerprints.end());
812     }
813
814     // common names
815     RUNNER_ASSERT(dao.getKeyCommonNameList(WidgetCertificateData::DISTRIBUTOR,
816                                            WidgetCertificateData::ENDENTITY).
817                       empty());
818     RUNNER_ASSERT(dao.getKeyCommonNameList(WidgetCertificateData::AUTHOR,
819                                            WidgetCertificateData::ENDENTITY).
820                       empty());
821     RUNNER_ASSERT(dao.getKeyCommonNameList(WidgetCertificateData::DISTRIBUTOR,
822                                            WidgetCertificateData::ROOT).empty());
823
824     FingerPrintList commonNames = dao.getKeyCommonNameList(
825             WidgetCertificateData::AUTHOR,
826             WidgetCertificateData::ROOT);
827
828     RUNNER_ASSERT(commonNames.size() == certListRef.size());
829     FOREACH(it, certListRef)
830     {
831         auto cn = std::find(commonNames.begin(),
832                             commonNames.end(),
833                             DPL::ToUTF8String(it->strCommonName));
834         RUNNER_ASSERT(cn != commonNames.end());
835     }
836 }
837
838 /*
839  * Name: widget_dao_test_is_widget_installed
840  * Description: Tests checking if widgets are installed
841  * Expected: installed widgets should be stated as installed
842  */
843 RUNNER_TEST(widget_dao_test_is_widget_installed)
844 {
845     RUNNER_ASSERT(WidgetDAO::isWidgetInstalled(L"tizenid201"));
846
847     WacSecurityMock sec;
848     WidgetRegisterInfo regInfo;
849
850     TizenAppId tizenAppId = REGISTER_WIDGET(regInfo, sec);
851
852     RUNNER_ASSERT(WidgetDAO::isWidgetInstalled(tizenAppId));
853 }
854
855 /*
856  * Name: widget_dao_test_unregister_widget
857  * Description: Tests unregistering widgets
858  * Expected: widget register informations should be successfully removed
859  */
860 RUNNER_TEST(widget_dao_test_unregister_widget)
861 {
862     WacSecurityMock sec;
863     WidgetHandleList handles = WidgetDAO::getHandleList();
864
865     WidgetRegisterInfo regInfo;
866
867     TizenAppId tizenAppId = REGISTER_WIDGET(regInfo, sec);
868
869     WidgetDAO::unregisterWidget(tizenAppId);
870
871     RUNNER_ASSERT_MSG(handles.size() == WidgetDAO::getHandleList().size(),
872                       "Widget unregister failed");
873 }
874
875 /*
876  * Name: widget_dao_test_register_or_update_widget
877  * Description: Tests reregistering widgets
878  * Expected: widget should be successfully replaced
879  */
880 RUNNER_TEST(widget_dao_test_register_or_update_widget)
881 {
882     WacSecurityMock sec;
883
884     WidgetRegisterInfo regInfo;
885     regInfo.configInfo.version = L"1.0";
886     regInfo.configInfo.authorName = L"AAA";
887
888     WidgetRegisterInfo regInfo2;
889     regInfo2.configInfo.version = L"1.1";
890     regInfo2.configInfo.authorName = L"BBB";
891
892     WrtDB::TizenAppId tizenAppId(L"abcdefghij");
893
894     //first installation
895     WidgetDAO::registerWidget(tizenAppId, regInfo, sec);
896     RUNNER_ASSERT_MSG(WidgetDAO::isWidgetInstalled(
897                           tizenAppId), "Widget is not registered");
898
899     //success full update
900     WidgetDAO::registerOrUpdateWidget(tizenAppId, regInfo2, sec);
901     RUNNER_ASSERT_MSG(WidgetDAO::isWidgetInstalled(
902                           tizenAppId), "Widget is not reregistered");
903     WidgetDAO dao(tizenAppId);
904     RUNNER_ASSERT_MSG(
905         *dao.getVersion() == L"1.1", "Data widget was not updated");
906     RUNNER_ASSERT_MSG(
907         *dao.getAuthorName() == L"BBB", "Data widget was not updated");
908
909     WidgetDAO::unregisterWidget(tizenAppId);
910 }
911
912 /*
913  * Name: widget_dao_test_get_widget_tizenAppId_list
914  * Description: Tests getTizenAppidList API for backendlib
915  * Expected: For all position in database should be returned one item in list
916  */
917 RUNNER_TEST(widget_dao_test_get_widget_tizenAppId_list)
918 {
919     TizenAppIdList tizenAppIds = WidgetDAO::getTizenAppidList();
920     RUNNER_ASSERT(tizenAppIds.size() >= 3);
921 }
922
923 /*
924  * Name: widget_dao_test_get_widget_list
925  * Description: Tests getTizenAppidList API for backendlib
926  * Expected: For all position in database should be returned one item in list
927  * Those item should contain valid tizenAppId
928  */
929 RUNNER_TEST(widget_dao_test_get_widget_list)
930 {
931     WidgetDAOReadOnlyList list = WidgetDAOReadOnly::getWidgetList();
932     RUNNER_ASSERT(list.size() >= 3);
933     RUNNER_ASSERT_MSG(!!list.front(), "widget dao exists");
934     WidgetDAOReadOnlyPtr dao = list.front();
935 }
936
937 /*
938  * Name: widget_dao_test_get_widget_attributes
939  * Description: Tests returning basic widget attributes by dao
940  * Expected: Attributes should match values inserted into database
941  */
942 RUNNER_TEST(widget_dao_test_get_widget_attributes)
943 {
944     {
945         TizenAppId tizenAppId = L"tizenid201";
946         WidgetDAO dao(tizenAppId);
947
948         RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(dao.getGUID(), "w_id_2000");
949         RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(dao.getVersion(), "1.0.0");
950         RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(dao.getAuthorName(), "a_name_2000");
951         RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(dao.getAuthorEmail(),
952                                            "a_email_2000");
953         RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(dao.getAuthorHref(), "a_href_2000");
954         RUNNER_ASSERT_WHAT_EQUALS(dao.getBaseFolder(), "basef_2000/");
955         RUNNER_ASSERT(dao.getWebkitPluginsRequired() == true);
956         RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(dao.getMinimumWacVersion(), "1.0");
957     }
958 }
959
960 /*
961  * Name: widget_dao_test_localization
962  * Description: Tests inserting and returning localization info
963  * Expected: Values inserted into database should match values received from
964  * database
965  */
966 RUNNER_TEST(widget_dao_test_localization)
967 {
968     WacSecurityMock sec;
969
970     // icon
971     WidgetRegisterInfo regInfo;
972     ConfigParserData::Icon icon(L"icon1");
973     icon.width = 10;
974     icon.height = 10;
975     LocaleSet locs;
976     locs.insert(DPL::FromUTF8String("en"));
977     locs.insert(DPL::FromUTF8String("pl"));
978     WidgetRegisterInfo::LocalizedIcon locIcon(icon, locs);
979     regInfo.localizationData.icons.push_back(locIcon);
980
981     //start file
982     WidgetRegisterInfo::StartFileProperties prop_en;
983     prop_en.encoding = DPL::FromUTF8String("encoding_en");
984     prop_en.type = DPL::FromUTF8String("type_en");
985
986     WidgetRegisterInfo::StartFileProperties prop_pl;
987     prop_pl.encoding = DPL::FromUTF8String("encoding_pl");
988     prop_pl.type = DPL::FromUTF8String("type_pl");
989
990     WidgetRegisterInfo::LocalizedStartFile file;
991     file.path = DPL::FromUTF8String("path");
992     file.propertiesForLocales.insert(
993         std::make_pair(DPL::FromUTF8String("en"), prop_en));
994     file.propertiesForLocales.insert(
995         std::make_pair(DPL::FromUTF8String("pl"), prop_pl));
996     regInfo.localizationData.startFiles.push_back(file);
997
998     // register widget
999     TizenAppId tizenAppId = REGISTER_WIDGET(regInfo, sec);
1000
1001     WidgetDAO dao(tizenAppId);
1002
1003     // check localized icons
1004     WidgetDAO::WidgetLocalizedIconList locList = dao.getLocalizedIconList();
1005     RUNNER_ASSERT(locList.size() == locs.size());
1006
1007     // non-localized icon
1008     WidgetDAO::WidgetIconList list = dao.getIconList();
1009     int iconId = list.front().iconId;
1010
1011     // compare every icon with the origin
1012     auto locsIt = locs.begin();
1013     auto iconIt = locList.begin();
1014     for (; locsIt != locs.end() && iconIt != locList.end(); ++locsIt,
1015          ++iconIt)
1016     {
1017         RUNNER_ASSERT(iconIt->appId == dao.getHandle());
1018         RUNNER_ASSERT(iconIt->iconId == iconId);
1019         RUNNER_ASSERT(iconIt->widgetLocale == *locsIt);
1020     }
1021
1022     // localized start file list
1023     WidgetDAO::LocalizedStartFileList fList = dao.getLocalizedStartFileList();
1024     RUNNER_ASSERT(fList.size() == file.propertiesForLocales.size());
1025
1026     int startFileId = dao.getStartFileList().front().startFileId;
1027
1028     FOREACH(it, fList)
1029     {
1030         RUNNER_ASSERT(it->appId == dao.getHandle());
1031         auto propIt = file.propertiesForLocales.find(it->widgetLocale);
1032         RUNNER_ASSERT(propIt != file.propertiesForLocales.end());
1033         RUNNER_ASSERT(it->encoding == propIt->second.encoding);
1034         RUNNER_ASSERT(it->type == propIt->second.type);
1035         RUNNER_ASSERT(it->startFileId == startFileId);
1036     }
1037 }
1038
1039 /*
1040  * Name: widget_dao_test_wac_security
1041  * Description: Tests inserting and returning wac security information
1042  * Expected: Values inserted into database should match values received from
1043  * database
1044  */
1045 RUNNER_TEST(widget_dao_test_wac_security)
1046 {
1047     WacSecurityMock sec;
1048     WidgetRegisterInfo regInfo;
1049     {
1050         // register widget
1051         TizenAppId tizenAppId = REGISTER_WIDGET(regInfo, sec);
1052         WidgetDAO dao(tizenAppId);
1053
1054         RUNNER_ASSERT(!dao.isDistributorSigned());
1055         RUNNER_ASSERT(!dao.isRecognized());
1056         RUNNER_ASSERT(!dao.isWacSigned());
1057     }
1058     sec.setDistributorSigned(true);
1059     sec.setRecognized(true);
1060     sec.setWacSigned(true);
1061     {
1062         // register widget
1063         TizenAppId tizenAppId = REGISTER_WIDGET(regInfo, sec);
1064         WidgetDAO dao(tizenAppId);
1065
1066         RUNNER_ASSERT(dao.isDistributorSigned());
1067         RUNNER_ASSERT(dao.isRecognized());
1068         RUNNER_ASSERT(dao.isWacSigned());
1069     }
1070 }
1071
1072 /*
1073  * Name: widget_dao_test_register_scp
1074  * Description: Tests inserting and returning scp policy information
1075  * Expected: Value inserted into database should match values received from
1076  * database
1077  */
1078 RUNNER_TEST(widget_dao_test_register_scp)
1079 {
1080     WacSecurityMock sec;
1081     WidgetRegisterInfo regInfo;
1082     DPL::OptionalString policy = DPL::FromUTF8String("Some awesome csp policy");
1083     regInfo.configInfo.cspPolicy = policy;
1084     {
1085         // register widget
1086         TizenAppId tizenAppId = REGISTER_WIDGET(regInfo, sec);
1087         WidgetDAO dao(tizenAppId);
1088
1089         RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(
1090             dao.getCspPolicy(), DPL::ToUTF8String(*policy));
1091     }
1092 }
1093
1094 /*
1095  * Name: widget_dao_test_register_csp_empty
1096  * Description: Tests inserting and returning empty csp policy
1097  * Expected: Value inserted into database should match values received from
1098  * database
1099  */
1100 RUNNER_TEST(widget_dao_test_register_csp_empty)
1101 {
1102     WacSecurityMock sec;
1103     WidgetRegisterInfo regInfo;
1104     {
1105         // register widget
1106         TizenAppId tizenAppId = REGISTER_WIDGET(regInfo, sec);
1107         WidgetDAO dao(tizenAppId);
1108
1109         RUNNER_ASSERT_MSG(dao.getCspPolicy().IsNull(), "Policy is not null");
1110     }
1111 }
1112 #undef RUNNER_ASSERT_WHAT_EQUALS