Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / resource / csdk / security / unittest / srmtestcommon.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include "gtest/gtest.h"
22 #include "oic_malloc.h"
23 #include "ocstack.h"
24 #include <stdlib.h>
25 #include "cbor.h"
26
27 #define STRINGIZE2(x) #x
28 #define STRINGIZE(x) STRINGIZE2(x)
29
30 // TODO: Remove this, once all cbor related are completed.
31 char* ReadFile(const char* filename)
32 {
33
34     FILE *fp = NULL;
35     char *data = NULL;
36     struct stat st;
37     // TODO: Find the location of the executable and concatenate the SVR file name
38     // before opening it.
39     fp = fopen(filename, "r");
40     if (fp)
41     {
42         if (stat(filename, &st) == 0)
43         {
44             data = (char*)OICMalloc(st.st_size);
45             if (data)
46             {
47                 if (fread(data, 1, st.st_size, fp) != (size_t)st.st_size)
48                 {
49                     printf("Error in reading file %s", filename);
50                 }
51             }
52         }
53         fclose(fp);
54     }
55     else
56     {
57         printf("Unable to open %s file", filename);
58     }
59
60     return data;
61 }
62
63 bool ReadCBORFile(const char* filename, const char* rsrcname, uint8_t **payload, size_t *pSize)
64 {
65     bool status = false;
66     if (!payload || !pSize)
67     {
68         printf("Passed parameter are INVALID \n");
69         return status;
70     }
71     uint8_t *data = NULL;
72     size_t size = 0;
73
74     int len = strlen(STRINGIZE(SECURITY_BUILD_UNITTEST_DIR)) + strlen(filename) + 1;
75     char *filepath = (char *)OICCalloc(1, len);
76     if (!filepath)
77     {
78         printf("filepath memory allocation failed. \n");
79         return false;
80     }
81     int ret = snprintf(filepath, len, "%s%s", STRINGIZE(SECURITY_BUILD_UNITTEST_DIR), filename);
82     printf("Root build path: %s \n", filepath);
83
84     if (ret == len-1)
85     {
86         FILE *fp = fopen(filepath, "rb");
87         if (fp)
88         {
89             struct stat st;
90             if (stat(filepath, &st) == 0)
91             {
92                 data = (uint8_t *)OICMalloc(st.st_size);
93                 if (data)
94                 {
95                     if (fread(data, 1, st.st_size, fp) != (size_t)st.st_size)
96                     {
97                         printf("Error in reading file %s\n", filename);
98                     }
99                     else
100                     {
101                         size = st.st_size;
102
103                         CborValue cbor = {0, };
104                         CborParser parser = {0, };
105                         cbor_parser_init(data, size, 0, &parser, &cbor);
106                         CborError cborFindResult = CborNoError;
107
108                         CborValue curVal = {0, };
109                         cborFindResult = cbor_value_map_find_value(&cbor, rsrcname, &curVal);
110                         if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
111                         {
112                             cborFindResult = cbor_value_dup_byte_string(&curVal, payload, pSize, NULL);
113                             if(CborNoError != cborFindResult)
114                             {
115                                 printf("Failed to getting %s data\n", rsrcname);
116                             }
117                             else
118                             {
119                                 status = true;
120                             }
121                         }
122                         else
123                         {
124                             printf("Failed to finding %s data\n", rsrcname);
125                         }
126
127                     }
128                     OICFree(data);
129                 }
130             }
131             fclose(fp);
132         }
133         else
134         {
135             printf("Unable to open %s file\n", filepath);
136         }
137     }
138     else
139     {
140         printf("Filepath copy failed.\n");
141     }
142     OICFree(filepath);
143     return status;
144 }
145
146 void SetPersistentHandler(OCPersistentStorage *ps, bool set)
147 {
148     if (set)
149     {
150         ps->open = fopen;
151         ps->read = fread;
152         ps->write = fwrite;
153         ps->close = fclose;
154         ps->unlink = unlink;
155     }
156     else
157     {
158         memset(ps, 0, sizeof(OCPersistentStorage));
159     }
160     EXPECT_EQ(OC_STACK_OK,
161             OCRegisterPersistentStorageHandler(ps));
162 }