Commit summary:
[platform/core/security/suspicious-activity-monitor.git] / utest / test_connection.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 "reportcomposer.h"
23 #include "connection.h"
24 #include "eventlistener.h"
25 #include "restservicemock.h"
26 #include <jsoncpp/json/reader.h>
27 #include <jsoncpp/json/writer.h>
28
29 using namespace communication;
30 using ::testing::_;
31 using ::testing::Eq;
32 using ::testing::Return;
33 using ::testing::ReturnNull;
34 using ::testing::Throw;
35 using ::testing::DoAll;
36 using ::testing::Invoke;
37 using ::testing::WithArg;
38 using ::testing::WithArgs;
39 using ::testing::AtLeast;
40
41 #define TAG "Tests"
42
43 namespace
44 {
45 const std::chrono::milliseconds TEST_KEEP_ALIVE(10);
46 const std::string TEST_SERVER_ADDRESS{"test-server"};
47 const std::string TEST_DEVICE_ID{"device-id"};
48
49 const std::string TEST_EVENT_TYPE{"report"};
50 const std::string TEST_EVENT_DATA1{"{\"value\": 1}"};
51 const std::string TEST_EVENT_DATA2{"{\"value\": 2}"};
52 const std::string TEST_UPDATES{ R"-([
53     {"type":"policy","uri":"policy-uri"},
54     {"type":"action","uri":"action-uri"},
55     {"type":"unused","uri":"unused-uri"}
56 ])-"};
57 const std::string TEST_POLICY_URI{"policy-uri"};
58 const std::string TEST_ACTION_URI{"action-uri"};
59 }
60
61 class EventListenerMock: public EventListener
62 {
63 public:
64     EventListenerMock(Connection& conn, const std::string& type): EventListener(conn, type)
65     {
66     }
67
68     MOCK_METHOD1(accept, void(Event& event));
69 };
70
71 namespace communication
72 {
73
74 bool operator==(const SessionInfo& si1, const SessionInfo& si2)
75 {
76     return si1.duid == si2.duid && si1.authToken == si2.authToken;
77 }
78
79 }
80
81 TEST(TestConnection, test_signal)
82 {
83     RestServiceMock rest;
84     Connection conn("", TEST_KEEP_ALIVE, &rest, [](){return true;});
85     Json::Reader reader;
86     Json::FastWriter writer;
87     ReportComposer rc;
88     Json::Value data1, data2;
89     ASSERT_TRUE(reader.parse(TEST_EVENT_DATA1, data1));
90     ASSERT_TRUE(reader.parse(TEST_EVENT_DATA2, data2));
91     rc.addEvent(std::make_pair(TEST_EVENT_TYPE, data1));
92     rc.addEvent(std::make_pair(TEST_EVENT_TYPE, data2));
93     SessionInfo checkSessState{"", ""};
94
95     EXPECT_EQ(rc.str(), writer.write(rc.get()));
96
97     EXPECT_CALL(rest, registerDevice(Eq(checkSessState), Eq(data1)))
98             .WillOnce(Throw(std::runtime_error("")))
99             .WillOnce(Return(TEST_DEVICE_ID));
100     EXPECT_CALL(rest, getUpdates(_))
101             .WillRepeatedly(Return(""));
102     EXPECT_CALL(rest, sendData(_, ::testing::Matcher<const Json::Value&>(rc.get())))
103             .Times(1);
104
105     EXPECT_ANY_THROW(conn.registerDevice(Json::Value(data1)));
106     EXPECT_EQ(TEST_DEVICE_ID, conn.registerDevice(Json::Value(data1)));
107
108     std::thread t(&Connection::loop, &conn);
109     conn.addReportEvent(TEST_EVENT_TYPE, std::move(data1));
110     conn.addReportEvent(TEST_EVENT_TYPE, std::move(data2));
111
112     std::this_thread::sleep_for(std::chrono::milliseconds(70));
113
114     conn.stop();
115
116     t.join();
117 }
118
119 void fakeInvoker(Event& e)
120 {
121     e.getContent();
122 }
123
124 TEST(TestConnection, test_Listeners)
125 {
126     try {
127         RestServiceMock rest;
128         Connection conn(TEST_DEVICE_ID, TEST_KEEP_ALIVE, &rest, [](){return true;});
129
130         EventListenerMock el1{conn, "policy"};
131         EventListenerMock el2{conn, "policy"};
132         EventListenerMock el3{conn, "action"};
133
134         EXPECT_CALL(rest, getUpdates(_))
135                 .Times(AtLeast(1))
136                 .WillRepeatedly(Return(TEST_UPDATES));
137         EXPECT_CALL(rest, doGet(_, TEST_POLICY_URI))
138                 .Times(AtLeast(1))
139                 .WillRepeatedly(Return(TEST_EVENT_DATA1));
140         EXPECT_CALL(rest, doGet(_, TEST_ACTION_URI))
141                 .Times(AtLeast(1))
142                 .WillRepeatedly(Return(TEST_EVENT_DATA2));
143         EXPECT_CALL(el1, accept(_))
144                 .Times(AtLeast(1))
145                 .WillRepeatedly(WithArgs<0>(Invoke(fakeInvoker)));
146         EXPECT_CALL(el2, accept(_))
147                 .Times(AtLeast(1))
148                 .WillRepeatedly(WithArgs<0>(Invoke(fakeInvoker)));
149         EXPECT_CALL(el3, accept(_))
150                 .Times(AtLeast(1))
151                 .WillRepeatedly(WithArgs<0>(Invoke(fakeInvoker)));
152
153         std::thread t(&Connection::loop, &conn);
154
155         std::this_thread::sleep_for(std::chrono::milliseconds(15));
156
157         conn.stop();
158
159         t.join();
160     } catch (std::exception& e) {
161         FAIL() << e.what();
162     }
163 }