1 //******************************************************************
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include <gtest/gtest.h>
22 #include <HippoMocks/hippomocks.h>
25 #include <condition_variable>
29 #include "NSProviderInterface.h"
30 #include "NSConsumerSimulator.h"
35 std::atomic_bool g_isStartedStack(false);
37 std::chrono::milliseconds g_waitForResponse(500);
39 std::condition_variable responseCon;
40 std::mutex mutexForCondition;
42 NSConsumerSimulator g_consumerSimul;
46 char g_sourceName[100];
49 class TestWithMock: public testing::Test
55 virtual ~TestWithMock() noexcept(noexcept(std::declval<Test>().~Test())) {}
57 virtual void TearDown() {
70 class NotificationProviderTest : public TestWithMock
73 NotificationProviderTest() = default;
74 ~NotificationProviderTest() = default;
76 static void NSRequestedSubscribeCallbackEmpty(NSConsumer *)
80 static void NSSyncCallbackEmpty(NSSyncInfo *)
84 static void NSMessageCallbackFromConsumerEmpty(
85 const int &, const std::string &, const std::string &, const std::string &)
89 static void NSSyncCallbackFromConsumerEmpty(int, int)
97 TestWithMock::SetUp();
99 if (g_isStartedStack == false)
101 OC::PlatformConfig cfg
103 OC::ServiceType::InProc,
107 OC::QualityOfService::HighQos
109 OC::OCPlatform::Configure(cfg);
113 OC::OCPlatform::stopPresence();
120 g_isStartedStack = true;
122 strncpy(g_title, "Title", strlen("Title"));
123 strncpy(g_body, "ContentText", strlen("ContentText"));
124 strncpy(g_sourceName, "OIC", strlen("OIC"));
131 TestWithMock::TearDown();
136 TEST_F(NotificationProviderTest, StartProviderPositiveWithNSPolicyTrue)
138 NSProviderConfig config;
139 config.subRequestCallback = NSRequestedSubscribeCallbackEmpty;
140 config.syncInfoCallback = NSSyncCallbackEmpty;
141 config.subControllability = true;
142 config.userInfo = strdup("user1");
144 NSResult ret = NSStartProvider(config);
146 std::unique_lock< std::mutex > lock{ mutexForCondition };
147 responseCon.wait_for(lock, g_waitForResponse);
149 EXPECT_EQ(ret, NS_OK);
152 TEST_F(NotificationProviderTest, StopProviderPositive)
154 NSResult ret = NSStopProvider();
156 std::unique_lock< std::mutex > lock{ mutexForCondition };
157 responseCon.wait_for(lock, g_waitForResponse);
159 EXPECT_EQ(ret, NS_OK);
162 TEST_F(NotificationProviderTest, StartProviderPositiveWithNSPolicyFalse)
164 NSProviderConfig config;
165 config.subRequestCallback = NSRequestedSubscribeCallbackEmpty;
166 config.syncInfoCallback = NSSyncCallbackEmpty;
167 config.subControllability = false;
168 config.userInfo = NULL;
170 NSResult ret = NSStartProvider(config);
172 std::unique_lock< std::mutex > lock{ mutexForCondition };
173 responseCon.wait_for(lock, g_waitForResponse);
174 g_consumerSimul.findProvider();
176 responseCon.wait_for(lock, g_waitForResponse);
178 EXPECT_EQ(ret, NS_OK);
181 TEST_F(NotificationProviderTest, ExpectCallbackWhenReceiveSubscribeRequestWithAccepterProvider)
184 mocks.OnCallFunc(NSRequestedSubscribeCallbackEmpty).Do(
185 [](NSConsumer * consumer)
187 g_consumerID = strdup(consumer->consumerId);
188 responseCon.notify_all();
191 NSProviderConfig config;
192 config.subRequestCallback = NSRequestedSubscribeCallbackEmpty;
193 config.syncInfoCallback = NSSyncCallbackEmpty;
194 config.subControllability = true;
195 config.userInfo = NULL;
197 NSStartProvider(config);
200 std::unique_lock< std::mutex > lock{ mutexForCondition };
201 responseCon.wait_for(lock, g_waitForResponse);
204 g_consumerSimul.setCallback(NSMessageCallbackFromConsumerEmpty,
205 NSSyncCallbackFromConsumerEmpty);
206 g_consumerSimul.findProvider();
208 std::unique_lock< std::mutex > lock{ mutexForCondition };
209 responseCon.wait_for(lock, g_waitForResponse);
211 ASSERT_NE(nullptr, g_consumerID) << "error: discovery failure";
214 TEST_F(NotificationProviderTest, NeverCallNotifyOnConsumerByAcceptIsFalse)
216 bool expectTrue = true;
219 ASSERT_NE(nullptr, g_consumerID) << "error: discovery failure";
221 mocks.OnCallFunc(NSMessageCallbackFromConsumerEmpty).Do(
222 [& expectTrue, &msgID](const int &id, const std::string&, const std::string&, const std::string&)
230 NSAcceptSubscription(g_consumerID, false);
232 NSMessage * msg = NSCreateMessage();
235 msgID = (int)msg->messageId;
236 msg->title = g_title;
237 msg->contentText = g_body;
238 msg->sourceName = g_sourceName;
241 std::unique_lock< std::mutex > lock{ mutexForCondition };
242 responseCon.wait_for(lock, g_waitForResponse);
244 EXPECT_EQ(expectTrue, true);
246 NSAcceptSubscription(g_consumerID, true);
247 responseCon.wait_for(lock, g_waitForResponse);
251 EXPECT_EQ(expectTrue, false);
255 TEST_F(NotificationProviderTest, ExpectCallNotifyOnConsumerByAcceptIsTrue)
259 ASSERT_NE(nullptr, g_consumerID) << "error: discovery failure";
261 mocks.ExpectCallFunc(NSMessageCallbackFromConsumerEmpty).Do(
262 [&msgID](const int &id, const std::string&, const std::string&, const std::string&)
266 responseCon.notify_all();
270 NSMessage * msg = NSCreateMessage();
273 msgID = (int)msg->messageId;
274 msg->title = g_title;
275 msg->contentText = g_body;
276 msg->sourceName = g_sourceName;
279 std::unique_lock< std::mutex > lock{ mutexForCondition };
280 responseCon.wait(lock);
284 TEST_F(NotificationProviderTest, ExpectCallbackSyncOnReadToConsumer)
288 ASSERT_NE(nullptr, g_consumerID) << "error: discovery failure";
290 mocks.ExpectCallFunc(NSSyncCallbackFromConsumerEmpty).Do(
291 [& id](int & type, int &syncId)
293 if (syncId == id && type == NS_SYNC_READ)
295 responseCon.notify_all();
299 NSMessage * msg = NSCreateMessage();
302 id = (int)msg->messageId;
303 msg->title = g_title;
304 msg->contentText = g_body;
305 msg->sourceName = g_sourceName;
307 NSProviderSendSyncInfo(msg->messageId, NS_SYNC_READ);
308 std::unique_lock< std::mutex > lock{ mutexForCondition };
309 responseCon.wait(lock);
313 TEST_F(NotificationProviderTest, ExpectCallbackSyncOnReadFromConsumer)
315 int type = NS_SYNC_READ;
318 ASSERT_NE(nullptr, g_consumerID) << "error: discovery failure";
320 mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
321 [& id](NSSyncInfo * sync)
323 if ((int)sync->messageId == id && sync->state == NS_SYNC_READ)
325 responseCon.notify_all();
329 NSMessage * msg = NSCreateMessage();
332 id = (int)msg->messageId;
333 msg->title = g_title;
334 msg->contentText = g_body;
335 msg->sourceName = g_sourceName;
337 g_consumerSimul.syncToProvider(type, id, msg->providerId);
338 std::unique_lock< std::mutex > lock{ mutexForCondition };
339 responseCon.wait(lock);
343 TEST_F(NotificationProviderTest, ExpectEqualAddedTopicsAndRegisteredTopics)
345 std::string str("TEST1");
346 std::string str2("TEST2");
347 NSProviderRegisterTopic(str.c_str());
348 NSProviderRegisterTopic(str2.c_str());
351 NSTopicLL * topics = NSProviderGetTopics();
359 NSTopicLL * iter = topics;
360 std::string compStr(iter->topicName);
361 std::string compStr2(iter->next->topicName);
363 if(str.compare(compStr) == 0 && str2.compare(compStr2) == 0)
369 NSProviderUnregisterTopic(str.c_str());
370 NSProviderUnregisterTopic(str2.c_str());
371 EXPECT_EQ(isSame, true);
374 TEST_F(NotificationProviderTest, ExpectEqualUnregisteredTopicsAndRegisteredTopics)
376 std::string str("TEST1");
377 std::string str2("TEST2");
378 NSProviderRegisterTopic(str.c_str());
379 NSProviderRegisterTopic(str2.c_str());
380 NSProviderUnregisterTopic(str2.c_str());
383 NSTopicLL * topics = NSProviderGetTopics();
391 NSTopicLL * iter = topics;
392 std::string compStr(iter->topicName);
394 if(str.compare(compStr) == 0)
400 NSProviderUnregisterTopic(str.c_str());
401 EXPECT_EQ(isSame, true);
404 TEST_F(NotificationProviderTest, ExpectEqualSetConsumerTopicsAndGetConsumerTopics)
406 std::string str("TEST1");
407 std::string str2("TEST2");
408 NSProviderRegisterTopic(str.c_str());
409 NSProviderRegisterTopic(str2.c_str());
410 NSProviderSetConsumerTopic(g_consumerID, str.c_str());
412 std::unique_lock< std::mutex > lock{ mutexForCondition };
413 responseCon.wait_for(lock, g_waitForResponse);
416 NSTopicLL * topics = NSProviderGetConsumerTopics(g_consumerID);
424 NSTopicLL * firstData = topics;
425 NSTopicLL * secondData = firstData->next;
427 if(str.compare(firstData->topicName) == 0 && str2.compare(secondData->topicName) == 0
428 && ((int)firstData->state) == 1 && ((int)secondData->state) == 0)
434 NSProviderUnregisterTopic(str.c_str());
435 NSProviderUnregisterTopic(str2.c_str());
436 EXPECT_EQ(isSame, true);
439 TEST_F(NotificationProviderTest, ExpectEqualUnSetConsumerTopicsAndGetConsumerTopics)
441 std::string str("TEST1");
442 std::string str2("TEST2");
443 NSProviderRegisterTopic(str.c_str());
444 NSProviderRegisterTopic(str2.c_str());
445 NSProviderSetConsumerTopic(g_consumerID, str.c_str());
446 NSProviderSetConsumerTopic(g_consumerID, str2.c_str());
447 NSProviderUnsetConsumerTopic(g_consumerID, str.c_str());
449 std::unique_lock< std::mutex > lock{ mutexForCondition };
450 responseCon.wait_for(lock, g_waitForResponse);
454 ASSERT_NE(nullptr, g_consumerID) << "error: discovery failure";
456 NSTopicLL * topics = NSProviderGetConsumerTopics(g_consumerID);
464 NSTopicLL * firstData = topics;
465 NSTopicLL * secondData = firstData->next;
467 if(str.compare(firstData->topicName) == 0 && str2.compare(secondData->topicName) == 0
468 && ((int)firstData->state) == 0 && ((int)secondData->state) == 1)
474 NSProviderUnregisterTopic(str.c_str());
475 NSProviderUnregisterTopic(str2.c_str());
476 EXPECT_EQ(isSame, true);
479 TEST_F(NotificationProviderTest, ExpectFailAcceptSubscription)
483 result = NSAcceptSubscription(NULL, true);
484 result = NSAcceptSubscription("\0", true);
486 EXPECT_EQ(result, NS_FAIL);
489 TEST_F(NotificationProviderTest, ExpectFailSendMessage)
493 result = NSSendMessage(NULL);
495 EXPECT_EQ(result, NS_FAIL);
498 TEST_F(NotificationProviderTest, ExpectFailRegisterTopic)
502 result = NSProviderRegisterTopic(NULL);
503 result = NSProviderRegisterTopic("\0");
505 EXPECT_EQ(result, NS_FAIL);
508 TEST_F(NotificationProviderTest, ExpectFailUnregisterTopic)
512 result = NSProviderUnregisterTopic(NULL);
513 result = NSProviderUnregisterTopic("\0");
515 EXPECT_EQ(result, NS_FAIL);
518 TEST_F(NotificationProviderTest, ExpectFailGetConsumerTopics)
521 NSTopicLL * topicLL = &topic;
523 topicLL = NSProviderGetConsumerTopics(NULL);
524 topicLL = NSProviderGetConsumerTopics("\0");
526 EXPECT_EQ(topicLL, (NSTopicLL *)NULL);
529 TEST_F(NotificationProviderTest, ExpectFailSetConsumerTopics)
533 result = NSProviderSetConsumerTopic(NULL, NULL);
534 result = NSProviderSetConsumerTopic(NULL, "\0");
535 result = NSProviderSetConsumerTopic("\0", NULL);
536 result = NSProviderSetConsumerTopic("\0", "\0");
537 result = NSProviderSetConsumerTopic("abc", NULL);
538 result = NSProviderSetConsumerTopic(NULL, "abc");
539 result = NSProviderSetConsumerTopic("abc", "\0");
540 result = NSProviderSetConsumerTopic("\0", "abc");
542 EXPECT_EQ(result, NS_FAIL);
545 TEST_F(NotificationProviderTest, ExpectFailUnsetConsumerTopics)
549 result = NSProviderUnsetConsumerTopic(NULL, NULL);
550 result = NSProviderUnsetConsumerTopic(NULL, "\0");
551 result = NSProviderUnsetConsumerTopic("\0", NULL);
552 result = NSProviderUnsetConsumerTopic("\0", "\0");
553 result = NSProviderUnsetConsumerTopic("abc", NULL);
554 result = NSProviderUnsetConsumerTopic(NULL, "abc");
555 result = NSProviderUnsetConsumerTopic("abc", "\0");
556 result = NSProviderUnsetConsumerTopic("\0", "abc");
558 EXPECT_EQ(result, NS_FAIL);
561 TEST_F(NotificationProviderTest, CancelObserves)
563 bool ret = g_consumerSimul.cancelObserves();
565 std::unique_lock< std::mutex > lock{ mutexForCondition };
566 responseCon.wait_for(lock, std::chrono::milliseconds(3000));
568 EXPECT_EQ(ret, true);