Imported Upstream version 1.2.0
[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
36 using namespace std;
37
38 // Helper Methods
39 void UTRequestHandler(const CAEndpoint_t * /*endPoint*/,
40                       const CARequestInfo_t * /*requestInfo*/)
41 {
42     EXPECT_TRUE(true) << "UTRequestHandler\n";
43 }
44
45 void UTResponseHandler(const CAEndpoint_t * /*endPoint*/,
46                        const CAResponseInfo_t * /*responseInfo*/)
47 {
48      EXPECT_TRUE(true) << "UTResponseHandler\n";
49 }
50
51 void UTErrorHandler(const CAEndpoint_t * /*endPoint*/,
52                     const CAErrorInfo_t * /*errorInfo*/)
53 {
54      EXPECT_TRUE(true) << "UTErrorHandler\n";
55 }
56
57 FILE *utopen(const char *path, const char *mode)
58 {
59     EXPECT_TRUE((path != NULL)) << "utopen\n";
60     FILE *stream = fopen(path, mode);
61     return stream;
62
63 }
64
65 size_t utread(void *ptr, size_t size, size_t nmemb, FILE *stream)
66 {
67     return fread(ptr, size, nmemb, stream);
68 }
69
70 size_t utwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
71 {
72     return fwrite(ptr, size, nmemb, stream);
73 }
74
75 int utclose(FILE *fp)
76 {
77     EXPECT_TRUE((fp != NULL)) << "utclose\n";
78     return fclose(fp);
79 }
80 int utunlink(const char *path)
81 {
82     EXPECT_TRUE((path != NULL)) << "utunlink\n";
83     return unlink(path);
84 }
85 static OCPersistentStorage gpsi;
86
87 //RegisterHandler Tests
88 TEST(RegisterHandlerTest, RegisterNullRequestHandler)
89 {
90     EXPECT_EQ(OC_STACK_INVALID_PARAM, SRMRegisterHandler(NULL, UTResponseHandler, NULL));
91 }
92
93 TEST(RegisterHandlerTest, RegisterNullResponseHandler)
94 {
95     EXPECT_EQ(OC_STACK_INVALID_PARAM, SRMRegisterHandler(UTRequestHandler, NULL, NULL));
96 }
97
98 TEST(RegisterHandlerTest, RegisterNullHandler)
99 {
100     EXPECT_EQ(OC_STACK_INVALID_PARAM, SRMRegisterHandler(NULL, NULL, NULL));
101 }
102
103 TEST(RegisterHandlerTest, RegisterValidHandler)
104 {
105     EXPECT_EQ(OC_STACK_OK, SRMRegisterHandler(UTRequestHandler, UTResponseHandler, UTErrorHandler));
106 }
107
108 // PersistentStorageHandler Tests
109 TEST(PersistentStorageHandlerTest, RegisterNullHandler)
110 {
111     EXPECT_EQ(OC_STACK_INVALID_PARAM,
112             SRMRegisterPersistentStorageHandler(NULL));
113 }
114
115 TEST(PersistentStorageHandlerTest, RegisterValidHandler)
116 {
117     gpsi.open = utopen;
118     gpsi.read = utread;
119     gpsi.write = utwrite;
120     gpsi.close = utclose;
121     gpsi.unlink = utunlink;
122
123     EXPECT_EQ(OC_STACK_OK,
124             SRMRegisterPersistentStorageHandler(&gpsi));
125     OCPersistentStorage *ps = SRMGetPersistentStorageHandler();
126     EXPECT_TRUE(&gpsi == ps);
127 }
128
129 #if !(defined(HAVE_LINUX_LIMITS_H) && defined(HAVE_PWD_H))
130 TEST(PersistentStorageHandlerTest, DISABLED_PersistentStorageValidHandlers)
131 {
132     /** @todo: Implement test on non-Linux platform */
133 }
134 #else
135 TEST(PersistentStorageHandlerTest, PersistentStorageValidHandlers)
136 {
137     OCPersistentStorage *psi = SRMGetPersistentStorageHandler();
138     EXPECT_TRUE(psi != NULL);
139
140     unsigned char buf[PATH_MAX];
141     FILE* streamIn = NULL;
142     FILE* streamOut = NULL;
143     struct passwd *pw = getpwuid(getuid());
144     const char *homeDir = pw->pw_dir;
145     char inFilePath [PATH_MAX];
146     char outFilePath [PATH_MAX];
147     snprintf(inFilePath, PATH_MAX, "%s/iotivity/Readme.scons.txt", homeDir );
148     snprintf(outFilePath, PATH_MAX, "%s/Downloads/Readme.scons.out.txt", homeDir );
149
150     streamIn = psi->open(inFilePath, "r");
151     streamOut = psi->open(outFilePath, "w");
152
153     if (streamIn && streamOut)
154     {
155         size_t value = 1;
156         while (value)
157         {
158             value = psi->read(buf, 1, sizeof(buf), streamIn);
159             psi->write(buf, 1, value, streamOut);
160         }
161     }
162
163     if (streamIn)
164     {
165         psi->close(streamIn);
166     }
167     if (streamOut)
168     {
169         psi->close(streamOut);
170     }
171     psi->unlink(outFilePath);
172 }
173 #endif
174