Skip to content

Commit

Permalink
test: url safe decoding pad/no pad
Browse files Browse the repository at this point in the history
  • Loading branch information
grjte committed Oct 29, 2024
1 parent 2b0c87e commit a6e21f1
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/decoder.nr
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,14 @@ fn test_decode_max_byte() {
let input: [u8; 2] = [47, 119]; // "/w"
let result: [u8; 1] = STANDARD_NO_PAD.decode(input);
assert(result == expected);

let input: [u8; 4] = [95, 119, 61, 61]; // "_w=="
let result: [u8; 1] = URL_SAFE.decode(input);
assert(result == expected);

let input: [u8; 2] = [95, 119]; // "_w"
let result: [u8; 1] = URL_SAFE_NO_PAD.decode(input);
assert(result == expected);
}

#[test(should_fail_with = "DecodeError: invalid symbol 255, offset 0")]
Expand All @@ -748,6 +756,59 @@ fn test_decode_standard_no_pad_fail_with_padding() {
assert(result == expected);
}

#[test]
fn test_decode_standard() {
// test decoding / and +
let input: [u8; 4] = [47, 43, 65, 61];
let expected: [u8; 2] = [255, 224];
let result: [u8; 2] = STANDARD.decode(input);

assert(result == expected);
}

#[test(should_fail_with = "DecodeError: invalid symbol 95, offset 0")]
fn test_decode_underscore_with_standard() {
// test decoding _ and -
let input: [u8; 4] = [95, 45, 65, 61];
let _: [u8; 2] = STANDARD.decode(input);
}

#[test]
fn test_decode_url_safe() {
// test decoding _ and -
let input: [u8; 4] = [95, 45, 65, 61];
let expected: [u8; 2] = [255, 224];
let result: [u8; 2] = URL_SAFE.decode(input);

assert(result == expected);
}

#[test(should_fail_with = "DecodeError: invalid symbol 47, offset 0")]
fn test_decode_slash_with_url_safe() {
// test decoding / and +
let input: [u8; 4] = [47, 43, 65, 61];
let _: [u8; 2] = URL_SAFE.decode(input);
}

#[test]
fn test_decode_url_safe_no_pad() {
// test decoding _ and -
let input: [u8; 3] = [95, 45, 65];
let expected: [u8; 2] = [255, 224];
let result: [u8; 2] = URL_SAFE_NO_PAD.decode(input);

assert(result == expected);
}

#[test(should_fail_with = "DecodeError: invalid input length for specified output length. Expected 3 input elements, but got 4.")]
fn test_decode_url_safe_no_pad_fail_with_padding() {
// test decoding _ and -
let input: [u8; 4] = [95, 45, 65, 61];
let expected: [u8; 2] = [255, 224];
let result: [u8; 2] = URL_SAFE_NO_PAD.decode(input);
assert(result == expected);
}

#[test]
fn test_decode_ascii() {
// base64: SGVsbG8gV29ybGQh
Expand All @@ -760,6 +821,10 @@ fn test_decode_ascii() {
assert(result == expected);
let result: [u8; 12] = STANDARD_NO_PAD.decode(input);
assert(result == expected);
let result: [u8; 12] = URL_SAFE.decode(input);
assert(result == expected);
let result: [u8; 12] = URL_SAFE_NO_PAD.decode(input);
assert(result == expected);
}

#[test]
Expand All @@ -780,6 +845,10 @@ fn test_decode_utf8() {
assert(result == expected);
let result: [u8; 27] = STANDARD_NO_PAD.decode(input);
assert(result == expected);
let result: [u8; 27] = URL_SAFE.decode(input);
assert(result == expected);
let result: [u8; 27] = URL_SAFE_NO_PAD.decode(input);
assert(result == expected);
}

#[test]
Expand Down

0 comments on commit a6e21f1

Please sign in to comment.