529c8b874312e5950a615a8fec53145cc490a558
[iotivity.git] / resource / provisioning / unittests / OCProvisioningTest.cpp
1 /* *****************************************************************
2  *
3  * Copyright 2015 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 <ocstack.h>
22 #include <oic_malloc.h>
23 #include <OCApi.h>
24 #include <OCPlatform_impl.h>
25 #include <oxmjustworks.h>
26 #include <oxmrandompin.h>
27 #include <srmutility.h>
28 #include <OCProvisioningManager.hpp>
29 #include <gtest/gtest.h>
30 #include "provisioningdatabasemanager.h"
31
32 #define TIMEOUT 5
33 #define DB_FILE "./dbPath.tmp"
34
35 namespace OCProvisioningTest
36 {
37     using namespace OC;
38
39     void resultCallback(PMResultList_t *result, int hasError)
40     {
41         (void)result;
42         (void)hasError;
43     }
44
45     TEST(ProvisionInitTest, TestWithEmptyPath)
46     {
47         std::string dbPath("");
48         EXPECT_EQ(OC_STACK_OK, OCSecure::provisionInit(dbPath));
49         EXPECT_EQ(OC_STACK_OK, OCSecure::provisionClose());
50     }
51
52     TEST(ProvisionInitTest, TestValidPath)
53     {
54         std::string dbPath(DB_FILE);
55         EXPECT_EQ(OC_STACK_OK, OCSecure::provisionInit(dbPath));
56         EXPECT_EQ(OC_STACK_OK, OCSecure::provisionClose());
57     }
58
59     TEST(DiscoveryTest, SecureResource)
60     {
61         std::shared_ptr< OC::OCSecureResource > secureResource;
62         OicUuid_t uuid;
63         ConvertStrToUuid("11111111-1111-1111-1111-111111111111", &uuid);
64
65         EXPECT_EQ(OC_STACK_OK, OCSecure::discoverSingleDevice(TIMEOUT, &uuid, secureResource));
66     }
67
68     TEST(DiscoveryTest, SecureResourceZeroTimeout)
69     {
70         std::shared_ptr< OC::OCSecureResource > secureResource;
71         OicUuid_t uuid;
72         ConvertStrToUuid("11111111-1111-1111-1111-111111111111", &uuid);
73
74         EXPECT_EQ(OC_STACK_INVALID_PARAM, OCSecure::discoverSingleDevice(0, &uuid, secureResource));
75     }
76
77     TEST(DiscoveryTest, UnownedDevices)
78     {
79         DeviceList_t list;
80         EXPECT_EQ(OC_STACK_OK, OCSecure::discoverUnownedDevices(TIMEOUT, list));
81     }
82
83     TEST(DiscoveryTest, UnownedDevicesZeroTimeout)
84     {
85         DeviceList_t list;
86         EXPECT_EQ(OC_STACK_INVALID_PARAM, OCSecure::discoverUnownedDevices(0, list));
87     }
88
89 #ifdef MULTIPLE_OWNER
90     TEST(MOTDiscoveryTest, MultipleOwnerEnabledDevices)
91     {
92         DeviceList_t list;
93         EXPECT_EQ(OC_STACK_OK, OCSecure::discoverMultipleOwnerEnabledDevices(TIMEOUT, list));
94     }
95
96     TEST(MOTDiscoveryTest, MultipleOwnerEnabledDevicesZeroTimeOut)
97     {
98         DeviceList_t list;
99         EXPECT_EQ(OC_STACK_INVALID_PARAM, OCSecure::discoverMultipleOwnerEnabledDevices(0, list));
100     }
101
102     TEST(MOTDiscoveryTest, MultipleOwnedDevices)
103     {
104         DeviceList_t list;
105         EXPECT_EQ(OC_STACK_OK, OCSecure::discoverMultipleOwnedDevices(TIMEOUT, list));
106     }
107
108     TEST(MOTDiscoveryTest, MultipleOwnedDevicesZeroTimeOut)
109     {
110         DeviceList_t list;
111         EXPECT_EQ(OC_STACK_INVALID_PARAM, OCSecure::discoverMultipleOwnedDevices(0, list));
112     }
113 #endif
114
115     TEST(DiscoveryTest, OwnedDevices)
116     {
117         DeviceList_t list;
118         EXPECT_EQ(OC_STACK_OK, OCSecure::discoverOwnedDevices(TIMEOUT, list));
119     }
120
121     TEST(DiscoveryTest, OwnedDevicesZeroTimeout)
122     {
123         DeviceList_t list;
124         EXPECT_EQ(OC_STACK_INVALID_PARAM, OCSecure::discoverOwnedDevices(0, list));
125     }
126
127     TEST(OwnershipTest, OwnershipTransferNullCallback)
128     {
129         OCSecureResource device;
130         EXPECT_EQ(OC_STACK_INVALID_CALLBACK, device.doOwnershipTransfer(nullptr));
131     }
132
133 #ifdef MULTIPLE_OWNER
134     TEST(MOTOwnershipTest, MOTOwnershipTransferNullCallback)
135     {
136         OCSecureResource device;
137         EXPECT_EQ(OC_STACK_INVALID_CALLBACK, device.doMultipleOwnershipTransfer(nullptr));
138     }
139
140     TEST(selectMOTMethodTest, selectMOTMethodNullCallback)
141     {
142         OCSecureResource device;
143         const OicSecOxm_t stsecOxm = OIC_PRECONFIG_PIN;
144         EXPECT_EQ(OC_STACK_INVALID_CALLBACK, device.selectMOTMethod(stsecOxm, nullptr));
145     }
146
147     TEST(changeMOTModeTest, changeMOTModeNullCallback)
148     {
149         OCSecureResource device;
150         const OicSecMomType_t momType = OIC_MULTIPLE_OWNER_ENABLE;
151         EXPECT_EQ(OC_STACK_INVALID_CALLBACK, device.changeMOTMode(momType, nullptr));
152     }
153
154     TEST(addPreconfigPINTest, addPreconfigPINNullPin)
155     {
156         OCSecureResource device;
157         EXPECT_EQ(OC_STACK_INVALID_PARAM, device.addPreconfigPIN(nullptr, 0));
158     }
159
160     TEST(provisionPreconfPinTest, provisionPreconfPinNullCallback)
161     {
162         OCSecureResource device;
163         const char *pin = "test";
164         size_t PinLength = 4;
165         EXPECT_EQ(OC_STACK_INVALID_CALLBACK, device.provisionPreconfPin(pin, PinLength, nullptr));
166     }
167
168     TEST(isMOTEnabledTest, isMOTEnabledWithoutDeviceInst)
169     {
170         OCSecureResource device;
171         EXPECT_FALSE(device.isMOTEnabled());
172     }
173
174     TEST(isMOTSupportedTest, isMOTSupportedWithoutDeviceInst)
175     {
176         OCSecureResource device;
177         EXPECT_FALSE(device.isMOTSupported());
178     }
179
180     TEST(getMOTMethodTest, getMOTMethodNullOxM)
181     {
182         OCSecureResource device;
183         EXPECT_EQ(OC_STACK_INVALID_PARAM, device.getMOTMethod(nullptr));
184     }
185 #endif
186
187     TEST(DeviceInfoTest, DevInfoFromNetwork)
188     {
189         std::string dbPath(DB_FILE);
190         EXPECT_EQ(OC_STACK_OK, OCSecure::provisionInit(dbPath));
191         DeviceList_t owned, unowned;
192         EXPECT_EQ(OC_STACK_OK, OCSecure::getDevInfoFromNetwork(TIMEOUT,
193                     owned, unowned));
194         EXPECT_EQ(OC_STACK_OK, OCSecure::provisionClose());
195     }
196
197     TEST(SetDisplayPinCBTest, SetDisplayPinCBTestNullCB)
198     {
199         EXPECT_EQ(OC_STACK_INVALID_PARAM, OCSecure::setDisplayPinCB(nullptr));
200     }
201
202     TEST(ProvisionAclTest, ProvisionAclTestNullAcl)
203     {
204         OCSecureResource device;
205         EXPECT_EQ(OC_STACK_INVALID_PARAM, device.provisionACL(nullptr, resultCallback));
206     }
207
208     TEST(ProvisionAclTest, ProvisionAclTestNullCallback)
209     {
210         OCSecureResource device;
211         OicSecAcl_t *acl = (OicSecAcl_t *)OICCalloc(1,sizeof(OicSecAcl_t));
212         EXPECT_EQ(OC_STACK_INVALID_CALLBACK, device.provisionACL(acl, nullptr));
213         OICFree(acl);
214     }
215
216     TEST(ProvisionAclTest, ProvisionAclTestNullCallbackNUllAcl)
217     {
218         OCSecureResource device;
219         EXPECT_EQ(OC_STACK_INVALID_PARAM, device.provisionACL(nullptr, nullptr));
220     }
221
222     TEST(ProvisionCredTest, ProvisionCredTestNullCallback)
223     {
224         OCSecureResource device, dev2;
225         Credential cred;
226         EXPECT_EQ(OC_STACK_INVALID_CALLBACK, device.provisionCredentials(cred, dev2, nullptr));
227     }
228
229     TEST(ProvisionPairwiseTest, ProvisionPairwiseTestNullCallback)
230     {
231         OCSecureResource device, dev2;
232         Credential cred;
233         OicSecAcl_t *acl1 = (OicSecAcl_t *)OICCalloc(1,sizeof(OicSecAcl_t));
234         OicSecAcl_t *acl2 = (OicSecAcl_t *)OICCalloc(1,sizeof(OicSecAcl_t));
235         EXPECT_EQ(OC_STACK_INVALID_CALLBACK, device.provisionPairwiseDevices(cred, acl1,
236                     dev2, acl2, nullptr));
237         OICFree(acl1);
238         OICFree(acl2);
239     }
240
241 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
242     TEST(setDeviceIdSeed, NullParam)
243     {
244         EXPECT_EQ(OC_STACK_INVALID_PARAM, OCSecure::setDeviceIdSeed(NULL, 0));
245     }
246
247     TEST(setDeviceIdSeed, InvalidParam)
248     {
249         uint8_t seed[1024] = {0};
250
251         EXPECT_EQ(OC_STACK_INVALID_PARAM, OCSecure::setDeviceIdSeed(seed, 0));
252         EXPECT_EQ(OC_STACK_INVALID_PARAM, OCSecure::setDeviceIdSeed(seed, sizeof(seed)));
253     }
254
255     TEST(setDeviceIdSeed, ValidValue)
256     {
257         uint8_t seed[16] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
258                             0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10};
259         EXPECT_EQ(OC_STACK_OK, OCSecure::setDeviceIdSeed(seed, sizeof(seed)));
260     }
261 #endif // __WITH_DTLS__ || __WITH_TLS__
262 }