Skip to content

Commit

Permalink
test(yaml): check handling of binary type (#5245)
Browse files Browse the repository at this point in the history
  • Loading branch information
kt3k authored Jul 2, 2024
1 parent 532ab28 commit 41385c0
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
51 changes: 51 additions & 0 deletions yaml/parse_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,57 @@ Deno.test({
assertEquals(parse(yaml), {
message: new Uint8Array([72, 101, 108, 108, 111]),
});

// ignore CR LF in base64 string
assertEquals(
parse(`message: !!binary |
YWJjZGVmZ\r
2hpamtsbW\r
5vcHFyc3R\r
1dnd4eXo=
`),
{
// deno-fmt-ignore
message: new Uint8Array([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]),
},
);

// check tailbits handling
// 2 padding characters
assertEquals(
parse(`message: !!binary "AQ=="`),
{
message: new Uint8Array([1]),
},
);
// 1 padding character
assertEquals(
parse(`message: !!binary "AQI="`),
{
message: new Uint8Array([1, 2]),
},
);
// no padding character
assertEquals(
parse(`message: !!binary "AQID"`),
{
message: new Uint8Array([1, 2, 3]),
},
);

// invalid base64 string
assertThrows(
() => parse("message: !!binary <>"),
YamlError,
"cannot resolve a node with !<tag:yaml.org,2002:binary> explicit tag at line 1, column 21:\n message: !!binary <>\n ^",
);

// empty base64 string is error
assertThrows(
() => parse("message: !!binary"),
YamlError,
"cannot resolve a node with !<tag:yaml.org,2002:binary> explicit tag at line 2, column 1:\n \n ^",
);
},
});

Expand Down
22 changes: 22 additions & 0 deletions yaml/stringify_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,28 @@ Deno.test({
},
});

Deno.test({
name: "stringify() serializes Uint8Array as !!binary",
fn() {
assertEquals(
stringify(new Uint8Array([1])),
"!<tag:yaml.org,2002:binary> AQ==\n",
);
assertEquals(
stringify(new Uint8Array([1, 2])),
"!<tag:yaml.org,2002:binary> AQI=\n",
);
assertEquals(
stringify(new Uint8Array([1, 2, 3])),
"!<tag:yaml.org,2002:binary> AQID\n",
);
assertEquals(
stringify(new Uint8Array(Array(50).keys())),
"!<tag:yaml.org,2002:binary> AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDE=\n",
);
},
});

Deno.test({
name: "stringify() throws with `!!js/*` yaml types with default schemas",
fn() {
Expand Down

0 comments on commit 41385c0

Please sign in to comment.