tinycbor: Import v0.2.1
[contrib/iotivity.git] / extlibs / tinycbor / tinycbor / tests / encoder / tst_encoder.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 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 Q_DECLARE_METATYPE(CborError)
29
30 class tst_Encoder : public QObject
31 {
32     Q_OBJECT
33 private slots:
34     void fixed_data();
35     void fixed();
36     void strings_data();
37     void strings() { fixed(); }
38     void arraysAndMaps_data();
39     void arraysAndMaps() { fixed(); }
40     void tags_data();
41     void tags();
42     void arrays_data() { tags_data(); }
43     void arrays();
44     void maps_data() { tags_data(); }
45     void maps();
46
47     void shortBuffer_data() { tags_data(); }
48     void shortBuffer();
49     void tooShortArrays_data() { tags_data(); }
50     void tooShortArrays();
51     void tooShortMaps_data() { tags_data(); }
52     void tooShortMaps();
53     void tooBigArrays_data() { tags_data(); }
54     void tooBigArrays();
55     void tooBigMaps_data() { tags_data(); }
56     void tooBigMaps();
57     void illegalSimpleType_data();
58     void illegalSimpleType();
59 };
60
61 #include "tst_encoder.moc"
62
63 template <size_t N> QByteArray raw(const char (&data)[N])
64 {
65     return QByteArray::fromRawData(data, N - 1);
66 }
67
68 struct SimpleType { uint8_t type; };
69 Q_DECLARE_METATYPE(SimpleType)
70
71 struct Float16Standin { uint16_t val; };
72 Q_DECLARE_METATYPE(Float16Standin)
73
74 struct Tag { CborTag tag; QVariant tagged; };
75 Q_DECLARE_METATYPE(Tag)
76
77 template <typename... Args>
78 QVariant make_list(const Args &... args)
79 {
80     return QVariantList{args...};
81 }
82
83 typedef QVector<QPair<QVariant, QVariant>> Map;
84 Q_DECLARE_METATYPE(Map)
85 QVariant make_map(const std::initializer_list<QPair<QVariant, QVariant>> &list)
86 {
87     return QVariant::fromValue(Map(list));
88 }
89
90 struct IndeterminateLengthArray : QVariantList { using QVariantList::QVariantList; };
91 struct IndeterminateLengthMap : Map { using Map::Map; };
92 Q_DECLARE_METATYPE(IndeterminateLengthArray)
93 Q_DECLARE_METATYPE(IndeterminateLengthMap)
94
95 QVariant make_ilarray(const std::initializer_list<QVariant> &list)
96 {
97     return QVariant::fromValue(IndeterminateLengthArray(list));
98 }
99
100 QVariant make_ilmap(const std::initializer_list<QPair<QVariant, QVariant>> &list)
101 {
102     return QVariant::fromValue(IndeterminateLengthMap(list));
103 }
104
105 static inline bool isOomError(CborError err)
106 {
107     return err == CborErrorOutOfMemory;
108 }
109
110 CborError encodeVariant(CborEncoder *encoder, const QVariant &v)
111 {
112     int type = v.userType();
113     switch (type) {
114     case QVariant::Int:
115     case QVariant::LongLong:
116         return cbor_encode_int(encoder, v.toLongLong());
117
118     case QVariant::UInt:
119     case QVariant::ULongLong:
120         return cbor_encode_uint(encoder, v.toULongLong());
121
122     case QVariant::Bool:
123         return cbor_encode_boolean(encoder, v.toBool());
124
125     case QVariant::Invalid:
126         return cbor_encode_undefined(encoder);
127
128     case QMetaType::VoidStar:
129         return cbor_encode_null(encoder);
130
131     case QVariant::Double:
132         return cbor_encode_double(encoder, v.toDouble());
133
134     case QMetaType::Float:
135         return cbor_encode_float(encoder, v.toFloat());
136
137     case QVariant::String: {
138         QByteArray string = v.toString().toUtf8();
139         return cbor_encode_text_string(encoder, string.constData(), string.length());
140     }
141
142     case QVariant::ByteArray: {
143         QByteArray string = v.toByteArray();
144         return cbor_encode_byte_string(encoder, reinterpret_cast<const quint8 *>(string.constData()), string.length());
145     }
146
147     default:
148         if (type == qMetaTypeId<SimpleType>())
149             return cbor_encode_simple_value(encoder, v.value<SimpleType>().type);
150         if (type == qMetaTypeId<Float16Standin>())
151             return cbor_encode_half_float(encoder, v.constData());
152         if (type == qMetaTypeId<Tag>()) {
153             CborError err = cbor_encode_tag(encoder, v.value<Tag>().tag);
154             if (err && !isOomError(err))
155                 return err;
156             return static_cast<CborError>(err | encodeVariant(encoder, v.value<Tag>().tagged));
157         }
158         if (type == QVariant::List || type == qMetaTypeId<IndeterminateLengthArray>()) {
159             CborEncoder sub;
160             QVariantList list = v.toList();
161             size_t len = list.length();
162             if (type == qMetaTypeId<IndeterminateLengthArray>()) {
163                 len = CborIndefiniteLength;
164                 list = v.value<IndeterminateLengthArray>();
165             }
166             CborError err = cbor_encoder_create_array(encoder, &sub, len);
167             if (err && !isOomError(err))
168                 return err;
169             foreach (const QVariant &v2, list) {
170                 err = static_cast<CborError>(err | encodeVariant(&sub, v2));
171                 if (err && !isOomError(err))
172                     return err;
173             }
174             return static_cast<CborError>(err | cbor_encoder_close_container_checked(encoder, &sub));
175         }
176         if (type == qMetaTypeId<Map>() || type == qMetaTypeId<IndeterminateLengthMap>()) {
177             CborEncoder sub;
178             Map map = v.value<Map>();
179             size_t len = map.length();
180             if (type == qMetaTypeId<IndeterminateLengthMap>()) {
181                 len = CborIndefiniteLength;
182                 map = v.value<IndeterminateLengthMap>();
183             }
184             CborError err = cbor_encoder_create_map(encoder, &sub, len);
185             if (err && !isOomError(err))
186                 return err;
187             for (auto pair : map) {
188                 err = static_cast<CborError>(err | encodeVariant(&sub, pair.first));
189                 if (err && !isOomError(err))
190                     return err;
191                 err = static_cast<CborError>(err | encodeVariant(&sub, pair.second));
192                 if (err && !isOomError(err))
193                     return err;
194             }
195             return (CborError)(err | cbor_encoder_close_container_checked(encoder, &sub));
196         }
197     }
198     return CborErrorUnknownType;
199 }
200
201 bool compareFailed;
202 void compare(const QVariant &input, const QByteArray &output)
203 {
204     QByteArray buffer(output.length(), Qt::Uninitialized);
205     CborEncoder encoder;
206     cbor_encoder_init(&encoder, reinterpret_cast<quint8 *>(buffer.data()), buffer.length(), 0);
207     QCOMPARE(int(encodeVariant(&encoder, input)), int(CborNoError));
208     buffer.resize(encoder.ptr - reinterpret_cast<const quint8 *>(buffer.constData()));
209     QCOMPARE(buffer, output);
210     QCOMPARE(encoder.added, size_t(1));
211 }
212
213 void addColumns()
214 {
215     QTest::addColumn<QByteArray>("output");
216     QTest::addColumn<QVariant>("input");
217 }
218
219 void addFixedData()
220 {
221     // unsigned integers
222     QTest::newRow("0U") << raw("\x00") << QVariant(0U);
223     QTest::newRow("1U") << raw("\x01") << QVariant(1U);
224     QTest::newRow("10U") << raw("\x0a") << QVariant(10U);
225     QTest::newRow("23U") << raw("\x17") << QVariant(23U);
226     QTest::newRow("24U") << raw("\x18\x18") << QVariant(24U);
227     QTest::newRow("255U") << raw("\x18\xff") << QVariant(255U);
228     QTest::newRow("256U") << raw("\x19\x01\x00") << QVariant(256U);
229     QTest::newRow("65535U") << raw("\x19\xff\xff") << QVariant(65535U);
230     QTest::newRow("65536U") << raw("\x1a\0\1\x00\x00") << QVariant(65536U);
231     QTest::newRow("4294967295U") << raw("\x1a\xff\xff\xff\xff") << QVariant(4294967295U);
232     QTest::newRow("4294967296U") << raw("\x1b\0\0\0\1\0\0\0\0") << QVariant(Q_UINT64_C(4294967296));
233     QTest::newRow("UINT64_MAX") << raw("\x1b" "\xff\xff\xff\xff" "\xff\xff\xff\xff")
234                                 << QVariant(std::numeric_limits<quint64>::max());
235
236     // signed integers containing positive numbers
237     QTest::newRow("0") << raw("\x00") << QVariant(0);
238     QTest::newRow("1") << raw("\x01") << QVariant(1);
239     QTest::newRow("10") << raw("\x0a") << QVariant(10);
240     QTest::newRow("23") << raw("\x17") << QVariant(23);
241     QTest::newRow("24") << raw("\x18\x18") << QVariant(24);
242     QTest::newRow("255") << raw("\x18\xff") << QVariant(255);
243     QTest::newRow("256") << raw("\x19\x01\x00") << QVariant(256);
244     QTest::newRow("65535") << raw("\x19\xff\xff") << QVariant(65535);
245     QTest::newRow("65536") << raw("\x1a\0\1\x00\x00") << QVariant(65536);
246     QTest::newRow("4294967295") << raw("\x1a\xff\xff\xff\xff") << QVariant(Q_INT64_C(4294967295));
247     QTest::newRow("4294967296") << raw("\x1b\0\0\0\1\0\0\0\0") << QVariant(Q_INT64_C(4294967296));
248
249     // negative integers
250     QTest::newRow("-1") << raw("\x20") << QVariant(-1);
251     QTest::newRow("-2") << raw("\x21") << QVariant(-2);
252     QTest::newRow("-24") << raw("\x37") << QVariant(-24);
253     QTest::newRow("-25") << raw("\x38\x18") << QVariant(-25);
254     QTest::newRow("-UINT8_MAX") << raw("\x38\xff") << QVariant(-256);
255     QTest::newRow("-UINT8_MAX-1") << raw("\x39\x01\x00") << QVariant(-257);
256     QTest::newRow("-UINT16_MAX") << raw("\x39\xff\xff") << QVariant(-65536);
257     QTest::newRow("-UINT16_MAX-1") << raw("\x3a\0\1\x00\x00") << QVariant(-65537);
258     QTest::newRow("-UINT32_MAX") << raw("\x3a\xff\xff\xff\xff") << QVariant(Q_INT64_C(-4294967296));
259     QTest::newRow("-UINT32_MAX-1") << raw("\x3b\0\0\0\1\0\0\0\0") << QVariant(Q_INT64_C(-4294967297));
260 //    QTest::newRow("-UINT64_MAX") << raw("\x3b" "\xff\xff\xff\xff" "\xff\xff\xff\xff")
261 //                                 << QVariant::fromValue(BigNegative{std::numeric_limits<quint64>::max()});
262
263     QTest::newRow("simple0") << raw("\xe0") << QVariant::fromValue(SimpleType{0});
264     QTest::newRow("simple19") << raw("\xf3") << QVariant::fromValue(SimpleType{19});
265     QTest::newRow("false") << raw("\xf4") << QVariant(false);
266     QTest::newRow("true") << raw("\xf5") << QVariant(true);
267     QTest::newRow("null") << raw("\xf6") << QVariant::fromValue<void *>(nullptr);
268     QTest::newRow("undefined") << raw("\xf7") << QVariant();
269     QTest::newRow("simple32") << raw("\xf8\x20") << QVariant::fromValue(SimpleType{32});
270     QTest::newRow("simple255") << raw("\xf8\xff") << QVariant::fromValue(SimpleType{255});
271
272     // floating point
273     QTest::newRow("0f16") << raw("\xf9\0\0") << QVariant::fromValue(Float16Standin{0x0000});
274
275     QTest::newRow("0.f") << raw("\xfa\0\0\0\0") << QVariant::fromValue(0.f);
276     QTest::newRow("0.")  << raw("\xfb\0\0\0\0\0\0\0\0") << QVariant(0.);
277     QTest::newRow("-1.f") << raw("\xfa\xbf\x80\0\0") << QVariant::fromValue(-1.f);
278     QTest::newRow("-1.") << raw("\xfb\xbf\xf0\0\0\0\0\0\0") << QVariant(-1.);
279     QTest::newRow("16777215.f") << raw("\xfa\x4b\x7f\xff\xff") << QVariant::fromValue(16777215.f);
280     QTest::newRow("16777215.") << raw("\xfb\x41\x6f\xff\xff\xe0\0\0\0") << QVariant::fromValue(16777215.);
281     QTest::newRow("-16777215.f") << raw("\xfa\xcb\x7f\xff\xff") << QVariant(-16777215.f);
282     QTest::newRow("-16777215.") << raw("\xfb\xc1\x6f\xff\xff\xe0\0\0\0") << QVariant::fromValue(-16777215.);
283
284 #ifdef Q_CC_MSVC
285     // MSVC NaNs have the sign bit unset
286     QTest::newRow("qnan_f") << raw("\xfa\x7f\xc0\0\0") << QVariant::fromValue<float>(qQNaN());
287     QTest::newRow("qnan") << raw("\xfb\x7f\xf8\0\0\0\0\0\0") << QVariant(qQNaN());
288     QTest::newRow("snan_f") << raw("\xfa\x7f\xc0\0\0") << QVariant::fromValue<float>(qSNaN());
289     QTest::newRow("snan") << raw("\xfb\x7f\xf0\0\0\0\0\0\1") << QVariant(qSNaN());
290 #else
291     // GCC NaNs have the sign bit set
292     QTest::newRow("qnan_f") << raw("\xfa\xff\xc0\0\0") << QVariant::fromValue<float>(qQNaN());
293     QTest::newRow("qnan") << raw("\xfb\xff\xf8\0\0\0\0\0\0") << QVariant(qQNaN());
294     QTest::newRow("snan_f") << raw("\xfa\x7f\xc0\0\0") << QVariant::fromValue<float>(qSNaN());
295     QTest::newRow("snan") << raw("\xfb\x7f\xf8\0\0\0\0\0\0") << QVariant(qSNaN());
296 #endif
297     QTest::newRow("-inf_f") << raw("\xfa\xff\x80\0\0") << QVariant::fromValue<float>(-qInf());
298     QTest::newRow("-inf") << raw("\xfb\xff\xf0\0\0\0\0\0\0") << QVariant(-qInf());
299     QTest::newRow("+inf_f") << raw("\xfa\x7f\x80\0\0") << QVariant::fromValue<float>(qInf());
300     QTest::newRow("+inf") << raw("\xfb\x7f\xf0\0\0\0\0\0\0") << QVariant(qInf());
301 }
302
303 void addStringsData()
304 {
305     // byte strings
306     QTest::newRow("emptybytestring") << raw("\x40") << QVariant(QByteArray(""));
307     QTest::newRow("bytestring1") << raw("\x41 ") << QVariant(QByteArray(" "));
308     QTest::newRow("bytestring1-nul") << raw("\x41\0") << QVariant(QByteArray("", 1));
309     QTest::newRow("bytestring5") << raw("\x45Hello") << QVariant(QByteArray("Hello"));
310     QTest::newRow("bytestring24") << raw("\x58\x18""123456789012345678901234")
311                                   << QVariant(QByteArray("123456789012345678901234"));
312     QTest::newRow("bytestring256") << raw("\x59\1\0") + QByteArray(256, '3')
313                                    << QVariant(QByteArray(256, '3'));
314
315     // text strings
316     QTest::newRow("emptytextstring") << raw("\x60") << QVariant("");
317     QTest::newRow("textstring1") << raw("\x61 ") << QVariant(" ");
318     QTest::newRow("textstring1-nul") << raw("\x61\0") << QVariant(QString::fromLatin1("", 1));
319     QTest::newRow("textstring5") << raw("\x65Hello") << QVariant("Hello");
320     QTest::newRow("textstring24") << raw("\x78\x18""123456789012345678901234")
321                                   << QVariant("123456789012345678901234");
322     QTest::newRow("textstring256") << raw("\x79\1\0") + QByteArray(256, '3')
323                                    << QVariant(QString(256, '3'));
324 }
325
326 void addArraysAndMaps()
327 {
328     QTest::newRow("emptyarray") << raw("\x80") << make_list();
329     QTest::newRow("emptymap") << raw("\xa0") << make_map({});
330
331     QTest::newRow("array-0") << raw("\x81\0") << make_list(0);
332     QTest::newRow("array-{0-0}") << raw("\x82\0\0") << make_list(0, 0);
333     QTest::newRow("array-Hello") << raw("\x81\x65Hello") << make_list("Hello");
334     QTest::newRow("array-array-0") << raw("\x81\x81\0") << make_list(make_list(0));
335     QTest::newRow("array-array-{0-0}") << raw("\x81\x82\0\0") << make_list(make_list(0, 0));
336     QTest::newRow("array-array-0-0") << raw("\x82\x81\0\0") << make_list(make_list(0),0);
337     QTest::newRow("array-array-Hello") << raw("\x81\x81\x65Hello") << make_list(make_list("Hello"));
338
339     QTest::newRow("map-0:0") << raw("\xa1\0\0") << make_map({{0,0}});
340     QTest::newRow("map-0:0-1:1") << raw("\xa2\0\0\1\1") << make_map({{0,0}, {1,1}});
341     QTest::newRow("map-0:{map-0:0-1:1}") << raw("\xa1\0\xa2\0\0\1\1") << make_map({{0, make_map({{0,0}, {1,1}})}});
342
343     QTest::newRow("array-map1") << raw("\x81\xa1\0\0") << make_list(make_map({{0,0}}));
344     QTest::newRow("array-map2") << raw("\x82\xa1\0\0\xa1\1\1") << make_list(make_map({{0,0}}), make_map({{1,1}}));
345
346     QTest::newRow("map-array1") << raw("\xa1\x62oc\x81\0") << make_map({{"oc", make_list(0)}});
347     QTest::newRow("map-array2") << raw("\xa1\x62oc\x84\0\1\2\3") << make_map({{"oc", make_list(0, 1, 2, 3)}});
348     QTest::newRow("map-array3") << raw("\xa2\x62oc\x82\0\1\2\3") << make_map({{"oc", make_list(0, 1)}, {2, 3}});
349
350     // indeterminate length
351     QTest::newRow("_emptyarray") << raw("\x9f\xff") << QVariant::fromValue(IndeterminateLengthArray{});
352     QTest::newRow("_emptymap") << raw("\xbf\xff") << make_ilmap({});
353
354     QTest::newRow("_array-0") << raw("\x9f\0\xff") << make_ilarray({0});
355     QTest::newRow("_array-{0-0}") << raw("\x9f\0\0\xff") << make_ilarray({0, 0});
356     QTest::newRow("_array-Hello") << raw("\x9f\x65Hello\xff") << make_ilarray({"Hello"});
357     QTest::newRow("_array-array-0") << raw("\x9f\x81\0\xff") << make_ilarray({make_list(0)});
358     QTest::newRow("_array-_array-0") << raw("\x9f\x9f\0\xff\xff") << make_ilarray({make_ilarray({0})});
359     QTest::newRow("_array-_array-{0-0}") << raw("\x9f\x9f\0\0\xff\xff") << make_ilarray({make_ilarray({0, 0})});
360     QTest::newRow("_array-_array-0-0") << raw("\x9f\x9f\0\xff\0\xff") << make_ilarray({make_ilarray({0}),0});
361     QTest::newRow("_array-_array-Hello") << raw("\x9f\x9f\x65Hello\xff\xff") << make_ilarray({make_ilarray({"Hello"})});
362
363     QTest::newRow("_map-0:0") << raw("\xbf\0\0\xff") << make_ilmap({{0,0}});
364     QTest::newRow("_map-0:0-1:1") << raw("\xbf\0\0\1\1\xff") << make_ilmap({{0,0}, {1,1}});
365     QTest::newRow("_map-0:{map-0:0-1:1}") << raw("\xbf\0\xa2\0\0\1\1\xff") << make_ilmap({{0, make_map({{0,0}, {1,1}})}});
366     QTest::newRow("_map-0:{_map-0:0-1:1}") << raw("\xbf\0\xbf\0\0\1\1\xff\xff") << make_ilmap({{0, make_ilmap({{0,0}, {1,1}})}});
367
368     QTest::newRow("_array-map1") << raw("\x9f\xa1\0\0\xff") << make_ilarray({make_map({{0,0}})});
369     QTest::newRow("_array-_map1") << raw("\x9f\xbf\0\0\xff\xff") << make_ilarray({make_ilmap({{0,0}})});
370     QTest::newRow("_array-map2") << raw("\x9f\xa1\0\0\xa1\1\1\xff") << make_ilarray({make_map({{0,0}}), make_map({{1,1}})});
371     QTest::newRow("_array-_map2") << raw("\x9f\xbf\0\0\xff\xbf\1\1\xff\xff") << make_ilarray({make_ilmap({{0,0}}), make_ilmap({{1,1}})});
372
373     QTest::newRow("_map-array1") << raw("\xbf\x62oc\x81\0\xff") << make_ilmap({{"oc", make_list(0)}});
374     QTest::newRow("_map-_array1") << raw("\xbf\x62oc\x9f\0\xff\xff") << make_ilmap({{"oc", make_ilarray({0})}});
375     QTest::newRow("_map-array2") << raw("\xbf\x62oc\x84\0\1\2\3\xff") << make_ilmap({{"oc", make_list(0, 1, 2, 3)}});
376     QTest::newRow("_map-_array2") << raw("\xbf\x62oc\x9f\0\1\2\3\xff\xff") << make_ilmap({{"oc", make_ilarray({0, 1, 2, 3})}});
377     QTest::newRow("_map-array3") << raw("\xbf\x62oc\x82\0\1\2\3\xff") << make_ilmap({{"oc", make_list(0, 1)}, {2, 3}});
378     QTest::newRow("_map-_array3") << raw("\xbf\x62oc\x9f\0\1\xff\2\3\xff") << make_ilmap({{"oc", make_ilarray({0, 1})}, {2, 3}});
379
380     // tagged
381     QTest::newRow("array-1(0)") << raw("\x81\xc1\0") << make_list(QVariant::fromValue(Tag{1, 0}));
382     QTest::newRow("array-1(map)") << raw("\x81\xc1\xa0") << make_list(QVariant::fromValue(Tag{1, make_map({})}));
383     QTest::newRow("map-1(2):3(4)") << raw("\xa1\xc1\2\xc3\4") << make_map({{QVariant::fromValue(Tag{1, 2}), QVariant::fromValue(Tag{3, 4})}});
384 }
385
386 void tst_Encoder::fixed_data()
387 {
388     addColumns();
389     addFixedData();
390 }
391
392 void tst_Encoder::fixed()
393 {
394     QFETCH(QVariant, input);
395     QFETCH(QByteArray, output);
396     compare(input, output);
397 }
398
399 void tst_Encoder::strings_data()
400 {
401     addColumns();
402     addStringsData();
403 }
404
405 void tst_Encoder::arraysAndMaps_data()
406 {
407     addColumns();
408     addArraysAndMaps();
409 }
410
411 void tst_Encoder::tags_data()
412 {
413     addColumns();
414     addFixedData();
415     addStringsData();
416     addArraysAndMaps();
417 }
418
419 void tst_Encoder::tags()
420 {
421     QFETCH(QVariant, input);
422     QFETCH(QByteArray, output);
423
424     compare(QVariant::fromValue(Tag{1, input}), "\xc1" + output);
425     if (compareFailed) return;
426
427     compare(QVariant::fromValue(Tag{24, input}), "\xd8\x18" + output);
428     if (compareFailed) return;
429
430     compare(QVariant::fromValue(Tag{255, input}), "\xd8\xff" + output);
431     if (compareFailed) return;
432
433     compare(QVariant::fromValue(Tag{256, input}), raw("\xd9\1\0") + output);
434     if (compareFailed) return;
435
436     compare(QVariant::fromValue(Tag{CborSignatureTag, input}), raw("\xd9\xd9\xf7") + output);
437     if (compareFailed) return;
438
439     compare(QVariant::fromValue(Tag{65535, input}), raw("\xd9\xff\xff") + output);
440     if (compareFailed) return;
441
442     compare(QVariant::fromValue(Tag{65536, input}), raw("\xda\0\1\0\0") + output);
443     if (compareFailed) return;
444
445     compare(QVariant::fromValue(Tag{UINT32_MAX, input}), raw("\xda\xff\xff\xff\xff") + output);
446     if (compareFailed) return;
447
448     compare(QVariant::fromValue(Tag{UINT32_MAX + Q_UINT64_C(1), input}), raw("\xdb\0\0\0\1\0\0\0\0") + output);
449     if (compareFailed) return;
450
451     compare(QVariant::fromValue(Tag{UINT64_MAX, input}), raw("\xdb\xff\xff\xff\xff\xff\xff\xff\xff") + output);
452     if (compareFailed) return;
453
454     // nested tags
455     compare(QVariant::fromValue(Tag{1, QVariant::fromValue(Tag{1, input})}), "\xc1\xc1" + output);
456 }
457
458 void tst_Encoder::arrays()
459 {
460     QFETCH(QVariant, input);
461     QFETCH(QByteArray, output);
462
463     compare(make_list(input), "\x81" + output);
464     if (compareFailed) return;
465
466     compare(make_list(input, input), "\x82" + output + output);
467     if (compareFailed) return;
468
469     {
470         QVariantList list{input};
471         QByteArray longoutput = output;
472
473         // make a list with 32 elements (1 << 5)
474         for (int i = 0; i < 5; ++i) {
475             list += list;
476             longoutput += longoutput;
477         }
478         compare(list, "\x98\x20" + longoutput);
479         if (compareFailed) return;
480
481         // now 256 elements (32 << 3)
482         for (int i = 0; i < 3; ++i) {
483             list += list;
484             longoutput += longoutput;
485         }
486         compare(list, raw("\x99\1\0") + longoutput);
487         if (compareFailed) return;
488     }
489
490     // nested lists
491     compare(make_list(make_list(input)), "\x81\x81" + output);
492     if (compareFailed) return;
493
494     compare(make_list(make_list(input, input)), "\x81\x82" + output + output);
495     if (compareFailed) return;
496
497     compare(make_list(make_list(input), input), "\x82\x81" + output + output);
498     if (compareFailed) return;
499
500     compare(make_list(make_list(input), make_list(input)), "\x82\x81" + output + "\x81" + output);
501 }
502
503 void tst_Encoder::maps()
504 {
505     QFETCH(QVariant, input);
506     QFETCH(QByteArray, output);
507
508     compare(make_map({{1, input}}), "\xa1\1" + output);
509     if (compareFailed) return;
510
511     compare(make_map({{1, input}, {input, 24}}), "\xa2\1" + output + output + "\x18\x18");
512     if (compareFailed) return;
513
514     compare(make_map({{input, input}}), "\xa1" + output + output);
515     if (compareFailed) return;
516
517     {
518         Map map{{1, input}};
519         QByteArray longoutput = "\1" + output;
520
521         // make a map with 32 elements (1 << 5)
522         for (int i = 0; i < 5; ++i) {
523             map += map;
524             longoutput += longoutput;
525         }
526         compare(QVariant::fromValue(map), "\xb8\x20" + longoutput);
527         if (compareFailed) return;
528
529         // now 256 elements (32 << 3)
530         for (int i = 0; i < 3; ++i) {
531             map += map;
532             longoutput += longoutput;
533         }
534         compare(QVariant::fromValue(map), raw("\xb9\1\0") + longoutput);
535         if (compareFailed) return;
536     }
537
538     // nested lists
539     compare(make_map({{1, make_map({{2, input}})}}), "\xa1\1\xa1\2" + output);
540     if (compareFailed) return;
541
542     compare(make_map({{1, make_map({{2, input}, {input, false}})}}), "\xa1\1\xa2\2" + output + output + "\xf4");
543     if (compareFailed) return;
544
545     compare(make_map({{1, make_map({{2, input}})}, {input, false}}), "\xa2\1\xa1\2" + output + output + "\xf4");
546     if (compareFailed) return;
547 }
548
549 void tst_Encoder::shortBuffer()
550 {
551     QFETCH(QVariant, input);
552     QFETCH(QByteArray, output);
553     QByteArray buffer(output.length(), Qt::Uninitialized);
554
555     for (int len = 0; len < output.length() - 1; ++len) {
556         CborEncoder encoder;
557         cbor_encoder_init(&encoder, reinterpret_cast<quint8 *>(buffer.data()), len, 0);
558         QCOMPARE(int(encodeVariant(&encoder, input)), int(CborErrorOutOfMemory));
559         QCOMPARE(len + int(encoder.ptr - encoder.end), output.length());
560     }
561 }
562
563 void tst_Encoder::tooShortArrays()
564 {
565     QFETCH(QVariant, input);
566     QFETCH(QByteArray, output);
567     QByteArray buffer(output.length() + 1, Qt::Uninitialized);
568
569     CborEncoder encoder, container;
570     cbor_encoder_init(&encoder, reinterpret_cast<quint8 *>(buffer.data()), buffer.length(), 0);
571     QCOMPARE(cbor_encoder_create_array(&encoder, &container, 2), CborNoError);
572     QCOMPARE(int(encodeVariant(&container, input)), int(CborNoError));
573     QCOMPARE(container.added, size_t(1));
574     QCOMPARE(int(cbor_encoder_close_container_checked(&encoder, &container)), int(CborErrorTooFewItems));
575 }
576
577 void tst_Encoder::tooShortMaps()
578 {
579     QFETCH(QVariant, input);
580     QFETCH(QByteArray, output);
581     QByteArray buffer(output.length() + 1, Qt::Uninitialized);
582
583     CborEncoder encoder, container;
584     cbor_encoder_init(&encoder, reinterpret_cast<quint8 *>(buffer.data()), buffer.length(), 0);
585     QCOMPARE(cbor_encoder_create_map(&encoder, &container, 2), CborNoError);
586     QCOMPARE(int(encodeVariant(&container, input)), int(CborNoError));
587     QCOMPARE(container.added, size_t(1));
588     QCOMPARE(int(cbor_encoder_close_container_checked(&encoder, &container)), int(CborErrorTooFewItems));
589 }
590
591 void tst_Encoder::tooBigArrays()
592 {
593     QFETCH(QVariant, input);
594     QFETCH(QByteArray, output);
595     QByteArray buffer(output.length() * 2 + 1, Qt::Uninitialized);
596
597     CborEncoder encoder, container;
598     cbor_encoder_init(&encoder, reinterpret_cast<quint8 *>(buffer.data()), buffer.length(), 0);
599     QCOMPARE(cbor_encoder_create_array(&encoder, &container, 1), CborNoError);
600     QCOMPARE(int(encodeVariant(&container, input)), int(CborNoError));
601     QCOMPARE(int(encodeVariant(&container, input)), int(CborNoError));
602     QCOMPARE(container.added, size_t(2));
603     QCOMPARE(int(cbor_encoder_close_container_checked(&encoder, &container)), int(CborErrorTooManyItems));
604 }
605
606 void tst_Encoder::tooBigMaps()
607 {
608     QFETCH(QVariant, input);
609     QFETCH(QByteArray, output);
610     QByteArray buffer(output.length() * 3 + 1, Qt::Uninitialized);
611
612     CborEncoder encoder, container;
613     cbor_encoder_init(&encoder, reinterpret_cast<quint8 *>(buffer.data()), buffer.length(), 0);
614     QCOMPARE(cbor_encoder_create_map(&encoder, &container, 1), CborNoError);
615     QCOMPARE(int(encodeVariant(&container, input)), int(CborNoError));
616     QCOMPARE(int(encodeVariant(&container, input)), int(CborNoError));
617     QCOMPARE(int(encodeVariant(&container, input)), int(CborNoError));
618     QCOMPARE(container.added, size_t(3));
619     QCOMPARE(int(cbor_encoder_close_container_checked(&encoder, &container)), int(CborErrorTooManyItems));
620 }
621
622 void tst_Encoder::illegalSimpleType_data()
623 {
624     QTest::addColumn<int>("type");
625     QTest::newRow("half-float") << 25;
626     QTest::newRow("float") << 26;
627     QTest::newRow("double") << 27;
628     QTest::newRow("28") << 28;
629     QTest::newRow("29") << 29;
630     QTest::newRow("30") << 30;
631     QTest::newRow("31") << 31;
632 }
633
634 void tst_Encoder::illegalSimpleType()
635 {
636     QFETCH(int, type);
637
638     quint8 buf[2];
639     CborEncoder encoder;
640     cbor_encoder_init(&encoder, buf, sizeof(buf), 0);
641     QCOMPARE(int(cbor_encode_simple_value(&encoder, type)), int(CborErrorIllegalSimpleType));
642 }
643
644 QTEST_MAIN(tst_Encoder)