Security CBOR conversion
[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
26 #define STRINGIZE2(x) #x
27 #define STRINGIZE(x) STRINGIZE2(x)
28
29 // TODO: Remove this, once all cbor related are completed.
30 char* ReadFile(const char* filename)
31 {
32
33     FILE *fp = NULL;
34     char *data = NULL;
35     struct stat st;
36     // TODO: Find the location of the executable and concatenate the SVR file name
37     // before opening it.
38     fp = fopen(filename, "r");
39     if (fp)
40     {
41         if (stat(filename, &st) == 0)
42         {
43             data = (char*)OICMalloc(st.st_size);
44             if (data)
45             {
46                 if (fread(data, 1, st.st_size, fp) != (size_t)st.st_size)
47                 {
48                     printf("Error in reading file %s", filename);
49                 }
50             }
51         }
52         fclose(fp);
53     }
54     else
55     {
56         printf("Unable to open %s file", filename);
57     }
58
59     return data;
60 }
61
62 bool ReadCBORFile(const char* filename, uint8_t **data, size_t *size)
63 {
64     bool status = false;
65     if (!data || !size)
66     {
67         printf("Passed parameter are INVALID \n");
68         return status;
69     }
70     int len = strlen(STRINGIZE(SECURITY_BUILD_UNITTEST_DIR)) + strlen(filename) + 1;
71     char *filepath = (char *)OICCalloc(1, len);
72     if (!filepath)
73     {
74         printf("filepath memory allocation failed. \n");
75         return false;
76     }
77     int ret = snprintf(filepath, len, "%s%s", STRINGIZE(SECURITY_BUILD_UNITTEST_DIR), filename);
78     printf("Root build path: %s \n", filepath);
79
80     if (ret == len-1)
81     {
82         FILE *fp = fopen(filepath, "rb");
83         if (fp)
84         {
85             struct stat st;
86             if (stat(filepath, &st) == 0)
87             {
88                 *data = (uint8_t *)OICMalloc(st.st_size);
89                 if (*data)
90                 {
91                     if (fread(*data, 1, st.st_size, fp) != (size_t)st.st_size)
92                     {
93                         printf("Error in reading file %s\n", filename);
94                     }
95                     else
96                     {
97                         *size = st.st_size;
98                         status = true;
99                     }
100                 }
101             }
102             fclose(fp);
103         }
104         else
105         {
106             printf("Unable to open %s file\n", filepath);
107         }
108     }
109     else
110     {
111         printf("Filepath copy failed.\n");
112     }
113     OICFree(filepath);
114     return status;
115 }
116
117 void SetPersistentHandler(OCPersistentStorage *ps, bool set)
118 {
119     if (set)
120     {
121         ps->open = fopen;
122         ps->read = fread;
123         ps->write = fwrite;
124         ps->close = fclose;
125         ps->unlink = unlink;
126     }
127     else
128     {
129         memset(ps, 0, sizeof(OCPersistentStorage));
130     }
131     EXPECT_EQ(OC_STACK_OK,
132             OCRegisterPersistentStorageHandler(ps));
133 }