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