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"
22 #include "oic_malloc.h"
27 #define STRINGIZE2(x) #x
28 #define STRINGIZE(x) STRINGIZE2(x)
30 // TODO: Remove this, once all cbor related are completed.
31 char* ReadFile(const char* filename)
37 // TODO: Find the location of the executable and concatenate the SVR file name
39 fp = fopen(filename, "r");
42 if (stat(filename, &st) == 0)
44 data = (char*)OICMalloc(st.st_size);
47 if (fread(data, 1, st.st_size, fp) != (size_t)st.st_size)
49 printf("Error in reading file %s", filename);
57 printf("Unable to open %s file", filename);
63 bool ReadCBORFile(const char* filename, const char* rsrcname, uint8_t **payload, size_t *pSize)
66 if (!payload || !pSize)
68 printf("Passed parameter are INVALID \n");
74 int len = strlen(STRINGIZE(SECURITY_BUILD_UNITTEST_DIR)) + strlen(filename) + 1;
75 char *filepath = (char *)OICCalloc(1, len);
78 printf("filepath memory allocation failed. \n");
81 int ret = snprintf(filepath, len, "%s%s", STRINGIZE(SECURITY_BUILD_UNITTEST_DIR), filename);
82 printf("Root build path: %s \n", filepath);
86 FILE *fp = fopen(filepath, "rb");
90 if (stat(filepath, &st) == 0)
92 data = (uint8_t *)OICMalloc(st.st_size);
95 if (fread(data, 1, st.st_size, fp) != (size_t)st.st_size)
97 printf("Error in reading file %s\n", filename);
103 CborValue cbor = {0, };
104 CborParser parser = {0, };
105 cbor_parser_init(data, size, 0, &parser, &cbor);
106 CborError cborFindResult = CborNoError;
108 CborValue curVal = {0, };
109 cborFindResult = cbor_value_map_find_value(&cbor, rsrcname, &curVal);
110 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
112 cborFindResult = cbor_value_dup_byte_string(&curVal, payload, pSize, NULL);
113 if(CborNoError != cborFindResult)
115 printf("Failed to getting %s data\n", rsrcname);
124 printf("Failed to finding %s data\n", rsrcname);
135 printf("Unable to open %s file\n", filepath);
140 printf("Filepath copy failed.\n");
146 void SetPersistentHandler(OCPersistentStorage *ps, bool set)
158 memset(ps, 0, sizeof(OCPersistentStorage));
160 EXPECT_EQ(OC_STACK_OK,
161 OCRegisterPersistentStorageHandler(ps));