Commit summary:
[platform/core/security/suspicious-activity-monitor.git] / utest / test_settingshandler.cpp
1 /**
2  * Samsung Ukraine R&D Center (SRK under a contract between)
3  * LLC "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
4  * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License
17  */
18 #include <gmock/gmock.h>
19 #include <gtest/gtest.h>
20 #include <functional>
21 #include <thread>
22 #include "connection.h"
23 #include "settingshandler.h"
24 #include "settings.h"
25 #include "restservicemock.h"
26
27 using namespace communication;
28 using namespace agent;
29 using ::testing::_;
30 using ::testing::Eq;
31 using ::testing::Return;
32 using ::testing::Throw;
33 using ::testing::TypedEq;
34 using ::testing::AtLeast;
35
36 #define TAG "Tests"
37
38 namespace
39 {
40 const std::chrono::milliseconds TEST_KEEP_ALIVE(10);
41 const std::string TEST_SERVER_ADDRESS{"test-server"};
42 const std::string TEST_DEVICE_ID{"device-id"};
43 //const std::string TEST_UPDATES{ R"-([
44 //    {"type":"policy","uri":"settings-uri","curi":"settings-curi"}
45 //])-"};
46 const std::string TEST_SETTINGS_URI{"settings-uri"};
47 const std::string TEST_SETTINGS_CURI{"settings-curi"};
48 const std::string TEST_UPDATES{ "[{\"type\":\"settings\",\"uri\":\"" + TEST_SETTINGS_URI + "\",\"curi\":\"" + TEST_SETTINGS_CURI +"\"}]"};
49 const std::chrono::milliseconds TEST_TIMEOUT{123};
50 const bool TEST_LOCK = true;
51 const std::string TEST_SETTINGS{"{\
52     \"timeout\": " + std::to_string(TEST_TIMEOUT.count()) + ", \
53     \"lock\": " + std::to_string(int(TEST_LOCK)) + " \
54 }"};
55 const std::string TEST_EMPTY_SETTINGS{"{}"};
56 const std::string TEST_MALFORMED_SETTINGS{"{[12}"};
57 const std::string TEST_SETTINGS_THROW{"{\"timeout\": \"asdf\", \"lock\": 0}"};
58 const std::string TEST_SETTINGS_NEG_TIMEOUT{"{\
59     \"timeout\": -123, \
60     \"lock\": " + std::to_string(int(TEST_LOCK)) + " \
61 }"};
62 }
63
64 class TestSettingsHandler: public ::testing::Test
65 {
66 public:
67     void SetUp()
68     {
69         Settings& settings = Settings::instance();
70         settings.setDeviceId(TEST_DEVICE_ID);
71         settings.setKeepAliveTimeout(TEST_KEEP_ALIVE);
72         settings.setServerAddress(TEST_SERVER_ADDRESS);
73         settings.setLock(!TEST_LOCK);
74     }
75
76     void TearDown()
77     {
78     }
79 };
80
81 /**
82  * @brief Test for SettingsHandler::accept method correct work
83  * 1. Generate normal update info
84  * 2. Generate settings update object
85  * 3. Check that settings changed to expected values
86  */
87 TEST_F(TestSettingsHandler, test_accept)
88 {
89     try {
90         Settings& settings = Settings::instance();
91         RestServiceMock rest;
92         ASSERT_NE(TEST_LOCK, settings.isLocked());
93
94         Connection conn(TEST_DEVICE_ID, TEST_KEEP_ALIVE, &rest, [](){return true;});
95         SettingsHandler handler(conn);
96
97         EXPECT_CALL(rest, getUpdates(_))
98                 .WillRepeatedly(Return(TEST_UPDATES));
99         EXPECT_CALL(rest, doGet(_, Eq(TEST_SETTINGS_URI)))
100                 .WillOnce(Return(TEST_SETTINGS));
101         EXPECT_CALL(rest, doPost(_, Eq(TEST_SETTINGS_CURI), TypedEq<const std::string&>("")))
102                 .Times(1);
103
104         std::thread t(&Connection::loop, &conn);
105
106         std::this_thread::sleep_for(std::chrono::milliseconds(15));
107
108         conn.stop();
109
110         t.join();
111
112         ASSERT_EQ(TEST_TIMEOUT, settings.getKeepAliveTimeout());
113         ASSERT_EQ(TEST_LOCK, settings.isLocked());
114     } catch (std::exception& e) {
115         FAIL() << e.what();
116     }
117 }
118
119 /**
120  * @brief Test for SettingsHandler::accept method correct work
121  * 1. Generate normal update info
122  * 2. Generate settings update object without properties
123  * 3. Check that settings not changed
124  */
125 TEST_F(TestSettingsHandler, test_no_known_settings)
126 {
127     try {
128         Settings& settings = Settings::instance();
129         RestServiceMock rest;
130         ASSERT_NE(TEST_LOCK, settings.isLocked());
131
132         Connection conn(TEST_DEVICE_ID, TEST_KEEP_ALIVE, &rest, [](){return true;});
133         SettingsHandler handler(conn);
134
135         EXPECT_CALL(rest, getUpdates(_))
136                 .Times(AtLeast(1))
137                 .WillRepeatedly(Return(TEST_UPDATES));
138         EXPECT_CALL(rest, doGet(_, Eq(TEST_SETTINGS_URI)))
139                 .Times(AtLeast(1))
140                 .WillRepeatedly(Return(TEST_EMPTY_SETTINGS));
141         EXPECT_CALL(rest, doPost(_, Eq(TEST_SETTINGS_CURI), TypedEq<const std::string&>("")))
142                 .Times(AtLeast(1));
143
144         std::thread t(&Connection::loop, &conn);
145
146         std::this_thread::sleep_for(std::chrono::milliseconds(15));
147
148         conn.stop();
149
150         t.join();
151
152         ASSERT_EQ(TEST_KEEP_ALIVE, settings.getKeepAliveTimeout());
153         ASSERT_NE(TEST_LOCK, settings.isLocked());
154     } catch (std::exception& e) {
155         FAIL() << e.what();
156     }
157 }
158
159 /**
160  * @brief Test for SettingsHandler::accept method correct work
161  * 1. Generate normal update info
162  * 2. Generate malformed settings update object
163  * 3. Check that settings not changed
164  */
165 TEST_F(TestSettingsHandler, test_malformed_json)
166 {
167     try {
168         Settings& settings = Settings::instance();
169         RestServiceMock rest;
170         ASSERT_NE(TEST_LOCK, settings.isLocked());
171
172         Connection conn(TEST_DEVICE_ID, TEST_KEEP_ALIVE, &rest, [](){return true;});
173         SettingsHandler handler(conn);
174
175         EXPECT_CALL(rest, getUpdates(_))
176                 .Times(AtLeast(1))
177                 .WillRepeatedly(Return(TEST_UPDATES));
178         EXPECT_CALL(rest, doGet(_, Eq(TEST_SETTINGS_URI)))
179                 .Times(AtLeast(1))
180                 .WillRepeatedly(Return(TEST_MALFORMED_SETTINGS));
181
182         std::thread t(&Connection::loop, &conn);
183
184         std::this_thread::sleep_for(std::chrono::milliseconds(15));
185
186         conn.stop();
187
188         t.join();
189
190         ASSERT_EQ(TEST_KEEP_ALIVE, settings.getKeepAliveTimeout());
191         ASSERT_NE(TEST_LOCK, settings.isLocked());
192     } catch (std::exception& e) {
193         FAIL() << e.what();
194     }
195 }
196
197 /**
198  * @brief Test for SettingsHandler::accept method correct work
199  * 1. Generate normal update info
200  * 2. Generate settings update object that cause exception
201  * 3. Check that settings not changed
202  */
203 TEST_F(TestSettingsHandler, test_accept_throw)
204 {
205     try {
206         Settings& settings = Settings::instance();
207         RestServiceMock rest;
208         ASSERT_NE(TEST_LOCK, settings.isLocked());
209
210         Connection conn(TEST_DEVICE_ID, TEST_KEEP_ALIVE, &rest, [](){return true;});
211         SettingsHandler handler(conn);
212
213         EXPECT_CALL(rest, getUpdates(_))
214                 .Times(AtLeast(1))
215                 .WillRepeatedly(Return(TEST_UPDATES));
216         EXPECT_CALL(rest, doGet(_, Eq(TEST_SETTINGS_URI)))
217                 .Times(AtLeast(1))
218                 .WillRepeatedly(Return(TEST_SETTINGS_THROW));
219
220         std::thread t(&Connection::loop, &conn);
221
222         std::this_thread::sleep_for(std::chrono::milliseconds(15));
223
224         conn.stop();
225
226         t.join();
227
228         ASSERT_EQ(TEST_KEEP_ALIVE, settings.getKeepAliveTimeout());
229         ASSERT_NE(TEST_LOCK, settings.isLocked());
230     } catch (std::exception& e) {
231         FAIL() << e.what();
232     }
233 }
234
235 /**
236  * @brief Test for SettingsHandler::accept method correct work
237  * 1. Generate normal update info
238  * 2. Generate settings update object with negative timeout property
239  * 3. Check that timeout not changed but lock state changed
240  */
241 TEST_F(TestSettingsHandler, test_negative_timeout)
242 {
243     try {
244         Settings& settings = Settings::instance();
245         RestServiceMock rest;
246         ASSERT_NE(TEST_LOCK, settings.isLocked());
247
248         Connection conn(TEST_DEVICE_ID, TEST_KEEP_ALIVE, &rest, [](){return true;});
249         SettingsHandler handler(conn);
250
251         EXPECT_CALL(rest, getUpdates(_))
252                 .Times(AtLeast(1))
253                 .WillRepeatedly(Return(TEST_UPDATES));
254         EXPECT_CALL(rest, doGet(_, Eq(TEST_SETTINGS_URI)))
255                 .Times(AtLeast(1))
256                 .WillRepeatedly(Return(TEST_SETTINGS_NEG_TIMEOUT));
257         EXPECT_CALL(rest, doPost(_, Eq(TEST_SETTINGS_CURI), TypedEq<const std::string&>("")))
258                 .Times(AtLeast(1));
259
260         std::thread t(&Connection::loop, &conn);
261
262         std::this_thread::sleep_for(std::chrono::milliseconds(15));
263
264         conn.stop();
265
266         t.join();
267
268         ASSERT_EQ(TEST_KEEP_ALIVE, settings.getKeepAliveTimeout());
269         ASSERT_EQ(TEST_LOCK, settings.isLocked());
270     } catch (std::exception& e) {
271         FAIL() << e.what();
272     }
273 }