Commit summary:
[platform/core/security/suspicious-activity-monitor.git] / utest / test_reportadapter.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 <thread>
20 #include <chrono>
21 #include <jsoncpp/json/reader.h>
22 #include <jsoncpp/json/value.h>
23
24 #include "connection.h"
25 #include "restservicemock.h"
26 #include "reportadapter.h"
27 #include "settings.h"
28
29 using ::testing::_;
30 using ::testing::A;
31 using ::testing::Eq;
32 using ::testing::Return;
33
34 namespace
35 {
36 const std::chrono::milliseconds TEST_TIMEOUT_INTERVAL{1};
37 const std::string TEST_REPORT_DATA{"{\"value\": 1}"};
38 }
39
40 TEST(TestReportAdapter, test_sendReport)
41 {
42     try {
43         auto& settings = agent::Settings::instance();
44
45         RestServiceMock rest;
46         communication::Connection conn("", TEST_TIMEOUT_INTERVAL, &rest, [](){return true;});
47         agent::ReportAdapter adapter(conn);
48
49         Json::Reader reader;
50         Json::Value value;
51         ASSERT_TRUE(reader.parse(TEST_REPORT_DATA, value));
52
53         EXPECT_CALL(rest, sendData(_, A<const Json::Value&>())).Times(1);
54         EXPECT_CALL(rest, getUpdates(_)).WillRepeatedly(Return(""));
55
56         std::thread t(&communication::Connection::loop, &conn);
57
58         settings.setLock(false);
59         adapter.sendReport(std::string{"syscall"}, value);
60         settings.setLock(true);
61         adapter.sendReport(std::string{"syscall"}, value);
62
63         std::this_thread::sleep_for(std::chrono::milliseconds(50));
64
65         conn.stop();
66
67         t.join();
68     } catch (std::exception& e) {
69         FAIL() << e.what();
70     }
71 }