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 CARemoteEndpoint_t *endPoint, const CARequestInfo_t *requestInfo)
34 EXPECT_TRUE(true) << "UTRequestHandler\n";
37 void UTResponseHandler(const CARemoteEndpoint_t *endPoint, const CAResponseInfo_t *responseInfo)
39 EXPECT_TRUE(true) << "UTResponseHandler\n";
42 void UTErrorHandler(const CARemoteEndpoint_t *endPoint, const CAErrorInfo_t *errorInfo)
44 EXPECT_TRUE(true) << "UTErrorHandler\n";
47 FILE *utopen(const char *path, const char *mode)
49 EXPECT_TRUE((path != NULL)) << "utopen\n";
50 FILE *stream = fopen(path, mode);
55 size_t utread(void *ptr, size_t size, size_t nmemb, FILE *stream)
57 return fread(ptr, size, nmemb, stream);
60 size_t utwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
62 return fwrite(ptr, size, nmemb, stream);
67 EXPECT_TRUE((fp != NULL)) << "utclose\n";
70 int utunlink(const char *path)
72 EXPECT_TRUE((path != NULL)) << "utunlink\n";
75 static OCPersistentStorage gpsi;
77 //RegisterHandler Tests
78 TEST(RegisterHandlerTest, RegisterNullRequestHandler)
80 EXPECT_EQ(OC_STACK_INVALID_PARAM, SRMRegisterHandler(NULL, UTResponseHandler, NULL));
83 TEST(RegisterHandlerTest, RegisterNullResponseHandler)
85 EXPECT_EQ(OC_STACK_INVALID_PARAM, SRMRegisterHandler(UTRequestHandler, NULL, NULL));
88 TEST(RegisterHandlerTest, RegisterNullHandler)
90 EXPECT_EQ(OC_STACK_INVALID_PARAM, SRMRegisterHandler(NULL, NULL, NULL));
93 TEST(RegisterHandlerTest, RegisterValidHandler)
95 EXPECT_EQ(OC_STACK_OK, SRMRegisterHandler(UTRequestHandler, UTResponseHandler, UTErrorHandler));
98 // PersistentStorageHandler Tests
99 TEST(PersistentStorageHandlerTest, RegisterNullHandler)
101 EXPECT_EQ(OC_STACK_INVALID_PARAM,
102 SRMRegisterPersistentStorageHandler(NULL));
105 TEST(PersistentStorageHandlerTest, RegisterValidHandler)
109 gpsi.write = utwrite;
110 gpsi.close = utclose;
111 gpsi.unlink = utunlink;
113 EXPECT_EQ(OC_STACK_OK,
114 SRMRegisterPersistentStorageHandler(&gpsi));
115 OCPersistentStorage *ps = SRMGetPersistentStorageHandler();
116 EXPECT_TRUE(&gpsi == ps);
119 TEST(PersistentStorageHandlerTest, PersistentStorageValidHandlers)
121 OCPersistentStorage *psi = SRMGetPersistentStorageHandler();
122 EXPECT_TRUE(psi != NULL);
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 );
134 streamIn = psi->open(inFilePath, "r");
135 streamOut = psi->open(outFilePath, "w");
137 if (streamIn && streamOut)
142 value = psi->read(buf, 1, sizeof(buf), streamIn);
143 psi->write(buf, 1, value, streamOut);
149 psi->close(streamIn);
153 psi->close(streamOut);
155 psi->unlink(outFilePath);