e87d90f558063839b0b6f08d4f1d4dd0da7694cf
[framework/web/wrt-installer.git] / tests / general / ParserRunnerTests.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    ParserRunnerTests.cpp
18  * @author  Adrian Szafranek (a.szafranek@samsung.com)
19  * @version 1.0
20  * @brief   Perform ParserRunner test's.
21  */
22
23 #include <parser_runner.h>
24 #include <dpl/scope_guard.h>
25 #include <dpl/test/test_runner.h>
26 #include <dpl/utils/path.h>
27
28 #include <installer_log.h>
29
30 #include <string>
31 #include <fstream>
32 #include <cstdio>
33 #include <memory>
34
35 RUNNER_TEST_GROUP_INIT(ParserRunner)
36
37 using namespace DPL::Utils;
38
39 namespace {
40 const std::string rootTest = "/tmp/";
41 const std::string schemaFile = "/usr/etc/wrt-installer/widgets.xsd";
42 const std::string configFile = "/usr/lib/wrt-plugins/tizen-content/config.xml";
43 const std::string installConfigFile = "/opt/share/widget/tests/installer/configs/InstallConfig.xml";
44 }
45
46 class StreamRedirector
47 {
48     public:
49     StreamRedirector(FILE* src, std::string dest)
50         : source(src), file_dest(dest)
51     {
52         fd = dup(fileno(source));
53         if ((fd < 0) || (freopen(file_dest.c_str(), "w", source) == NULL))
54         {
55             _D("Error catching stream.");
56         }
57     }
58     ~StreamRedirector()
59     {
60         if (TEMP_FAILURE_RETRY(fflush(source)) == 0)
61         {
62             if (dup2(fd, fileno(source)) >= 0)
63             {
64                 close(fd);
65                 clearerr(source);
66             }
67             else
68             {
69                 _D("Error returning stream.");
70             }
71         }
72         else
73         {
74             _D("Error returning stream.");
75         }
76     }
77
78     private:
79     FILE* source;
80     std::string file_dest;
81     int fd;
82 };
83
84
85 /*
86 Name: parserRunner01
87 Description: Tests validation and parsing functionality.
88 Test performed for proper files existing in system, wrong files or empty parameters.
89 */
90 RUNNER_TEST(parserRunner01)
91 {
92     ParserRunner parser;
93
94     std::unique_ptr<StreamRedirector> sd(new StreamRedirector(stderr, "/dev/null"));
95
96
97     if (Path(installConfigFile).Exists() && Path(schemaFile).Exists()) {
98         RUNNER_ASSERT(parser.Validate(installConfigFile, schemaFile) == true);
99     }
100
101     if (Path(configFile).Exists() && Path(schemaFile).Exists()) {
102         RUNNER_ASSERT(parser.Validate(configFile, schemaFile) == false);
103     }
104
105     RUNNER_ASSERT(parser.Validate("", "") == false);
106 }
107
108 /*
109 Name: parserRunner02
110 Description: Tests validation and parsing functionality.
111 Test performed for wrong 'xml', empty 'xsd' file and wrong 'xml', non empty 'xsd' file.
112 */
113 RUNNER_TEST(parserRunner02)
114 {
115     ParserRunner parser;
116
117     std::unique_ptr<StreamRedirector> sd(new StreamRedirector(stderr, "/dev/null"));
118
119
120     Path pathF1 = Path(rootTest + "testFile1.xml");
121     if (!pathF1.Exists()) {
122         MakeEmptyFile(pathF1);
123
124         DPL_SCOPE_EXIT(&pathF1) { TryRemove(pathF1); };
125
126         std::ofstream ofs1;
127         ofs1.open(pathF1.Fullpath(), std::ofstream::out);
128         if (!ofs1) {
129             RUNNER_ASSERT_MSG(false, "Error creating file");
130         }
131         ofs1 << "wrongContent";
132         ofs1.close();
133
134         RUNNER_ASSERT(parser.Validate(pathF1.Fullpath(), "") == false);
135         if (Path(schemaFile).Exists()) {
136             RUNNER_ASSERT(parser.Validate(pathF1.Fullpath(), schemaFile) == false);
137         }
138     }
139 }
140
141 /*
142 Name: parserRunner03
143 Description: Tests validation and parsing functionality.
144 Test performed for empty 'xml', wrong 'xsd' file and non empty 'xml', wrong 'xsd' file.
145 */
146 RUNNER_TEST(parserRunner03)
147 {
148     ParserRunner parser;
149
150     std::unique_ptr<StreamRedirector> sd(new StreamRedirector(stderr, "/dev/null"));
151
152
153     Path pathF2 = Path(rootTest + "testFile2.xsd");
154     if (!pathF2.Exists()) {
155         MakeEmptyFile(pathF2);
156
157         DPL_SCOPE_EXIT(&pathF2) { TryRemove(pathF2); };
158
159         std::ofstream ofs2;
160         ofs2.open(pathF2.Fullpath(), std::ofstream::out);
161         if (!ofs2) {
162             RUNNER_ASSERT_MSG(false, "Error creating file");
163         }
164         ofs2 << "<element name=\"Test\" type=\"ds:TransformType\"/>";
165         ofs2.close();
166
167         RUNNER_ASSERT(parser.Validate("", pathF2.Fullpath()) == false);
168         if (Path(configFile).Exists()) {
169             RUNNER_ASSERT(parser.Validate(configFile, pathF2.Fullpath()) == false);
170         }
171     }
172 }