Merge test folder of test branch
[iotivity.git] / test / src / tc / ca / gtest / src / stc / CANetworkCommonTest.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 "CAHelper.h"
22
23 class CANetworkCommonTest_stc: public ::testing::Test
24 {
25 protected:
26     CAHelper m_caHelper;
27     CAResult_t m_result;
28
29     virtual void SetUp()
30     {
31         CommonTestUtil::runCommonTCSetUpPart();
32     }
33
34     virtual void TearDown()
35     {
36         CommonTestUtil::runCommonTCTearDownPart();
37     }
38 };
39
40 /**
41  * @since 2015-01-15
42  * @see CAResult_t CAInitialize()
43  * @objective Without making any prior call to CAInitialize API, check the return value of other CA APIs
44  * @target CAResult_t CAStartListeningServer()
45  * @target CAResult_t CAStopListeningServer()
46  * @target CAResult_t CAStartDiscoveryServer()
47  * @target CAResult_t CASendRequest(const CAEndpoint_t *object, const CARequestInfo_t *requestInfo)
48  * @target CAResult_t CASendResponse(const CAEndpoint_t *object, const CAResponseInfo_t *responseInfo)
49  * @target CAResult_t CASelectNetwork(CATransportAdapter_t interestedNetwork)
50  * @target CAResult_t CAUnSelectNetwork(CATransportAdapter_t nonInterestedNetwork)
51  * @target CAResult_t CAGetNetworkInformation(CAEndpoint_t **info, size_t *size)
52  * @target CAResult_t CAHandleRequestResponse()
53  * @test_data none
54  * @pre_condition Do not call CAInitialize
55  * @procedure call 1. Call CAStartListeningServer
56  *                 2. Check it's return value
57  *                 3. Call CAStopListeningServer
58  *                 4. Check it's return value
59  *                 5. Call CAStartDiscoveryServer
60  *                 6. Check it's return value
61  *                 7. Call CASendRequest
62  *                 8. Check it's return value
63  *                 9. Call CASendResponse
64  *                 10. Check it's return value
65  *                 11. Call CASelectNetwork
66  *                 12. Check it's return value
67  *                 13. Call CAUnSelectNetwork
68  *                 14. Check it's return value
69  *                 15. Call CAGetNetworkInformation
70  *                 16. Check it's return value
71  *                 17. Call CAHandleRequestResponse
72  *                 18. Check it's return value
73  * @post_condition none
74  * @expected All of them will return CA_STATUS_NOT_INITIALIZED
75  */
76 #if (defined(__LINUX__) || defined(__TIZEN__) || defined(__ANDROID__) || defined(__WINDOWS__)) && defined(__ALL_TRANSPORT__)
77 TEST_F(CANetworkCommonTest_stc, CallOtherCAApiWithoutCallingCAInitialize_N)
78 {
79     CAEndpoint_t* endpoint = NULL;
80     size_t size = 0;
81     CARequestInfo_t requestInfo;
82     CAResponseInfo_t responseInfo;
83
84     m_result = CAStartListeningServer();
85     if (m_result != CA_STATUS_NOT_INITIALIZED)
86     {
87         SET_FAILURE(m_caHelper.getFailureMessage("CAStartListeningServer", m_result, CA_STATUS_NOT_INITIALIZED));
88     }
89
90     m_result = CAStopListeningServer();
91     if (m_result != CA_STATUS_NOT_INITIALIZED)
92     {
93         SET_FAILURE(m_caHelper.getFailureMessage("CAStopListeningServer", m_result, CA_STATUS_NOT_INITIALIZED));
94     }
95
96     m_result = CAStartDiscoveryServer();
97     if (m_result != CA_STATUS_NOT_INITIALIZED)
98     {
99         SET_FAILURE(m_caHelper.getFailureMessage("CAStartDiscoveryServer", m_result, CA_STATUS_NOT_INITIALIZED));
100     }
101
102     m_result = CASendRequest(endpoint, &requestInfo);
103     if (m_result != CA_STATUS_NOT_INITIALIZED)
104     {
105         SET_FAILURE(m_caHelper.getFailureMessage("CASendRequest", m_result, CA_STATUS_NOT_INITIALIZED));
106     }
107
108     m_result = CASendResponse(endpoint, &responseInfo);
109     if (m_result != CA_STATUS_NOT_INITIALIZED)
110     {
111         SET_FAILURE(m_caHelper.getFailureMessage("CASendResponse", m_result, CA_STATUS_NOT_INITIALIZED));
112     }
113
114     m_result = CASelectNetwork((CATransportAdapter_t)m_caHelper.m_availableNetwork);
115     if (m_result != CA_STATUS_NOT_INITIALIZED)
116     {
117         SET_FAILURE(m_caHelper.getFailureMessage("CASelectNetwork", m_result, CA_STATUS_NOT_INITIALIZED));
118     }
119
120     m_result = CAUnSelectNetwork((CATransportAdapter_t)m_caHelper.m_availableNetwork);
121     if (m_result != CA_STATUS_NOT_INITIALIZED)
122     {
123         SET_FAILURE(m_caHelper.getFailureMessage("CAUnSelectNetwork", m_result, CA_STATUS_NOT_INITIALIZED));
124     }
125
126     m_result = CAGetNetworkInformation(&endpoint, &size);
127     if (m_result != CA_STATUS_NOT_INITIALIZED)
128     {
129         SET_FAILURE(m_caHelper.getFailureMessage("CAGetNetworkInformation", m_result, CA_STATUS_NOT_INITIALIZED));
130     }
131
132     if(endpoint != NULL)
133     {
134         free(endpoint);
135     }
136
137     m_result = CAHandleRequestResponse();
138     if (m_result != CA_STATUS_NOT_INITIALIZED)
139     {
140         SET_FAILURE(m_caHelper.getFailureMessage("CAHandleRequestResponse", m_result, CA_STATUS_NOT_INITIALIZED));
141     }
142 }
143 #endif
144
145 /**
146  * @since 2014-12-15
147  * @see none
148  * @objective Test 'CAInitialize', 'CATerminate' and other CA API in loops
149  * @target  CAResult_t CAInitialize()
150  * @target CAResult_t CAStartListeningServer()
151  * @target CAResult_t CASelectNetwork(CATransportAdapter_t interestedNetwork)
152  * @target  void CATerminate()
153  * @test_data none
154  * @pre_condition none
155  * @procedure 1. Call CAInitialize API
156  *            2. Check it's return value
157  *            3. Call CASelectNetwork API
158  *            4. Check it's return value
159  *            5. Call CAStartListeningServer API
160  *            6. Check it's return value
161  *            7. Call CATerminate API
162  *            8. Repeat step 1-7 multiple times
163  * @post_condition none
164  * @expected All APIs will return CA_STATUS_OK
165  */
166 #if (defined(__LINUX__) || defined(__TIZEN__) || defined(__ANDROID__) || defined(__WINDOWS__)) && defined(__ALL_TRANSPORT__)
167 TEST_F(CANetworkCommonTest_stc, CAInitializeAndCATerminateLoopWithOtherFn_P)
168 {
169     for (int count = 0; count < TRY_COUNT; count++)
170     {
171         if (!m_caHelper.initialize())
172         {
173             SET_FAILURE(m_caHelper.getFailureMessage());
174             return;
175         }
176
177         if (!m_caHelper.selectNetwork())
178         {
179             SET_FAILURE(m_caHelper.getFailureMessage());
180             CATerminate();
181             return;
182         }
183
184         if (!m_caHelper.startListeningServer())
185         {
186             SET_FAILURE(m_caHelper.getFailureMessage());
187             CATerminate();
188             return;
189         }
190
191         CATerminate();
192     }
193 }
194 #endif
195
196 /**
197  * @since 2016-02-22
198  * @see CAResult_t CAInitialize()
199  * @see void CATerminate()
200  * @objective Test 'CASelectNetwork' and 'CAUnSelectNetwork' with state transition condition check [STCC]
201  * @target CAResult_t CASelectNetwork(CATransportAdapter_t interestedNetwork)
202  * @target CAResult_t CAUnSelectNetwork(CATransportAdapter_t nonInterestedNetwork)
203  * @test_data try to unselect-select-unselect network
204  * @pre_condition call CAinitialize
205  * @procedure  1. Call CAUnSelectNetwork
206  *             2. Check it's return value as CA_STATUS_FAILED
207  *             3. Call CASelectNetwork
208  *             4. Check it's return value
209  *             5. Call CAUnSelectNetwork
210  *             6. Check it's return value
211  * @post_condition Terminate CA using CATerminate function
212  * @expected 2. It will fail to unselect network initially
213  *           4. It will select network
214  *           6. It will unselect network
215  */
216 #if (defined(__LINUX__) || defined(__TIZEN__) || defined(__ANDROID__) || defined(__WINDOWS__)) && defined(__ALL_TRANSPORT__)
217 TEST_F(CANetworkCommonTest_stc, CASelectNetworkUnSelectNetwor_STCC_P)
218 {
219     if (!m_caHelper.initialize())
220     {
221         SET_FAILURE(m_caHelper.getFailureMessage());
222         return;
223     }
224
225     if (!m_caHelper.unselectNetwork(m_caHelper.m_availableNetwork, CA_STATUS_FAILED))
226     {
227         SET_FAILURE(m_caHelper.getFailureMessage());
228         CATerminate();
229         return;
230     }
231
232     if (!m_caHelper.selectNetwork(m_caHelper.m_availableNetwork, CA_STATUS_OK))
233     {
234         SET_FAILURE(m_caHelper.getFailureMessage());
235         CATerminate();
236         return;
237     }
238
239     if (!m_caHelper.unselectNetwork(m_caHelper.m_availableNetwork, CA_STATUS_OK))
240     {
241         SET_FAILURE(m_caHelper.getFailureMessage());
242     }
243
244     CATerminate();
245 }
246 #endif
247
248 /**
249  * @since 2016-02-22
250  * @see CAResult_t CAInitialize()
251  * @see void CATerminate()
252  * @objective Test 'CAUnSelectNetwork' as a loop check without selecting any network [ITLC]
253  * @target CAResult_t CAUnSelectNetwork(CATransportAdapter_t nonInterestedNetwork)
254  * @test_data try to unselect network multiple times
255  * @pre_condition do not call CASelectNetwork
256  * @procedure  1. Call CAInitialize
257  *             2. Check it's return value
258  *             3. Call CAUnSelectNetwork
259  *             4. Check it's return value as CA_STAUS_FAILED
260  *             5. Repeat steps 3-4 for multiple times
261  * @post_condition Terminate CA using CATerminate function
262  * @expected It will always return CA_STATUS_FAILED
263  */
264 #if (defined(__LINUX__) || defined(__TIZEN__) || defined(__ANDROID__) || defined(__WINDOWS__)) && defined(__ALL_TRANSPORT__)
265 TEST_F(CANetworkCommonTest_stc, CAUnSelectNetworkLoop_ITLC_N)
266 {
267     if (!m_caHelper.initialize())
268     {
269         SET_FAILURE(m_caHelper.getFailureMessage());
270         return;
271     }
272
273     for (int count = 0; count < TRY_COUNT; count++)
274     {
275         if (!m_caHelper.unselectNetwork(m_caHelper.m_availableNetwork, CA_STATUS_FAILED))
276         {
277             SET_FAILURE(m_caHelper.getFailureMessage());
278             CATerminate();
279             return;
280         }
281     }
282
283     CATerminate();
284 }
285 #endif
286
287 /**
288  * @since 2016-02-22
289  * @see CAResult_t CAInitialize()
290  * @see void CATerminate()
291  * @objective Test 'CAUnSelectNetwork' with network type which is not selected in 'CASelectNetwork' [EG]
292  * @target CAResult_t CASelectNetwork(CATransportAdapter_t interestedNetwork)
293  * @target CAResult_t CAUnSelectNetwork(CATransportAdapter_t nonInterestedNetwork)
294  * @test_data try to unselect network
295  * @pre_condition call CAinitialize
296  * @procedure  1. Call CASelectNetwork
297  *             2. Check it's return value
298  *             3. Call CAUnSelectNetwork with another network type that is not selected in step 1
299  *             4. Check it's return value as CA_STATUS_FAILED
300  *             5. Call CAUnselectNetwork with network type as selected in step 1
301  *             6. Check it's return value as CA_STATUS_OK
302  * @post_condition Terminate CA using CATerminate function
303  * @expected 4. It will return CA_STATUS_FAILED
304  */
305 #if (defined(__LINUX__) || defined(__TIZEN__) || defined(__ANDROID__) || defined(__WINDOWS__)) && defined(__ALL_TRANSPORT__)
306 TEST_F(CANetworkCommonTest_stc, CASelectNetworkUnSelectNetwork_EG_N)
307 {
308     if (!m_caHelper.initialize())
309     {
310         SET_FAILURE(m_caHelper.getFailureMessage());
311         return;
312     }
313
314     if (!m_caHelper.selectNetwork(m_caHelper.m_availableNetwork, CA_STATUS_OK))
315     {
316         SET_FAILURE(m_caHelper.getFailureMessage());
317         CATerminate();
318         return;
319     }
320
321     if (!m_caHelper.unselectNetwork(CA_DEFAULT_ADAPTER, CA_STATUS_FAILED))
322     {
323         SET_FAILURE(m_caHelper.getFailureMessage());
324     }
325
326     if (!m_caHelper.unselectNetwork(m_caHelper.m_availableNetwork, CA_STATUS_OK))
327     {
328         SET_FAILURE(m_caHelper.getFailureMessage());
329     }
330
331     CATerminate();
332 }
333 #endif
334
335 /**
336  * @since 2016-02-23
337  * @see CAResult_t CAInitialize()
338  * @see void CATerminate()
339  * @see CAResult_t CAStartListeningServer()
340  * @objective Test CASelectNetwork and CAUnselectNetwork with checking the network status [SCV]
341  * @target CAResult_t CASelectNetwork(CATransportAdapter_t interestedNetwork)
342  * @target CAResult_t CAUnSelectNetwork(CATransportAdapter_t nonInterestedNetwork)
343  * @test_data none
344  * @pre_condition Initialize CA using CAInitialize
345  * @procedure  1. Call CASelectNetwork
346  *             2. Call CAStartListeningServer
347  *             3. Check its return value
348  *             4. Call CAUnSelectNetwork
349  *             5. Call CAStartListeningServer
350  *             6. Check its return value
351  * @post_condition Terminate CA using CATerminate
352  * @expected 2. It will return CA_STATUS_OK
353  *           6. It will return CA_STATUS_FAILED
354  */
355 #if (defined(__LINUX__) || defined(__TIZEN__) || defined(__ANDROID__) || defined(__WINDOWS__)) && defined(__ALL_TRANSPORT__)
356 TEST_F(CANetworkCommonTest_stc, CASelectNetworkUnSelectNetwork_SCV_N)
357 {
358     if (!m_caHelper.initServerNetwork())
359     {
360         SET_FAILURE(m_caHelper.getFailureMessage());
361         return;
362     }
363
364     if (!m_caHelper.unselectNetwork())
365     {
366         SET_FAILURE(m_caHelper.getFailureMessage());
367     }
368
369     if (!m_caHelper.startListeningServer(CA_STATUS_FAILED))
370     {
371         SET_FAILURE(m_caHelper.getFailureMessage());
372         CATerminate();
373         return;
374     }
375
376     CATerminate();
377 }
378 #endif
379
380 /**
381  * @since 2016-02-23
382  * @see CAResult_t CAInitialize()
383  * @see void CATerminate()
384  * @objective Test CASelectNetwork and CAUnselectNetwork in a loop [SLCC]
385  * @target CAResult_t CASelectNetwork(CATransportAdapter_t interestedNetwork)
386  * @target CAResult_t CAUnSelectNetwork(CATransportAdapter_t nonInterestedNetwork)
387  * @test_data none
388  * @pre_condition Initialize CA using CAInitialize
389  * @procedure  1. Call CASelectNetwork
390  *             2. Call CAUnselectNetwork
391  *             3. Repeat Steps 1~2 for multiple times
392  * @post_condition Terminate CA using CATerminate
393  * @expected It will select and unselect network in each time and will return CA_STATUS_OK
394  */
395 #if (defined(__LINUX__) || defined(__TIZEN__) || defined(__ANDROID__) || defined(__WINDOWS__)) && defined(__ALL_TRANSPORT__)
396 TEST_F(CANetworkCommonTest_stc, CASelectNetworkUnSelectNetworkLoop_SLCC_P)
397 {
398     if (!m_caHelper.initialize())
399     {
400         SET_FAILURE(m_caHelper.getFailureMessage());
401         return;
402     }
403
404     for (int count = 0; count < TRY_COUNT; count++)
405     {
406         if (!m_caHelper.selectNetwork())
407         {
408             SET_FAILURE(m_caHelper.getFailureMessage());
409             CATerminate();
410             return;
411         }
412         if (!m_caHelper.unselectNetwork())
413         {
414             SET_FAILURE(m_caHelper.getFailureMessage());
415             CATerminate();
416             return;
417         }
418     }
419
420     CATerminate();
421 }
422 #endif
423
424 /**
425  * @since 2016-02-23
426  * @see CAResult_t CAInitialize()
427  * @see void CATerminate()
428  * @objective Test functionality of 'CAStartListeningServer' and 'CAStopListeningServer' in a loop [VLCC]
429  * @target CAResult_t CASelectNetwork(CATransportAdapter_t interestedNetwork)
430  * @target CAResult_t CAStartListeningServer()
431  * @target CAResult_t CAStopListeningServer()
432  * @test_data none
433  * @pre_condition Initialize CA using CAInitialize
434  * @procedure  1. Select one of the available networks
435  *             2. Start the listening server
436  *             3. Call the CAStopListeningServer API
437  *             4. Check it's return value
438  *             5. Repeat Steps 1~4 for multiple times
439  * @post_condition Terminate CA using CATerminate
440  * @expected It will start and stop the listening server and will return CA_STATUS_OK
441  */
442 #if (defined(__LINUX__) || defined(__TIZEN__) || defined(__ANDROID__) || defined(__WINDOWS__)) && defined(__ALL_TRANSPORT__)
443 TEST_F(CANetworkCommonTest_stc, CAStartListeningServerStopListeningServer_VLCC_P)
444 {
445     for (int count = 0; count < TRY_COUNT; count++)
446     {
447         if (!m_caHelper.initServerNetwork())
448         {
449             SET_FAILURE(m_caHelper.getFailureMessage());
450             return;
451         }
452
453         if (!m_caHelper.stopListeningServer())
454         {
455             SET_FAILURE(m_caHelper.getFailureMessage());
456             CATerminate();
457             return;
458         }
459     }
460
461     CATerminate();
462 }
463 #endif
464
465 /**
466  * @since 2016-02-24
467  * @see CAResult_t CAInitialize()
468  * @see CAResult_t CASelectNetwork(CATransportAdapter_t interestedNetwork)
469  * @see void CATerminate()
470  * @objective Test 'CAStartDiscoveryServer' positively to start the discovery service in a loop [VLCC]
471  * @target CAResult_t CAStartDiscoveryServer()
472  * @test_data none
473  * @pre_condition 1. Initialize CA using CAInitialize
474  *                2. Select network
475  * @procedure  1. Call the CAStartDiscoveryServer API
476  *             2. Check it's return value
477  *             3. Repeat Steps 1~2 for multiple times
478  * @post_condition Terminate CA using CATerminate function
479  * @expected It will start the discovery server and will return CA_STATUS_OK
480  */
481 #if (defined(__LINUX__) || defined(__TIZEN__) || defined(__ANDROID__) || defined(__WINDOWS__)) && defined(__ALL_TRANSPORT__)
482 TEST_F(CANetworkCommonTest_stc, CAStartDiscoveryServer_VLCC_P)
483 {
484     for (int count = 0; count < TRY_COUNT; count++)
485     {
486
487         if (!m_caHelper.initClientNetwork())
488         {
489             SET_FAILURE(m_caHelper.getFailureMessage());
490             return;
491         }
492
493         CommonUtil::waitInSecond(SLEEP_TIME);
494     }
495
496     CATerminate();
497 }
498 #endif
499
500 /**
501  * @since 2016-02-25
502  * @see CAResult_t CAInitialize()
503  * @see void CATerminate()
504  * @objective Test functionality of 'CACreateEndpoint' and 'CADestroyEndpoint' in a loop [VLCC]
505  * @target CAResult_t CACreateEndpoint(CATransportFlags_t flags, CATransportAdapter_t adapter, const char *addr,
506  *         uint16_t port, CAEndpoint_t **object)
507  * @target void CADestroyToken(CAToken_t token)
508  * @test_data none
509  * @pre_condition Initialize CA using CAInitialize
510  * @procedure  1. Select network
511  *             2. Call CACreateEndpoint API
512  *             3. Check it's return value
513  *             4. Call CADestroyEndpoint API
514  *             5. Repeat Steps 1~4 for multiple times
515  * @post_condition Terminate CA using CATerminate
516  * @expected It will create and destroy end point and will return CA_STATUS_OK
517  */
518 #if (defined(__LINUX__) || defined(__TIZEN__) || defined(__ANDROID__) || defined(__WINDOWS__)) && defined(__ALL_TRANSPORT__)
519 TEST_F(CANetworkCommonTest_stc, CACreateEndpointDestroyEndpoint_VLCC_P)
520 {
521     CAResult_t m_result = (CAResult_t)NULL;
522
523     for (int count = 0; count < TRY_COUNT; count++)
524     {
525         if (!m_caHelper.initialize())
526         {
527             SET_FAILURE(m_caHelper.getFailureMessage());
528             return;
529         }
530
531         CAEndpoint_t* m_endpoint = NULL;
532
533         m_result = CACreateEndpoint(CA_DEFAULT_FLAGS, (CATransportAdapter_t)m_caHelper.m_availableNetwork, ENDPOINT_IP, ENDPOINT_PORT, &m_endpoint);
534         if (m_result != CA_STATUS_OK || m_endpoint == NULL)
535         {
536             SET_FAILURE(m_caHelper.getFailureMessage("CACreateEndpoint", CA_STATUS_OK));
537             CATerminate();
538             return;
539         }
540
541        CADestroyEndpoint(m_endpoint);
542     }
543
544     CATerminate();
545 }
546 #endif