Commit summary:
[platform/core/security/suspicious-activity-monitor.git] / utest / test_notificationhandler.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 <jsoncpp/json/reader.h>
20 #include "notificationhandler.h"
21 #include "restservicemock.h"
22 #include "app_control_mock.h"
23 #include "settings.h"
24
25 using ::testing::_;
26 using ::testing::A;
27 using ::testing::DoAll;
28 using ::testing::SaveArg;
29 using ::testing::Eq;
30 using ::testing::StrEq;
31 using ::testing::TypedEq;
32 using ::testing::Return;
33
34 namespace
35 {
36 const std::string TEST_DUID{"device-id"};
37 const std::chrono::milliseconds TEST_KEEPALIVE{1};
38 const std::string TEST_EVENT_URI{"test-uri"};
39 const std::string TEST_EVENT_CURI{"test-curi"};
40 const std::string ENFORCE_KEY{"notify_user"};
41 const std::string EMPTY_CONTENT{""};
42 const std::string NOTIFICATION_SERVICE{"org.example.samnotificationservice"};
43 const std::string UNINSTALL_APP_EMPTY_CONTENT{R"#({"type":"notify-user","data":{"action":"uninstall_application"}})#"};
44 const std::string UNINSTALL_APP_CONTENT{R"#({"type":"notify-user","data":{"action":"uninstall_application","subject":"org.example.photostealware"}})#"};
45 const std::string UNINSTALL_APP_EX_DATA_KEY{ENFORCE_KEY};
46 const std::string UNINSTALL_APP_EX_DATA_VAL{"{\"title\":\"SAM notification\",\"body\":\"Stealware process was detected [photostealware]. Remove it?\",\"buttons\":[{\"action\":{\"data\":{\"app_id\":\"org.example.photostealware\"},\"type\":\"uninstall_application\"},\"caption\":\"YES\"},{\"caption\":\"NO\"}]}"};
47 const std::string KILL_PROCESS_EMPTY_CONTENT{R"#({"type":"notify-user","data":{"action":"kill_process"}})#"};
48 const std::string KILL_PROCESS_CONTENT{R"#({"type":"notify-user","data":{"action":"kill_process","pid":1234,"path":"/usr/app/xmrstack"}})#"};
49 const std::string KILL_PROCESS_EX_DATA_KEY{ENFORCE_KEY};
50 const std::string KILL_PROCESS_EX_DATA_VAL{""};
51 const std::string UNKNOWN_ACTION_CONTENT{R"#({"type":"notify-user","data":{"action":"undef"}})#"};
52 }
53
54 /**
55  * @brief TEST that event not confirmed when device is locked
56  */
57 TEST(TestNotificationHandler, test_accept_when_locked)
58 {
59     RestServiceMock rest;
60     communication::Connection conn(TEST_DUID, TEST_KEEPALIVE, &rest, [](){return true;});
61     agent::NotificationHandler handler(conn);
62     Json::Value event_msg;
63     event_msg["uri"] = TEST_EVENT_URI;
64     event_msg["curi"] = TEST_EVENT_CURI;
65     event_msg["type"] = ENFORCE_KEY;
66     communication::Event event(event_msg, conn);
67
68     agent::Settings::instance().setLock(true);
69     EXPECT_NO_THROW(handler.accept(event));
70     agent::Settings::instance().setLock(false);
71 }
72
73 /**
74  * @brief TEST for empty content
75  */
76 TEST(TestNotificationHandler, test_accept_empty_content)
77 {
78     RestServiceMock rest;
79     communication::Connection conn(TEST_DUID, TEST_KEEPALIVE, &rest, [](){return true;});
80     agent::NotificationHandler handler(conn);
81     Json::Value event_msg;
82     event_msg["uri"] = TEST_EVENT_URI;
83     event_msg["curi"] = TEST_EVENT_CURI;
84     event_msg["type"] = ENFORCE_KEY;
85     communication::Event event(event_msg, conn);
86
87     EXPECT_CALL(rest, doGet(_, Eq(TEST_EVENT_URI))).WillOnce(Return(EMPTY_CONTENT));
88     EXPECT_CALL(rest, doPost(_, Eq(TEST_EVENT_CURI), A<const std::string&>())).Times(1);
89
90     ASSERT_NO_THROW(handler.accept(event));
91 }
92
93 /**
94  * @brief TEST for uninstall application with empty subject
95  */
96 TEST(TestNotificationHandler, test_accept_uninstall_application_empty_content)
97 {
98     RestServiceMock rest;
99     communication::Connection conn(TEST_DUID, TEST_KEEPALIVE, &rest, [](){return true;});
100     agent::NotificationHandler handler(conn);
101     Json::Value event_msg;
102     event_msg["uri"] = TEST_EVENT_URI;
103     event_msg["curi"] = TEST_EVENT_CURI;
104     event_msg["type"] = ENFORCE_KEY;
105     communication::Event event(event_msg, conn);
106
107     EXPECT_CALL(rest, doGet(_, Eq(TEST_EVENT_URI))).WillOnce(Return(UNINSTALL_APP_EMPTY_CONTENT));
108     EXPECT_CALL(rest, doPost(_, Eq(TEST_EVENT_CURI), A<const std::string&>())).Times(1);
109
110     ASSERT_NO_THROW(handler.accept(event));
111 }
112
113 /**
114  * @brief TEST for uninstall application
115  */
116 TEST(TestNotificationHandler, test_accept_uninstall_application)
117 {
118     RestServiceMock rest;
119     communication::Connection conn(TEST_DUID, TEST_KEEPALIVE, &rest, [](){return true;});
120     agent::NotificationHandler handler(conn);
121     Json::Value event_msg;
122     event_msg["uri"] = TEST_EVENT_URI;
123     event_msg["curi"] = TEST_EVENT_CURI;
124     event_msg["type"] = ENFORCE_KEY;
125     communication::Event event(event_msg, conn);
126     AppControlMock app_ctrl;
127
128     EXPECT_CALL(app_ctrl, app_control_create(_)).WillOnce(Return(0));
129     EXPECT_CALL(app_ctrl, app_control_set_operation(_)).WillOnce(Return(0));
130     EXPECT_CALL(app_ctrl, app_control_set_app_id(StrEq(NOTIFICATION_SERVICE))).WillOnce(Return(0));
131     EXPECT_CALL(app_ctrl, app_control_add_extra_data(StrEq(UNINSTALL_APP_EX_DATA_KEY), _)).WillOnce(Return(0));
132     EXPECT_CALL(app_ctrl, app_control_send_launch_request(_, _)).WillOnce(Return(0));
133     EXPECT_CALL(app_ctrl, app_control_destroy()).WillOnce(Return(0));
134
135     EXPECT_CALL(rest, doGet(_, Eq(TEST_EVENT_URI))).WillOnce(Return(UNINSTALL_APP_CONTENT));
136     EXPECT_CALL(rest, doPost(_, Eq(TEST_EVENT_CURI), A<const std::string&>())).Times(1);
137
138     ASSERT_NO_THROW(handler.accept(event));
139 }
140
141 /**
142  * @brief TEST for kill process with empty content
143  */
144 TEST(TestNotificationHandler, test_accept_kill_process_empty_content)
145 {
146     RestServiceMock rest;
147     communication::Connection conn(TEST_DUID, TEST_KEEPALIVE, &rest, [](){return true;});
148     agent::NotificationHandler handler(conn);
149     Json::Value event_msg;
150     event_msg["uri"] = TEST_EVENT_URI;
151     event_msg["curi"] = TEST_EVENT_CURI;
152     event_msg["type"] = ENFORCE_KEY;
153     communication::Event event(event_msg, conn);
154
155     EXPECT_CALL(rest, doGet(_, Eq(TEST_EVENT_URI))).WillOnce(Return(KILL_PROCESS_EMPTY_CONTENT));
156     EXPECT_CALL(rest, doPost(_, Eq(TEST_EVENT_CURI), A<const std::string&>())).Times(1);
157
158     ASSERT_NO_THROW(handler.accept(event));
159 }
160
161 /**
162  * @brief TEST for kill process
163  */
164 TEST(TestNotificationHandler, test_accept_kill_process)
165 {
166     RestServiceMock rest;
167     communication::Connection conn(TEST_DUID, TEST_KEEPALIVE, &rest, [](){return true;});
168     agent::NotificationHandler handler(conn);
169     Json::Value event_msg;
170     event_msg["uri"] = TEST_EVENT_URI;
171     event_msg["curi"] = TEST_EVENT_CURI;
172     event_msg["type"] = ENFORCE_KEY;
173     communication::Event event(event_msg, conn);
174     AppControlMock app_ctrl;
175
176     EXPECT_CALL(app_ctrl, app_control_create(_)).WillOnce(Return(0));
177     EXPECT_CALL(app_ctrl, app_control_set_operation(_)).WillOnce(Return(0));
178     EXPECT_CALL(app_ctrl, app_control_set_app_id(StrEq(NOTIFICATION_SERVICE))).WillOnce(Return(0));
179     EXPECT_CALL(app_ctrl, app_control_add_extra_data(StrEq(KILL_PROCESS_EX_DATA_KEY), _)).WillOnce(Return(0));
180     EXPECT_CALL(app_ctrl, app_control_send_launch_request(_, _)).WillOnce(Return(0));
181     EXPECT_CALL(app_ctrl, app_control_destroy()).WillOnce(Return(0));
182
183     EXPECT_CALL(rest, doGet(_, Eq(TEST_EVENT_URI))).WillOnce(Return(KILL_PROCESS_CONTENT));
184     EXPECT_CALL(rest, doPost(_, Eq(TEST_EVENT_CURI), A<const std::string&>())).Times(1);
185
186     ASSERT_NO_THROW(handler.accept(event));
187 }
188
189 /**
190  * @brief TEST for unknown action
191  */
192 TEST(TestNotificationHandler, test_accept_unknown_action)
193 {
194     RestServiceMock rest;
195     communication::Connection conn(TEST_DUID, TEST_KEEPALIVE, &rest, [](){return true;});
196     agent::NotificationHandler handler(conn);
197     Json::Value event_msg;
198     event_msg["uri"] = TEST_EVENT_URI;
199     event_msg["curi"] = TEST_EVENT_CURI;
200     event_msg["type"] = ENFORCE_KEY;
201     communication::Event event(event_msg, conn);
202
203     EXPECT_CALL(rest, doGet(_, Eq(TEST_EVENT_URI))).WillOnce(Return(UNKNOWN_ACTION_CONTENT));
204     EXPECT_CALL(rest, doPost(_, Eq(TEST_EVENT_CURI), A<const std::string&>())).Times(1);
205
206     ASSERT_NO_THROW(handler.accept(event));
207 }