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"
24 #include <linux/limits.h>
26 #include "cainterface.h"
27 #include "secureresourcemanager.h"
32 void UTRequestHandler(const CAEndpoint_t * /*endPoint*/,
33 const CARequestInfo_t * /*requestInfo*/)
35 EXPECT_TRUE(true) << "UTRequestHandler\n";
38 void UTResponseHandler(const CAEndpoint_t * /*endPoint*/,
39 const CAResponseInfo_t * /*responseInfo*/)
41 EXPECT_TRUE(true) << "UTResponseHandler\n";
44 void UTErrorHandler(const CAEndpoint_t * /*endPoint*/,
45 const CAErrorInfo_t * /*errorInfo*/)
47 EXPECT_TRUE(true) << "UTErrorHandler\n";
50 FILE *utopen(const char *path, const char *mode)
52 EXPECT_TRUE((path != NULL)) << "utopen\n";
53 FILE *stream = fopen(path, mode);
58 size_t utread(void *ptr, size_t size, size_t nmemb, FILE *stream)
60 return fread(ptr, size, nmemb, stream);
63 size_t utwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
65 return fwrite(ptr, size, nmemb, stream);
70 EXPECT_TRUE((fp != NULL)) << "utclose\n";
73 int utunlink(const char *path)
75 EXPECT_TRUE((path != NULL)) << "utunlink\n";
78 static OCPersistentStorage gpsi;
80 //RegisterHandler Tests
81 TEST(RegisterHandlerTest, RegisterNullRequestHandler)
83 EXPECT_EQ(OC_STACK_INVALID_PARAM, SRMRegisterHandler(NULL, UTResponseHandler, NULL));
86 TEST(RegisterHandlerTest, RegisterNullResponseHandler)
88 EXPECT_EQ(OC_STACK_INVALID_PARAM, SRMRegisterHandler(UTRequestHandler, NULL, NULL));
91 TEST(RegisterHandlerTest, RegisterNullHandler)
93 EXPECT_EQ(OC_STACK_INVALID_PARAM, SRMRegisterHandler(NULL, NULL, NULL));
96 TEST(RegisterHandlerTest, RegisterValidHandler)
98 EXPECT_EQ(OC_STACK_OK, SRMRegisterHandler(UTRequestHandler, UTResponseHandler, UTErrorHandler));
101 // PersistentStorageHandler Tests
102 TEST(PersistentStorageHandlerTest, RegisterNullHandler)
104 EXPECT_EQ(OC_STACK_INVALID_PARAM,
105 SRMRegisterPersistentStorageHandler(NULL));
108 TEST(PersistentStorageHandlerTest, RegisterValidHandler)
112 gpsi.write = utwrite;
113 gpsi.close = utclose;
114 gpsi.unlink = utunlink;
116 EXPECT_EQ(OC_STACK_OK,
117 SRMRegisterPersistentStorageHandler(&gpsi));
118 OCPersistentStorage *ps = SRMGetPersistentStorageHandler();
119 EXPECT_TRUE(&gpsi == ps);
122 TEST(PersistentStorageHandlerTest, PersistentStorageValidHandlers)
124 OCPersistentStorage *psi = SRMGetPersistentStorageHandler();
125 EXPECT_TRUE(psi != NULL);
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 );
137 streamIn = psi->open(inFilePath, "r");
138 streamOut = psi->open(outFilePath, "w");
140 if (streamIn && streamOut)
145 value = psi->read(buf, 1, sizeof(buf), streamIn);
146 psi->write(buf, 1, value, streamOut);
152 psi->close(streamIn);
156 psi->close(streamOut);
158 psi->unlink(outFilePath);