Merge test folder of test branch
[iotivity.git] / test / src / tc / es / gtest / src / btc / mediator / ESEnrolleeSecurityTest.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 <gtest/gtest.h>
22 #include "CommonTestUtil.h"
23 #include <atomic>
24 #include <functional>
25 #include <condition_variable>
26 #include <mutex>
27 #include <chrono>
28 #include <exception>
29 #include "easysetup.h"
30 #include <escommon.h>
31 #include "ESEnrolleeCommon.h"
32 #include <iostream>
33 #include "ESMediatorHelper.h"
34 #include "OCRepresentation.h"
35
36 #define CBOR_DB_PATH "./oic_svr_db_client.dat"
37
38 using namespace std;
39 using namespace OC;
40 using namespace OIC::Service;
41
42
43 static std::shared_ptr< OC::OCResource > g_resource;
44
45 static FILE* clientOpen(const char *UNUSED_PARAM, const char *mode)
46 {
47     (void) UNUSED_PARAM;
48     return fopen(CBOR_DB_PATH, mode);
49 }
50
51 class ESEnrolleeSecurityTest_btc: public ::testing::Test
52 {
53 public:
54
55 protected:
56
57     virtual void SetUp()
58     {
59         CommonTestUtil::runCommonTCSetUpPart();
60         vector< string > types =
61         { RES_TYPE };
62         vector< string > ifaces =
63         { INTERFACE_TYPE };
64
65         OCPersistentStorage ps
66         { clientOpen, fread, fwrite, fclose, unlink };
67
68         PlatformConfig cfg
69         { OC::ServiceType::InProc, OC::ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos,
70                 &ps };
71
72         OCPlatform::Configure(cfg);
73
74         if (OCSecure::provisionInit(EMPTY_STRING) != OC_STACK_OK)
75         {
76             return false;
77         }
78
79         g_resource = OCPlatform::constructResourceObject(HOST_ADDRESS, RES_ADDRESS, CT_DEFAULT,
80         false, types, ifaces);
81     }
82
83     virtual void TearDown()
84     {
85         CommonTestUtil::runCommonTCTearDownPart();
86     }
87
88 };
89
90 /**
91  * @since 2016-08-16
92  * @objective Test 'registerCallbackHandler' API with negative basic way
93  * @target Void registerCallbackHandler(SecurityProvStatusCb securityProvStatusCb,
94  SecurityPinCb securityPinCb, SecProvisioningDbPathCb secProvisioningDbPathCb);
95  * @test_data    None
96  * @pre_condition None
97  * @procedure Perform registerCallbackHandler()
98  * @post_condition None
99  * @expected successfully called api
100  **/
101 /*
102 TEST_F(ESEnrolleeSecurityTest_btc, registerCallbackHandler_ETC_N)
103 {
104     try
105     {
106         std::shared_ptr < EnrolleeSecurity > m_enrolleeSecurity;
107         m_enrolleeSecurity = std::make_shared < EnrolleeSecurity > (g_resource, "");
108         m_enrolleeSecurity->registerCallbackHandler(NULL, NULL, NULL);
109     }
110     catch (exception& e)
111     {
112         SET_FAILURE("Exception occurred in OwnershipTransfer: " + std::string(e.what()));
113     }
114 }*/
115
116 /**
117  * @since 2016-08-16
118  * @objective Test 'getUUID' API with positive basic way
119  * @target Void getUUID();
120  * @test_data    None
121  * @pre_condition None
122  * @procedure Perform getUUID()
123  * @post_condition None
124  * @expected successfully called api
125  **/
126 TEST_F(ESEnrolleeSecurityTest_btc, getUUID_SRC_P)
127 {
128     try
129     {
130         std::shared_ptr < EnrolleeSecurity > m_enrolleeSecurity;
131         m_enrolleeSecurity = std::make_shared < EnrolleeSecurity > (g_resource);
132         std::shared_ptr < SecProvisioningStatus > securityProvisioningStatus = std::make_shared
133                 < SecProvisioningStatus > (m_enrolleeSecurity->getUUID(), ES_ERROR);
134
135         if (securityProvisioningStatus == NULL)
136         {
137             SET_FAILURE("Security Provisioning Status  is null.");
138         }
139     }
140     catch (exception& e)
141     {
142         SET_FAILURE("Exception occurred in OwnershipTransfer: " + std::string(e.what()));
143     }
144 }
145
146 /**
147  * @since 2016-08-16
148  * @objective Test 'getUUID' API with positive basic way
149  * @target Void getUUID();
150  * @test_data    None
151  * @pre_condition None
152  * @procedure Perform getUUID()
153  * @post_condition None
154  * @expected successfully called api
155  **/
156 TEST_F(ESEnrolleeSecurityTest_btc, getUUID_ETC_N)
157 {
158     try
159     {
160         std::shared_ptr < EnrolleeSecurity > m_enrolleeSecurity;
161         m_enrolleeSecurity = std::make_shared < EnrolleeSecurity > (g_resource);
162         if (m_enrolleeSecurity->getUUID() != "")
163         {
164             SET_FAILURE("Expected UUID not found");
165         }
166     }
167     catch (exception& e)
168     {
169         SET_FAILURE("Exception occurred in OwnershipTransfer: " + std::string(e.what()));
170     }
171 }
172
173 /**
174  * @since 2016-09-01
175  * @objective Test 'provisionOwnership' API with positive basic way
176  * @target Void provisionOwnership();
177  * @test_data    None
178  * @pre_condition None
179  * @procedure Perform provisionOwnership()
180  * @post_condition None
181  * @expected successfully called api.
182  **/
183 TEST_F(ESEnrolleeSecurityTest_btc, provisionOwnership_SRC_P)
184 {
185     std::shared_ptr < EnrolleeSecurity > m_enrolleeSecurity;
186     m_enrolleeSecurity = std::make_shared < EnrolleeSecurity > (g_resource);
187     if (m_enrolleeSecurity->provisionOwnership(NULL) != ES_OK)
188         {
189             SET_FAILURE("Provision Ownership is null");
190         }
191 }
192
193 /**
194  * @since 2016-08-16
195  * @objective Test 'provisionOwnership' API with negative basic way
196  * @target Void provisionOwnership();
197  * @test_data    None
198  * @pre_condition None
199  * @procedure Perform provisionOwnership()
200  * @post_condition None
201  * @expected API should through ES_SECURE_RESOURCE_DISCOVERY_FAILURE
202  **/
203 TEST_F(ESEnrolleeSecurityTest_btc, provisionOwnership_ETC_N)
204 {
205     try
206     {
207         std::shared_ptr < EnrolleeSecurity > m_enrolleeSecurity;
208         m_enrolleeSecurity = std::make_shared < EnrolleeSecurity > (g_resource);
209         ASSERT_EQ(ES_SECURE_RESOURCE_DISCOVERY_FAILURE, m_enrolleeSecurity->provisionOwnership(NULL))<<"Expect result ES_SECURE_RESOURCE_DISCOVERY_FAILURE is not found.";
210     }
211     catch (exception& e)
212     {
213         SET_FAILURE("Exception occurred in provisionOwnership: " + std::string(e.what()));
214     }
215 }