Merge branch 'tizen' into security-manager
[platform/core/test/security-tests.git] / tests / common / test_cases_timeout.cpp
1 /*
2  * Copyright (c) 2015 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 /**
18  * @file        test_cases_timeout.cpp
19  * @author      Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
20  * @version     1.0
21  * @brief       Inner tests for timeout mechanism
22  */
23
24 #include <chrono>
25 #include <string>
26 #include <thread>
27
28 #include <dpl/test/test_runner.h>
29 #include <common/timeout.h>
30
31 RUNNER_TEST_GROUP_INIT(TIMEOUT)
32
33 void timeout_test_ret(int waitDuration, int functionDuration, Timeout::ExpectMode expect) {
34     float fRet = 3.1415;
35     auto ret = Timeout::callAndWait(std::chrono::seconds(waitDuration), expect,
36                          Timeout::CancelFunction(),
37                          ([fRet](int sec) -> float {
38                              std::this_thread::sleep_for(std::chrono::seconds(sec));
39                              return fRet;
40                          }), functionDuration);
41     RUNNER_ASSERT_MSG(ret == fRet,
42                          "Function returned = " << ret << " while expected value was " << fRet);
43 }
44
45 RUNNER_TEST(it01_expected_timeout)
46 {
47     timeout_test_ret(3, 5, Timeout::ExpectMode::TIMEOUT);
48 }
49
50 RUNNER_TEST(it02_unexpected_finish)
51 {
52     bool thrown = false;
53     try {
54         timeout_test_ret(3, 5, Timeout::ExpectMode::FINISHED);
55     } catch (const DPL::Test::TestException&) {
56         thrown = true;
57     }
58     RUNNER_ASSERT_MSG(thrown,
59                          "Test should throw DPL::Test::TestException");
60 }
61
62 RUNNER_TEST(it03_ignored_timeout)
63 {
64     timeout_test_ret(3, 5, Timeout::ExpectMode::IGNORE);
65 }
66
67 RUNNER_TEST(it04_expected_finish)
68 {
69     timeout_test_ret(5, 3, Timeout::ExpectMode::FINISHED);
70 }
71
72 RUNNER_TEST(it05_unexpected_timeout)
73 {
74     bool thrown = false;
75     try {
76         timeout_test_ret(5, 3, Timeout::ExpectMode::TIMEOUT);
77     } catch (const DPL::Test::TestException&) {
78         thrown = true;
79     }
80     RUNNER_ASSERT_MSG(thrown,
81                          "Test should throw DPL::Test::TestException");
82 }
83
84 RUNNER_TEST(it06_ignored_finish)
85 {
86     timeout_test_ret(5, 3, Timeout::ExpectMode::IGNORE);
87 }
88
89 void timeout_test_throw(int waitDuration, int functionDuration, Timeout::ExpectMode expect) {
90     std::string exceptionString("exceptionString");
91     bool thrown = false;
92     try {
93         Timeout::callAndWait(std::chrono::seconds(waitDuration), expect,
94                              Timeout::CancelFunction(),
95                              ([exceptionString](int sec) -> float {
96                                  std::this_thread::sleep_for(std::chrono::seconds(sec));
97                                  throw exceptionString;
98                              }), functionDuration);
99     } catch (const std::string &str) {
100         RUNNER_ASSERT_MSG(str == exceptionString,
101                              "Function thrown = " << str
102                              << " while expected value was " << exceptionString);
103         thrown = true;
104     }
105     RUNNER_ASSERT_MSG(thrown,
106                          "Test should throw std::string(" << exceptionString << ")");
107 }
108
109 RUNNER_TEST(it07_throw_expected_timeout)
110 {
111     timeout_test_throw(3, 5, Timeout::ExpectMode::TIMEOUT);
112 }
113
114 RUNNER_TEST(it08_throw_unexpected_finish)
115 {
116     bool thrown = false;
117     try {
118         timeout_test_throw(3, 5, Timeout::ExpectMode::FINISHED);
119     } catch (const DPL::Test::TestException&) {
120         thrown = true;
121     }
122     RUNNER_ASSERT_MSG(thrown,
123                          "Test should throw DPL::Test::TestException");
124 }
125
126 RUNNER_TEST(it09_throw_ignored_timeout)
127 {
128     timeout_test_throw(3, 5, Timeout::ExpectMode::IGNORE);
129 }
130
131 RUNNER_TEST(it10_throw_expected_finish)
132 {
133     timeout_test_throw(5, 3, Timeout::ExpectMode::FINISHED);
134 }
135
136 RUNNER_TEST(it11_throw_unexpected_timeout)
137 {
138     bool thrown = false;
139     try {
140         timeout_test_throw(5, 3, Timeout::ExpectMode::TIMEOUT);
141     } catch (const DPL::Test::TestException&) {
142         thrown = true;
143     }
144     RUNNER_ASSERT_MSG(thrown,
145                          "Test should throw DPL::Test::TestException");
146 }
147
148 RUNNER_TEST(it12_throw_ignored_finish)
149 {
150     timeout_test_throw(5, 3, Timeout::ExpectMode::IGNORE);
151 }