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