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 "iotivity_config.h"
22 #include "gtest/gtest.h"
29 #ifdef HAVE_LINUX_LIMITS_H
30 #include <linux/limits.h>
33 #include "cainterface.h"
34 #include "secureresourcemanager.h"
39 void UTRequestHandler(const CAEndpoint_t * /*endPoint*/,
40 const CARequestInfo_t * /*requestInfo*/)
42 EXPECT_TRUE(true) << "UTRequestHandler\n";
45 void UTResponseHandler(const CAEndpoint_t * /*endPoint*/,
46 const CAResponseInfo_t * /*responseInfo*/)
48 EXPECT_TRUE(true) << "UTResponseHandler\n";
51 void UTErrorHandler(const CAEndpoint_t * /*endPoint*/,
52 const CAErrorInfo_t * /*errorInfo*/)
54 EXPECT_TRUE(true) << "UTErrorHandler\n";
57 FILE *utopen(const char *path, const char *mode)
59 EXPECT_TRUE((path != NULL)) << "utopen\n";
60 FILE *stream = fopen(path, mode);
65 size_t utread(void *ptr, size_t size, size_t nmemb, FILE *stream)
67 return fread(ptr, size, nmemb, stream);
70 size_t utwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
72 return fwrite(ptr, size, nmemb, stream);
77 EXPECT_TRUE((fp != NULL)) << "utclose\n";
80 int utunlink(const char *path)
82 EXPECT_TRUE((path != NULL)) << "utunlink\n";
85 static OCPersistentStorage gpsi;
87 //RegisterHandler Tests
88 TEST(RegisterHandlerTest, RegisterNullRequestHandler)
90 EXPECT_EQ(OC_STACK_INVALID_PARAM, SRMRegisterHandler(NULL, UTResponseHandler, NULL));
93 TEST(RegisterHandlerTest, RegisterNullResponseHandler)
95 EXPECT_EQ(OC_STACK_INVALID_PARAM, SRMRegisterHandler(UTRequestHandler, NULL, NULL));
98 TEST(RegisterHandlerTest, RegisterNullHandler)
100 EXPECT_EQ(OC_STACK_INVALID_PARAM, SRMRegisterHandler(NULL, NULL, NULL));
103 TEST(RegisterHandlerTest, RegisterValidHandler)
105 EXPECT_EQ(OC_STACK_OK, SRMRegisterHandler(UTRequestHandler, UTResponseHandler, UTErrorHandler));
108 // PersistentStorageHandler Tests
109 TEST(PersistentStorageHandlerTest, RegisterNullHandler)
111 EXPECT_EQ(OC_STACK_INVALID_PARAM,
112 SRMRegisterPersistentStorageHandler(NULL));
115 TEST(PersistentStorageHandlerTest, RegisterValidHandler)
119 gpsi.write = utwrite;
120 gpsi.close = utclose;
121 gpsi.unlink = utunlink;
123 EXPECT_EQ(OC_STACK_OK,
124 SRMRegisterPersistentStorageHandler(&gpsi));
125 OCPersistentStorage *ps = SRMGetPersistentStorageHandler();
126 EXPECT_TRUE(&gpsi == ps);
129 #if !(defined(HAVE_LINUX_LIMITS_H) && defined(HAVE_PWD_H))
130 TEST(PersistentStorageHandlerTest, DISABLED_PersistentStorageValidHandlers)
132 /** @todo: Implement test on non-Linux platform */
135 TEST(PersistentStorageHandlerTest, PersistentStorageValidHandlers)
137 OCPersistentStorage *psi = SRMGetPersistentStorageHandler();
138 EXPECT_TRUE(psi != NULL);
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 );
150 streamIn = psi->open(inFilePath, "r");
151 streamOut = psi->open(outFilePath, "w");
153 if (streamIn && streamOut)
158 value = psi->read(buf, 1, sizeof(buf), streamIn);
159 psi->write(buf, 1, value, streamOut);
165 psi->close(streamIn);
169 psi->close(streamOut);
171 psi->unlink(outFilePath);