2 #include "iotivity_mock.h"
3 #include "securitycontext.h"
4 #include "iotdevice_mock.h"
5 #include "mqclient_mock.h"
8 using namespace NetworkManager;
9 using ::testing::ReturnRef;
10 using ::testing::Return;
11 using ::testing::Throw;
13 using ::testing::StrEq;
15 using ::testing::DoAll;
16 using ::testing::SaveArg;
17 using ::testing::InvokeArgument;
19 TEST(TestSecurityContext, construct_wrong_param)
21 ASSERT_THROW(SecurityContext(nullptr), NMexception);
24 TEST(TestSecurityContext, getOwnedDevices)
27 SecurityContext sc(&iot);
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")));
37 EXPECT_THROW(sc.getOwnedDevices(), IoTInternalError);
38 EXPECT_THROW(sc.getOwnedDevices(), std::logic_error);
41 TEST(TestSecurityContext, getUnOwnedDevices)
44 SecurityContext sc(&iot);
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 = "";
54 std::vector<IoTDevicePtr> devs;
55 auto fake_del = [](IoTDeviceMock*) { };
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));
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));
82 const IoTDevicesMap& dev_map = sc.getUnOwnedDevices();
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();
100 virtual ~ICbCall() {}
101 virtual void call(NM_NotificationData data) = 0;
104 class CbCallMock: public ICbCall
107 MOCK_METHOD1(call, void(NM_NotificationData data));
110 void TestNotificationCb(NM_NotificationData data, void* user_data)
112 reinterpret_cast<ICbCall*>(user_data)->call(data);
117 bool operator==(const NM_NotificationData& o1, const NM_NotificationData& o2)
119 return o1.type == o2.type &&
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;
131 TEST(TestSecurityContext, subscribeNotifications)
133 const NM_NotificationType type = NT_Notify;
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";
144 MqClientMock mqclient;
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);
159 NM_NotificationData data {
172 EXPECT_CALL(iot, isSignedIn())
173 .WillOnce(Return(false))
174 .WillOnce(Return(true));
175 EXPECT_CALL(iot, getCloudAuthId())
177 .WillRepeatedly(ReturnRef(cloudAuthID));
178 EXPECT_CALL(iot, getMqHandler())
180 .WillRepeatedly(Return(&mqclient));
181 EXPECT_CALL(mqclient, subscribe(_, _))
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);
189 EXPECT_CALL(cbmock, call(Eq(data))).Times(1);
191 EXPECT_THROW(sc.subscribeNotifications(TestNotificationCb, &cbmock), IoTInternalError);
192 EXPECT_NO_THROW(sc.subscribeNotifications(TestNotificationCb, &cbmock));
196 TEST(TestSecurityContext, getAgentsList)
199 SecurityContext sc(&iot);
201 EXPECT_THROW(sc.getAgentsList(std::string{}), BadParameterException);
204 TEST(TestSecurityContext, getDeviceReport)
207 SecurityContext sc(&iot);
209 EXPECT_THROW(sc.getDeviceReport(std::string{}), BadParameterException);