1 //******************************************************************
3 // Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "gtest/gtest.h"
28 #ifdef HAVE_LINUX_LIMITS_H
29 #include <linux/limits.h>
32 #include "cainterface.h"
33 #include "secureresourcemanager.h"
38 void UTRequestHandler(const CAEndpoint_t * /*endPoint*/,
39 const CARequestInfo_t * /*requestInfo*/)
41 EXPECT_TRUE(true) << "UTRequestHandler\n";
44 void UTResponseHandler(const CAEndpoint_t * /*endPoint*/,
45 const CAResponseInfo_t * /*responseInfo*/)
47 EXPECT_TRUE(true) << "UTResponseHandler\n";
50 void UTErrorHandler(const CAEndpoint_t * /*endPoint*/,
51 const CAErrorInfo_t * /*errorInfo*/)
53 EXPECT_TRUE(true) << "UTErrorHandler\n";
56 FILE *utopen(const char *path, const char *mode)
58 EXPECT_TRUE((path != NULL)) << "utopen\n";
59 FILE *stream = fopen(path, mode);
64 size_t utread(void *ptr, size_t size, size_t nmemb, FILE *stream)
66 return fread(ptr, size, nmemb, stream);
69 size_t utwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
71 return fwrite(ptr, size, nmemb, stream);
76 EXPECT_TRUE((fp != NULL)) << "utclose\n";
79 int utunlink(const char *path)
81 EXPECT_TRUE((path != NULL)) << "utunlink\n";
84 static OCPersistentStorage gpsi;
86 //RegisterHandler Tests
87 TEST(RegisterHandlerTest, RegisterNullRequestHandler)
89 EXPECT_EQ(OC_STACK_INVALID_PARAM, SRMRegisterHandler(NULL, UTResponseHandler, NULL));
92 TEST(RegisterHandlerTest, RegisterNullResponseHandler)
94 EXPECT_EQ(OC_STACK_INVALID_PARAM, SRMRegisterHandler(UTRequestHandler, NULL, NULL));
97 TEST(RegisterHandlerTest, RegisterNullHandler)
99 EXPECT_EQ(OC_STACK_INVALID_PARAM, SRMRegisterHandler(NULL, NULL, NULL));
102 TEST(RegisterHandlerTest, RegisterValidHandler)
104 EXPECT_EQ(OC_STACK_OK, SRMRegisterHandler(UTRequestHandler, UTResponseHandler, UTErrorHandler));
107 // PersistentStorageHandler Tests
108 TEST(PersistentStorageHandlerTest, RegisterNullHandler)
110 EXPECT_EQ(OC_STACK_INVALID_PARAM,
111 SRMRegisterPersistentStorageHandler(NULL));
114 TEST(PersistentStorageHandlerTest, RegisterValidHandler)
118 gpsi.write = utwrite;
119 gpsi.close = utclose;
120 gpsi.unlink = utunlink;
122 EXPECT_EQ(OC_STACK_OK,
123 SRMRegisterPersistentStorageHandler(&gpsi));
124 OCPersistentStorage *ps = SRMGetPersistentStorageHandler();
125 EXPECT_TRUE(&gpsi == ps);
128 #if !(defined(HAVE_LINUX_LIMITS_H) && defined(HAVE_PWD_H))
129 TEST(PersistentStorageHandlerTest, DISABLED_PersistentStorageValidHandlers)
131 /** @todo: Implement test on non-Linux platform */
134 TEST(PersistentStorageHandlerTest, PersistentStorageValidHandlers)
136 OCPersistentStorage *psi = SRMGetPersistentStorageHandler();
137 EXPECT_TRUE(psi != NULL);
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 );
149 streamIn = psi->open(inFilePath, "r");
150 streamOut = psi->open(outFilePath, "w");
152 if (streamIn && streamOut)
157 value = psi->read(buf, 1, sizeof(buf), streamIn);
158 psi->write(buf, 1, value, streamOut);
164 psi->close(streamIn);
168 psi->close(streamOut);
170 psi->unlink(outFilePath);