7acd145e1361e12b003df03b33aba43be4892f0a
[platform/upstream/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 <OCProvisioningManager.h>
28 #include <gtest/gtest.h>
29
30 #define TIMEOUT 5
31
32 namespace OCProvisioningTest
33 {
34     using namespace OC;
35
36     void resultCallback(PMResultList_t *result, int hasError)
37     {
38         (void)result;
39         (void)hasError;
40     }
41
42     TEST(ProvisionInitTest, TestWithEmptyPath)
43     {
44         std::string dbPath("");
45         EXPECT_EQ(OC_STACK_OK, OCSecure::provisionInit(dbPath));
46     }
47
48     TEST(ProvisionInitTest, TestValidPath)
49     {
50         std::string dbPath("./dbPath");
51         EXPECT_EQ(OC_STACK_OK, OCSecure::provisionInit(dbPath));
52     }
53
54     TEST(DiscoveryTest, UnownedDevices)
55     {
56         DeviceList_t list;
57         EXPECT_EQ(OC_STACK_OK, OCSecure::discoverUnownedDevices(TIMEOUT, list));
58     }
59
60     TEST(DiscoveryTest, UnownedDevicesZeroTimeout)
61     {
62         DeviceList_t list;
63         EXPECT_EQ(OC_STACK_OK, OCSecure::discoverUnownedDevices(0, list));
64     }
65
66     TEST(DiscoveryTest, OwnedDevices)
67     {
68         DeviceList_t list;
69         EXPECT_EQ(OC_STACK_OK, OCSecure::discoverOwnedDevices(TIMEOUT, list));
70     }
71
72     TEST(DiscoveryTest, OwnedDevicesZeroTimeout)
73     {
74         DeviceList_t list;
75         EXPECT_EQ(OC_STACK_OK, OCSecure::discoverOwnedDevices(0, list));
76     }
77
78     TEST(OwnershipTest, SetOwnershipTransferCBDataNull)
79     {
80         EXPECT_EQ(OC_STACK_INVALID_PARAM, OCSecure::setOwnerTransferCallbackData(
81                     OIC_JUST_WORKS, NULL, NULL));
82     }
83
84     TEST(OwnershipTest, SetOwnershipTransferCBData)
85     {
86         OTMCallbackData_t justWorksCBData;
87         justWorksCBData.loadSecretCB = LoadSecretJustWorksCallback;
88         justWorksCBData.createSecureSessionCB = CreateSecureSessionJustWorksCallback;
89         justWorksCBData.createSelectOxmPayloadCB = CreateJustWorksSelectOxmPayload;
90         justWorksCBData.createOwnerTransferPayloadCB = CreateJustWorksOwnerTransferPayload;
91         EXPECT_EQ(OC_STACK_OK, OCSecure::setOwnerTransferCallbackData(OIC_JUST_WORKS,
92                                         &justWorksCBData, NULL));
93     }
94
95     TEST(OwnershipTest, SetOwnershipTransferCBDataInvalidType)
96     {
97         OTMCallbackData_t justWorksCBData;
98         justWorksCBData.loadSecretCB = LoadSecretJustWorksCallback;
99         justWorksCBData.createSecureSessionCB = CreateSecureSessionJustWorksCallback;
100         justWorksCBData.createSelectOxmPayloadCB = CreateJustWorksSelectOxmPayload;
101         justWorksCBData.createOwnerTransferPayloadCB = CreateJustWorksOwnerTransferPayload;
102         EXPECT_EQ(OC_STACK_INVALID_PARAM, OCSecure::setOwnerTransferCallbackData(OIC_OXM_COUNT,
103                                         &justWorksCBData, NULL));
104     }
105
106     TEST(OwnershipTest, SetOwnershipTransferCBDataNullInputPin)
107     {
108         OTMCallbackData_t pinBasedCBData;
109         pinBasedCBData.loadSecretCB = InputPinCodeCallback;
110         pinBasedCBData.createSecureSessionCB = CreateSecureSessionRandomPinCallback;
111         pinBasedCBData.createSelectOxmPayloadCB = CreatePinBasedSelectOxmPayload;
112         pinBasedCBData.createOwnerTransferPayloadCB = CreatePinBasedOwnerTransferPayload;
113         OTMSetOwnershipTransferCallbackData(OIC_RANDOM_DEVICE_PIN, &pinBasedCBData);
114
115         EXPECT_EQ(OC_STACK_INVALID_PARAM, OCSecure::setOwnerTransferCallbackData(
116                     OIC_RANDOM_DEVICE_PIN, &pinBasedCBData, NULL));
117     }
118
119     TEST(OwnershipTest, OwnershipTransferNullCallback)
120     {
121         OCSecureResource device;
122         EXPECT_EQ(OC_STACK_INVALID_PARAM, device.doOwnershipTransfer(nullptr));
123     }
124
125     TEST(DeviceInfoTest, DevInfoFromNetwork)
126     {
127         DeviceList_t owned, unowned;
128         EXPECT_EQ(OC_STACK_OK, OCSecure::getDevInfoFromNetwork(TIMEOUT,
129                     owned, unowned));
130     }
131
132     TEST(SetDisplayPinCBTest, SetDisplayPinCBTestNullCB)
133     {
134         EXPECT_EQ(OC_STACK_INVALID_PARAM, OCSecure::setDisplayPinCB(nullptr));
135     }
136
137     TEST(ProvisionAclTest, ProvisionAclTestNullAcl)
138     {
139         OCSecureResource device;
140         EXPECT_EQ(OC_STACK_INVALID_PARAM, device.provisionACL(nullptr, resultCallback));
141     }
142
143     TEST(ProvisionAclTest, ProvisionAclTestNullCallback)
144     {
145         OCSecureResource device;
146         OicSecAcl_t *acl = (OicSecAcl_t *)OICCalloc(1,sizeof(OicSecAcl_t));
147         EXPECT_EQ(OC_STACK_INVALID_PARAM, device.provisionACL(acl, nullptr));
148         OICFree(acl);
149     }
150
151     TEST(ProvisionAclTest, ProvisionAclTestNullCallbackNUllAcl)
152     {
153         OCSecureResource device;
154         EXPECT_EQ(OC_STACK_INVALID_PARAM, device.provisionACL(nullptr, nullptr));
155     }
156
157     TEST(ProvisionCredTest, ProvisionCredTestNullCallback)
158     {
159         OCSecureResource device, dev2;
160         Credential cred;
161         EXPECT_EQ(OC_STACK_INVALID_PARAM, device.provisionCredentials(cred, dev2, nullptr));
162     }
163
164     TEST(ProvisionPairwiseTest, ProvisionPairwiseTestNullCallback)
165     {
166         OCSecureResource device, dev2;
167         Credential cred;
168         OicSecAcl_t *acl1 = (OicSecAcl_t *)OICCalloc(1,sizeof(OicSecAcl_t));
169         OicSecAcl_t *acl2 = (OicSecAcl_t *)OICCalloc(1,sizeof(OicSecAcl_t));
170         EXPECT_EQ(OC_STACK_INVALID_PARAM, device.provisionPairwiseDevices(cred, acl1,
171                     dev2, acl2, nullptr));
172         OICFree(acl1);
173         OICFree(acl2);
174     }
175
176 }