Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
json
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Chen Yisong
json
Commits
93cdf059
Unverified
Commit
93cdf059
authored
Jul 15, 2020
by
Niels Lohmann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
✅
add tests
parent
01e196f6
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
173 additions
and
0 deletions
+173
-0
unit-cbor.cpp
test/src/unit-cbor.cpp
+173
-0
No files found.
test/src/unit-cbor.cpp
View file @
93cdf059
...
@@ -2548,3 +2548,176 @@ TEST_CASE("examples from RFC 7049 Appendix A")
...
@@ -2548,3 +2548,176 @@ TEST_CASE("examples from RFC 7049 Appendix A")
CHECK
(
json
::
parse
(
"{
\"
Fun
\"
: true,
\"
Amt
\"
: -2}"
)
==
json
::
from_cbor
(
std
::
vector
<
uint8_t
>
({
0xbf
,
0x63
,
0x46
,
0x75
,
0x6e
,
0xf5
,
0x63
,
0x41
,
0x6d
,
0x74
,
0x21
,
0xff
})));
CHECK
(
json
::
parse
(
"{
\"
Fun
\"
: true,
\"
Amt
\"
: -2}"
)
==
json
::
from_cbor
(
std
::
vector
<
uint8_t
>
({
0xbf
,
0x63
,
0x46
,
0x75
,
0x6e
,
0xf5
,
0x63
,
0x41
,
0x6d
,
0x74
,
0x21
,
0xff
})));
}
}
}
}
TEST_CASE
(
"Tagged values"
)
{
json
j
=
"s"
;
auto
v
=
json
::
to_cbor
(
j
);
SECTION
(
"0xC6..0xD4"
)
{
for
(
std
::
uint8_t
b
:
{
0xC6
,
0xC7
,
0xC8
,
0xC9
,
0xCA
,
0xCB
,
0xCC
,
0xCD
,
0xCE
,
0xCF
,
0xD0
,
0xD1
,
0xD2
,
0xD3
,
0xD4
})
{
// add tag to value
auto
v_tagged
=
v
;
v_tagged
.
insert
(
v_tagged
.
begin
(),
b
);
// check that parsing fails in error mode
CHECK_THROWS_AS
(
json
::
from_cbor
(
v_tagged
),
json
::
parse_error
);
CHECK_THROWS_AS
(
json
::
from_cbor
(
v_tagged
,
true
,
true
,
json
::
cbor_tag_handler_t
::
error
),
json
::
parse_error
);
// check that parsing succeeds and gets original value in ignore mode
auto
j_tagged
=
json
::
from_cbor
(
v_tagged
,
true
,
true
,
json
::
cbor_tag_handler_t
::
ignore
);
CHECK
(
j_tagged
==
j
);
}
}
SECTION
(
"0xD8 - 1 byte follows"
)
{
SECTION
(
"success"
)
{
// add tag to value
auto
v_tagged
=
v
;
v_tagged
.
insert
(
v_tagged
.
begin
(),
0x42
);
// 1 byte
v_tagged
.
insert
(
v_tagged
.
begin
(),
0xD8
);
// tag
// check that parsing fails in error mode
CHECK_THROWS_AS
(
json
::
from_cbor
(
v_tagged
),
json
::
parse_error
);
CHECK_THROWS_AS
(
json
::
from_cbor
(
v_tagged
,
true
,
true
,
json
::
cbor_tag_handler_t
::
error
),
json
::
parse_error
);
// check that parsing succeeds and gets original value in ignore mode
auto
j_tagged
=
json
::
from_cbor
(
v_tagged
,
true
,
true
,
json
::
cbor_tag_handler_t
::
ignore
);
CHECK
(
j_tagged
==
j
);
}
SECTION
(
"missing byte after tag"
)
{
// add tag to value
auto
v_tagged
=
v
;
v_tagged
.
insert
(
v_tagged
.
begin
(),
0xD8
);
// tag
// check that parsing fails in all modes
CHECK_THROWS_AS
(
json
::
from_cbor
(
v_tagged
),
json
::
parse_error
);
CHECK_THROWS_AS
(
json
::
from_cbor
(
v_tagged
,
true
,
true
,
json
::
cbor_tag_handler_t
::
error
),
json
::
parse_error
);
CHECK_THROWS_AS
(
json
::
from_cbor
(
v_tagged
,
true
,
true
,
json
::
cbor_tag_handler_t
::
ignore
),
json
::
parse_error
);
}
}
SECTION
(
"0xD9 - 2 byte follow"
)
{
SECTION
(
"success"
)
{
// add tag to value
auto
v_tagged
=
v
;
v_tagged
.
insert
(
v_tagged
.
begin
(),
0x42
);
// 1 byte
v_tagged
.
insert
(
v_tagged
.
begin
(),
0x23
);
// 1 byte
v_tagged
.
insert
(
v_tagged
.
begin
(),
0xD9
);
// tag
// check that parsing fails in error mode
CHECK_THROWS_AS
(
json
::
from_cbor
(
v_tagged
),
json
::
parse_error
);
CHECK_THROWS_AS
(
json
::
from_cbor
(
v_tagged
,
true
,
true
,
json
::
cbor_tag_handler_t
::
error
),
json
::
parse_error
);
// check that parsing succeeds and gets original value in ignore mode
auto
j_tagged
=
json
::
from_cbor
(
v_tagged
,
true
,
true
,
json
::
cbor_tag_handler_t
::
ignore
);
CHECK
(
j_tagged
==
j
);
}
SECTION
(
"missing byte after tag"
)
{
// add tag to value
auto
v_tagged
=
v
;
v_tagged
.
insert
(
v_tagged
.
begin
(),
0x23
);
// 1 byte
v_tagged
.
insert
(
v_tagged
.
begin
(),
0xD9
);
// tag
// check that parsing fails in all modes
CHECK_THROWS_AS
(
json
::
from_cbor
(
v_tagged
),
json
::
parse_error
);
CHECK_THROWS_AS
(
json
::
from_cbor
(
v_tagged
,
true
,
true
,
json
::
cbor_tag_handler_t
::
error
),
json
::
parse_error
);
CHECK_THROWS_AS
(
json
::
from_cbor
(
v_tagged
,
true
,
true
,
json
::
cbor_tag_handler_t
::
ignore
),
json
::
parse_error
);
}
}
SECTION
(
"0xDA - 4 bytes follow"
)
{
SECTION
(
"success"
)
{
// add tag to value
auto
v_tagged
=
v
;
v_tagged
.
insert
(
v_tagged
.
begin
(),
0x42
);
// 1 byte
v_tagged
.
insert
(
v_tagged
.
begin
(),
0x23
);
// 1 byte
v_tagged
.
insert
(
v_tagged
.
begin
(),
0x22
);
// 1 byte
v_tagged
.
insert
(
v_tagged
.
begin
(),
0x11
);
// 1 byte
v_tagged
.
insert
(
v_tagged
.
begin
(),
0xDA
);
// tag
// check that parsing fails in error mode
CHECK_THROWS_AS
(
json
::
from_cbor
(
v_tagged
),
json
::
parse_error
);
CHECK_THROWS_AS
(
json
::
from_cbor
(
v_tagged
,
true
,
true
,
json
::
cbor_tag_handler_t
::
error
),
json
::
parse_error
);
// check that parsing succeeds and gets original value in ignore mode
auto
j_tagged
=
json
::
from_cbor
(
v_tagged
,
true
,
true
,
json
::
cbor_tag_handler_t
::
ignore
);
CHECK
(
j_tagged
==
j
);
}
SECTION
(
"missing bytes after tag"
)
{
// add tag to value
auto
v_tagged
=
v
;
v_tagged
.
insert
(
v_tagged
.
begin
(),
0x23
);
// 1 byte
v_tagged
.
insert
(
v_tagged
.
begin
(),
0x22
);
// 1 byte
v_tagged
.
insert
(
v_tagged
.
begin
(),
0x11
);
// 1 byte
v_tagged
.
insert
(
v_tagged
.
begin
(),
0xDA
);
// tag
// check that parsing fails in all modes
CHECK_THROWS_AS
(
json
::
from_cbor
(
v_tagged
),
json
::
parse_error
);
CHECK_THROWS_AS
(
json
::
from_cbor
(
v_tagged
,
true
,
true
,
json
::
cbor_tag_handler_t
::
error
),
json
::
parse_error
);
CHECK_THROWS_AS
(
json
::
from_cbor
(
v_tagged
,
true
,
true
,
json
::
cbor_tag_handler_t
::
ignore
),
json
::
parse_error
);
}
}
SECTION
(
"0xDB - 8 bytes follow"
)
{
SECTION
(
"success"
)
{
// add tag to value
auto
v_tagged
=
v
;
v_tagged
.
insert
(
v_tagged
.
begin
(),
0x42
);
// 1 byte
v_tagged
.
insert
(
v_tagged
.
begin
(),
0x23
);
// 1 byte
v_tagged
.
insert
(
v_tagged
.
begin
(),
0x22
);
// 1 byte
v_tagged
.
insert
(
v_tagged
.
begin
(),
0x11
);
// 1 byte
v_tagged
.
insert
(
v_tagged
.
begin
(),
0x42
);
// 1 byte
v_tagged
.
insert
(
v_tagged
.
begin
(),
0x23
);
// 1 byte
v_tagged
.
insert
(
v_tagged
.
begin
(),
0x22
);
// 1 byte
v_tagged
.
insert
(
v_tagged
.
begin
(),
0x11
);
// 1 byte
v_tagged
.
insert
(
v_tagged
.
begin
(),
0xDB
);
// tag
// check that parsing fails in error mode
CHECK_THROWS_AS
(
json
::
from_cbor
(
v_tagged
),
json
::
parse_error
);
CHECK_THROWS_AS
(
json
::
from_cbor
(
v_tagged
,
true
,
true
,
json
::
cbor_tag_handler_t
::
error
),
json
::
parse_error
);
// check that parsing succeeds and gets original value in ignore mode
auto
j_tagged
=
json
::
from_cbor
(
v_tagged
,
true
,
true
,
json
::
cbor_tag_handler_t
::
ignore
);
CHECK
(
j_tagged
==
j
);
}
SECTION
(
"missing byte after tag"
)
{
// add tag to value
auto
v_tagged
=
v
;
v_tagged
.
insert
(
v_tagged
.
begin
(),
0x42
);
// 1 byte
v_tagged
.
insert
(
v_tagged
.
begin
(),
0x23
);
// 1 byte
v_tagged
.
insert
(
v_tagged
.
begin
(),
0x22
);
// 1 byte
v_tagged
.
insert
(
v_tagged
.
begin
(),
0x11
);
// 1 byte
v_tagged
.
insert
(
v_tagged
.
begin
(),
0x23
);
// 1 byte
v_tagged
.
insert
(
v_tagged
.
begin
(),
0x22
);
// 1 byte
v_tagged
.
insert
(
v_tagged
.
begin
(),
0x11
);
// 1 byte
v_tagged
.
insert
(
v_tagged
.
begin
(),
0xDB
);
// tag
// check that parsing fails in all modes
CHECK_THROWS_AS
(
json
::
from_cbor
(
v_tagged
),
json
::
parse_error
);
CHECK_THROWS_AS
(
json
::
from_cbor
(
v_tagged
,
true
,
true
,
json
::
cbor_tag_handler_t
::
error
),
json
::
parse_error
);
CHECK_THROWS_AS
(
json
::
from_cbor
(
v_tagged
,
true
,
true
,
json
::
cbor_tag_handler_t
::
ignore
),
json
::
parse_error
);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment