### ----------------------------------------------------
### -- CBOR::Simple
### -- Licenses: Artistic-2.0
### -- Authors: Geoffrey Broadwell
### -- File: t/02-malformed.rakutest
### ----------------------------------------------------
use Test;
use lib $*PROGRAM.sibling('lib');
use CodecMatches;

use CBOR::Simple;


plan 94;


### Below adapted from https://tools.ietf.org/html/rfc8949#appendix-F

# WELL-FORMEDNESS ERROR KIND 2 (TOO LITTLE DATA)

malformed($_, "End of input in a head") for
    < 18 19 1a 1b 1901 1a0102 1b01020304050607
      38 58 78 98             9a01ff00
      b8 d8 f8    f900 fa0000 fb000000 >;

malformed($_, "Definite-length strings with short data") for
    < 41 61 5affffffff00 5bffffffffffffffff010203
      7affffffff00 7b7fffffffffffffff010203 >;

malformed($_, "Definite-length maps and arrays not closed with enough items") for
    < 81 818181818181818181 8200 a1 a20102 a100 a2000000 >;

malformed($_, "Tag number not followed by tag content") for < c0 >;

malformed($_, "Indefinite-length strings not closed by a \"break\" stop code") for
    < 5f4100 7f6100 >;

malformed($_, "Indefinite-length maps and arrays not closed by a \"break\" stop code") for
    < 9f 9f0102 bf bf01020102 819f 9f8000 9f9f9f9f9fffffffff
      9f819f819f9fffffff >;


# WELL-FORMEDNESS ERROR KIND 3 (SYNTAX ERROR)

malformed($_, "Reserved additional information values") for
    < 1c 1d 1e 3c 3d 3e 5c 5d 5e 7c 7d 7e
      9c 9d 9e bc bd be dc dd de fc fd fe >;

malformed($_, "Reserved two-byte encodings of simple values") for
    < f800 f801 f818 f81f >;

malformed($_, "Indefinite-length string chunks not of the correct type") for
    < 5f00ff 5f21ff 5f6100ff 5f80ff 5fa0ff 5fc000ff 5fe0ff 7f4100ff >;

malformed($_, "Indefinite-length string chunks not definite length") for
    < 5f5f4100ffff 7f7f6100ffff >;

malformed($_, "Break occurring on its own outside of an indefinite-length item") for
    < ff >;

malformed($_, "Break occurring in a definite-length array or map or a tag") for
    < 81ff 8200ff a1ff a1ff00 a100ff a20000ff 9f81ff 9f829f819f9fffffffff >;

malformed($_, "Break in an indefinite-length map that would lead to an odd number of items (break in a value position)") for
    < bf00ff bf000000ff >;

malformed($_, "Major type 0, 1, 6 with additional information 31") for
    < 1f 3f df >;


done-testing;


