replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / security / unittest / securityresourcemanager.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 Intel Mobile Communications GmbH 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 "iotivity_config.h"
22 #include "gtest/gtest.h"
23 #ifdef HAVE_PWD_H
24 #include <pwd.h>
25 #endif
26 #ifdef HAVE_GRP_H
27 #include <grp.h>
28 #endif
29 #ifdef HAVE_LINUX_LIMITS_H
30 #include <linux/limits.h>
31 #endif
32 #include "ocstack.h"
33 #include "cainterface.h"
34 #include "secureresourcemanager.h"
35 #include "srmtestcommon.h"
36
37 using namespace std;
38
39 // Helper Methods
40 void UTRequestHandler(const CAEndpoint_t * /*endPoint*/,
41                       const CARequestInfo_t * /*requestInfo*/)
42 {
43     EXPECT_TRUE(true) << "UTRequestHandler\n";
44 }
45
46 void UTResponseHandler(const CAEndpoint_t * /*endPoint*/,
47                        const CAResponseInfo_t * /*responseInfo*/)
48 {
49      EXPECT_TRUE(true) << "UTResponseHandler\n";
50 }
51
52 void UTErrorHandler(const CAEndpoint_t * /*endPoint*/,
53                     const CAErrorInfo_t * /*errorInfo*/)
54 {
55      EXPECT_TRUE(true) << "UTErrorHandler\n";
56 }
57
58 static OCPersistentStorage gpsi;
59
60 //RegisterHandler Tests
61 TEST(RegisterHandlerTest, RegisterNullRequestHandler)
62 {
63     EXPECT_EQ(OC_STACK_INVALID_PARAM, SRMRegisterHandler(NULL, UTResponseHandler, NULL));
64 }
65
66 TEST(RegisterHandlerTest, RegisterNullResponseHandler)
67 {
68     EXPECT_EQ(OC_STACK_INVALID_PARAM, SRMRegisterHandler(UTRequestHandler, NULL, NULL));
69 }
70
71 TEST(RegisterHandlerTest, RegisterNullHandler)
72 {
73     EXPECT_EQ(OC_STACK_INVALID_PARAM, SRMRegisterHandler(NULL, NULL, NULL));
74 }
75
76 TEST(RegisterHandlerTest, RegisterValidHandler)
77 {
78     EXPECT_EQ(OC_STACK_OK, SRMRegisterHandler(UTRequestHandler, UTResponseHandler, UTErrorHandler));
79 }
80
81 // PersistentStorageHandler Tests
82 TEST(PersistentStorageHandlerTest, RegisterNullHandler)
83 {
84     EXPECT_EQ(OC_STACK_INVALID_PARAM,
85             SRMRegisterPersistentStorageHandler(NULL));
86 }
87
88 TEST(PersistentStorageHandlerTest, RegisterValidHandler)
89 {
90     SetPersistentHandler(&gpsi, true);
91     OCPersistentStorage *ps = SRMGetPersistentStorageHandler();
92     EXPECT_TRUE(&gpsi == ps);
93 }
94
95 #if !(defined(HAVE_LINUX_LIMITS_H) && defined(HAVE_PWD_H))
96 TEST(PersistentStorageHandlerTest, DISABLED_PersistentStorageValidHandlers)
97 {
98     /** @todo: Implement test on non-Linux platform */
99 }
100 #else
101 TEST(PersistentStorageHandlerTest, PersistentStorageValidHandlers)
102 {
103     OCPersistentStorage *psi = SRMGetPersistentStorageHandler();
104     EXPECT_TRUE(psi != NULL);
105
106     unsigned char buf[PATH_MAX];
107     FILE* streamIn = NULL;
108     FILE* streamOut = NULL;
109     struct passwd *pw = getpwuid(getuid());
110     const char *homeDir = pw->pw_dir;
111     char inFilePath [PATH_MAX];
112     char outFilePath [PATH_MAX];
113     snprintf(inFilePath, PATH_MAX, "%s/iotivity/Readme.scons.txt", homeDir );
114     snprintf(outFilePath, PATH_MAX, "%s/Downloads/Readme.scons.out.txt", homeDir );
115
116     streamIn = psi->open(inFilePath, "r");
117     streamOut = psi->open(outFilePath, "w");
118
119     if (streamIn && streamOut)
120     {
121         size_t value = 1;
122         while (value)
123         {
124             value = psi->read(buf, 1, sizeof(buf), streamIn);
125             psi->write(buf, 1, value, streamOut);
126         }
127     }
128
129     if (streamIn)
130     {
131         psi->close(streamIn);
132     }
133     if (streamOut)
134     {
135         psi->close(streamOut);
136     }
137     psi->unlink(outFilePath);
138 }
139 #endif
140