Debian Codegraph @

t/gh_28_json_test_suite.t

raw
1 # the following test cases are taken from JSONTestSuite
2 # by Nicolas Seriot (https://github.com/nst/JSONTestSuite)
3
4 use strict;
5 use warnings;
6 use Test::More;
7
8 BEGIN { plan skip_all => 'this test is for Perl 5.8 or later' if $] < 5.008; }
9
10 BEGIN { plan tests => 20 };
11
12 BEGIN { $ENV{PERL_JSON_BACKEND} = "JSON::backportPP"; }
13
14 use JSON;
15
16 my $DECODER = JSON->new->utf8->allow_nonref;
17
18 # n_multidigit_number_then_00
19 decode_should_fail(qq!123\x00!);
20
21 # number_-01
22 decode_should_fail(qq![-01]!);
23
24 # number_neg_int_starting_with_zero
25 decode_should_fail(qq![-012]!);
26
27 # n_object_trailing_comment
28 decode_should_fail(qq!{"a":"b"}/**/!);
29
30 # n_object_trailing_comment_slash_open
31 decode_should_fail(qq!{"a":"b"}//!);
32
33 # n_structure_null-byte-outside-sting
34 decode_should_fail(qq![\x00]!);
35
36 # n_structure_object_with_comment
37 decode_should_fail(qq!{"a":/*comment*/"b"}!);
38
39 # n_structure_whitespace_formfeed
40 decode_should_fail(qq![\0x0c]!);
41
42 # y_string_utf16BE_no_BOM
43 decode_should_pass(qq!\x00[\x00"\x00\xE9\x00"\x00]!);
44
45 # y_string_utf16LE_no_BOM
46 decode_should_pass(qq![\x00"\x00\xE9\x00"\x00]\x00!);
47
48 sub decode_should_pass {
49 my $json = shift;
50 my $result = eval { $DECODER->decode($json); };
51 ok !$@, $@ || '';
52 ok defined $result;
53 }
54
55 sub decode_should_fail {
56 my $json = shift;
57 my $result = eval { $DECODER->decode($json); };
58 ok $@, $@ || '';
59 ok !defined $result;
60 }