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