tizen 2.3.1 release
[framework/web/wearable/wrt-commons.git] / tests / test / runner_multiprocess.cpp
1 /*
2  * Copyright (c) 2013 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    runner_multiprocess.cpp
18  * @author  Marcin Niesluchowski (m.niesluchow@samsung.com)
19  * @version 1.0
20  * @brief   Implementation file for test cases for engine internal tests
21  */
22
23 #include <dpl/test/test_runner_multiprocess.h>
24 #include <dpl/test/test_runner.h>
25 #include <dpl/log/log.h>
26 #include <list>
27
28 namespace {
29 std::list<std::string> split_string(std::string str, std::string delimiter)
30 {
31     size_t pos = 0;
32     std::string token;
33     std::list<std::string> stringList;
34     while ((pos = str.find(delimiter)) != std::string::npos) {
35         token = str.substr(0, pos);
36         stringList.push_back(token);
37         str.erase(0, pos + delimiter.length());
38     }
39     if(str.length() != 0){
40         stringList.push_back(token);
41     }
42     return stringList;
43 }
44 }
45
46 #define RUNNER_MULTIPROCESS_TEST_EXPECT(name, messages)                        \
47     static void testExpectFunction##name();                                    \
48     RUNNER_TEST(name)                                                          \
49     {                                                                          \
50         Try                                                                    \
51         {                                                                      \
52             DPL::Test::RunMultiProc(&testExpectFunction##name);                \
53         }                                                                      \
54         Catch(DPL::Test::TestRunner::TestFailed)                               \
55         {                                                                      \
56             std::string eMsg = messages;                                       \
57             std::list<std::string> eMessages = split_string(eMsg, "|");        \
58             std::string rMessage = _rethrown_exception.GetMessage();           \
59             if(eMsg.length() == 0 && rMessage.length() != 0) {                 \
60                 RUNNER_ASSERT_MSG(false, rMessage);                            \
61             }                                                                  \
62             bool failedFound = false;                                          \
63             for(std::list<std::string>::iterator it = eMessages.begin();       \
64                 it != eMessages.end();                                         \
65                 ++it)                                                          \
66             {                                                                  \
67                 if (!(*it).compare("TEST_FAILED")) {                           \
68                     failedFound = true;                                        \
69                     continue;                                                  \
70                 }                                                              \
71                 RUNNER_ASSERT_MSG(rMessage.find(*it)!=std::string::npos,       \
72                     "Key word " << *it << " not found in " << rMessage);       \
73             }                                                                  \
74             RUNNER_ASSERT_MSG(                                                 \
75                 rMessage.find("Reading pipe error")==std::string::npos,        \
76                 "Reading pipe error");                                         \
77             RUNNER_ASSERT_MSG(                                                 \
78                 rMessage.find("Timeout error")==std::string::npos,             \
79                 "Timeout error");                                              \
80             RUNNER_ASSERT_MSG(failedFound, "No TEST_FAILED found");            \
81         }                                                                      \
82         Catch(DPL::Test::TestRunner::Ignored)                                  \
83         {                                                                      \
84             std::string eMsg = messages;                                       \
85             std::list<std::string> eMessages = split_string(eMsg, "|");        \
86             std::string rMessage = _rethrown_exception.GetMessage();           \
87             if(eMsg.length() == 0 && rMessage.length() != 0) {                 \
88                 RUNNER_ASSERT_MSG(false, rMessage);                            \
89             }                                                                  \
90             bool ignoredFound = false;                                         \
91             for(std::list<std::string>::iterator it = eMessages.begin();       \
92                 it != eMessages.end();                                         \
93                 ++it)                                                          \
94             {                                                                  \
95                 if (!(*it).compare("TEST_IGNORED")) {                           \
96                     ignoredFound = true;                                       \
97                     continue;                                                  \
98                 }                                                              \
99                 RUNNER_ASSERT_MSG(rMessage.find(*it)!=std::string::npos,       \
100                     "Key word " << *it << " not found in " << rMessage);       \
101             }                                                                  \
102             RUNNER_ASSERT_MSG(                                                 \
103                 rMessage.find("Reading pipe error")==std::string::npos,        \
104                 "Reading pipe error");                                         \
105             RUNNER_ASSERT_MSG(                                                 \
106                 rMessage.find("Timeout error")==std::string::npos,             \
107                 "Timeout error");                                              \
108             RUNNER_ASSERT_MSG(ignoredFound, "No TEST_IGNORED found");          \
109         }                                                                      \
110     }                                                                          \
111     void testExpectFunction##name()                                            \
112
113 RUNNER_TEST_GROUP_INIT(DPL_TESTS_TEST_MULTIPROCESS)
114
115 RUNNER_MULTIPROCESS_TEST_EXPECT(tm00_pass, "")
116 {
117     RUNNER_ASSERT_MSG(1, "This test should pass");
118 }
119
120 RUNNER_MULTIPROCESS_TEST_EXPECT(tm01_pass, "")
121 {
122     pid_t pid = fork();
123     if(pid){
124         sleep(2);
125         RUNNER_ASSERT_MSG(1, "This test should pass");
126     } else {
127         RUNNER_ASSERT_MSG(1, "This test should pass");
128     }
129     RUNNER_ASSERT_MSG(1, "This test should pass");
130 }
131
132 RUNNER_MULTIPROCESS_TEST_EXPECT(tm02_pass, "")
133 {
134     pid_t pid = fork();
135     if(pid){
136         RUNNER_ASSERT_MSG(1, "This test should pass");
137     } else {
138         sleep(2);
139         RUNNER_ASSERT_MSG(1, "This test should pass");
140     }
141     RUNNER_ASSERT_MSG(1, "This test should pass");
142 }
143
144 RUNNER_MULTIPROCESS_TEST_EXPECT(tm03_pass, "")
145 {
146     pid_t pid = fork();
147     if(pid){
148         pid = fork();
149         if(pid){
150             sleep(1);
151         } else {
152             sleep(2);
153         }
154     } else {
155         if(pid){
156             sleep(2);
157         } else {
158             sleep(1);
159         }
160     }
161 }
162
163 RUNNER_MULTIPROCESS_TEST_EXPECT(tm04_fail, "TEST_FAILED|"
164                                            "This test should fail")
165 {
166     RUNNER_ASSERT_MSG(0, "This test should fail");
167 }
168
169 RUNNER_MULTIPROCESS_TEST_EXPECT(tm05_fail,"TEST_FAILED|"
170                                           "Test failed 1|"
171                                           "Test failed 2|"
172                                           "Test failed 3|"
173                                           "Test failed 4")
174 {
175     pid_t pid = fork();
176     if(pid){
177         pid = fork();
178         if(pid){
179             RUNNER_ASSERT_MSG(0, "Test failed 1");
180         } else {
181             RUNNER_ASSERT_MSG(0, "Test failed 2");
182         }
183     } else {
184         pid = fork();
185         if(pid){
186             RUNNER_ASSERT_MSG(0, "Test failed 3");
187         } else {
188             RUNNER_ASSERT_MSG(0, "Test failed 4");
189         }
190     }
191 }
192
193 RUNNER_MULTIPROCESS_TEST_EXPECT(tm06_fail, "TEST_FAILED|"
194                                           "Test failed 1|"
195                                           "Test failed 2|"
196                                           "Test failed 3|"
197                                           "Test failed 4")
198 {
199     pid_t pid = fork();
200     if(pid){
201         pid = fork();
202         if(pid){
203             sleep(2);
204             RUNNER_ASSERT_MSG(0, "Test failed 1");
205         } else {
206             RUNNER_ASSERT_MSG(0, "Test failed 2");
207         }
208     } else {
209         pid = fork();
210         if(pid){
211             RUNNER_ASSERT_MSG(0, "Test failed 3");
212         } else {
213             RUNNER_ASSERT_MSG(0, "Test failed 4");
214         }
215     }
216 }
217
218 RUNNER_MULTIPROCESS_TEST_EXPECT(tm07_fail, "TEST_FAILED|"
219                                           "Test failed 1|"
220                                           "Test failed 2|"
221                                           "Test failed 3|"
222                                           "Test failed 4")
223 {
224     pid_t pid = fork();
225     if(pid){
226         pid = fork();
227         if(pid){
228             RUNNER_ASSERT_MSG(0, "Test failed 1");
229         } else {
230             RUNNER_ASSERT_MSG(0, "Test failed 2");
231         }
232     } else {
233         pid = fork();
234         if(pid){
235             sleep(2);
236             RUNNER_ASSERT_MSG(0, "Test failed 3");
237         } else {
238             RUNNER_ASSERT_MSG(0, "Test failed 4");
239         }
240     }
241 }
242
243 RUNNER_MULTIPROCESS_TEST_EXPECT(tm08_fail_unknown_exception, "TEST_FAILED|"
244                                                              "unknown exception")
245 {
246     throw("hello");
247 }
248
249 RUNNER_MULTIPROCESS_TEST_EXPECT(tm09_fail_unknown_exception, "TEST_FAILED|"
250                                                             "unknown exception")
251 {
252     throw(1);
253 }
254
255 RUNNER_MULTIPROCESS_TEST_EXPECT(tm10_ignore, "TEST_IGNORED|"
256                                              "Test ignored")
257 {
258     RUNNER_IGNORED_MSG("Test ignored");
259 }
260
261 RUNNER_MULTIPROCESS_TEST_EXPECT(tm11_ignore, "TEST_IGNORED|"
262                                             "Test ignored 1|"
263                                             "Test ignored 2|"
264                                             "Test ignored 3|"
265                                             "Test ignored 4")
266 {
267     pid_t pid = fork();
268     if(pid){
269         pid = fork();
270         if(pid){
271             RUNNER_IGNORED_MSG("Test ignored 1");
272         } else {
273             RUNNER_IGNORED_MSG("Test ignored 2");
274         }
275     } else {
276         pid = fork();
277         if(pid){
278             sleep(2);
279             RUNNER_IGNORED_MSG("Test ignored 3");
280         } else {
281             RUNNER_IGNORED_MSG("Test ignored 4");
282         }
283     }
284 }
285
286 RUNNER_MULTIPROCESS_TEST_EXPECT(tm12_fail, "TEST_FAILED|"
287                                           "Test failed 1|"
288                                           "Test ignored 2|"
289                                           "Test ignored 3|"
290                                           "Test ignored 4")
291 {
292     pid_t pid = fork();
293     if(pid){
294         pid = fork();
295         if(pid){
296             RUNNER_ASSERT_MSG(0, "Test failed 1");
297         } else {
298             RUNNER_IGNORED_MSG("Test ignored 2");
299         }
300     } else {
301         pid = fork();
302         if(pid){
303             sleep(2);
304             RUNNER_IGNORED_MSG("Test ignored 3");
305         } else {
306             RUNNER_IGNORED_MSG("Test ignored 4");
307         }
308     }
309 }