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