Return accurate error codes for error cases.
[platform/upstream/iotivity.git] / resource / csdk / stack / test / cbortests.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
22 extern "C"
23 {
24     #include "ocstack.h"
25     #include "ocpayload.h"
26     #include "ocpayloadcbor.h"
27     #include "logger.h"
28     #include "oic_malloc.h"
29 }
30
31 #include "gtest/gtest.h"
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <errno.h>
35 #include <fcntl.h>
36 #ifdef HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif
39 #include <stdlib.h>
40
41 //-----------------------------------------------------------------------------
42 // Includes
43 //-----------------------------------------------------------------------------
44 #include <stdio.h>
45 #include <string.h>
46
47 #include <iostream>
48 #include <stdint.h>
49
50 #include "gtest_helper.h"
51
52 class CborByteStringTest : public ::testing::Test {
53     protected:
54         virtual void SetUp() {
55             // Create Payload
56             payload_in = OCRepPayloadCreate();
57             ASSERT_TRUE(payload_in != NULL);
58         }
59
60         virtual void TearDown() {
61             OCPayloadDestroy((OCPayload*)payload_in);
62         }
63
64         OCRepPayload* payload_in;
65 };
66
67 TEST_F(CborByteStringTest, ByteStringSetGetTest)
68 {
69     OCRepPayloadSetUri(payload_in, "/a/quake_sensor");
70     OCRepPayloadSetPropInt(payload_in, "scale", 4);
71
72     uint8_t binval[] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0x0, 0xA, 0xB, 0xC,
73                         0xD, 0xE, 0xF};
74     OCByteString quakedata_in = { binval, sizeof(binval)};
75
76     EXPECT_EQ(true, OCRepPayloadSetPropByteString(payload_in, "quakedata", quakedata_in));
77
78     OCByteString quakedata_out = { NULL, 0};
79     ASSERT_EQ(true, OCRepPayloadGetPropByteString(payload_in, "quakedata", &quakedata_out));
80
81     EXPECT_EQ(quakedata_in.len, quakedata_out.len);
82     EXPECT_EQ(0, memcmp(quakedata_in.bytes, quakedata_out.bytes, quakedata_in.len));
83
84     // Cleanup
85     OICFree(quakedata_out.bytes);
86 }
87
88 TEST_F(CborByteStringTest, ByteStringConvertParseTest)
89 {
90     OCRepPayloadSetUri(payload_in, "/a/quake_sensor");
91     OCRepPayloadSetPropInt(payload_in, "scale", 4);
92
93     uint8_t binval[] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0x0, 0xA, 0xB, 0xC,
94                         0xD, 0xE, 0xF};
95     OCByteString quakedata_in = { binval, sizeof(binval)};
96
97     // Set ByteString in Payload
98     EXPECT_EQ(true, OCRepPayloadSetPropByteString(payload_in, "quakedata", quakedata_in));
99
100     // Convert OCPayload to CBOR
101     uint8_t *payload_cbor = NULL;
102     size_t payload_cbor_size = 0;
103     EXPECT_EQ(OC_STACK_OK, OCConvertPayload((OCPayload*) payload_in, &payload_cbor, &payload_cbor_size));
104
105 #ifdef CBOR_BIN_STRING_DEBUG
106     FILE *fp = fopen("binstring.cbor", "wb+");
107     if (fp)
108     {
109         fwrite(payload_cbor, 1, payload_cbor_size, fp);
110         fclose(fp);
111     }
112 #endif //CBOR_BIN_STRING_DEBUG
113
114     // Parse CBOR back to OCPayload
115     OCPayload* payload_out = NULL;
116     EXPECT_EQ(OC_STACK_OK, OCParsePayload(&payload_out, PAYLOAD_TYPE_REPRESENTATION,
117                  payload_cbor, payload_cbor_size));
118
119     OCByteString quakedata_out = {NULL, 0};
120     ASSERT_EQ(true, OCRepPayloadGetPropByteString((OCRepPayload*)payload_out, "quakedata", &quakedata_out));
121
122     // Compare input and output data
123     EXPECT_EQ(quakedata_in.len, quakedata_out.len);
124     EXPECT_EQ(0, memcmp(quakedata_in.bytes, quakedata_out.bytes, quakedata_in.len));
125
126     // Cleanup
127     OICFree(payload_cbor);
128     OICFree(quakedata_out.bytes);
129     OCPayloadDestroy((OCPayload*)payload_out);
130 }
131
132 TEST_F(CborByteStringTest, ByteStringArraySetGetTest )
133 {
134     OCRepPayloadSetUri(payload_in, "/a/quake_sensor");
135     OCRepPayloadSetPropInt(payload_in, "scale", 4);
136
137     size_t dimensions_in[MAX_REP_ARRAY_DEPTH] = { 3, 0, 0};
138     uint8_t binval1[] = {0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19};
139     uint8_t binval2[] = {0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29};
140     uint8_t binval3[] = {0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39};
141
142     OCByteString quakedata_in[3] = {{binval1, sizeof(binval1)},
143                                     {binval2, sizeof(binval2)},
144                                     {binval3, sizeof(binval3)}};
145
146     EXPECT_EQ(true, OCRepPayloadSetByteStringArray(payload_in, "quakedata",
147                 quakedata_in, dimensions_in));
148
149     OCByteString* quakedata_out = NULL;
150     size_t dimensions_out[MAX_REP_ARRAY_DEPTH] = {0};
151     ASSERT_EQ(true, OCRepPayloadGetByteStringArray(payload_in, "quakedata",
152                 &quakedata_out, dimensions_out));
153
154     for(size_t i = 0; i < dimensions_in[0]; i++)
155     {
156         EXPECT_EQ(quakedata_in[i].len, quakedata_out[i].len);
157         EXPECT_EQ(0, memcmp(quakedata_in[i].bytes, quakedata_out[i].bytes, quakedata_in[i].len));
158     }
159
160     // Cleanup
161     for(size_t i = 0; i < dimensions_out[0]; i++)
162     {
163         OICFree(quakedata_out[i].bytes);
164     }
165     OICFree(quakedata_out);
166 }
167
168
169 TEST_F(CborByteStringTest, ByteStringArrayConvertParseTest )
170 {
171     OCRepPayloadSetUri(payload_in, "/a/quake_sensor");
172     OCRepPayloadSetPropInt(payload_in, "scale", 4);
173
174     size_t dimensions_in[MAX_REP_ARRAY_DEPTH] = { 3, 0, 0};
175     uint8_t binval1[] = {0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19};
176     uint8_t binval2[] = {0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29};
177     uint8_t binval3[] = {0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39};
178
179     OCByteString quakedata_in[3] = {{binval1, sizeof(binval1)},
180                                     {binval2, sizeof(binval2)},
181                                     {binval3, sizeof(binval3)}};
182
183     EXPECT_EQ(true, OCRepPayloadSetByteStringArray(payload_in, "quakedata",
184                 quakedata_in, dimensions_in));
185
186     // Convert OCPayload to CBOR
187     uint8_t *payload_cbor = NULL;
188     size_t payload_cbor_size = 0;
189     EXPECT_EQ(OC_STACK_OK, OCConvertPayload((OCPayload*) payload_in, &payload_cbor, &payload_cbor_size));
190 #ifdef CBOR_BIN_STRING_DEBUG
191     FILE *fp = fopen("binstringarr.cbor", "wb+");
192     if (fp)
193     {
194         fwrite(payload_cbor, 1, payload_cbor_size, fp);
195         fclose(fp);
196     }
197 #endif //CBOR_BIN_STRING_DEBUG
198
199     // Parse CBOR back to OCPayload
200     OCPayload* payload_out = NULL;
201     EXPECT_EQ(OC_STACK_OK, OCParsePayload(&payload_out, PAYLOAD_TYPE_REPRESENTATION,
202                 payload_cbor, payload_cbor_size));
203
204     OCByteString* quakedata_out = NULL;
205     size_t dimensions_out[MAX_REP_ARRAY_DEPTH] = {0};
206     ASSERT_EQ(true, OCRepPayloadGetByteStringArray((OCRepPayload*)payload_out, "quakedata",
207                 &quakedata_out, dimensions_out));
208
209     for(size_t i = 0; i < dimensions_in[0]; i++)
210     {
211         EXPECT_EQ(quakedata_in[i].len, quakedata_out[i].len);
212         EXPECT_EQ(0, memcmp(quakedata_in[i].bytes, quakedata_out[i].bytes, quakedata_in[i].len));
213     }
214
215     // Cleanup
216     OICFree(payload_cbor);
217     for(size_t i = 0; i < dimensions_out[0]; i++)
218     {
219         OICFree(quakedata_out[i].bytes);
220     }
221     OICFree(quakedata_out);
222
223     OCPayloadDestroy((OCPayload*)payload_out);
224 }