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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
25 #include "ocpayload.h"
26 #include "ocpayloadcbor.h"
28 #include "oic_malloc.h"
31 #include "gtest/gtest.h"
32 #include <sys/types.h>
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
50 #include "gtest_helper.h"
52 class CborByteStringTest : public ::testing::Test {
54 virtual void SetUp() {
56 payload_in = OCRepPayloadCreate();
57 ASSERT_TRUE(payload_in != NULL);
60 virtual void TearDown() {
61 OCPayloadDestroy((OCPayload*)payload_in);
64 OCRepPayload* payload_in;
67 TEST_F(CborByteStringTest, ByteStringSetGetTest)
69 OCRepPayloadSetUri(payload_in, "/a/quake_sensor");
70 OCRepPayloadSetPropInt(payload_in, "scale", 4);
72 uint8_t binval[] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0x0, 0xA, 0xB, 0xC,
74 OCByteString quakedata_in = { binval, sizeof(binval)};
76 EXPECT_EQ(true, OCRepPayloadSetPropByteString(payload_in, "quakedata", quakedata_in));
78 OCByteString quakedata_out = { NULL, 0};
79 ASSERT_EQ(true, OCRepPayloadGetPropByteString(payload_in, "quakedata", &quakedata_out));
81 EXPECT_EQ(quakedata_in.len, quakedata_out.len);
82 EXPECT_EQ(0, memcmp(quakedata_in.bytes, quakedata_out.bytes, quakedata_in.len));
85 OICFree(quakedata_out.bytes);
88 TEST_F(CborByteStringTest, ByteStringConvertParseTest)
90 OCRepPayloadSetUri(payload_in, "/a/quake_sensor");
91 OCRepPayloadSetPropInt(payload_in, "scale", 4);
93 uint8_t binval[] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0x0, 0xA, 0xB, 0xC,
95 OCByteString quakedata_in = { binval, sizeof(binval)};
97 // Set ByteString in Payload
98 EXPECT_EQ(true, OCRepPayloadSetPropByteString(payload_in, "quakedata", quakedata_in));
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));
105 #ifdef CBOR_BIN_STRING_DEBUG
106 FILE *fp = fopen("binstring.cbor", "wb+");
109 fwrite(payload_cbor, 1, payload_cbor_size, fp);
112 #endif //CBOR_BIN_STRING_DEBUG
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));
119 OCByteString quakedata_out = {NULL, 0};
120 ASSERT_EQ(true, OCRepPayloadGetPropByteString((OCRepPayload*)payload_out, "quakedata", &quakedata_out));
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));
127 OICFree(payload_cbor);
128 OICFree(quakedata_out.bytes);
129 OCPayloadDestroy((OCPayload*)payload_out);
132 TEST_F(CborByteStringTest, ByteStringArraySetGetTest )
134 OCRepPayloadSetUri(payload_in, "/a/quake_sensor");
135 OCRepPayloadSetPropInt(payload_in, "scale", 4);
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};
142 OCByteString quakedata_in[3] = {{binval1, sizeof(binval1)},
143 {binval2, sizeof(binval2)},
144 {binval3, sizeof(binval3)}};
146 EXPECT_EQ(true, OCRepPayloadSetByteStringArray(payload_in, "quakedata",
147 quakedata_in, dimensions_in));
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));
154 for(size_t i = 0; i < dimensions_in[0]; i++)
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));
161 for(size_t i = 0; i < dimensions_out[0]; i++)
163 OICFree(quakedata_out[i].bytes);
165 OICFree(quakedata_out);
169 TEST_F(CborByteStringTest, ByteStringArrayConvertParseTest )
171 OCRepPayloadSetUri(payload_in, "/a/quake_sensor");
172 OCRepPayloadSetPropInt(payload_in, "scale", 4);
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};
179 OCByteString quakedata_in[3] = {{binval1, sizeof(binval1)},
180 {binval2, sizeof(binval2)},
181 {binval3, sizeof(binval3)}};
183 EXPECT_EQ(true, OCRepPayloadSetByteStringArray(payload_in, "quakedata",
184 quakedata_in, dimensions_in));
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+");
194 fwrite(payload_cbor, 1, payload_cbor_size, fp);
197 #endif //CBOR_BIN_STRING_DEBUG
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));
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));
209 for(size_t i = 0; i < dimensions_in[0]; i++)
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));
216 OICFree(payload_cbor);
217 for(size_t i = 0; i < dimensions_out[0]; i++)
219 OICFree(quakedata_out[i].bytes);
221 OICFree(quakedata_out);
223 OCPayloadDestroy((OCPayload*)payload_out);