Merge test folder of test branch
[iotivity.git] / test / src / tc / ic / gtest / src / btc / ICResourceDirectoryTest.cpp
1 /*******************************************************************
2  *
3  * Copyright 2017 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 #include "ICHelper.h"
21
22 class ICResourceDirectoryTest_btc: public ::testing::Test
23 {
24 public:
25     ResourceHandles m_resourceHandles;
26     OCPresenceHandle m_ocPresenceHandle = nullptr;
27     QueryParamsList m_queryParams =
28     { };
29     const std::vector< std::string > m_vecDeviceIdentifier;bool m_isCallbackInvoked;
30     OCStackResult m_actualResult;
31
32 protected:
33     ICHelper* m_ICHelper;
34
35     ICResourceDirectoryTest_btc()
36     {
37         m_isCallbackInvoked = false;
38         m_ICHelper = nullptr;
39         m_actualResult = OC_STACK_ERROR;
40     }
41
42     virtual void SetUp()
43     {
44         CommonTestUtil::runCommonTCSetUpPart();
45         m_ICHelper = ICHelper::getInstance();
46         m_actualResult = OC_STACK_ERROR;
47         m_isCallbackInvoked = false;
48     }
49
50     virtual void TearDown()
51     {
52         CommonTestUtil::runCommonTCTearDownPart();
53     }
54
55 public:
56     void onPublish(const OCRepresentation& rep, const int& eCode)
57     {
58         m_isCallbackInvoked = true;
59         IOTIVITYTEST_LOG(INFO, "[ICResourceDirectoryTest_btc] Response from onPublish\n");
60     }
61
62     void onDelete(const int& eCode)
63     {
64         m_isCallbackInvoked = true;
65         IOTIVITYTEST_LOG(INFO, "[ICResourceDirectoryTest_btc] Response from onDelete\n");
66     }
67
68     void onObserve(const HeaderOptions headerOption, const OCRepresentation&, const int& val1,
69             const int& val2)
70     {
71         m_isCallbackInvoked = true;
72         IOTIVITYTEST_LOG(INFO, "[ICResourceDirectoryTest_btc] Response from onObserve\n");
73     }
74 };
75
76 /**
77  * @since           2016-08-25
78  * @see             none
79  * @objective       Test 'subscribeDevicePresence' positively to subscribes to a server's device presence change events
80  * @target          subscribeDevicePresence(OCPresenceHandle& presenceHandle,const std::string& host,const std::vector<std::string>& di,
81  *                                          OCConnectivityType connectivityType,ObserveCallback callback)
82  * @test_data       1. presenceHandle       OCPresenceHandle
83  *                  2. host                 host address
84  *                  3. di                   device Identity
85  *                  4. connectivityType     CT_DEFAULT
86  *                  5. callback             onObserve as callback
87  * @pre_condition   none
88  * @procedure       1. Call subscribeDevicePresence API
89  *                  2. Check it's return value
90  * @post_condition  none
91  * @expected        It will subscribes to a server's device presence change events & return OC_STACK_OK
92  */
93
94 #if defined(__LINUX__) || defined(__TIZEN__)
95 TEST_F(ICResourceDirectoryTest_btc, SubscribeDevicePresenceWithValidParameters_SRC_FSV_P)
96 {
97     try
98     {
99         m_actualResult = OCPlatform::subscribeDevicePresence(m_ocPresenceHandle,IC_HOST_ADDRESS, m_vecDeviceIdentifier,IC_CONNECTIVITY_TYPE,
100                 std::bind(&ICResourceDirectoryTest_btc::onObserve, this,placeholders::_1, placeholders::_2, placeholders::_3, placeholders::_4));
101         ASSERT_EQ(OC_STACK_OK,m_actualResult) << "Actual result string : " << CommonUtil::s_OCStackResultString.at(m_actualResult);
102     }
103     catch(OCException ex)
104     {
105         FAIL() << "OCException result string : " << CommonUtil::s_OCStackResultString.at(ex.code());
106     }
107     SUCCEED();
108 }
109 #endif
110
111
112 /**
113  * @since           2016-08-25
114  * @see             none
115  * @objective       Test 'subscribeDevicePresence' negatively with Invalid Host address
116  * @target          subscribeDevicePresence(OCPresenceHandle& presenceHandle,const std::string& host,const std::vector<std::string>& di,
117  *                                          OCConnectivityType connectivityType,ObserveCallback callback)
118  * @test_data       1. presenceHandle       OCPresenceHandle
119  *                  2. host                 invalid host address
120  *                  3. di                   device Identity
121  *                  4. connectivityType     CT_DEFAULT
122  *                  5. callback             onObserve as callback
123  * @pre_condition   none
124  * @procedure       1. Call subscribeDevicePresence API
125  *                  2. Check it's return value
126  * @post_condition  none
127  * @expected        It will not subscribes & will return OC_STACK_INVALID_URI
128  */
129
130 #if defined(__LINUX__) || defined(__TIZEN__)
131 TEST_F(ICResourceDirectoryTest_btc, SubscribeDevicePresenceWithInvalidHostAddress_USV_N)
132 {
133     try
134     {
135         m_actualResult = OCPlatform::subscribeDevicePresence(m_ocPresenceHandle,IC_HOST_ADDRESS_INVALID, m_vecDeviceIdentifier,IC_CONNECTIVITY_TYPE,
136                 std::bind(&ICResourceDirectoryTest_btc::onObserve, this,placeholders::_1, placeholders::_2, placeholders::_3, placeholders::_4));
137         FAIL() << "Actual result string : " << CommonUtil::s_OCStackResultString.at(m_actualResult);
138     }
139     catch(OCException ex)
140     {
141         EXPECT_EQ(OC_STACK_INVALID_URI, ex.code()) << "OCException result string : " << CommonUtil::s_OCStackResultString.at(ex.code());
142     }
143     SUCCEED();
144 }
145 #endif
146
147 /**
148  * @since           2016-08-25
149  * @see             none
150  * @objective       Test 'subscribeDevicePresence' negatively with Empty IC_HOST_ADDRESS
151  * @target          subscribeDevicePresence(OCPresenceHandle& presenceHandle,const std::string& host,const std::vector<std::string>& di,
152  *                                          OCConnectivityType connectivityType,ObserveCallback callback)
153  * @test_data       1. presenceHandle       OCPresenceHandle
154  *                  2. host                 empty host address
155  *                  3. di                   device Identity
156  *                  4. connectivityType     CT_DEFAULT
157  *                  5. callback             onObserve as callback
158  * @pre_condition   none
159  * @procedure       1. Call subscribeDevicePresence API
160  *                  2. Check it's return value
161  * @post_condition  none
162  * @expected        It will not subscribes & should return OC_STACK_INVALID_PARAM
163  */
164
165 #if defined(__LINUX__) || defined(__TIZEN__)
166 TEST_F(ICResourceDirectoryTest_btc, SubscribeDevicePresenceWithEmptyHost_ESV_N)
167 {
168     try
169     {
170         m_actualResult = OCPlatform::subscribeDevicePresence(m_ocPresenceHandle,IC_HOST_ADDRESS_EMPTY, m_vecDeviceIdentifier,IC_CONNECTIVITY_TYPE,
171                 std::bind(&ICResourceDirectoryTest_btc::onObserve, this,placeholders::_1, placeholders::_2, placeholders::_3, placeholders::_4));
172         FAIL() << "Actual result string : " << CommonUtil::s_OCStackResultString.at(m_actualResult);
173     }
174     catch(OCException ex)
175     {
176         EXPECT_EQ(OC_STACK_INVALID_PARAM, ex.code()) << "OCException result string : " << CommonUtil::s_OCStackResultString.at(ex.code());
177     }
178     SUCCEED();
179 }
180 #endif
181
182 /**
183  * @since           2016-08-25
184  * @see             none
185  * @objective       Test 'subscribeDevicePresence' negatively with null OnObserve
186  * @target          subscribeDevicePresence(OCPresenceHandle& presenceHandle,const std::string& host,const std::vector<std::string>& di,
187  *                                          OCConnectivityType connectivityType,ObserveCallback callback)
188  * @test_data       1. presenceHandle       OCPresenceHandle
189  *                  2. host                 host address
190  *                  3. di                   device Identity
191  *                  4. connectivityType     CT_DEFAULT
192  *                  5. callback             null as callback
193  * @pre_condition   none
194  * @procedure       1. Call subscribeDevicePresence API
195  *                  2. Check it's return value
196  * @post_condition  none
197  * @expected        It will not subscribes & should not return OC_STACK_OK
198  */
199
200 #if defined(__LINUX__) || defined(__TIZEN__)
201 TEST_F(ICResourceDirectoryTest_btc, SubscribeDevicePresenceWithNullCallback_NV_N)
202 {
203     try
204     {
205         m_actualResult = OCPlatform::subscribeDevicePresence(m_ocPresenceHandle,IC_HOST_ADDRESS_EMPTY, m_vecDeviceIdentifier,
206                 IC_CONNECTIVITY_TYPE,IC_CALLBACK_HANDLER_NULL);
207         FAIL() << "Actual result string : " << CommonUtil::s_OCStackResultString.at(m_actualResult);
208     }
209     catch(OCException ex)
210     {
211         ASSERT_EQ(OC_STACK_INVALID_PARAM, ex.code()) << "OCException result string : " << CommonUtil::s_OCStackResultString.at(ex.code());
212     }
213     SUCCEED();
214 }
215 #endif
216
217 /**
218  * @since           2016-08-25
219  * @see             subscribeDevicePresence(OCPresenceHandle& presenceHandle,const std::string& host,const std::vector<std::string>& di,
220  *                                          OCConnectivityType connectivityType,ObserveCallback callback)
221  * @objective       Test 'unsubscribePresence' with positive value
222  * @target          unsubscribePresence(OCPresenceHandle presenceHandle)
223  * @test_data       1. presenceHandle       OCPresenceHandle
224  * @pre_condition   none
225  * @procedure       1. Call subscribeDevicePresence API
226  *                  2. Check it's return value
227  *                  3. Call unsubscribePresence API
228  *                  4. Check it's return value
229  * @post_condition  none
230  * @expected        It will unsubscribes & should return OC_STACK_OK
231  */
232
233 #if defined(__LINUX__) || defined(__TIZEN__)
234 TEST_F(ICResourceDirectoryTest_btc, UnsubscribePresenceWithValidParameters_SRC_FSV_P)
235 {
236     try
237     {
238         m_actualResult = OCPlatform::subscribeDevicePresence(m_ocPresenceHandle,IC_HOST_ADDRESS, m_vecDeviceIdentifier,IC_CONNECTIVITY_TYPE,
239                 std::bind(&ICResourceDirectoryTest_btc::onObserve, this,placeholders::_1, placeholders::_2, placeholders::_3, placeholders::_4));
240         EXPECT_EQ(OC_STACK_OK,m_actualResult) << "Actual result string : " << CommonUtil::s_OCStackResultString.at(m_actualResult);
241
242         m_actualResult = OC_STACK_ERROR;
243         m_actualResult = OCPlatform::unsubscribePresence(m_ocPresenceHandle);
244         ASSERT_EQ(OC_STACK_OK,m_actualResult) << "Actual result string : " << CommonUtil::s_OCStackResultString.at(m_actualResult);
245     }
246     catch(OCException ex)
247     {
248         FAIL() << "OCException result string : " << CommonUtil::s_OCStackResultString.at(ex.code());
249     }
250     SUCCEED();
251 }
252 #endif
253
254 /**
255  * @since           2016-08-25
256  * @see             none
257  * @objective       Test 'subscribeDevicePresence' negatively with Invalid Host address
258  * @target          unsubscribePresence(OCPresenceHandle presenceHandle)
259  * @test_data       1. presenceHandle       nullptr as OCPresenceHandle
260  * @pre_condition   none
261  * @procedure       1. Call subscribeDevicePresence API
262  *                  2. Check it's return value
263  * @post_condition  none
264  * @expected        It will not subscribes & will return OC_STACK_INVALID_PARAM
265  */
266
267 #if defined(__LINUX__) || defined(__TIZEN__)
268 TEST_F(ICResourceDirectoryTest_btc, UnSubscribeDevicePresenceWithInvalidOCPresentHandler_USV_N)
269 {
270     try
271     {
272         m_actualResult = OCPlatform::unsubscribePresence(nullptr);
273         FAIL() << "Actual result string : " << CommonUtil::s_OCStackResultString.at(m_actualResult);
274     }
275     catch(OCException ex)
276     {
277         ASSERT_EQ(OC_STACK_INVALID_PARAM, ex.code()) << "OCException result string : " << CommonUtil::s_OCStackResultString.at(ex.code());
278     }
279     SUCCEED();
280 }
281 #endif
282
283 /**
284  * @since           2017-01-19
285  * @see             OCAccountManager::Ptr constructAccountManagerObject(const std::string& host,
286                                                             OCConnectivityType connectivityType)
287  * @objective       Test 'constructAccountManagerObject' with positive value
288  * @target          constructAccountManagerObject(OCPresenceHandle presenceHandle)
289  * @test_data       1. presenceHandle       OCPresenceHandle
290  * @pre_condition   none
291  * @procedure       1. Call constructAccountManagerObject API
292  *                  2. Check it's return value
293  * @post_condition  none
294  * @expected        It will return an object
295  */
296
297 #if defined(__LINUX__) || defined(__TIZEN__)
298 TEST_F(ICResourceDirectoryTest_btc, ConstructAccountManagerObjectWithValidParameters_SRC_FSV_P)
299 {
300     try
301     {
302         ASSERT_NE(nullptr, OCPlatform::constructAccountManagerObject(IC_HOST_ADDRESS,IC_CONNECTIVITY_TYPE));
303     }
304     catch(OCException ex)
305     {
306         FAIL() << "OCException result string : " << CommonUtil::s_OCStackResultString.at(ex.code());
307     }
308     SUCCEED();
309 }
310 #endif
311
312 /**
313  * @since           2016-08-25
314  * @see             none
315  * @objective       Test 'constructAccountManagerObject' negatively with Invalid Host address
316  * @target          OCAccountManager::Ptr constructAccountManagerObject(const std::string& host,
317                                                             OCConnectivityType connectivityType)
318  * @test_data       1. presenceHandle       EMPTY_VALUE as host and connectivityType
319  * @pre_condition   none
320  * @procedure       1. Call constructAccountManagerObject API
321  *                  2. Check it's return value
322  * @post_condition  none
323  * @expected        It will not return OC_STACK_INVALID_PARAM
324  */
325
326 #if defined(__LINUX__) || defined(__TIZEN__)
327 TEST_F(ICResourceDirectoryTest_btc, ConstructAccountManagerObjectWithEmptyHost_ESV_N)
328 {
329     try
330     {
331         OCPlatform::constructAccountManagerObject(EMPTY_VALUE, IC_CONNECTIVITY_TYPE);
332         FAIL() << "Actual result string : " << CommonUtil::s_OCStackResultString.at(m_actualResult);
333     }
334     catch(OCException ex)
335     {
336         ASSERT_EQ( OC_STACK_ERROR, ex.code()) << "OCException result string : " << CommonUtil::s_OCStackResultString.at(ex.code());
337     }
338     SUCCEED();
339 }
340 #endif
341
342 /**
343  * @since           2016-08-25
344  * @see             none
345  * @objective       Test 'constructAccountManagerObject' negatively with Invalid Host address
346  * @target          OCAccountManager::Ptr constructAccountManagerObject(const std::string& host,
347                                                             OCConnectivityType connectivityType)
348  * @test_data       1. presenceHandle       INVALID_PARAMETER as host and connectivityType
349  * @pre_condition   none
350  * @procedure       1. Call constructAccountManagerObject API
351  *                  2. Check it's return value
352  * @post_condition  none
353  * @expected        It will not return OC_STACK_INVALID_PARAM
354  */
355
356 #if defined(__LINUX__) || defined(__TIZEN__)
357 TEST_F(ICResourceDirectoryTest_btc, ConstructAccountManagerObjectWithInvalidHost_ESV_N)
358 {
359     try
360     {
361         ICHelper::waitForServerResponse();
362         OCPlatform::constructAccountManagerObject(INVALID_PARAMETER, IC_CONNECTIVITY_TYPE);
363         FAIL() << "Actual result string : " << CommonUtil::s_OCStackResultString.at(m_actualResult);
364     }
365     catch(OCException ex)
366     {
367         ASSERT_EQ(OC_STACK_ERROR, ex.code()) << "OCException result string : " << CommonUtil::s_OCStackResultString.at(ex.code());
368     }
369     SUCCEED();
370 }
371 #endif