1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 Intel Corporation
4 **
5 ** Permission is hereby granted, free of charge, to any person obtaining a copy
6 ** of this software and associated documentation files (the "Software"), to deal
7 ** in the Software without restriction, including without limitation the rights
8 ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 ** copies of the Software, and to permit persons to whom the Software is
10 ** furnished to do so, subject to the following conditions:
11 **
12 ** The above copyright notice and this permission notice shall be included in
13 ** all copies or substantial portions of the Software.
14 **
15 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 ** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 ** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 ** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 ** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 ** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 ** THE SOFTWARE.
22 **
23 ****************************************************************************/
24
25 #include <QtTest>
26 #include "cbor.h"
27
28 #if QT_VERSION >= QT_VERSION_CHECK(5, 9, 0)
29 #include <qfloat16.h>
30 #endif
31
32 Q_DECLARE_METATYPE(CborError)
33 namespace QTest {
toString(const CborError & err)34 template<> char *toString<CborError>(const CborError &err)
35 {
36 return qstrdup(cbor_error_string(err));
37 }
38 }
39
40 class tst_Encoder : public QObject
41 {
42 Q_OBJECT
43 private slots:
44 void floatAsHalfFloat_data();
45 void floatAsHalfFloat();
46 void halfFloat_data();
47 void halfFloat();
48 void floatAsHalfFloatCloseToZero_data();
49 void floatAsHalfFloatCloseToZero();
50 void floatAsHalfFloatNaN();
51 void fixed_data();
52 void fixed();
53 void strings_data();
strings()54 void strings() { fixed(); }
55 void arraysAndMaps_data();
arraysAndMaps()56 void arraysAndMaps() { fixed(); }
57 void tags_data();
58 void tags();
arrays_data()59 void arrays_data() { tags_data(); }
60 void arrays();
maps_data()61 void maps_data() { tags_data(); }
62 void maps();
63
writerApi_data()64 void writerApi_data() { tags_data(); }
65 void writerApi();
writerApiFail_data()66 void writerApiFail_data() { tags_data(); }
67 void writerApiFail();
shortBuffer_data()68 void shortBuffer_data() { tags_data(); }
69 void shortBuffer();
tooShortArrays_data()70 void tooShortArrays_data() { tags_data(); }
71 void tooShortArrays();
tooShortMaps_data()72 void tooShortMaps_data() { tags_data(); }
73 void tooShortMaps();
tooBigArrays_data()74 void tooBigArrays_data() { tags_data(); }
75 void tooBigArrays();
tooBigMaps_data()76 void tooBigMaps_data() { tags_data(); }
77 void tooBigMaps();
78 void illegalSimpleType_data();
79 void illegalSimpleType();
80 };
81
82 #include "tst_encoder.moc"
83 #include "data.cpp"
84
isOomError(CborError err)85 static inline bool isOomError(CborError err)
86 {
87 return err == CborErrorOutOfMemory;
88 }
89
encodeVariant(CborEncoder * encoder,const QVariant & v)90 CborError encodeVariant(CborEncoder *encoder, const QVariant &v)
91 {
92 int type = v.userType();
93 switch (type) {
94 case QVariant::Int:
95 case QVariant::LongLong:
96 return cbor_encode_int(encoder, v.toLongLong());
97
98 case QVariant::UInt:
99 case QVariant::ULongLong:
100 return cbor_encode_uint(encoder, v.toULongLong());
101
102 case QVariant::Bool:
103 return cbor_encode_boolean(encoder, v.toBool());
104
105 case QVariant::Invalid:
106 return cbor_encode_undefined(encoder);
107
108 case QMetaType::VoidStar:
109 return cbor_encode_null(encoder);
110
111 case QVariant::Double:
112 return cbor_encode_double(encoder, v.toDouble());
113
114 case QMetaType::Float:
115 return cbor_encode_float(encoder, v.toFloat());
116
117 case QVariant::String: {
118 QByteArray string = v.toString().toUtf8();
119 return cbor_encode_text_string(encoder, string.constData(), string.length());
120 }
121
122 case QVariant::ByteArray: {
123 QByteArray string = v.toByteArray();
124 return cbor_encode_byte_string(encoder, reinterpret_cast<const quint8 *>(string.constData()), string.length());
125 }
126
127 default:
128 if (type == qMetaTypeId<NegativeInteger>())
129 return cbor_encode_negative_int(encoder, v.value<NegativeInteger>().abs);
130 if (type == qMetaTypeId<SimpleType>())
131 return cbor_encode_simple_value(encoder, v.value<SimpleType>().type);
132 #if QT_VERSION < QT_VERSION_CHECK(5, 9, 0)
133 if (type == qMetaTypeId<Float16Standin>())
134 return cbor_encode_half_float(encoder, v.constData());
135 #else
136 if (type == qMetaTypeId<qfloat16>())
137 return cbor_encode_half_float(encoder, v.constData());
138 #endif
139 if (type == qMetaTypeId<Tag>()) {
140 CborError err = cbor_encode_tag(encoder, v.value<Tag>().tag);
141 if (err && !isOomError(err))
142 return err;
143 return static_cast<CborError>(err | encodeVariant(encoder, v.value<Tag>().tagged));
144 }
145 if (type == QVariant::List || type == qMetaTypeId<IndeterminateLengthArray>()) {
146 CborEncoder sub;
147 QVariantList list = v.toList();
148 size_t len = list.length();
149 if (type == qMetaTypeId<IndeterminateLengthArray>()) {
150 len = CborIndefiniteLength;
151 list = v.value<IndeterminateLengthArray>();
152 }
153 CborError err = cbor_encoder_create_array(encoder, &sub, len);
154 if (err && !isOomError(err))
155 return err;
156 foreach (const QVariant &v2, list) {
157 err = static_cast<CborError>(err | encodeVariant(&sub, v2));
158 if (err && !isOomError(err))
159 return err;
160 }
161 return cbor_encoder_close_container_checked(encoder, &sub);
162 }
163 if (type == qMetaTypeId<Map>() || type == qMetaTypeId<IndeterminateLengthMap>()) {
164 CborEncoder sub;
165 Map map = v.value<Map>();
166 size_t len = map.length();
167 if (type == qMetaTypeId<IndeterminateLengthMap>()) {
168 len = CborIndefiniteLength;
169 map = v.value<IndeterminateLengthMap>();
170 }
171 CborError err = cbor_encoder_create_map(encoder, &sub, len);
172 if (err && !isOomError(err))
173 return err;
174 for (auto pair : map) {
175 err = static_cast<CborError>(err | encodeVariant(&sub, pair.first));
176 if (err && !isOomError(err))
177 return err;
178 err = static_cast<CborError>(err | encodeVariant(&sub, pair.second));
179 if (err && !isOomError(err))
180 return err;
181 }
182 return cbor_encoder_close_container_checked(encoder, &sub);
183 }
184 }
185 return CborErrorUnknownType;
186 }
187
188 template <typename Input, typename FnUnderTest>
encodeOne(Input input,FnUnderTest fn_under_test,QByteArray & buffer,CborError & error)189 void encodeOne(Input input, FnUnderTest fn_under_test, QByteArray &buffer, CborError &error)
190 {
191 uint8_t *bufptr = reinterpret_cast<quint8 *>(buffer.data());
192 CborEncoder encoder;
193 cbor_encoder_init(&encoder, bufptr, buffer.length(), 0);
194
195 error = fn_under_test(&encoder, input);
196
197 if (error == CborNoError) {
198 QCOMPARE(encoder.remaining, size_t(1));
199 QCOMPARE(cbor_encoder_get_extra_bytes_needed(&encoder), size_t(0));
200
201 buffer.resize(int(cbor_encoder_get_buffer_size(&encoder, bufptr)));
202 }
203 }
204
205 template <typename Input, typename FnUnderTest>
compare(Input input,FnUnderTest fn_under_test,const QByteArray & output)206 void compare(Input input, FnUnderTest fn_under_test, const QByteArray &output)
207 {
208 QByteArray buffer(output.length(), Qt::Uninitialized);
209 CborError error;
210
211 encodeOne(input, fn_under_test, buffer, error);
212 if (QTest::currentTestFailed())
213 return;
214
215 QCOMPARE(error, CborNoError);
216 QCOMPARE(buffer, output);
217 }
218
compare(const QVariant & input,const QByteArray & output)219 void compare(const QVariant &input, const QByteArray &output)
220 {
221 compare(input, encodeVariant, output);
222 }
223
floatAsHalfFloat_data()224 void tst_Encoder::floatAsHalfFloat_data()
225 {
226 addHalfFloat();
227 }
228
floatAsHalfFloat()229 void tst_Encoder::floatAsHalfFloat()
230 {
231 QFETCH(unsigned, rawInput);
232 QFETCH(double, floatInput);
233 QFETCH(QByteArray, output);
234
235 if (rawInput == 0U || rawInput == 0x8000U)
236 QSKIP("zero values are out of scope of this test case", QTest::SkipSingle);
237
238 if (qIsNaN(floatInput))
239 QSKIP("NaN values are out of scope of this test case", QTest::SkipSingle);
240
241 output.prepend('\xf9');
242
243 compare((float)floatInput, cbor_encode_float_as_half_float, output);
244 }
245
halfFloat_data()246 void tst_Encoder::halfFloat_data()
247 {
248 addHalfFloat();
249 }
250
halfFloat()251 void tst_Encoder::halfFloat()
252 {
253 QFETCH(unsigned, rawInput);
254 QFETCH(QByteArray, output);
255
256 uint16_t v = (uint16_t)rawInput;
257 output.prepend('\xf9');
258
259 compare(&v, cbor_encode_half_float, output);
260 }
261
floatAsHalfFloatCloseToZero_data()262 void tst_Encoder::floatAsHalfFloatCloseToZero_data()
263 {
264 QTest::addColumn<double>("floatInput");
265
266 QTest::newRow("+0") << 0.0;
267 QTest::newRow("-0") << -0.0;
268
269 QTest::newRow("below min.denorm") << ldexp(1.0, -14) * ldexp(1.0, -11);
270 QTest::newRow("above -min.denorm") << ldexp(-1.0, -14) * ldexp(1.0, -11);
271 }
272
floatAsHalfFloatCloseToZero()273 void tst_Encoder::floatAsHalfFloatCloseToZero()
274 {
275 QFETCH(double, floatInput);
276
277 QByteArray buffer(4, Qt::Uninitialized);
278 CborError error;
279
280 encodeOne((float)floatInput, cbor_encode_float_as_half_float, buffer, error);
281
282 QCOMPARE(error, CborNoError);
283
284 QVERIFY2(
285 buffer == raw("\xf9\x00\x00") || buffer == raw("\xf9\x80\x00"),
286 "Got value " + QByteArray::number(floatInput) + " encoded to: " + buffer);
287 }
288
floatAsHalfFloatNaN()289 void tst_Encoder::floatAsHalfFloatNaN()
290 {
291 QByteArray buffer(4, Qt::Uninitialized);
292 CborError error;
293
294 encodeOne(myNaNf(), cbor_encode_float_as_half_float, buffer, error);
295
296 QCOMPARE(error, CborNoError);
297 QCOMPARE(buffer.size(), 3);
298
299 uint8_t ini_byte = (uint8_t)buffer[0],
300 exp = (uint8_t)buffer[1] & 0x7cU,
301 manth = (uint8_t)buffer[1] & 0x03U,
302 mantl = (uint8_t)buffer[2];
303
304 QCOMPARE((unsigned)ini_byte, 0xf9U);
305 QCOMPARE((unsigned)exp, 0x7cU);
306 QVERIFY((manth | mantl) != 0);
307 }
308
fixed_data()309 void tst_Encoder::fixed_data()
310 {
311 addColumns();
312 addFixedData();
313 }
314
fixed()315 void tst_Encoder::fixed()
316 {
317 QFETCH(QVariant, input);
318 QFETCH(QByteArray, output);
319 compare(input, output);
320 }
321
strings_data()322 void tst_Encoder::strings_data()
323 {
324 addColumns();
325 addStringsData();
326 }
327
arraysAndMaps_data()328 void tst_Encoder::arraysAndMaps_data()
329 {
330 addColumns();
331 addArraysAndMaps();
332 }
333
tags_data()334 void tst_Encoder::tags_data()
335 {
336 addColumns();
337 addFixedData();
338 addStringsData();
339 addArraysAndMaps();
340 }
341
tags()342 void tst_Encoder::tags()
343 {
344 QFETCH(QVariant, input);
345 QFETCH(QByteArray, output);
346
347 compare(QVariant::fromValue(Tag{1, input}), "\xc1" + output);
348 if (QTest::currentTestFailed()) return;
349
350 compare(QVariant::fromValue(Tag{24, input}), "\xd8\x18" + output);
351 if (QTest::currentTestFailed()) return;
352
353 compare(QVariant::fromValue(Tag{255, input}), "\xd8\xff" + output);
354 if (QTest::currentTestFailed()) return;
355
356 compare(QVariant::fromValue(Tag{256, input}), raw("\xd9\1\0") + output);
357 if (QTest::currentTestFailed()) return;
358
359 compare(QVariant::fromValue(Tag{CborSignatureTag, input}), raw("\xd9\xd9\xf7") + output);
360 if (QTest::currentTestFailed()) return;
361
362 compare(QVariant::fromValue(Tag{65535, input}), raw("\xd9\xff\xff") + output);
363 if (QTest::currentTestFailed()) return;
364
365 compare(QVariant::fromValue(Tag{65536, input}), raw("\xda\0\1\0\0") + output);
366 if (QTest::currentTestFailed()) return;
367
368 compare(QVariant::fromValue(Tag{UINT32_MAX, input}), raw("\xda\xff\xff\xff\xff") + output);
369 if (QTest::currentTestFailed()) return;
370
371 compare(QVariant::fromValue(Tag{UINT32_MAX + Q_UINT64_C(1), input}), raw("\xdb\0\0\0\1\0\0\0\0") + output);
372 if (QTest::currentTestFailed()) return;
373
374 compare(QVariant::fromValue(Tag{UINT64_MAX, input}), raw("\xdb\xff\xff\xff\xff\xff\xff\xff\xff") + output);
375 if (QTest::currentTestFailed()) return;
376
377 // nested tags
378 compare(QVariant::fromValue(Tag{1, QVariant::fromValue(Tag{1, input})}), "\xc1\xc1" + output);
379 }
380
arrays()381 void tst_Encoder::arrays()
382 {
383 QFETCH(QVariant, input);
384 QFETCH(QByteArray, output);
385
386 compare(make_list(input), "\x81" + output);
387 if (QTest::currentTestFailed()) return;
388
389 compare(make_list(input, input), "\x82" + output + output);
390 if (QTest::currentTestFailed()) return;
391
392 {
393 QVariantList list{input};
394 QByteArray longoutput = output;
395
396 // make a list with 32 elements (1 << 5)
397 for (int i = 0; i < 5; ++i) {
398 list += list;
399 longoutput += longoutput;
400 }
401 compare(list, "\x98\x20" + longoutput);
402 if (QTest::currentTestFailed()) return;
403
404 // now 256 elements (32 << 3)
405 for (int i = 0; i < 3; ++i) {
406 list += list;
407 longoutput += longoutput;
408 }
409 compare(list, raw("\x99\1\0") + longoutput);
410 if (QTest::currentTestFailed()) return;
411 }
412
413 // nested lists
414 compare(make_list(make_list(input)), "\x81\x81" + output);
415 if (QTest::currentTestFailed()) return;
416
417 compare(make_list(make_list(input, input)), "\x81\x82" + output + output);
418 if (QTest::currentTestFailed()) return;
419
420 compare(make_list(make_list(input), input), "\x82\x81" + output + output);
421 if (QTest::currentTestFailed()) return;
422
423 compare(make_list(make_list(input), make_list(input)), "\x82\x81" + output + "\x81" + output);
424 }
425
maps()426 void tst_Encoder::maps()
427 {
428 QFETCH(QVariant, input);
429 QFETCH(QByteArray, output);
430
431 compare(make_map({{1, input}}), "\xa1\1" + output);
432 if (QTest::currentTestFailed()) return;
433
434 compare(make_map({{1, input}, {input, 24}}), "\xa2\1" + output + output + "\x18\x18");
435 if (QTest::currentTestFailed()) return;
436
437 compare(make_map({{input, input}}), "\xa1" + output + output);
438 if (QTest::currentTestFailed()) return;
439
440 {
441 Map map{{1, input}};
442 QByteArray longoutput = "\1" + output;
443
444 // make a map with 32 elements (1 << 5)
445 for (int i = 0; i < 5; ++i) {
446 map += map;
447 longoutput += longoutput;
448 }
449 compare(QVariant::fromValue(map), "\xb8\x20" + longoutput);
450 if (QTest::currentTestFailed()) return;
451
452 // now 256 elements (32 << 3)
453 for (int i = 0; i < 3; ++i) {
454 map += map;
455 longoutput += longoutput;
456 }
457 compare(QVariant::fromValue(map), raw("\xb9\1\0") + longoutput);
458 if (QTest::currentTestFailed()) return;
459 }
460
461 // nested maps
462 compare(make_map({{1, make_map({{2, input}})}}), "\xa1\1\xa1\2" + output);
463 if (QTest::currentTestFailed()) return;
464
465 compare(make_map({{1, make_map({{2, input}, {input, false}})}}), "\xa1\1\xa2\2" + output + output + "\xf4");
466 if (QTest::currentTestFailed()) return;
467
468 compare(make_map({{1, make_map({{2, input}})}, {input, false}}), "\xa2\1\xa1\2" + output + output + "\xf4");
469 if (QTest::currentTestFailed()) return;
470 }
471
writerApi()472 void tst_Encoder::writerApi()
473 {
474 QFETCH(QVariant, input);
475 QFETCH(QByteArray, output);
476
477 // instead of writing to a QByteArray like all other tests, write to a QBuffer
478 QBuffer buffer;
479 buffer.open(QIODevice::ReadWrite);
480 auto callback = [](void *token, const void *data, size_t len, CborEncoderAppendType) {
481 auto buffer = static_cast<QBuffer *>(token);
482 buffer->write(static_cast<const char *>(data), len);
483 return CborNoError;
484 };
485
486 CborEncoder encoder;
487 cbor_encoder_init_writer(&encoder, callback, &buffer);
488 QCOMPARE(encodeVariant(&encoder, input), CborNoError);
489
490 buffer.reset();
491 QCOMPARE(buffer.readAll(), output);
492 }
493
writerApiFail()494 void tst_Encoder::writerApiFail()
495 {
496 QFETCH(QVariant, input);
497 QFETCH(QByteArray, output);
498
499 // same as above, but we'll produce an error during writing and we expect
500 // it to be returned
501 int callCount = 0;
502 auto callback = [](void *token, const void *, size_t, CborEncoderAppendType) {
503 ++*static_cast<int *>(token);
504 return CborErrorIO;
505 };
506
507 CborEncoder encoder;
508 cbor_encoder_init_writer(&encoder, callback, &callCount);
509 QCOMPARE(encodeVariant(&encoder, input), CborErrorIO);
510 QCOMPARE(callCount, 1);
511 }
512
shortBuffer()513 void tst_Encoder::shortBuffer()
514 {
515 QFETCH(QVariant, input);
516 QFETCH(QByteArray, output);
517 QByteArray buffer(output.length(), Qt::Uninitialized);
518
519 for (int len = 0; len < output.length(); ++len) {
520 CborEncoder encoder;
521 cbor_encoder_init(&encoder, reinterpret_cast<quint8 *>(buffer.data()), len, 0);
522 QCOMPARE(encodeVariant(&encoder, input), CborErrorOutOfMemory);
523 QVERIFY(cbor_encoder_get_extra_bytes_needed(&encoder) != 0);
524 QCOMPARE(len + cbor_encoder_get_extra_bytes_needed(&encoder), size_t(output.length()));
525 }
526 }
527
tooShortArrays()528 void tst_Encoder::tooShortArrays()
529 {
530 QFETCH(QVariant, input);
531 QFETCH(QByteArray, output);
532 QByteArray buffer(output.length() + 1, Qt::Uninitialized);
533
534 CborEncoder encoder, container;
535 cbor_encoder_init(&encoder, reinterpret_cast<quint8 *>(buffer.data()), buffer.length(), 0);
536 QCOMPARE(cbor_encoder_create_array(&encoder, &container, 2), CborNoError);
537 QCOMPARE(encodeVariant(&container, input), CborNoError);
538 QCOMPARE(container.remaining, size_t(2));
539 QCOMPARE(cbor_encoder_close_container_checked(&encoder, &container), CborErrorTooFewItems);
540 }
541
tooShortMaps()542 void tst_Encoder::tooShortMaps()
543 {
544 QFETCH(QVariant, input);
545 QFETCH(QByteArray, output);
546 QByteArray buffer(output.length() + 1, Qt::Uninitialized);
547
548 CborEncoder encoder, container;
549 cbor_encoder_init(&encoder, reinterpret_cast<quint8 *>(buffer.data()), buffer.length(), 0);
550 QCOMPARE(cbor_encoder_create_map(&encoder, &container, 2), CborNoError);
551 QCOMPARE(encodeVariant(&container, input), CborNoError);
552 QCOMPARE(container.remaining, size_t(4));
553 QCOMPARE(cbor_encoder_close_container_checked(&encoder, &container), CborErrorTooFewItems);
554 }
555
tooBigArrays()556 void tst_Encoder::tooBigArrays()
557 {
558 QFETCH(QVariant, input);
559 QFETCH(QByteArray, output);
560 QByteArray buffer(output.length() * 2 + 1, Qt::Uninitialized);
561
562 CborEncoder encoder, container;
563 cbor_encoder_init(&encoder, reinterpret_cast<quint8 *>(buffer.data()), buffer.length(), 0);
564 QCOMPARE(cbor_encoder_create_array(&encoder, &container, 1), CborNoError);
565 QCOMPARE(encodeVariant(&container, input), CborNoError);
566 QCOMPARE(encodeVariant(&container, input), CborNoError);
567 QCOMPARE(container.remaining, size_t(0));
568 QCOMPARE(cbor_encoder_close_container_checked(&encoder, &container), CborErrorTooManyItems);
569 }
570
tooBigMaps()571 void tst_Encoder::tooBigMaps()
572 {
573 QFETCH(QVariant, input);
574 QFETCH(QByteArray, output);
575 QByteArray buffer(output.length() * 3 + 1, Qt::Uninitialized);
576
577 CborEncoder encoder, container;
578 cbor_encoder_init(&encoder, reinterpret_cast<quint8 *>(buffer.data()), buffer.length(), 0);
579 QCOMPARE(cbor_encoder_create_map(&encoder, &container, 1), CborNoError);
580 QCOMPARE(encodeVariant(&container, input), CborNoError);
581 QCOMPARE(encodeVariant(&container, input), CborNoError);
582 QCOMPARE(encodeVariant(&container, input), CborNoError);
583 QCOMPARE(container.remaining, size_t(0));
584 QCOMPARE(cbor_encoder_close_container_checked(&encoder, &container), CborErrorTooManyItems);
585 }
586
illegalSimpleType_data()587 void tst_Encoder::illegalSimpleType_data()
588 {
589 QTest::addColumn<int>("type");
590 QTest::newRow("half-float") << 25;
591 QTest::newRow("float") << 26;
592 QTest::newRow("double") << 27;
593 QTest::newRow("28") << 28;
594 QTest::newRow("29") << 29;
595 QTest::newRow("30") << 30;
596 QTest::newRow("31") << 31;
597 }
598
illegalSimpleType()599 void tst_Encoder::illegalSimpleType()
600 {
601 QFETCH(int, type);
602
603 quint8 buf[2];
604 CborEncoder encoder;
605 cbor_encoder_init(&encoder, buf, sizeof(buf), 0);
606 QCOMPARE(cbor_encode_simple_value(&encoder, type), CborErrorIllegalSimpleType);
607 }
608
609 QTEST_MAIN(tst_Encoder)
610