Initialize Tizen 2.3
[framework/web/wrt-installer.git] / tests / general / TaskConfigurationTests.cpp
1 /*
2  * Copyright (c) 2014 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    TaskConfigurationTests.cpp
18  * @author  Dominik Duda (d.duda@samsung.com)
19  * @version 1.0
20  * @brief   Tests functions from
21  * wrt-installer/src/jobs/widget_install/task_configuration.cpp
22  */
23 #include <string>
24 #include <installer_log.h>
25 #include <InstallerWrapper.h>
26 #include <dpl/utils/wrt_utility.h>
27 #include <dpl/test/test_runner.h>
28 #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
29 #include <widget_install/widget_install_errors.h>
30 #include <widget_install/widget_installer_struct.h>
31 #include <widget_install/task_configuration.h>
32 #include <widget_install/job_widget_install.h>
33 #include <installer_callbacks_translate.h>
34 #include <pkg-manager/pkgmgr_signal_dummy.h>
35 #include <memory>
36
37 using namespace Jobs;
38 using namespace WidgetInstall;
39
40 RUNNER_TEST_GROUP_INIT(TaskConfiguration)
41
42 namespace{
43 const std::string wgtTmpPath = "/tmp/J7ZwBudste";
44 const std::string wgtPath = InstallerWrapper::miscWidgetsStuff +
45         "widgets/widgetUpdateVer100Signed.wgt";
46 const std::string tarCMD = "tar -xf " + InstallerWrapper::miscWidgetsStuff +
47                 "widgets/widgetInDir.tar -C /tmp";
48
49 std::string tizenId;
50
51 void staticWrtInitPreloadStatusCallback(std::string tizenId, WrtErrStatus status,
52                                            void* userdata)
53 {
54     return;
55 }
56 }
57
58 /*
59 Name: task_configuration_test_01
60 Description: Installs widget which is needed for further tests.
61 This installation will test standard use of the TaskConfiguration class.
62 Expected: The widget should be successfully installed.
63 */
64 RUNNER_TEST(task_configuration_test_01)
65 {
66     //Install the widget to get a tizenID
67     InstallerWrapper::install(wgtPath, tizenId);
68
69     //Uninstall the widget in order to be sure that a next installation
70     //will be first.
71     RUNNER_ASSERT_MSG(InstallerWrapper::uninstall(tizenId),
72             "Failed to uninstall a widget");
73
74     //That will be the first installation of the widget.
75     RUNNER_ASSERT_MSG(
76             InstallerWrapper::install(wgtPath, tizenId) == InstallerWrapper::Success,
77             "Failed to install a widget");
78 }
79
80 /*
81 Name: task_configuration_test_02
82 Description: Tests recognizing an update installation.
83 Expected: All task configration steps should be completed without errors.
84 */
85 RUNNER_TEST(task_configuration_test_02)
86 {
87     const WidgetInstallationStruct installerStruct(
88                     InstallerCallbacksTranslate::installFinishedCallback,
89                     InstallerCallbacksTranslate::installProgressCallback,
90                     new InstallerCallbacksTranslate::StatusCallbackStruct(
91                             NULL, &staticWrtInitPreloadStatusCallback, NULL),
92                     InstallMode(),
93                     std::make_shared<PackageManager::PkgmgrSignalDummy>());
94
95     InstallerContext i_context;
96
97     i_context.mode = InstallMode();
98     i_context.requestedPath = wgtPath;
99     i_context.job = new Jobs::WidgetInstall::JobWidgetInstall(wgtPath, "",
100                     installerStruct);
101
102     TaskConfiguration taskConf(i_context);
103     size_t stepsCnt = taskConf.GetStepCount();
104
105     unsigned int i = 0;
106     bool result = true;
107
108     while(i < stepsCnt && result)
109     {
110         i++;
111         result = taskConf.NextStep();
112     }
113
114     RUNNER_ASSERT(i == stepsCnt);
115 }
116
117 /*
118 Name: task_configuration_test_03
119 Description: Tests widget installation with incorrect config.xml file.
120 Expected: Task configuration process should throw an exception when parsing
121 configuration file.
122 */
123 RUNNER_TEST(task_configuration_test_03)
124 {
125     const WidgetInstallationStruct installerStruct(
126                     InstallerCallbacksTranslate::installFinishedCallback,
127                     InstallerCallbacksTranslate::installProgressCallback,
128                     new InstallerCallbacksTranslate::StatusCallbackStruct(
129                             NULL, &staticWrtInitPreloadStatusCallback, NULL),
130                     InstallMode(),
131                     std::make_shared<PackageManager::PkgmgrSignalDummy>());
132
133     InstallerContext i_context;
134
135     i_context.mode = InstallMode();
136     i_context.requestedPath = InstallerWrapper::miscWidgetsStuff + "widgets/widgetFakeConfig.wgt";
137
138     i_context.job =
139             new Jobs::WidgetInstall::JobWidgetInstall(wgtPath, "",
140                     installerStruct);
141
142     TaskConfiguration taskConf(i_context);
143     size_t stepsCnt = taskConf.GetStepCount();
144
145     unsigned int i = 0;
146     bool result = true;
147
148     Try{
149         while(i < stepsCnt && result)
150         {
151             i++;
152             result = taskConf.NextStep();
153         }
154     }
155     Catch(WidgetInstall::Exceptions::WidgetConfigFileInvalid){
156         RUNNER_ASSERT(i == 4);
157         return;
158     }
159
160     RUNNER_ASSERT_MSG(false,
161             "An Exception should be thrown if config.xml file is incorrect.");
162 }
163
164 /*
165 Name: task_configuration_test_04
166 Description: Tests task configuration for widget installation directly from
167 a directory.
168 Expected: Widget should be successfully installed.
169 */
170 RUNNER_TEST(task_configuration_test_04)
171 {
172     int ret = system(tarCMD.c_str());
173
174     RUNNER_ASSERT_MSG(!WIFEXITED(ret) || !WIFSIGNALED(ret),
175             "Cannot untar a widget to check direct installation from the directory!");
176
177     const WidgetInstallationStruct installerStruct(
178                     InstallerCallbacksTranslate::installFinishedCallback,
179                     InstallerCallbacksTranslate::installProgressCallback,
180                     new InstallerCallbacksTranslate::StatusCallbackStruct(
181                             NULL, &staticWrtInitPreloadStatusCallback, NULL),
182                     InstallMode(),
183                     std::make_shared<PackageManager::PkgmgrSignalDummy>());
184
185     InstallerContext i_context;
186
187     i_context.mode = InstallMode();
188     i_context.mode.extension = InstallMode::ExtensionType::DIR;
189     i_context.requestedPath = wgtTmpPath;
190
191     i_context.job =
192             new Jobs::WidgetInstall::JobWidgetInstall(wgtPath, "",
193                     installerStruct);
194
195     TaskConfiguration taskConf(i_context);
196     size_t stepsCnt = taskConf.GetStepCount();
197
198     unsigned int i = 0;
199     bool result = true;
200
201     while(i < stepsCnt && result)
202     {
203         i++;
204         result = taskConf.NextStep();
205     }
206
207     RUNNER_ASSERT(i == stepsCnt);
208 }
209
210 /*
211 Name: task_configuration_test_05
212 Description: Tests if an exception will ocure when there is no config.xml file
213 in the widget directory.
214 Expected: An exception should be thrown.
215 */
216 RUNNER_TEST(task_configuration_test_05)
217 {
218     int ret = system(tarCMD.c_str());
219
220     RUNNER_ASSERT_MSG(!WIFEXITED(ret) || !WIFSIGNALED(ret),
221             "Cannot untar widget to check direct installation from directory!");
222
223     const WidgetInstallationStruct installerStruct(
224                     InstallerCallbacksTranslate::installFinishedCallback,
225                     InstallerCallbacksTranslate::installProgressCallback,
226                     new InstallerCallbacksTranslate::StatusCallbackStruct(
227                             NULL, &staticWrtInitPreloadStatusCallback, NULL),
228                     InstallMode(),
229                     std::make_shared<PackageManager::PkgmgrSignalDummy>());
230
231     InstallerContext i_context;
232
233     i_context.mode = InstallMode();
234     i_context.mode.extension = InstallMode::ExtensionType::DIR;
235     i_context.requestedPath = wgtTmpPath + "/TestWgt.wgt";
236
237     i_context.job =
238             new Jobs::WidgetInstall::JobWidgetInstall(wgtPath, "",
239                     installerStruct);
240
241     TaskConfiguration taskConf(i_context);
242     size_t stepsCnt = taskConf.GetStepCount();
243
244     unsigned int i = 0;
245     bool result = true;
246
247     Try{
248         while(i < stepsCnt && result)
249         {
250             i++;
251
252             if (i == 3)
253             {
254                 //Remove config file
255                 RUNNER_ASSERT(WrtUtilRemove(wgtTmpPath + "/config.xml"));
256             }
257
258             result = taskConf.NextStep();
259         }
260     }
261     Catch(WrtDB::WidgetDAOReadOnly::Exception::WidgetNotExist){
262         RUNNER_ASSERT(i == 3);
263         return;
264     }
265
266     RUNNER_ASSERT_MSG(false,
267             "An Exception should be thrown after deletion of config.xml file.");
268 }
269
270 /*
271 Name: task_configuration_test_06
272 Description: Tests if missing config file will be detected during parsing step.
273 Expected: An exception should be thrown if there is no config file.
274 */
275 RUNNER_TEST(task_configuration_test_06)
276 {
277     int ret = system(tarCMD.c_str());
278
279     RUNNER_ASSERT_MSG(!WIFEXITED(ret) || !WIFSIGNALED(ret),
280             "Cannot untar widget to check direct installation from directory!");
281
282     const WidgetInstallationStruct installerStruct(
283                     InstallerCallbacksTranslate::installFinishedCallback,
284                     InstallerCallbacksTranslate::installProgressCallback,
285                     new InstallerCallbacksTranslate::StatusCallbackStruct(
286                             NULL, &staticWrtInitPreloadStatusCallback, NULL),
287                     InstallMode(),
288                     std::make_shared<PackageManager::PkgmgrSignalDummy>());
289
290     InstallerContext i_context;
291
292     i_context.mode = InstallMode();
293     i_context.mode.extension = InstallMode::ExtensionType::DIR;
294     i_context.requestedPath = wgtTmpPath;
295
296     i_context.job =
297             new Jobs::WidgetInstall::JobWidgetInstall(wgtPath, "",
298                     installerStruct);
299
300     TaskConfiguration taskConf(i_context);
301     size_t stepsCnt = taskConf.GetStepCount();
302
303     unsigned int i = 0;
304     bool result = true;
305
306     Try{
307         while(i < stepsCnt && result)
308         {
309             i++;
310
311             if (i == 4)
312             {
313                 //Remove config file
314                 RUNNER_ASSERT(WrtUtilRemove(wgtTmpPath + "/config.xml"));
315             }
316
317             result = taskConf.NextStep();
318         }
319     }
320     Catch(WidgetInstall::Exceptions::MissingConfig){
321         RUNNER_ASSERT(i == 4);
322         return;
323     }
324
325     RUNNER_ASSERT_MSG(false,
326             "An Exception should be thrown in parsing step if there is no "
327             "config.xml file in the directory.");
328 }
329
330 /*
331 Name: task_configuration_test_07
332 Description: Tests reinstallation of a widget from the directory.
333 Expected: A widget should be successfully installed.
334 */
335 RUNNER_TEST(task_configuration_test_07)
336 {
337     int ret = system(tarCMD.c_str());
338
339     RUNNER_ASSERT_MSG(!WIFEXITED(ret) || !WIFSIGNALED(ret),
340             "Cannot untar widget to check direct installation from directory!");
341
342     //This widget is needed to be installed to find the tizen PkgId in the database
343     //during reinstallation step.
344     RUNNER_ASSERT_MSG(
345             InstallerWrapper::install(wgtTmpPath + "/TestWgt.wgt", tizenId) == InstallerWrapper::Success,
346             "Failed to install a widget");
347
348     const WidgetInstallationStruct installerStruct(
349                     InstallerCallbacksTranslate::installFinishedCallback,
350                     InstallerCallbacksTranslate::installProgressCallback,
351                     new InstallerCallbacksTranslate::StatusCallbackStruct(
352                             NULL, &staticWrtInitPreloadStatusCallback, NULL),
353                     InstallMode(),
354                     std::make_shared<PackageManager::PkgmgrSignalDummy>());
355
356     InstallerContext i_context;
357
358     i_context.mode = InstallMode();
359     i_context.mode.command = InstallMode::Command::REINSTALL;
360     i_context.mode.extension = InstallMode::ExtensionType::DIR;
361     i_context.requestedPath = wgtTmpPath;
362
363     i_context.job =
364             new Jobs::WidgetInstall::JobWidgetInstall(wgtPath, "",
365                     installerStruct);
366
367     TaskConfiguration taskConf(i_context);
368     size_t stepsCnt = taskConf.GetStepCount();
369
370     unsigned int i = 0;
371     bool result = true;
372
373     while(i < stepsCnt && result)
374     {
375         i++;
376         result = taskConf.NextStep();
377     }
378
379     RUNNER_ASSERT(i == stepsCnt);
380 }
381
382 /*
383 Name: task_configuration_test_08
384 Description: Tests recovery installation of the widget from the directory.
385 Expected: A widget should be successfully installed.
386 */
387 RUNNER_TEST(task_configuration_test_08)
388 {
389     int ret = system(tarCMD.c_str());
390
391     RUNNER_ASSERT_MSG(!WIFEXITED(ret) || !WIFSIGNALED(ret),
392             "Cannot untar widget to check direct installation from directory!");
393
394     //This widget is needed to be installed to find the tizen PkgId in the database
395     //during reinstallation step.
396     RUNNER_ASSERT_MSG(
397             InstallerWrapper::install(wgtTmpPath + "/TestWgt.wgt", tizenId) == InstallerWrapper::Success,
398             "Failed to install a widget");
399
400     const WidgetInstallationStruct installerStruct(
401                     InstallerCallbacksTranslate::installFinishedCallback,
402                     InstallerCallbacksTranslate::installProgressCallback,
403                     new InstallerCallbacksTranslate::StatusCallbackStruct(
404                             NULL, &staticWrtInitPreloadStatusCallback, NULL),
405                     InstallMode(),
406                     std::make_shared<PackageManager::PkgmgrSignalDummy>());
407
408     InstallerContext i_context;
409
410     i_context.mode = InstallMode();
411     i_context.mode.command = InstallMode::Command::RECOVERY;
412     i_context.mode.extension = InstallMode::ExtensionType::DIR;
413     i_context.requestedPath = wgtTmpPath;
414
415     i_context.job =
416             new Jobs::WidgetInstall::JobWidgetInstall(wgtPath, "",
417                     installerStruct);
418
419     TaskConfiguration taskConf(i_context);
420     size_t stepsCnt = taskConf.GetStepCount();
421
422     unsigned int i = 0;
423     bool result = true;
424
425     while(i < stepsCnt && result)
426     {
427         i++;
428         result = taskConf.NextStep();
429     }
430
431     RUNNER_ASSERT(i == stepsCnt);
432 }
433
434 /*
435 Name: task_configuration_test_09
436 Description: Tests if a tizenAppID and tizenPkgID will be generated if were not
437 set earlier.
438 Expected: IDs should be properly generated.
439 */
440 RUNNER_TEST(task_configuration_test_09)
441 {
442     const WidgetInstallationStruct installerStruct(
443                     InstallerCallbacksTranslate::installFinishedCallback,
444                     InstallerCallbacksTranslate::installProgressCallback,
445                     new InstallerCallbacksTranslate::StatusCallbackStruct(
446                             NULL, &staticWrtInitPreloadStatusCallback, NULL),
447                     InstallMode(),
448                     std::make_shared<PackageManager::PkgmgrSignalDummy>());
449
450     InstallerContext i_context;
451
452     i_context.mode = InstallMode();
453     i_context.requestedPath = wgtPath;
454
455     i_context.job =
456             new Jobs::WidgetInstall::JobWidgetInstall(wgtPath, "",
457                     installerStruct);
458
459     TaskConfiguration taskConf(i_context);
460     size_t stepsCnt = taskConf.GetStepCount();
461
462     unsigned int i = 0;
463     bool result = true;
464
465     while(i < stepsCnt && result)
466     {
467         i++;
468
469         if (i == 5){
470             i_context.widgetConfig.tzAppid = L"";
471             i_context.widgetConfig.tzPkgid = L"";
472             i_context.widgetConfig.configInfo.tizenAppId = boost::none;
473             i_context.widgetConfig.configInfo.tizenPkgId = boost::none;
474         }
475
476         result = taskConf.NextStep();
477     }
478
479     RUNNER_ASSERT(i == stepsCnt &&
480             i_context.widgetConfig.tzAppid != L"" &&
481             i_context.widgetConfig.tzPkgid != L"");
482 }
483
484 /*
485 Name: task_configuration_test_10
486 Description: Tests if a tizenPkgID will be generated if was not set earlier.
487 Expected: ID should be properly generated.
488 */
489 RUNNER_TEST(task_configuration_test_10)
490 {
491     const WidgetInstallationStruct installerStruct(
492                     InstallerCallbacksTranslate::installFinishedCallback,
493                     InstallerCallbacksTranslate::installProgressCallback,
494                     new InstallerCallbacksTranslate::StatusCallbackStruct(
495                             NULL, &staticWrtInitPreloadStatusCallback, NULL),
496                     InstallMode(),
497                     std::make_shared<PackageManager::PkgmgrSignalDummy>());
498
499     InstallerContext i_context;
500
501     i_context.mode = InstallMode();
502     i_context.requestedPath = wgtPath;
503
504     i_context.job =
505             new Jobs::WidgetInstall::JobWidgetInstall(wgtPath, "",
506                     installerStruct);
507
508     TaskConfiguration taskConf(i_context);
509     size_t stepsCnt = taskConf.GetStepCount();
510
511     unsigned int i = 0;
512     bool result = true;
513
514     while(i < stepsCnt && result)
515     {
516         i++;
517
518         if (i == 5){
519             i_context.widgetConfig.tzPkgid = L"";
520             i_context.widgetConfig.configInfo.tizenPkgId = boost::none;
521         }
522
523         result = taskConf.NextStep();
524     }
525
526     RUNNER_ASSERT(i == stepsCnt &&
527             i_context.widgetConfig.tzPkgid != L"");
528 }
529
530 /*
531 Name: task_configuration_test_11
532 Description: Tests if a tizenAppId and tizenPkgID will be generated if
533 tizenApp ID is too short and tizenPkgID is not set.
534 Expected: IDs should be properly generated. An exception should be thrown
535 in step 9 beacuse this widget is already installed.
536 */
537 RUNNER_TEST(task_configuration_test_11)
538 {
539     const WidgetInstallationStruct installerStruct(
540                     InstallerCallbacksTranslate::installFinishedCallback,
541                     InstallerCallbacksTranslate::installProgressCallback,
542                     new InstallerCallbacksTranslate::StatusCallbackStruct(
543                             NULL, &staticWrtInitPreloadStatusCallback, NULL),
544                     InstallMode(),
545                     std::make_shared<PackageManager::PkgmgrSignalDummy>());
546
547     InstallerContext i_context;
548
549     i_context.mode = InstallMode();
550     i_context.requestedPath = wgtPath;
551
552     i_context.job =
553             new Jobs::WidgetInstall::JobWidgetInstall(wgtPath, "",
554                     installerStruct);
555
556     TaskConfiguration taskConf(i_context);
557     size_t stepsCnt = taskConf.GetStepCount();
558
559     unsigned int i = 0;
560     bool result = true;
561
562     Try{
563         while(i < stepsCnt && result)
564         {
565             i++;
566
567             if (i == 5){
568                 i_context.widgetConfig.tzPkgid = L"";
569                 i_context.widgetConfig.configInfo.tizenAppId = L"abcd";
570                 i_context.widgetConfig.configInfo.tizenPkgId = boost::none;
571             }
572
573             result = taskConf.NextStep();
574         }
575     }
576     Catch(Jobs::WidgetInstall::Exceptions::WidgetConfigFileInvalid){
577         RUNNER_ASSERT(i == 9 &&
578                 i_context.widgetConfig.tzPkgid != L"");
579         return;
580     }
581
582     RUNNER_ASSERT_MSG(false, "An exception should be thrown because this widget"
583             " is already installed!");
584 }
585
586 /*
587 Name: task_configuration_test_12
588 Description: Tests if a tizenAppId and tizenPkgID will be generated if
589 tizenApp ID has incorrect characters and tizenPkgID is not set.
590 Expected: IDs should be properly generated. An exception should be thrown
591 in step 9 beacuse this widget is already installed.
592 */
593 RUNNER_TEST(task_configuration_test_12)
594 {
595     const WidgetInstallationStruct installerStruct(
596                     InstallerCallbacksTranslate::installFinishedCallback,
597                     InstallerCallbacksTranslate::installProgressCallback,
598                     new InstallerCallbacksTranslate::StatusCallbackStruct(
599                             NULL, &staticWrtInitPreloadStatusCallback, NULL),
600                     InstallMode(),
601                     std::make_shared<PackageManager::PkgmgrSignalDummy>());
602
603     InstallerContext i_context;
604
605     i_context.mode = InstallMode();
606     i_context.requestedPath = wgtPath;
607
608     i_context.job =
609             new Jobs::WidgetInstall::JobWidgetInstall(wgtPath, "",
610                     installerStruct);
611
612     TaskConfiguration taskConf(i_context);
613     size_t stepsCnt = taskConf.GetStepCount();
614
615     unsigned int i = 0;
616     bool result = true;
617
618     Try{
619         while(i < stepsCnt && result)
620         {
621             i++;
622
623             if (i == 5){
624                 i_context.widgetConfig.tzPkgid = L"";
625                 i_context.widgetConfig.configInfo.tizenAppId = L"1234!@#$qw";
626                 i_context.widgetConfig.configInfo.tizenPkgId = boost::none;
627
628                 FOREACH(localizedData, i_context.widgetConfig.configInfo.localizedDataSet)
629                 {
630                     localizedData->second.name = L"1234!@#$qw";
631                 }
632             }
633
634             result = taskConf.NextStep();
635         }
636     }
637     Catch(Jobs::WidgetInstall::Exceptions::WidgetConfigFileInvalid){
638         RUNNER_ASSERT(i == 9 &&
639                 i_context.widgetConfig.tzPkgid != L"");
640         return;
641     }
642
643     RUNNER_ASSERT_MSG(false, "An exception should be thrown because this widget"
644             " is already installed!");
645 }