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