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