Merge test folder of test branch
[iotivity.git] / test / src / tc / pm / gtest / cpp / src / stc / PMCppTest.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  *      LICENSE-2.0" target="_blank">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
22 #include "PMCppHelper.h"
23 #include "PMCppUtilityHelper.h"
24 #include "CommonTestUtil.h"
25
26 class PMCppTest_stc: public ::testing::Test
27 {
28 protected:
29     PMCppHelper m_PMCppHelper;
30     DeviceList_t m_UnownedDevList, m_OwnedDevList;
31     OicSecAcl_t *m_acl1, *m_acl2;
32     InputPinCallbackHandle callbackHandle;
33     DisplayPinCallbackHandle displayPinCallbackHandle;
34
35     virtual void SetUp()
36     {
37         CommonTestUtil::runCommonTCSetUpPart();
38         CommonUtil::killApp(KILL_SERVERS);
39         PMCppUtilityHelper::removeAllResFile();
40         CommonUtil::copyFile(JUSTWORKS_SERVER_CBOR_O1_UNOWNED_BACKUP, JUSTWORKS_SERVER_CBOR_O1);
41         CommonUtil::copyFile(JUSTWORKS_SERVER_CBOR_O2_UNOWNED_BACKUP, JUSTWORKS_SERVER_CBOR_O2);
42         CommonUtil::copyFile(CLIENT_CBOR_01_UNOWNED_BACKUP, CLIENT_CBOR_01);
43         CommonUtil::waitInSecond(DELAY_MEDIUM);
44         CommonUtil::launchApp(JUSTWORKS_SERVER1);
45         CommonUtil::launchApp(JUSTWORKS_SERVER2);
46         CommonUtil::waitInSecond(DELAY_MEDIUM);
47
48         m_UnownedDevList.clear();
49         m_OwnedDevList.clear();
50         m_acl1 = NULL;
51         m_acl2 = NULL;
52
53         callbackHandle = nullptr;
54         displayPinCallbackHandle = nullptr;
55
56         if (!m_PMCppHelper.provisionInit())
57         {
58             SET_FAILURE(m_PMCppHelper.getFailureMessage());
59             return;
60         }
61
62         if(!m_PMCppHelper.discoverUnownedDevices(DISCOVERY_TIMEOUT, m_UnownedDevList, OC_STACK_OK))
63         {
64             SET_FAILURE(m_PMCppHelper.getFailureMessage());
65             return;
66         }
67
68         if(!m_PMCppHelper.doOwnershipTransfer(m_UnownedDevList, PMCppCallbackHelper::provisionPostCB, OC_STACK_OK))
69         {
70             SET_FAILURE(m_PMCppHelper.getFailureMessage());
71             return;
72         }
73
74         if (!m_PMCppHelper.discoverOwnedDevices(DISCOVERY_TIMEOUT, m_OwnedDevList, OC_STACK_OK))
75         {
76             SET_FAILURE(m_PMCppHelper.getFailureMessage());
77             return;
78         }
79
80         m_acl1 = (OicSecAcl_t *)OICCalloc(1,sizeof(OicSecAcl_t));
81         PMCppHelper::createAcl(m_acl1, DEVICE_INDEX_ONE, FULL_PERMISSION, m_OwnedDevList);
82
83         m_acl2 = (OicSecAcl_t *)OICCalloc(1,sizeof(OicSecAcl_t));
84         PMCppHelper::createAcl(m_acl2, DEVICE_INDEX_TWO, FULL_PERMISSION, m_OwnedDevList);
85     }
86
87     virtual void TearDown()
88     {
89         CommonTestUtil::runCommonTCTearDownPart();
90         CommonUtil::killApp(KILL_SERVERS);
91     }
92 };
93
94 /**
95  * @since           2015-11-30
96  * @see             static OCStackResult provisionInit(const std::string& dbPath)
97  * @see             static OCStackResult discoverOwnedDevices(unsigned short timeout, DeviceList_t &list)
98  * @objective       test provisionACL positively with max range of Acl permission
99  * @target          OCStackResult provisionACL(const OicSecAcl_t* acl, ResultCallBack resultCallback)
100  * @test_data       Acl permission with max Range
101  * @pre_condition   start two justworks simulators
102  * @procedure       1. call provisionInit
103  *                  2. call discoverOwnedDevices
104  *                  3. call provisionACL
105  *                  4. call deleteACLList for m_acl1
106  * @post_condition  None
107  * @expected        provisionACL will return OC_STACK_OK
108  */
109 #if defined(__LINUX__) || defined(__TIZEN__) || defined(__WINDOWS__)
110 TEST_F(PMCppTest_stc, ProvisionAclPermisison_UBV_P)
111 {
112     m_acl1 = (OicSecAcl_t *)OICCalloc(1, sizeof(OicSecAcl_t));
113     PMCppHelper::createAcl(m_acl1, DEVICE_INDEX_ONE, MAX_PERMISSION_RANGE, m_OwnedDevList);
114
115     if(!m_PMCppHelper.provisionACL(m_OwnedDevList, m_acl1, PMCppCallbackHelper::provisionPostCB, OC_STACK_OK))
116     {
117         SET_FAILURE(m_PMCppHelper.getFailureMessage());
118     }
119
120     m_PMCppHelper.deleteACLList(m_acl1);
121 }
122 #endif
123
124 /**
125  * @since           2015-11-30
126  * @see             static OCStackResult provisionInit(const std::string& dbPath)
127  * @see             static OCStackResult discoverOwnedDevices(unsigned short timeout, DeviceList_t &list)
128  * @objective       test provisionACL positively with minimum range of Acl permission
129  * @target          OCStackResult provisionACL(const OicSecAcl_t* acl, ResultCallBack resultCallback)
130  * @test_data       Acl permission with minimum Range
131  * @pre_condition   start two justworks simulators
132  * @procedure       1. call provisionInit
133  *                  2. call discoverOwnedDevices
134  *                  3. call provisionACL
135  *                  4. call deleteACLList for m_acl1
136  * @post_condition  None
137  * @expected        provisionACL will return OC_STACK_OK
138  */
139 #if defined(__LINUX__) || defined(__TIZEN__) || defined(__WINDOWS__)
140 TEST_F(PMCppTest_stc, ProvisionAclPermisison_LBV_P)
141 {
142     m_acl1 = (OicSecAcl_t *)OICCalloc(1,sizeof(OicSecAcl_t));
143     PMCppHelper::createAcl(m_acl1, DEVICE_INDEX_ONE, NO_PERMISSION, m_OwnedDevList);
144
145     if(!m_PMCppHelper.provisionACL(m_OwnedDevList, m_acl1, PMCppCallbackHelper::provisionPostCB, OC_STACK_OK))
146     {
147         SET_FAILURE(m_PMCppHelper.getFailureMessage());
148     }
149
150     m_PMCppHelper.deleteACLList(m_acl1);
151 }
152 #endif
153
154 /**
155  * @since           2015-11-30
156  * @see             static OCStackResult provisionInit(const std::string& dbPath)
157  * @see             static OCStackResult discoverOwnedDevices(unsigned short timeout, DeviceList_t &list)
158  * @objective       test provisionCredentials positively with representative value of SYMMETRIC_PAIR_WISE_KEY
159  * @target          OCStackResult provisionCredentials(const Credential &cred, const OCSecureResource &device2, ResultCallBack resultCallback)
160  * @test_data       representative value of SYMMETRIC_PAIR_WISE_KEY as CredType
161  * @pre_condition   start two justworks simulators
162  * @procedure       1. call provisionInit
163  *                  2. call discoverOwnedDevices
164  *                  3. call provisionCredentials
165  * @post_condition  None
166  * @expected        provisionCredentials will return OC_STACK_OK
167  */
168 #if defined(__LINUX__) || defined(__TIZEN__) || defined(__WINDOWS__)
169 TEST_F(PMCppTest_stc, ProvisionCredentialsCredType_REV_P)
170 {
171     OicSecCredType_t type = (OicSecCredType_t)1;
172     size_t keySize = OWNER_PSK_LENGTH_256;
173     Credential cred(type, keySize);
174
175     if(!m_PMCppHelper.provisionCredentials(m_OwnedDevList, cred, *m_OwnedDevList[1].get(), PMCppCallbackHelper::provisionPostCB, OC_STACK_OK))
176     {
177         SET_FAILURE(m_PMCppHelper.getFailureMessage());
178     }
179 }
180 #endif
181
182 /**
183  * @since           2015-11-30
184  * @see             static OCStackResult provisionInit(const std::string& dbPath)
185  * @see             static OCStackResult discoverOwnedDevices(unsigned short timeout, DeviceList_t &list)
186  * @objective       test provisionACL negatively with resultCallback and acl as NULL
187  * @target          OCStackResult provisionACL(const OicSecAcl_t* acl, ResultCallBack resultCallback)
188  * @test_data       resultCallback = NULL & acl = NULL
189  * @pre_condition   start two justworks simulators
190  * @procedure       1. call provisionInit
191  *                  2. call discoverOwnedDevices
192  *                  3. call provisionACL
193  *                  4. call provisionACL
194  *                  5. call deleteACLList for m_acl1
195  * @post_condition  None
196  * @expected        provisionACL will return OC_STACK_INVALID_PARAM
197  */
198 #if defined(__LINUX__) || defined(__TIZEN__) || defined(__WINDOWS__)
199 TEST_F(PMCppTest_stc, ProvisionAclCBAcl_NV_N)
200 {
201     m_acl1 = (OicSecAcl_t *)OICCalloc(1,sizeof(OicSecAcl_t));
202     PMCppHelper::createAcl(m_acl1, DEVICE_INDEX_ONE, FULL_PERMISSION, m_OwnedDevList);
203
204     if(!m_PMCppHelper.provisionACL(m_OwnedDevList, m_acl1, PMCppCallbackHelper::provisionPostCB, OC_STACK_OK))
205     {
206         SET_FAILURE(m_PMCppHelper.getFailureMessage());
207         return;
208     }
209
210     if(!m_PMCppHelper.provisionACL(m_OwnedDevList, NULL, NULL, OC_STACK_INVALID_PARAM))
211     {
212         SET_FAILURE(m_PMCppHelper.getFailureMessage());
213     }
214
215     m_PMCppHelper.deleteACLList(m_acl1);
216 }
217 #endif
218
219 /**
220  * @since           2016-07-26
221  * @see             static OCStackResult provisionInit(const std::string& dbPath)
222  * @see             static OCStackResult discoverOwnedDevices(unsigned short timeout, DeviceList_t &list)
223  * @objective       test provisionACL positively calling API multiple times
224  * @target          OCStackResult provisionACL(const OicSecAcl_t* acl, ResultCallBack resultCallback)
225  * @test_data       Regular data for provisionACL
226  * @pre_condition   start two justworks simulators
227  * @procedure       1. call provisionInit
228  *                  4. call discoverOwnedDevices
229  *                  5. call provisionACL
230  *                  6. call provisionACL
231  *                  7. call deleteACLList for m_acl1
232  * @post_condition  None
233  * @expected        provisionACL will return OC_STACK_OK while calling it multiple times.
234  */
235 #if defined(__LINUX__) || defined(__TIZEN__) || defined(__WINDOWS__)
236 TEST_F(PMCppTest_stc, ProvisionAclMultipleTimes_SQV_P)
237 {
238     m_acl1 = (OicSecAcl_t *)OICCalloc(1,sizeof(OicSecAcl_t));
239     PMCppHelper::createAcl(m_acl1, DEVICE_INDEX_ONE, FULL_PERMISSION, m_OwnedDevList);
240
241     if(!m_PMCppHelper.provisionACL(m_OwnedDevList, m_acl1, PMCppCallbackHelper::provisionPostCB, OC_STACK_OK))
242     {
243         SET_FAILURE(m_PMCppHelper.getFailureMessage());
244         return;
245     }
246
247     if(!m_PMCppHelper.provisionACL(m_OwnedDevList, m_acl1, PMCppCallbackHelper::provisionPostCB, OC_STACK_OK))
248     {
249         SET_FAILURE(m_PMCppHelper.getFailureMessage());
250     }
251
252     m_PMCppHelper.deleteACLList(m_acl1);
253 }
254 #endif
255
256 /**
257  * @since           2016-07-26
258  * @see             static OCStackResult provisionInit(const std::string& dbPath)
259  * @see             static OCStackResult discoverOwnedDevices(unsigned short timeout, DeviceList_t &list)
260  * @objective       test provisionCredentials negatively, calling provisionCredentials multiple times.
261  * @target          OCStackResult provisionCredentials(const Credential &cred, const OCSecureResource &device2, ResultCallBack resultCallback)
262  * @test_data       Regular data for provisionCredentials
263  * @pre_condition   start two justworks simulators
264  * @procedure       1. call provisionInit
265  *                  2. call discoverOwnedDevices
266  *                  3. call provisionCredentials
267  *                  4. call provisionCredentials
268  * @post_condition  None
269  * @expected        provisionCredentials will return OC_STACK_INVALID_PARAM while calling for the second time.
270  */
271 #if defined(__LINUX__) || defined(__TIZEN__) || defined(__WINDOWS__)
272 TEST_F(PMCppTest_stc, ProvisionCredentialsMultipleTimes_EG_N)
273 {
274     OicSecCredType_t type = SYMMETRIC_PAIR_WISE_KEY;
275     size_t keySize = OWNER_PSK_LENGTH_128;
276     Credential cred(type, keySize);
277
278     if(!m_PMCppHelper.provisionCredentials(m_OwnedDevList, cred, *m_OwnedDevList[1].get(), PMCppCallbackHelper::provisionPostCB, OC_STACK_OK))
279     {
280         SET_FAILURE(m_PMCppHelper.getFailureMessage());
281         return;
282     }
283
284     if(!m_PMCppHelper.provisionCredentials(m_OwnedDevList, cred, *m_OwnedDevList[1].get(), PMCppCallbackHelper::provisionPostCB, OC_STACK_INVALID_PARAM))
285     {
286         SET_FAILURE(m_PMCppHelper.getFailureMessage());
287     }
288 }
289 #endif
290
291 /**
292  * @since           2016-07-26
293  * @see             static OCStackResult provisionInit(const std::string& dbPath)
294  * @see             static OCStackResult discoverOwnedDevices(unsigned short timeout, DeviceList_t &list)
295  * @objective       test provisionPairwiseDevices negatively calling the API multiple times.
296  * @target          OCStackResult provisionPairwiseDevices(const Credential &cred, const OicSecAcl_t* acl1, const OCSecureResource &device2, const OicSecAcl_t* acl2, ResultCallBack resultCallback)
297  * @test_data       Regular data for provisionPairwiseDevices
298  * @pre_condition   start two justworks simulators
299  * @procedure       1. call provisionInit
300  *                  2. call discoverOwnedDevices
301  *                  3. call provisionPairwiseDevices
302  *                  4. call provisionPairwiseDevices
303  *                  5. call deleteACLList for m_acl1
304  *                  6. call deleteACLList for m_acl2
305  * @post_condition  None
306  * @expected        provisionPairwiseDevices will return OC_STACK_INVALID_PARAM while calling for the second time.
307  */
308 #if defined(__LINUX__) || defined(__TIZEN__) || defined(__WINDOWS__)
309 TEST_F(PMCppTest_stc, ProvisionPairwiseDevicesMultipleTimes_EG_N)
310 {
311     m_acl1 = (OicSecAcl_t *)OICCalloc(1,sizeof(OicSecAcl_t));
312     PMCppHelper::createAcl(m_acl1, DEVICE_INDEX_ONE, FULL_PERMISSION, m_OwnedDevList);
313
314     m_acl2 = (OicSecAcl_t *)OICCalloc(1,sizeof(OicSecAcl_t));
315     PMCppHelper::createAcl(m_acl2, DEVICE_INDEX_ONE, FULL_PERMISSION, m_OwnedDevList);
316
317     Credential cred(SYMMETRIC_PAIR_WISE_KEY , OWNER_PSK_LENGTH_128);
318
319     if(!m_PMCppHelper.provisionPairwiseDevices(m_OwnedDevList, cred, m_acl1, *m_OwnedDevList[1].get(), m_acl1, PMCppCallbackHelper::provisionPostCB, OC_STACK_OK))
320     {
321         SET_FAILURE(m_PMCppHelper.getFailureMessage());
322         return;
323     }
324
325     if(!m_PMCppHelper.provisionPairwiseDevices(m_OwnedDevList, cred, m_acl1, *m_OwnedDevList[1].get(), m_acl1, PMCppCallbackHelper::provisionPostCB, OC_STACK_INVALID_PARAM))
326     {
327         SET_FAILURE(m_PMCppHelper.getFailureMessage());
328     }
329
330     m_PMCppHelper.deleteACLList(m_acl1);
331     m_PMCppHelper.deleteACLList(m_acl2);
332 }
333 #endif
334
335 /**
336  * @since           2017-03-07
337  * @see             static OCStackResult provisionInit(const std::string& dbPath)
338  * @objective       test setInputPinCallback negatively with call mutiple times
339  * @target          static OCStackResult setInputPinCallback(InputPinCallback inputPin)
340  * @test_data       call mutiple times
341  * @pre_condition   start two justworks simulators
342  * @procedure       1. call provisionInit
343  *                  2. call setInputPinCallback
344  *                  3. call setInputPinCallback
345  * @post_condition  None
346  * @expected        setInputPinCallback will return OC_STACK_DUPLICATE_REQUEST
347  */
348 #if defined(__LINUX__) || defined(__TIZEN__) || defined(__WINDOWS__)
349 TEST_F(PMCppTest_stc, setInputPinCallback_ALVC_N)
350 {
351     if(!m_PMCppHelper.setInputPinCallback((InputPinCallback)PMCppHelper::inputPinCB, OC_STACK_OK))
352     {
353         SET_FAILURE(m_PMCppHelper.getFailureMessage());
354         return;
355     }
356
357     if(!m_PMCppHelper.setInputPinCallback((InputPinCallback)PMCppHelper::inputPinCB, OC_STACK_DUPLICATE_REQUEST))
358     {
359         SET_FAILURE(m_PMCppHelper.getFailureMessage());
360         return;
361     }
362 }
363 #endif
364
365 /**
366  * @since           2017-03-07
367  * @see             static OCStackResult provisionInit(const std::string& dbPath)
368  * @objective       test registerInputPinCallback negatively with call mutiple times
369  * @target          static OCStackResult registerInputPinCallback(InputPinCB inputPinCB, InputPinCallbackHandle* inputPinCallbackHandle);
370  * @test_data       inputPinCallbackHandle = NULL
371  * @pre_condition   start two justworks simulators
372  * @procedure       1. call provisionInit
373  *                  2. call registerInputPinCallback
374  *                  3. call registerInputPinCallback
375  * @post_condition  None
376  * @expected        registerInputPinCallback will return OC_STACK_DUPLICATE_REQUEST
377  */
378 #if defined(__LINUX__) || defined(__TIZEN__) || defined(__WINDOWS__)
379 TEST_F(PMCppTest_stc, registerInputPinCallback_ALVC_N)
380 {
381     if(!m_PMCppHelper.registerInputPinCallback(PMCppHelper::OnInputPinCB, &callbackHandle, OC_STACK_OK))
382     {
383         SET_FAILURE(m_PMCppHelper.getFailureMessage());
384         return;
385     }
386
387     if(!m_PMCppHelper.registerInputPinCallback(PMCppHelper::OnInputPinCB, &callbackHandle, OC_STACK_DUPLICATE_REQUEST))
388     {
389         SET_FAILURE(m_PMCppHelper.getFailureMessage());
390         return;
391     }
392 }
393 #endif
394
395 /**
396  * @since           2015-11-30
397  * @see             static OCStackResult provisionInit(const std::string& dbPath)
398  * @objective       test setDisplayPinCB negativelycall multiple times
399  * @target          static OCStackResult setDisplayPinCB(GeneratePinCallback displayPin)
400  * @test_data       regular data
401  * @pre_condition   start two justworks simulators
402  * @procedure       1. call provisionInit
403  *                  2. call setDisplayPinCB
404  *                  3. call setDisplayPinCB
405  * @post_condition  None
406  * @expected        setDisplayPinCB will return OC_STACK_DUPLICATE_REQUEST
407  */
408 #if defined(__LINUX__) || defined(__TIZEN__) || defined(__WINDOWS__)
409 TEST_F(PMCppTest_stc, SetDisplayPinCB_ALVC_N)
410 {
411     if(!m_PMCppHelper.setDisplayPinCB((GeneratePinCallback)PMCppHelper::inputPinCB, OC_STACK_OK))
412     {
413         SET_FAILURE(m_PMCppHelper.getFailureMessage());
414         return;
415     }
416
417     if(!m_PMCppHelper.setDisplayPinCB((GeneratePinCallback)PMCppHelper::inputPinCB, OC_STACK_DUPLICATE_REQUEST))
418     {
419         SET_FAILURE(m_PMCppHelper.getFailureMessage());
420         return;
421     }
422 }
423 #endif
424
425 /**
426  * @since           2017-03-16
427  * @see             static OCStackResult provisionInit(const std::string& dbPath)
428  * @objective       test registerInputPinCallback Negatively multiple times
429  * @target          static OCStackResult registerDisplayPinCallback(DisplayPinCB displayPinCB, DisplayPinCallbackHandle* displayPinCallbackHandle);
430  * @test_data       regular data
431  * @pre_condition   start two justworks simulators
432  * @procedure       1. call provisionInit
433  *                  2. call registerDisplayPinCallback
434  *                  3. call registerDisplayPinCallback
435  * @post_condition  None
436  * @expected        registerDisplayPinCallback will return OC_STACK_DUPLICATE_REQUEST
437  */
438 #if defined(__LINUX__) || defined(__TIZEN__) || defined(__WINDOWS__)
439 TEST_F(PMCppTest_stc, registerDisplayPinCallbackMultipleTimes_ALVC_N)
440 {
441     if(!m_PMCppHelper.registerDisplayPinCallback(PMCppHelper::OnDisplayPinCB, &displayPinCallbackHandle, OC_STACK_OK))
442     {
443         SET_FAILURE(m_PMCppHelper.getFailureMessage());
444         return;
445     }
446
447     if(!m_PMCppHelper.registerDisplayPinCallback(PMCppHelper::OnDisplayPinCB, &displayPinCallbackHandle, OC_STACK_DUPLICATE_REQUEST))
448     {
449         SET_FAILURE(m_PMCppHelper.getFailureMessage());
450         return;
451     }
452 }
453 #endif