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