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