Merge pull request #41 from RS7-SECIOTSRK/develop
[platform/core/security/suspicious-activity-monitor.git] / device_core / utest / test_securitycontext.cpp
1 #include <cstring>
2 #include "iotivity_mock.h"
3 #include "securitycontext.h"
4 #include "iotdevice_mock.h"
5 #include "mqclient_mock.h"
6 #include "nmlib.h"
7
8 using namespace NetworkManager;
9 using ::testing::ReturnRef;
10 using ::testing::Return;
11 using ::testing::Throw;
12 using ::testing::Eq;
13 using ::testing::StrEq;
14 using ::testing::_;
15 using ::testing::DoAll;
16 using ::testing::SaveArg;
17 using ::testing::InvokeArgument;
18
19 TEST(TestSecurityContext, construct_wrong_param)
20 {
21     ASSERT_THROW(SecurityContext(nullptr), NMexception);
22 }
23
24 TEST(TestSecurityContext, getOwnedDevices)
25 {
26     IoTivityMock iot;
27     SecurityContext sc(&iot);
28
29     EXPECT_CALL(iot, isSignedIn())
30             .WillOnce(Return(false))
31             .WillOnce(Return(true));
32     EXPECT_CALL(iot, isConnected())
33             .WillOnce(Return(false));
34     EXPECT_CALL(iot, signIn())
35             .WillOnce(Throw(std::logic_error("error")));
36
37     EXPECT_THROW(sc.getOwnedDevices(), IoTInternalError);
38     EXPECT_THROW(sc.getOwnedDevices(), std::logic_error);
39 }
40
41 TEST(TestSecurityContext, getUnOwnedDevices)
42 {
43     IoTivityMock iot;
44     SecurityContext sc(&iot);
45     IoTDeviceMock dev1;
46     IoTDeviceMock dev2;
47     IoTDeviceMock dev3;
48     IoTDeviceMock dev4;
49
50     const std::string uuid_dev1 = "11111111-1111-1111-1111-111111111111"; // <= the same uid as for the third
51     const std::string uuid_dev2 = "22222222-2222-2222-2222-222222222222";
52     const std::string uuid_dev4 = "";
53
54     std::vector<IoTDevicePtr> devs;
55     auto fake_del = [](IoTDeviceMock*) { };
56
57     devs.push_back(std::shared_ptr<IoTDeviceMock>(&dev1, fake_del));
58     devs.push_back(std::shared_ptr<IoTDeviceMock>(&dev2, fake_del));
59     devs.push_back(std::shared_ptr<IoTDeviceMock>(&dev3, fake_del));
60     devs.push_back(std::shared_ptr<IoTDeviceMock>(&dev4, fake_del));
61
62     EXPECT_CALL(iot, findResourceList(Eq(true), StrEq(HubClient::RESOURCE_TYPE), StrEq(OC_RSRVD_WELL_KNOWN_URI), Eq(CT_DEFAULT)))
63             .WillOnce(Return(std::vector<std::shared_ptr<OC::OCResource>>{}));
64     EXPECT_CALL(iot, findDevices(Eq(false), Eq(CT_ADAPTER_IP), _))
65             .WillOnce(Return(devs));
66     EXPECT_CALL(dev1, getUUID())
67             .WillRepeatedly(ReturnRef(uuid_dev1));
68     EXPECT_CALL(dev2, getUUID())
69             .WillRepeatedly(ReturnRef(uuid_dev2));
70     EXPECT_CALL(dev3, getUUID())
71             .WillRepeatedly(ReturnRef(uuid_dev1));
72     EXPECT_CALL(dev4, getUUID())
73             .WillRepeatedly(ReturnRef(uuid_dev4));
74     EXPECT_CALL(dev1, isCloudAccessibility())
75             .WillOnce(Return(true));
76     EXPECT_CALL(dev2, isCloudAccessibility())
77             .WillOnce(Return(false));
78     EXPECT_CALL(dev3, isCloudAccessibility())
79             .WillOnce(Return(true));
80
81     try {
82         const IoTDevicesMap& dev_map = sc.getUnOwnedDevices();
83
84         ASSERT_EQ(1, dev_map.size());
85         EXPECT_EQ(dev_map.begin()->first, uuid_dev1);
86         EXPECT_THROW(sc.getIoTDevice(uuid_dev2), IoTInternalError);
87         auto d = sc.getIoTDevice(uuid_dev1);
88         EXPECT_EQ(d->getUUID(), uuid_dev1);
89     } catch (std::exception& e) {
90         FAIL() << "Exception: " << e.what();
91     }
92 }
93
94 namespace
95 {
96
97 class ICbCall
98 {
99 public:
100     virtual ~ICbCall() {}
101     virtual void call(NM_NotificationData data) = 0;
102 };
103
104 class CbCallMock: public ICbCall
105 {
106 public:
107     MOCK_METHOD1(call, void(NM_NotificationData data));
108 };
109
110 void TestNotificationCb(NM_NotificationData data, void* user_data)
111 {
112     reinterpret_cast<ICbCall*>(user_data)->call(data);
113 }
114
115 }
116
117 bool operator==(const NM_NotificationData& o1, const NM_NotificationData& o2)
118 {
119     return o1.type == o2.type &&
120             o1.pid == o2.pid &&
121             o1.time == o2.time &&
122             strcmp(o1.title, o2.title) == 0 &&
123             strcmp(o1.message, o2.message) == 0 &&
124             strcmp(o1.duid, o2.duid) == 0 &&
125             strcmp(o1.policy, o2.policy) == 0 &&
126             strcmp(o1.appname, o2.appname) == 0 &&
127             strcmp(o1.parentUuid, o2.parentUuid) == 0 &&
128             o1.callbackState == o2.callbackState;
129 }
130
131 TEST(TestSecurityContext, subscribeNotifications)
132 {
133     const NM_NotificationType type = NT_Notify;
134     const int pid = 555;
135     const int time = 11223344;
136     const std::string title = "title";
137     const std::string message = "message";
138     const std::string duid = "duid";
139     const std::string policy = "policy";
140     const std::string appname = "appname";
141     const std::string parentUuid = "parentUuid";
142     const std::string cloudAuthID = "1";
143     IoTivityMock iot;
144     MqClientMock mqclient;
145     CbCallMock cbmock;
146     SecurityContext sc(&iot);
147     OC::HeaderOptions header_opts;
148     OC::OCRepresentation rep;
149     rep.setValue("type", int(type));
150     rep.setValue("pid", pid);
151     rep.setValue("time", time);
152     rep.setValue("title", title);
153     rep.setValue("message", message);
154     rep.setValue("duid", duid);
155     rep.setValue("policy", policy);
156     rep.setValue("appname", appname);
157     rep.setValue("parentUuid", parentUuid);
158
159     NM_NotificationData data {
160         type,
161         pid,
162         time,
163         title.c_str(),
164         message.c_str(),
165         duid.c_str(),
166         policy.c_str(),
167         appname.c_str(),
168         parentUuid.c_str(),
169         NORMAL_CALLBACK
170     };
171
172     EXPECT_CALL(iot, isSignedIn())
173             .WillOnce(Return(false))
174             .WillOnce(Return(true));
175     EXPECT_CALL(iot, getCloudAuthId())
176             .Times(2)
177             .WillRepeatedly(ReturnRef(cloudAuthID));
178     EXPECT_CALL(iot, getMqHandler())
179             .Times(2)
180             .WillRepeatedly(Return(&mqclient));
181     EXPECT_CALL(mqclient, subscribe(_, _))
182             .WillOnce(DoAll(
183                           InvokeArgument<1>(header_opts, rep, OC_STACK_COMM_ERROR, 0),
184                           InvokeArgument<1>(header_opts, rep, OC_STACK_OK, 0),
185                           InvokeArgument<1>(header_opts, rep, OC_STACK_OK, 1),
186                           InvokeArgument<1>(header_opts, rep, OC_STACK_OK, MAX_SEQUENCE_NUMBER + 1)));
187     EXPECT_CALL(mqclient, unsubscribe(_)).Times(1);
188
189     EXPECT_CALL(cbmock, call(Eq(data))).Times(1);
190
191     EXPECT_THROW(sc.subscribeNotifications(TestNotificationCb, &cbmock), IoTInternalError);
192     EXPECT_NO_THROW(sc.subscribeNotifications(TestNotificationCb, &cbmock));
193 }
194
195
196 TEST(TestSecurityContext, getAgentsList)
197 {
198     IoTivityMock iot;
199     SecurityContext sc(&iot);
200
201     EXPECT_THROW(sc.getAgentsList(std::string{}), BadParameterException);
202 }
203
204 TEST(TestSecurityContext, getDeviceReport)
205 {
206     IoTivityMock iot;
207     SecurityContext sc(&iot);
208
209     EXPECT_THROW(sc.getDeviceReport(std::string{}), BadParameterException);
210 }