Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(yaml): check handling of binary type #5245

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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