replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / notification / unittest / NSProviderTest.cpp
1 //******************************************************************
2 //
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include <gtest/gtest.h>
22 #include <HippoMocks/hippomocks.h>
23 #include <atomic>
24 #include <functional>
25 #include <condition_variable>
26 #include <mutex>
27 #include <chrono>
28
29 #include "NSProviderInterface.h"
30 #include "NSConsumerSimulator.h"
31 #include "NSCommon.h"
32
33 namespace
34 {
35     std::atomic_bool g_isStartedStack(false);
36
37     std::chrono::milliseconds g_waitForResponse(500);
38
39     std::condition_variable responseCon;
40     std::mutex mutexForCondition;
41
42     NSConsumerSimulator g_consumerSimul;
43     char * g_consumerID;
44     char g_title[100];
45     char g_body[100];
46     char g_sourceName[100];
47 }
48
49 class TestWithMock: public testing::Test
50 {
51 public:
52     MockRepository mocks;
53
54 protected:
55     virtual ~TestWithMock() noexcept(noexcept(std::declval<Test>().~Test())) {}
56
57     virtual void TearDown() {
58         try
59         {
60             mocks.VerifyAll();
61         }
62         catch (...)
63         {
64             mocks.reset();
65             throw;
66         }
67     }
68 };
69
70 class NotificationProviderTest : public TestWithMock
71 {
72 public:
73     NotificationProviderTest() = default;
74     ~NotificationProviderTest() = default;
75
76     static void NSRequestedSubscribeCallbackEmpty(NSConsumer *)
77     {
78     }
79
80     static void NSSyncCallbackEmpty(NSSyncInfo *)
81     {
82     }
83
84     static void NSMessageCallbackFromConsumerEmpty(
85             const int &, const std::string &, const std::string &, const std::string &)
86     {
87     }
88
89     static void NSSyncCallbackFromConsumerEmpty(int, int)
90     {
91     }
92
93 protected:
94
95     void SetUp()
96     {
97         TestWithMock::SetUp();
98
99         if (g_isStartedStack == false)
100         {
101             OC::PlatformConfig cfg
102             {
103                 OC::ServiceType::InProc,
104                 OC::ModeType::Both,
105                 "0.0.0.0",
106                 0,
107                 OC::QualityOfService::HighQos
108             };
109             OC::OCPlatform::Configure(cfg);
110
111             try
112             {
113                 OC::OCPlatform::stopPresence();
114             }
115             catch (...)
116             {
117
118             }
119
120             g_isStartedStack = true;
121
122             strncpy(g_title, "Title", strlen("Title"));
123             strncpy(g_body, "ContentText", strlen("ContentText"));
124             strncpy(g_sourceName, "OIC", strlen("OIC"));
125         }
126
127     }
128
129     void TearDown()
130     {
131         TestWithMock::TearDown();
132     }
133
134 };
135
136 TEST_F(NotificationProviderTest, StartProviderPositiveWithNSPolicyTrue)
137 {
138     NSProviderConfig config;
139     config.subRequestCallback = NSRequestedSubscribeCallbackEmpty;
140     config.syncInfoCallback = NSSyncCallbackEmpty;
141     config.subControllability = true;
142     config.userInfo = strdup("user1");
143
144     NSResult ret = NSStartProvider(config);
145
146     std::unique_lock< std::mutex > lock{ mutexForCondition };
147     responseCon.wait_for(lock, g_waitForResponse);
148
149     EXPECT_EQ(ret, NS_OK);
150 }
151
152 TEST_F(NotificationProviderTest, StopProviderPositive)
153 {
154     NSResult ret = NSStopProvider();
155
156     std::unique_lock< std::mutex > lock{ mutexForCondition };
157     responseCon.wait_for(lock, g_waitForResponse);
158
159     EXPECT_EQ(ret, NS_OK);
160 }
161
162 TEST_F(NotificationProviderTest, StartProviderPositiveWithNSPolicyFalse)
163 {
164     NSProviderConfig config;
165     config.subRequestCallback = NSRequestedSubscribeCallbackEmpty;
166     config.syncInfoCallback = NSSyncCallbackEmpty;
167     config.subControllability = false;
168     config.userInfo = NULL;
169
170     NSResult ret = NSStartProvider(config);
171
172     std::unique_lock< std::mutex > lock{ mutexForCondition };
173     responseCon.wait_for(lock, g_waitForResponse);
174     g_consumerSimul.findProvider();
175
176     responseCon.wait_for(lock, g_waitForResponse);
177     NSStopProvider();
178     EXPECT_EQ(ret, NS_OK);
179 }
180
181 TEST_F(NotificationProviderTest, ExpectCallbackWhenReceiveSubscribeRequestWithAccepterProvider)
182 {
183     g_consumerID = NULL;
184     mocks.OnCallFunc(NSRequestedSubscribeCallbackEmpty).Do(
185             [](NSConsumer * consumer)
186             {
187                 g_consumerID = strdup(consumer->consumerId);
188                 responseCon.notify_all();
189             });
190
191     NSProviderConfig config;
192     config.subRequestCallback = NSRequestedSubscribeCallbackEmpty;
193     config.syncInfoCallback = NSSyncCallbackEmpty;
194     config.subControllability = true;
195     config.userInfo = NULL;
196
197     NSStartProvider(config);
198
199     {
200         std::unique_lock< std::mutex > lock{ mutexForCondition };
201         responseCon.wait_for(lock, g_waitForResponse);
202     }
203
204     g_consumerSimul.setCallback(NSMessageCallbackFromConsumerEmpty,
205             NSSyncCallbackFromConsumerEmpty);
206     g_consumerSimul.findProvider();
207
208     std::unique_lock< std::mutex > lock{ mutexForCondition };
209     responseCon.wait_for(lock, g_waitForResponse);
210
211     ASSERT_NE(nullptr, g_consumerID) << "error: discovery failure";
212 }
213
214 TEST_F(NotificationProviderTest, NeverCallNotifyOnConsumerByAcceptIsFalse)
215 {
216     bool expectTrue = true;
217     int msgID;
218
219     ASSERT_NE(nullptr, g_consumerID) << "error: discovery failure";
220
221     mocks.OnCallFunc(NSMessageCallbackFromConsumerEmpty).Do(
222             [& expectTrue, &msgID](const int &id, const std::string&, const std::string&, const std::string&)
223             {
224                 if (id == msgID)
225                 {
226                     expectTrue = false;
227                 }
228             });
229
230     NSAcceptSubscription(g_consumerID, false);
231
232     NSMessage * msg = NSCreateMessage();
233     if(msg)
234     {
235         msgID = (int)msg->messageId;
236         msg->title = g_title;
237         msg->contentText = g_body;
238         msg->sourceName = g_sourceName;
239         NSSendMessage(msg);
240
241         std::unique_lock< std::mutex > lock{ mutexForCondition };
242         responseCon.wait_for(lock, g_waitForResponse);
243
244         EXPECT_EQ(expectTrue, true);
245
246         NSAcceptSubscription(g_consumerID, true);
247         responseCon.wait_for(lock, g_waitForResponse);
248     }
249     else
250     {
251         EXPECT_EQ(expectTrue, false);
252     }
253 }
254
255 TEST_F(NotificationProviderTest, ExpectCallNotifyOnConsumerByAcceptIsTrue)
256 {
257     int msgID;
258
259     ASSERT_NE(nullptr, g_consumerID) << "error: discovery failure";
260
261     mocks.ExpectCallFunc(NSMessageCallbackFromConsumerEmpty).Do(
262             [&msgID](const int &id, const std::string&, const std::string&, const std::string&)
263             {
264                 if (id == msgID)
265                 {
266                     responseCon.notify_all();
267                 }
268             });
269
270     NSMessage * msg = NSCreateMessage();
271     if(msg)
272     {
273         msgID = (int)msg->messageId;
274         msg->title = g_title;
275         msg->contentText = g_body;
276         msg->sourceName = g_sourceName;
277         NSSendMessage(msg);
278
279         std::unique_lock< std::mutex > lock{ mutexForCondition };
280         responseCon.wait(lock);
281     }
282 }
283
284 TEST_F(NotificationProviderTest, ExpectCallbackSyncOnReadToConsumer)
285 {
286     int id;
287
288     ASSERT_NE(nullptr, g_consumerID) << "error: discovery failure";
289
290     mocks.ExpectCallFunc(NSSyncCallbackFromConsumerEmpty).Do(
291             [& id](int & type, int &syncId)
292             {
293                 if (syncId == id && type == NS_SYNC_READ)
294                 {
295                     responseCon.notify_all();
296                 }
297             });
298
299     NSMessage * msg = NSCreateMessage();
300     if(msg)
301     {
302         id = (int)msg->messageId;
303         msg->title = g_title;
304         msg->contentText = g_body;
305         msg->sourceName = g_sourceName;
306
307         NSProviderSendSyncInfo(msg->messageId, NS_SYNC_READ);
308         std::unique_lock< std::mutex > lock{ mutexForCondition };
309         responseCon.wait(lock);
310     }
311 }
312
313 TEST_F(NotificationProviderTest, ExpectCallbackSyncOnReadFromConsumer)
314 {
315     int type = NS_SYNC_READ;
316     int id;
317
318     ASSERT_NE(nullptr, g_consumerID) << "error: discovery failure";
319
320     mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
321             [& id](NSSyncInfo * sync)
322             {
323                 if ((int)sync->messageId == id && sync->state == NS_SYNC_READ)
324                 {
325                     responseCon.notify_all();
326                 }
327             });
328
329     NSMessage * msg = NSCreateMessage();
330     if(msg)
331     {
332         id = (int)msg->messageId;
333         msg->title = g_title;
334         msg->contentText = g_body;
335         msg->sourceName = g_sourceName;
336
337         g_consumerSimul.syncToProvider(type, id, msg->providerId);
338         std::unique_lock< std::mutex > lock{ mutexForCondition };
339         responseCon.wait(lock);
340     }
341 }
342
343 TEST_F(NotificationProviderTest, ExpectEqualAddedTopicsAndRegisteredTopics)
344 {
345     std::string str("TEST1");
346     std::string str2("TEST2");
347     NSProviderRegisterTopic(str.c_str());
348     NSProviderRegisterTopic(str2.c_str());
349
350     bool isSame = true;
351     NSTopicLL * topics = NSProviderGetTopics();
352
353     if(!topics)
354     {
355         isSame = false;
356     }
357     else
358     {
359         NSTopicLL * iter = topics;
360         std::string compStr(iter->topicName);
361         std::string compStr2(iter->next->topicName);
362
363         if(str.compare(compStr) == 0 && str2.compare(compStr2) == 0)
364         {
365             isSame = true;
366         }
367     }
368
369     NSProviderUnregisterTopic(str.c_str());
370     NSProviderUnregisterTopic(str2.c_str());
371     EXPECT_EQ(isSame, true);
372 }
373
374 TEST_F(NotificationProviderTest, ExpectEqualUnregisteredTopicsAndRegisteredTopics)
375 {
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());
381
382     bool isSame = true;
383     NSTopicLL * topics = NSProviderGetTopics();
384
385     if(!topics)
386     {
387         isSame = false;
388     }
389     else
390     {
391         NSTopicLL * iter = topics;
392         std::string compStr(iter->topicName);
393
394         if(str.compare(compStr) == 0)
395         {
396             isSame = true;
397         }
398     }
399
400     NSProviderUnregisterTopic(str.c_str());
401     EXPECT_EQ(isSame, true);
402 }
403
404 TEST_F(NotificationProviderTest, ExpectEqualSetConsumerTopicsAndGetConsumerTopics)
405 {
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());
411
412     std::unique_lock< std::mutex > lock{ mutexForCondition };
413     responseCon.wait_for(lock, g_waitForResponse);
414
415     bool isSame = false;
416     NSTopicLL * topics = NSProviderGetConsumerTopics(g_consumerID);
417
418     if(!topics)
419     {
420         isSame = false;
421     }
422     else
423     {
424         NSTopicLL * firstData = topics;
425         NSTopicLL * secondData = firstData->next;
426
427         if(str.compare(firstData->topicName) == 0 && str2.compare(secondData->topicName) == 0
428                 && ((int)firstData->state) == 1 && ((int)secondData->state) == 0)
429         {
430             isSame = true;
431         }
432     }
433
434     NSProviderUnregisterTopic(str.c_str());
435     NSProviderUnregisterTopic(str2.c_str());
436     EXPECT_EQ(isSame, true);
437 }
438
439 TEST_F(NotificationProviderTest, ExpectEqualUnSetConsumerTopicsAndGetConsumerTopics)
440 {
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());
448
449     std::unique_lock< std::mutex > lock{ mutexForCondition };
450     responseCon.wait_for(lock, g_waitForResponse);
451
452     bool isSame = false;
453
454     ASSERT_NE(nullptr, g_consumerID) << "error: discovery failure";
455
456     NSTopicLL * topics = NSProviderGetConsumerTopics(g_consumerID);
457
458     if(!topics)
459     {
460         isSame = false;
461     }
462     else
463     {
464         NSTopicLL * firstData = topics;
465         NSTopicLL * secondData = firstData->next;
466
467         if(str.compare(firstData->topicName) == 0 && str2.compare(secondData->topicName) == 0
468                 && ((int)firstData->state) == 0 && ((int)secondData->state) == 1)
469         {
470             isSame = true;
471         }
472     }
473
474     NSProviderUnregisterTopic(str.c_str());
475     NSProviderUnregisterTopic(str2.c_str());
476     EXPECT_EQ(isSame, true);
477 }
478
479 TEST_F(NotificationProviderTest, ExpectFailAcceptSubscription)
480 {
481     NSResult result;
482     result = NS_SUCCESS;
483     result = NSAcceptSubscription(NULL, true);
484     result = NSAcceptSubscription("\0", true);
485
486     EXPECT_EQ(result, NS_FAIL);
487 }
488
489 TEST_F(NotificationProviderTest, ExpectFailSendMessage)
490 {
491     NSResult result;
492     result = NS_SUCCESS;
493     result = NSSendMessage(NULL);
494
495     EXPECT_EQ(result, NS_FAIL);
496 }
497
498 TEST_F(NotificationProviderTest, ExpectFailRegisterTopic)
499 {
500     NSResult result;
501     result = NS_SUCCESS;
502     result = NSProviderRegisterTopic(NULL);
503     result = NSProviderRegisterTopic("\0");
504
505     EXPECT_EQ(result, NS_FAIL);
506 }
507
508 TEST_F(NotificationProviderTest, ExpectFailUnregisterTopic)
509 {
510     NSResult result;
511     result = NS_SUCCESS;
512     result = NSProviderUnregisterTopic(NULL);
513     result = NSProviderUnregisterTopic("\0");
514
515     EXPECT_EQ(result, NS_FAIL);
516 }
517
518 TEST_F(NotificationProviderTest, ExpectFailGetConsumerTopics)
519 {
520     NSTopicLL topic;
521     NSTopicLL * topicLL = &topic;
522
523     topicLL = NSProviderGetConsumerTopics(NULL);
524     topicLL = NSProviderGetConsumerTopics("\0");
525
526     EXPECT_EQ(topicLL, (NSTopicLL *)NULL);
527 }
528
529 TEST_F(NotificationProviderTest, ExpectFailSetConsumerTopics)
530 {
531     NSResult result;
532     result = NS_SUCCESS;
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");
541
542     EXPECT_EQ(result, NS_FAIL);
543 }
544
545 TEST_F(NotificationProviderTest, ExpectFailUnsetConsumerTopics)
546 {
547     NSResult result;
548     result = NS_SUCCESS;
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");
557
558     EXPECT_EQ(result, NS_FAIL);
559 }
560
561 TEST_F(NotificationProviderTest, CancelObserves)
562 {
563     bool ret = g_consumerSimul.cancelObserves();
564
565     std::unique_lock< std::mutex > lock{ mutexForCondition };
566     responseCon.wait_for(lock, std::chrono::milliseconds(3000));
567
568     EXPECT_EQ(ret, true);
569 }