File: CBORReaderTest.cpp

package info (click to toggle)
wpewebkit 2.38.6-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 311,508 kB
  • sloc: cpp: 2,653,313; javascript: 289,013; ansic: 121,268; xml: 64,149; python: 35,534; ruby: 17,287; perl: 15,877; asm: 11,072; yacc: 2,326; sh: 1,863; lex: 1,319; java: 937; makefile: 146; pascal: 60
file content (769 lines) | stat: -rw-r--r-- 29,208 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
// Copyright 2017 The Chromium Authors. All rights reserved.
// Copyright (C) 2018 Apple Inc. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//    * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//    * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//    * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "config.h"

#if ENABLE(WEB_AUTHN)

#include <WebCore/CBORReader.h>
#include <limits>
#include <utility>

// Leveraging RFC 7049 examples from https://github.com/cbor/test-vectors/blob/master/appendix_a.json.
namespace TestWebKitAPI {

using namespace cbor;

TEST(CBORReaderTest, TestReadUint)
{
    struct UintTestCase {
        const int64_t value;
        const Vector<uint8_t> cborData;
    };

    static const UintTestCase kUintTestCases[] = {
        { 0, { 0x00 } },
        { 1, { 0x01 } },
        { 23, { 0x17 } },
        { 24, { 0x18, 0x18 } },
        { std::numeric_limits<uint8_t>::max(), { 0x18, 0xff } },
        { 1LL << 8, { 0x19, 0x01, 0x00 } },
        { std::numeric_limits<uint16_t>::max(), { 0x19, 0xff, 0xff } },
        { 1LL << 16, { 0x1a, 0x00, 0x01, 0x00, 0x00 } },
        { std::numeric_limits<uint32_t>::max(), { 0x1a, 0xff, 0xff, 0xff, 0xff } },
        { 1LL << 32, { 0x1b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 } },
        { std::numeric_limits<int64_t>::max(),
            { 0x1b, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF } },
    };

    for (const UintTestCase& testCase : kUintTestCases) {
        std::optional<CBORValue> cbor = CBORReader::read(testCase.cborData);
        ASSERT_TRUE(cbor.has_value());
        ASSERT_TRUE(cbor.value().type() == CBORValue::Type::Unsigned);
        EXPECT_EQ(cbor.value().getInteger(), testCase.value);
    }
}

TEST(CBORReaderTest, TestUintEncodedWithNonMinimumByteLength)
{
    static const Vector<uint8_t> nonMinimalUintEncodings[] = {
        // Uint 23 encoded with 1 byte.
        { 0x18, 0x17 },
        // Uint 255 encoded with 2 bytes.
        { 0x19, 0x00, 0xff },
        // Uint 65535 encoded with 4 byte.
        { 0x1a, 0x00, 0x00, 0xff, 0xff },
        // Uint 4294967295 encoded with 8 byte.
        { 0x1b, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff },
        // When decoding byte has more than one syntax error, the first syntax
        // error encountered during deserialization is returned as the error code.
        {
            0xa2, // map with non-minimally encoded key
            0x17, // key 24
            0x61, 0x42, // value :"B"
            0x18, 0x17, // key 23 encoded with extra byte
            0x61, 0x45 // value "E"
        },
        {
            0xa2, // map with out of order and non-minimally encoded key
            0x18, 0x17, // key 23 encoded with extra byte
            0x61, 0x45, // value "E"
            0x17, // key 23
            0x61, 0x42 // value :"B"
        },
        {
            0xa2, // map with duplicate non-minimally encoded key
            0x18, 0x17, // key 23 encoded with extra byte
            0x61, 0x45, // value "E"
            0x18, 0x17, // key 23 encoded with extra byte
            0x61, 0x42 // value :"B"
        },
    };

    CBORReader::DecoderError errorCode;
    for (const auto& nonMinimalUint : nonMinimalUintEncodings) {
        std::optional<CBORValue> cbor = CBORReader::read(nonMinimalUint, &errorCode);
        EXPECT_FALSE(cbor.has_value());
        EXPECT_TRUE(errorCode == CBORReader::DecoderError::NonMinimalCBOREncoding);
    }
}

TEST(CBORReaderTest, TestReadNegativeInt)
{
    struct NegativeIntTestCase {
        const int64_t negativeInt;
        const Vector<uint8_t> cborData;
    };

    static const NegativeIntTestCase kNegativeIntTestCases[] = {
        { -1LL, { 0x20 } },
        { -24LL, { 0x37 } },
        { -25LL, { 0x38, 0x18 } },
        { -256LL, { 0x38, 0xff } },
        { -1000LL, { 0x39, 0x03, 0xe7 } },
        { -1000000LL, { 0x3a, 0x00, 0x0f, 0x42, 0x3f } },
        { -4294967296LL, { 0x3a, 0xff, 0xff, 0xff, 0xff } },
        { std::numeric_limits<int64_t>::min(),
            { 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } }
    };

    for (const NegativeIntTestCase& testCase : kNegativeIntTestCases) {
        std::optional<CBORValue> cbor = CBORReader::read(testCase.cborData);
        ASSERT_TRUE(cbor.has_value());
        ASSERT_TRUE(cbor.value().type() == CBORValue::Type::Negative);
        EXPECT_EQ(cbor.value().getInteger(), testCase.negativeInt);
    }
}

TEST(CBORReaderTest, TestReadBytes)
{
    struct ByteTestCase {
        const Vector<uint8_t> value;
        const Vector<uint8_t> cborData;
    };

    static const ByteTestCase kByteStringTestCases[] = {
        {{ }, {0x40}},
        {{0x01, 0x02, 0x03, 0x04}, {0x44, 0x01, 0x02, 0x03, 0x04}},
    };

    for (const ByteTestCase& testCase : kByteStringTestCases) {
        std::optional<CBORValue> cbor = CBORReader::read(testCase.cborData);
        ASSERT_TRUE(cbor.has_value());
        ASSERT_TRUE(cbor.value().type() == CBORValue::Type::ByteString);
        EXPECT_TRUE(cbor.value().getByteString() == testCase.value);
    }
}

TEST(CBORReaderTest, TestReadString)
{
    struct StringTestCase {
        const String value;
        const Vector<uint8_t> cborData;
    };

    static const StringTestCase kStringTestCases[] = {
        { emptyString(), { 0x60 } },
        { "a"_s, { 0x61, 0x61 } },
        { "IETF"_s, { 0x64, 0x49, 0x45, 0x54, 0x46 } },
        { "\"\\"_s, { 0x62, 0x22, 0x5c } },
        { String::fromUTF8("\xc3\xbc"), { 0x62, 0xc3, 0xbc } },
        { String::fromUTF8("\xe6\xb0\xb4"), { 0x63, 0xe6, 0xb0, 0xb4 } },
        { String::fromUTF8("\xf0\x90\x85\x91"), { 0x64, 0xf0, 0x90, 0x85, 0x91 } },
    };

    for (const StringTestCase& testCase : kStringTestCases) {
        std::optional<CBORValue> cbor = CBORReader::read(testCase.cborData);
        ASSERT_TRUE(cbor.has_value());
        ASSERT_TRUE(cbor.value().type() == CBORValue::Type::String);
        EXPECT_TRUE(cbor.value().getString() == testCase.value);
    }
}

TEST(CBORReaderTest, TestReadStringWithNUL)
{
    static const struct {
        const String value;
        const Vector<uint8_t> cborData;
    } kStringTestCases[] = {
        { "string_without_nul"_str,
            { 0x72, 0x73, 0x74, 0x72, 0x69, 0x6E, 0x67, 0x5F, 0x77, 0x69, 0x74, 0x68,
                0x6F, 0x75, 0x74, 0x5F, 0x6E, 0x75, 0x6C } },
        { String("nul_terminated_string\0", 22),
            { 0x76, 0x6E, 0x75, 0x6C, 0x5F, 0x74, 0x65, 0x72, 0x6D, 0x69, 0x6E, 0x61,
                0x74, 0x65, 0x64, 0x5F, 0x73, 0x74, 0x72, 0x69, 0x6E, 0x67, 0x00 } },
        { String("embedded\0nul", 12),
            { 0x6C, 0x65, 0x6D, 0x62, 0x65, 0x64, 0x64, 0x65, 0x64, 0x00, 0x6E, 0x75,
                0x6C } },
        { String("trailing_nuls\0\0", 15),
            { 0x6F, 0x74, 0x72, 0x61, 0x69, 0x6C, 0x69, 0x6E, 0x67, 0x5F, 0x6E, 0x75,
                0x6C, 0x73, 0x00, 0x00 } },
    };

    for (const auto& testCase : kStringTestCases) {
        std::optional<CBORValue> cbor = CBORReader::read(testCase.cborData);
        ASSERT_TRUE(cbor.has_value());
        ASSERT_TRUE(cbor.value().type() == CBORValue::Type::String);
        EXPECT_TRUE(cbor.value().getString() == testCase.value);
    }
}

TEST(CBORReaderTest, TestReadStringWithInvalidByteSequenceAfterNUL)
{
    // UTF-8 validation should not stop at the first NUL character in the string.
    // That is, a string with an invalid byte sequence should fail UTF-8
    // validation even if the invalid character is located after one or more NUL
    // characters. Here, 0xA6 is an unexpected continuation byte.
    static const Vector<uint8_t> stringWithInvalidContinuationByte = {
        0x63, 0x00, 0x00, 0xA6
    };
    CBORReader::DecoderError errorCode;
    std::optional<CBORValue> cbor = CBORReader::read(stringWithInvalidContinuationByte, &errorCode);
    EXPECT_FALSE(cbor.has_value());
    EXPECT_TRUE(errorCode == CBORReader::DecoderError::InvalidUTF8);
}

TEST(CBORReaderTest, TestReadArray)
{
    static const Vector<uint8_t> kArrayTestCaseCBOR = {
        0x98, 0x19, // array of 25 elements
        0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
        0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
        0x18, 0x18, 0x19,
    };

    std::optional<CBORValue> cbor = CBORReader::read(kArrayTestCaseCBOR);
    ASSERT_TRUE(cbor.has_value());
    const CBORValue cborArray = WTFMove(cbor.value());
    ASSERT_TRUE(cborArray.type() == CBORValue::Type::Array);
    ASSERT_EQ(cborArray.getArray().size(), 25u);

    Vector<CBORValue> array;
    for (int i = 0; i < 25; i++) {
        ASSERT_TRUE(cborArray.getArray()[i].type() == CBORValue::Type::Unsigned);
        EXPECT_EQ(cborArray.getArray()[i].getInteger(), static_cast<int64_t>(i + 1));
    }
}

TEST(CBORReaderTest, TestReadMapWithMapValue)
{
    static const Vector<uint8_t> kMapTestCaseCBOR = {
        0xa4, // map with 4 key value pairs:
        0x18, 0x18, // 24
        0x63, 0x61, 0x62, 0x63, // "abc"

        0x60, // ""
        0x61, 0x2e, // "."

        0x61, 0x62, // "b"
        0x61, 0x42, // "B"

        0x62, 0x61, 0x61, // "aa"
        0x62, 0x41, 0x41, // "AA"
    };

    std::optional<CBORValue> cbor = CBORReader::read(kMapTestCaseCBOR);
    ASSERT_TRUE(cbor.has_value());
    const CBORValue cborVal = WTFMove(cbor.value());
    ASSERT_TRUE(cborVal.type() == CBORValue::Type::Map);
    ASSERT_EQ(cborVal.getMap().size(), 4u);

    const CBORValue keyUint(24);
    ASSERT_EQ(cborVal.getMap().count(keyUint), 1u);
    ASSERT_TRUE(cborVal.getMap().find(keyUint)->second.type() == CBORValue::Type::String);
    EXPECT_TRUE(cborVal.getMap().find(keyUint)->second.getString() == "abc"_s);

    const CBORValue keyEmptyString("");
    ASSERT_EQ(cborVal.getMap().count(keyEmptyString), 1u);
    ASSERT_TRUE(cborVal.getMap().find(keyEmptyString)->second.type() == CBORValue::Type::String);
    EXPECT_TRUE(cborVal.getMap().find(keyEmptyString)->second.getString() == "."_s);

    const CBORValue keyB("b");
    ASSERT_EQ(cborVal.getMap().count(keyB), 1u);
    ASSERT_TRUE(cborVal.getMap().find(keyB)->second.type() == CBORValue::Type::String);
    EXPECT_TRUE(cborVal.getMap().find(keyB)->second.getString() == "B"_s);

    const CBORValue keyAa("aa");
    ASSERT_EQ(cborVal.getMap().count(keyAa), 1u);
    ASSERT_TRUE(cborVal.getMap().find(keyAa)->second.type() == CBORValue::Type::String);
    EXPECT_TRUE(cborVal.getMap().find(keyAa)->second.getString() == "AA"_s);
}

TEST(CBORReaderTest, TestReadMapWithIntegerKeys)
{
    static const Vector<uint8_t> kMapWithIntegerKeyCBOR = {
        0xA4, // map with 4 key value pairs
        0x01, // key : 1
        0x61, 0x61, // value : "a"

        0x09, // key : 9
        0x61, 0x62, // value : "b"

        0x19, 0x03, 0xE7, // key : 999
        0x61, 0x63, // value "c"

        0x19, 0x04, 0x57, // key : 1111
        0x61, 0x64, // value : "d"
    };

    std::optional<CBORValue> cbor = CBORReader::read(kMapWithIntegerKeyCBOR);
    ASSERT_TRUE(cbor.has_value());
    const CBORValue cborVal = WTFMove(cbor.value());
    ASSERT_TRUE(cborVal.type() == CBORValue::Type::Map);
    ASSERT_EQ(cborVal.getMap().size(), 4u);

    const CBORValue key1(1);
    ASSERT_EQ(cborVal.getMap().count(key1), 1u);
    ASSERT_TRUE(cborVal.getMap().find(key1)->second.type() == CBORValue::Type::String);
    ASSERT_TRUE(cborVal.getMap().find(key1)->second.getString() == "a"_s);

    const CBORValue key9(9);
    ASSERT_EQ(cborVal.getMap().count(key9), 1u);
    ASSERT_TRUE(cborVal.getMap().find(key9)->second.type() == CBORValue::Type::String);
    ASSERT_TRUE(cborVal.getMap().find(key9)->second.getString() == "b"_s);

    const CBORValue key999(999);
    ASSERT_EQ(cborVal.getMap().count(key999), 1u);
    ASSERT_TRUE(cborVal.getMap().find(key999)->second.type() == CBORValue::Type::String);
    ASSERT_TRUE(cborVal.getMap().find(key999)->second.getString() == "c"_s);

    const CBORValue key1111(1111);
    ASSERT_EQ(cborVal.getMap().count(key1111), 1u);
    ASSERT_TRUE(cborVal.getMap().find(key1111)->second.type() == CBORValue::Type::String);
    ASSERT_TRUE(cborVal.getMap().find(key1111)->second.getString() == "d"_s);
}

TEST(CBORReaderTest, TestReadMapWithArray)
{
    static const Vector<uint8_t> kMapArrayTestCaseCBOR = {
        0xa2, // map of 2 pairs
        0x61, 0x61, // "a"
        0x01,

        0x61, 0x62, // "b"
        0x82, // array with 2 elements
        0x02,
        0x03,
    };

    std::optional<CBORValue> cbor = CBORReader::read(kMapArrayTestCaseCBOR);
    ASSERT_TRUE(cbor.has_value());
    const CBORValue cborVal = WTFMove(cbor.value());
    ASSERT_TRUE(cborVal.type() == CBORValue::Type::Map);
    ASSERT_EQ(cborVal.getMap().size(), 2u);

    const CBORValue keyA("a");
    ASSERT_EQ(cborVal.getMap().count(keyA), 1u);
    ASSERT_TRUE(cborVal.getMap().find(keyA)->second.type() == CBORValue::Type::Unsigned);
    EXPECT_EQ(cborVal.getMap().find(keyA)->second.getInteger(), 1u);

    const CBORValue keyB("b");
    ASSERT_EQ(cborVal.getMap().count(keyB), 1u);
    ASSERT_TRUE(cborVal.getMap().find(keyB)->second.type() == CBORValue::Type::Array);

    const CBORValue nestedArray = cborVal.getMap().find(keyB)->second.clone();
    ASSERT_EQ(nestedArray.getArray().size(), 2u);
    for (int i = 0; i < 2; i++) {
        ASSERT_TRUE(nestedArray.getArray()[i].type() == CBORValue::Type::Unsigned);
        EXPECT_EQ(nestedArray.getArray()[i].getInteger(), static_cast<int64_t>(i + 2));
    }
}

TEST(CBORReaderTest, TestReadNestedMap)
{
    static const Vector<uint8_t> kNestedMapTestCase = {
        0xa2, // map of 2 pairs
        0x61, 0x61, // "a"
        0x01,

        0x61, 0x62, // "b"
        0xa2, // map of 2 pairs
        0x61, 0x63, // "c"
        0x02,

        0x61, 0x64, // "d"
        0x03,
    };

    std::optional<CBORValue> cbor = CBORReader::read(kNestedMapTestCase);
    ASSERT_TRUE(cbor.has_value());
    const CBORValue cborVal = WTFMove(cbor.value());
    ASSERT_TRUE(cborVal.type() == CBORValue::Type::Map);
    ASSERT_EQ(cborVal.getMap().size(), 2u);

    const CBORValue keyA("a");
    ASSERT_EQ(cborVal.getMap().count(keyA), 1u);
    ASSERT_TRUE(cborVal.getMap().find(keyA)->second.type() == CBORValue::Type::Unsigned);
    EXPECT_EQ(cborVal.getMap().find(keyA)->second.getInteger(), 1u);

    const CBORValue keyB("b");
    ASSERT_EQ(cborVal.getMap().count(keyB), 1u);
    const CBORValue nestedMap = cborVal.getMap().find(keyB)->second.clone();
    ASSERT_TRUE(nestedMap.type() == CBORValue::Type::Map);
    ASSERT_EQ(nestedMap.getMap().size(), 2u);

    const CBORValue keyC("c");
    ASSERT_EQ(nestedMap.getMap().count(keyC), 1u);
    ASSERT_TRUE(nestedMap.getMap().find(keyC)->second.type() == CBORValue::Type::Unsigned);
    EXPECT_EQ(nestedMap.getMap().find(keyC)->second.getInteger(), 2u);

    const CBORValue keyD("d");
    ASSERT_EQ(nestedMap.getMap().count(keyD), 1u);
    ASSERT_TRUE(nestedMap.getMap().find(keyD)->second.type() == CBORValue::Type::Unsigned);
    EXPECT_EQ(nestedMap.getMap().find(keyD)->second.getInteger(), 3u);
}

TEST(CBORReaderTest, TestIntegerRange)
{
    static const Vector<uint8_t> kMaxPositiveInt = {
        0x1b, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
    };
    static const Vector<uint8_t> kMinNegativeInt = {
        0x3b, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
    };

    std::optional<CBORValue> maxPositiveInt = CBORReader::read(kMaxPositiveInt);
    ASSERT_TRUE(maxPositiveInt.has_value());
    EXPECT_EQ(maxPositiveInt.value().getInteger(), INT64_MAX);

    std::optional<CBORValue> minNegativeInt = CBORReader::read(kMinNegativeInt);
    ASSERT_TRUE(minNegativeInt.has_value());
    EXPECT_EQ(minNegativeInt.value().getInteger(), INT64_MIN);
}

TEST(CBORReaderTest, TestIntegerOutOfRangeError)
{
    static const Vector<uint8_t> kOutOfRangePositiveInt = {
        0x1b, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
    };

    static const Vector<uint8_t> kOutOfRangeNegativeInt = {
        0x3b, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
    };

    CBORReader::DecoderError errorCode;
    std::optional<CBORValue> positiveIntOutOfRangeCBOR = CBORReader::read(kOutOfRangePositiveInt, &errorCode);
    EXPECT_FALSE(positiveIntOutOfRangeCBOR);
    EXPECT_TRUE(errorCode == CBORReader::DecoderError::OutOfRangeIntegerValue);

    std::optional<CBORValue> negativeIntOutOfRangeCBOR = CBORReader::read(kOutOfRangeNegativeInt, &errorCode);
    EXPECT_FALSE(negativeIntOutOfRangeCBOR);
    EXPECT_TRUE(errorCode == CBORReader::DecoderError::OutOfRangeIntegerValue);
}

TEST(CBORReaderTest, TestReadSimpleValue)
{
    static const struct {
        const CBORValue::SimpleValue value;
        const Vector<uint8_t> cborData;
    } kSimpleValueTestCases[] = {
        { CBORValue::SimpleValue::FalseValue, { 0xf4 } },
        { CBORValue::SimpleValue::TrueValue, { 0xf5 } },
        { CBORValue::SimpleValue::NullValue, { 0xf6 } },
        { CBORValue::SimpleValue::Undefined, { 0xf7 } },
    };

    for (const auto& testCase : kSimpleValueTestCases) {
        std::optional<CBORValue> cbor = CBORReader::read(testCase.cborData);
        ASSERT_TRUE(cbor.has_value());
        ASSERT_TRUE(cbor.value().type() == CBORValue::Type::SimpleValue);
        ASSERT_TRUE(cbor.value().getSimpleValue() == testCase.value);
    }
}

TEST(CBORReaderTest, TestReadUnsupportedFloatingPointNumbers)
{
    static const Vector<uint8_t> floatingPointCbors[] = {
        // 16 bit floating point value.
        { 0xf9, 0x10, 0x00 },
        // 32 bit floating point value.
        { 0xfa, 0x10, 0x00, 0x00, 0x00 },
        // 64 bit floating point value.
        { 0xfb, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
    };

    for (const auto& unsupported_floating_point : floatingPointCbors) {
        CBORReader::DecoderError errorCode;
        std::optional<CBORValue> cbor = CBORReader::read(unsupported_floating_point, &errorCode);
        EXPECT_FALSE(cbor.has_value());
        EXPECT_TRUE(errorCode == CBORReader::DecoderError::UnsupportedFloatingPointValue);
    }
}

TEST(CBORReaderTest, TestIncompleteCBORDataError)
{
    static const Vector<uint8_t> incompleteCborList[] = {
        // Additional info byte corresponds to unsigned int that corresponds
        // to 2 additional bytes. But actual data encoded  in one byte.
        { 0x19, 0x03 },
        // CBOR bytestring of length 3 encoded with additional info of length 4.
        { 0x44, 0x01, 0x02, 0x03 },
        // CBOR string data "IETF" of length 4 encoded with additional info of
        // length 5.
        { 0x65, 0x49, 0x45, 0x54, 0x46 },
        // CBOR array of length 1 encoded with additional info of length 2.
        { 0x82, 0x02 },
        // CBOR map with single key value pair encoded with additional info of
        // length 2.
        { 0xa2, 0x61, 0x61, 0x01 },
    };

    for (const auto& incomplete_data : incompleteCborList) {
        CBORReader::DecoderError errorCode;
        std::optional<CBORValue> cbor = CBORReader::read(incomplete_data, &errorCode);
        EXPECT_FALSE(cbor.has_value());
        EXPECT_TRUE(errorCode == CBORReader::DecoderError::IncompleteCBORData);
    }
}

// While RFC 7049 allows CBOR map keys with all types, current decoder only
// supports unsigned integer and string keys.
TEST(CBORReaderTest, TestUnsupportedMapKeyFormatError)
{
    static const Vector<uint8_t> kMapWithUintKey = {
        0xa2, // map of 2 pairs

        0x82, 0x01, 0x02, // invalid key : [1, 2]
        0x02, // value : 2

        0x61, 0x64, // key : "d"
        0x03, // value : 3
    };

    CBORReader::DecoderError errorCode;
    std::optional<CBORValue> cbor = CBORReader::read(kMapWithUintKey, &errorCode);
    EXPECT_FALSE(cbor.has_value());
    EXPECT_TRUE(errorCode == CBORReader::DecoderError::IncorrectMapKeyType);
}

TEST(CBORReaderTest, TestUnknownAdditionalInfoError)
{
    static const Vector<uint8_t> kUnknownAdditionalInfoList[] = {
        // "IETF" encoded with major type 3 and additional info of 28.
        { 0x7C, 0x49, 0x45, 0x54, 0x46 },
        // "\"\\" encoded with major type 3 and additional info of 29.
        { 0x7D, 0x22, 0x5c },
        // "\xc3\xbc" encoded with major type 3 and additional info of 30.
        { 0x7E, 0xc3, 0xbc },
        // "\xe6\xb0\xb4" encoded with major type 3 and additional info of 31.
        { 0x7F, 0xe6, 0xb0, 0xb4 },
        // Major type 7, additional information 28: unassigned.
        { 0xFC },
        // Major type 7, additional information 29: unassigned.
        { 0xFD },
        // Major type 7, additional information 30: unassigned.
        { 0xFE },
        // Major type 7, additional information 31: "break" stop code for
        // indefinite-length items.
        { 0xFF },
    };

    for (const auto& incorrect_cbor : kUnknownAdditionalInfoList) {
        CBORReader::DecoderError errorCode;
        std::optional<CBORValue> cbor = CBORReader::read(incorrect_cbor, &errorCode);
        EXPECT_FALSE(cbor.has_value());
        EXPECT_TRUE(errorCode == CBORReader::DecoderError::UnknownAdditionalInfo);
    }
}

TEST(CBORReaderTest, TestTooMuchNestingError)
{
    static const Vector<uint8_t> kZeroDepthCBORList[] = {
        // Unsigned int with value 100.
        { 0x18, 0x64 },
        // CBOR bytestring of length 4.
        { 0x44, 0x01, 0x02, 0x03, 0x04 },
        // CBOR string of corresponding to "IETF.
        { 0x64, 0x49, 0x45, 0x54, 0x46 },
        // Empty CBOR array.
        { 0x80 },
        // Empty CBOR Map
        { 0xa0 },
    };

    for (const auto& zeroDepthData : kZeroDepthCBORList) {
        CBORReader::DecoderError errorCode;
        std::optional<CBORValue> cbor = CBORReader::read(zeroDepthData, &errorCode, 0);
        EXPECT_TRUE(cbor.has_value());
        EXPECT_TRUE(errorCode == CBORReader::DecoderError::CBORNoError);
    }

    // Corresponds to a CBOR structure with a nesting depth of 2:
    //      {"a": 1,
    //       "b": [2, 3]}
    static const Vector<uint8_t> kNestedCBORData = {
        0xa2, // map of 2 pairs
        0x61, 0x61, // "a"
        0x01,

        0x61, 0x62, // "b"
        0x82, // array with 2 elements
        0x02,
        0x03,
    };

    CBORReader::DecoderError errorCode;
    std::optional<CBORValue> cborSingleLayerMax = CBORReader::read(kNestedCBORData, &errorCode, 1);
    EXPECT_FALSE(cborSingleLayerMax.has_value());
    EXPECT_TRUE(errorCode == CBORReader::DecoderError::TooMuchNesting);

    std::optional<CBORValue> cborDoubleLayerMax = CBORReader::read(kNestedCBORData, &errorCode, 2);
    EXPECT_TRUE(cborDoubleLayerMax.has_value());
    EXPECT_TRUE(errorCode == CBORReader::DecoderError::CBORNoError);
}

TEST(CBORReaderTest, TestOutOfOrderKeyError)
{
    static const Vector<uint8_t> kMapsWithUnsortedKeys[] = {
        {0xa2, // map with 2 keys with same major type and length
            0x61, 0x62, // key "b"
            0x61, 0x42, // value :"B"

            0x61, 0x61, // key "a" (out of order byte-wise lexically)
            0x61, 0x45, // value "E"
        },
        {0xa2, // map with 2 keys with different major type
            0x61, 0x62, // key "b"
            0x02, // value 2

            // key 1000 (out of order since lower major type sorts first)
            0x19, 0x03, 0xe8,
            0x61, 0x61, // value a
        },
        {0xa2, // map with 2 keys with same major type
            0x19, 0x03, 0xe8, // key 1000  (out of order due to longer length)
            0x61, 0x61, // value "a"

            0x0a, // key 10
            0x61, 0x62}, // value "b"
    };

    CBORReader::DecoderError errorCode;
    for (const auto& unsortedMap : kMapsWithUnsortedKeys) {
        std::optional<CBORValue> cbor =
        CBORReader::read(unsortedMap, &errorCode);
        EXPECT_FALSE(cbor.has_value());
        EXPECT_TRUE(errorCode == CBORReader::DecoderError::OutOfOrderKey);
    }
}

TEST(CBORReaderTest, TestDuplicateKeyError)
{
    static const Vector<uint8_t> kMapWithDuplicateKey = {
        0xa6, // map of 6 pairs:
        0x60, // ""
        0x61, 0x2e, // "."

        0x61, 0x62, // "b"
        0x61, 0x42, // "B"

        0x61, 0x62, // "b" (Duplicate key)
        0x61, 0x43, // "C"

        0x61, 0x64, // "d"
        0x61, 0x44, // "D"

        0x61, 0x65, // "e"
        0x61, 0x44, // "D"

        0x62, 0x61, 0x61, // "aa"
        0x62, 0x41, 0x41, // "AA"
    };

    CBORReader::DecoderError errorCode;

    std::optional<CBORValue> cbor = CBORReader::read(kMapWithDuplicateKey, &errorCode);
    EXPECT_FALSE(cbor.has_value());
    EXPECT_TRUE(errorCode == CBORReader::DecoderError::DuplicateKey);
}

// Leveraging Markus Kuhn’s UTF-8 decoder stress test. See
// http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt for details.
TEST(CBORReaderTest, TestIncorrectStringEncodingError)
{
    static const Vector<uint8_t> utf8CharacterEncodings[] = {
        // Corresponds to utf8 encoding of "퟿" (section 2.3.1 of stress test).
        { 0x63, 0xED, 0x9F, 0xBF },
        // Corresponds to utf8 encoding of "" (section 2.3.2 of stress test).
        { 0x63, 0xEE, 0x80, 0x80 },
        // Corresponds to utf8 encoding of "�"  (section 2.3.3 of stress test).
        { 0x63, 0xEF, 0xBF, 0xBD },
    };

    CBORReader::DecoderError errorCode;
    for (const auto& cbor_byte : utf8CharacterEncodings) {
        std::optional<CBORValue> correctlyEncodedCbor = CBORReader::read(cbor_byte, &errorCode);
        EXPECT_TRUE(correctlyEncodedCbor.has_value());
        EXPECT_TRUE(errorCode == CBORReader::DecoderError::CBORNoError);
    }

    // Incorrect UTF8 encoding referenced by section 3.5.3 of the stress test.
    Vector<uint8_t> impossible_utf_byte { 0x64, 0xfe, 0xfe, 0xff, 0xff };
    std::optional<CBORValue> incorrectlyEncodedCbor = CBORReader::read(impossible_utf_byte, &errorCode);
    EXPECT_FALSE(incorrectlyEncodedCbor);
    EXPECT_TRUE(errorCode == CBORReader::DecoderError::InvalidUTF8);
}

TEST(CBORReaderTest, TestExtraneousCBORDataError)
{
    static const Vector<uint8_t> zeroPaddedCborList[] = {
        // 1 extra byte after a 2-byte unsigned int.
        { 0x19, 0x03, 0x05, 0x00 },
        // 1 extra byte after a 4-byte cbor byte array.
        { 0x44, 0x01, 0x02, 0x03, 0x04, 0x00 },
        // 1 extra byte after a 4-byte string.
        { 0x64, 0x49, 0x45, 0x54, 0x46, 0x00 },
        // 1 extra byte after CBOR array of length 2.
        { 0x82, 0x01, 0x02, 0x00 },
        // 1 extra key value pair after CBOR map of size 2.
        { 0xa1, 0x61, 0x63, 0x02, 0x61, 0x64, 0x03 },
    };

    for (const auto& extraneous_cborData : zeroPaddedCborList) {
        CBORReader::DecoderError errorCode;
        std::optional<CBORValue> cbor = CBORReader::read(extraneous_cborData, &errorCode);
        EXPECT_FALSE(cbor.has_value());
        EXPECT_TRUE(errorCode == CBORReader::DecoderError::ExtraneousData);
    }
}

TEST(CBORReaderTest, TestUnsupportedSimplevalue)
{
    static const Vector<uint8_t> unsupportedSimpleValues[] = {
        // Simple value (0, unassigned)
        { 0xE0 },
        // Simple value (19, unassigned)
        { 0xF3 },
        // Simple value (24, reserved)
        { 0xF8, 0x18 },
        // Simple value (28, reserved)
        { 0xF8, 0x1C },
        // Simple value (29, reserved)
        { 0xF8, 0x1D },
        // Simple value (30, reserved)
        { 0xF8, 0x1E },
        // Simple value (31, reserved)
        { 0xF8, 0x1F },
        // Simple value (32, unassigned)
        { 0xF8, 0x20 },
        // Simple value (255, unassigned)
        { 0xF8, 0xFF },
    };

    for (const auto& unsupportedSimpleVal : unsupportedSimpleValues) {
        CBORReader::DecoderError errorCode;
        std::optional<CBORValue> cbor = CBORReader::read(unsupportedSimpleVal, &errorCode);
        EXPECT_FALSE(cbor.has_value());
        EXPECT_TRUE(errorCode == CBORReader::DecoderError::UnsupportedSimpleValue);
    }
}

} // namespace TestWebKitAPI

#endif // ENABLE(WEB_AUTHN)