2019-07-07 01:22:47 +02:00
|
|
|
import encoding.base64
|
2019-06-25 15:31:56 +02:00
|
|
|
|
2019-09-28 21:21:48 +02:00
|
|
|
struct TestPair {
|
2019-06-30 02:59:25 +02:00
|
|
|
decoded string
|
|
|
|
encoded string
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2021-05-08 12:32:29 +02:00
|
|
|
pairs = [
|
2019-06-30 02:59:25 +02:00
|
|
|
// RFC 3548 examples
|
2019-09-28 21:21:48 +02:00
|
|
|
TestPair{'\x14\xfb\x9c\x03\xd9\x7e', 'FPucA9l+'},
|
|
|
|
TestPair{'\x14\xfb\x9c\x03\xd9', 'FPucA9k='},
|
|
|
|
TestPair{'\x14\xfb\x9c\x03', 'FPucAw=='},
|
2019-06-30 02:59:25 +02:00
|
|
|
// RFC 4648 examples
|
2019-09-28 21:21:48 +02:00
|
|
|
TestPair{'', ''},
|
|
|
|
TestPair{'f', 'Zg=='},
|
|
|
|
TestPair{'fo', 'Zm8='},
|
|
|
|
TestPair{'foo', 'Zm9v'},
|
|
|
|
TestPair{'foob', 'Zm9vYg=='},
|
|
|
|
TestPair{'fooba', 'Zm9vYmE='},
|
|
|
|
TestPair{'foobar', 'Zm9vYmFy'},
|
2019-06-30 02:59:25 +02:00
|
|
|
// Wikipedia examples
|
2019-09-28 21:21:48 +02:00
|
|
|
TestPair{'sure.', 'c3VyZS4='},
|
|
|
|
TestPair{'sure', 'c3VyZQ=='},
|
|
|
|
TestPair{'sur', 'c3Vy'},
|
|
|
|
TestPair{'su', 'c3U='},
|
|
|
|
TestPair{'leasure.', 'bGVhc3VyZS4='},
|
|
|
|
TestPair{'easure.', 'ZWFzdXJlLg=='},
|
|
|
|
TestPair{'asure.', 'YXN1cmUu'},
|
|
|
|
TestPair{'sure.', 'c3VyZS4='},
|
2019-06-30 02:59:25 +02:00
|
|
|
]
|
2020-10-15 16:04:06 +02:00
|
|
|
|
2021-05-08 12:32:29 +02:00
|
|
|
man_pair = TestPair{'Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.', 'TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4='}
|
2019-06-30 02:59:25 +02:00
|
|
|
)
|
|
|
|
|
2019-06-25 15:31:56 +02:00
|
|
|
fn test_decode() {
|
2021-02-26 07:22:12 +01:00
|
|
|
assert base64.decode(man_pair.encoded) == man_pair.decoded.bytes()
|
2019-06-27 20:06:00 +02:00
|
|
|
|
|
|
|
// Test for incorrect padding.
|
2021-03-23 23:01:29 +01:00
|
|
|
assert base64.decode('aGk') == ''.bytes()
|
2021-02-26 07:22:12 +01:00
|
|
|
assert base64.decode('aGk=') == 'hi'.bytes()
|
2021-03-23 23:01:29 +01:00
|
|
|
assert base64.decode('aGk==') == ''.bytes()
|
2019-06-30 02:59:25 +02:00
|
|
|
|
|
|
|
for i, p in pairs {
|
|
|
|
got := base64.decode(p.encoded)
|
2021-02-26 07:22:12 +01:00
|
|
|
if got != p.decoded.bytes() {
|
2021-05-08 12:32:29 +02:00
|
|
|
eprintln('pairs[$i]: expected = $p.decoded, got = $got')
|
2021-02-26 07:22:12 +01:00
|
|
|
assert false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_decode_str() {
|
|
|
|
assert base64.decode_str(man_pair.encoded) == man_pair.decoded
|
|
|
|
|
|
|
|
// Test for incorrect padding.
|
2021-03-23 23:01:29 +01:00
|
|
|
assert base64.decode_str('aGk') == ''
|
2021-02-26 07:22:12 +01:00
|
|
|
assert base64.decode_str('aGk=') == 'hi'
|
2021-03-23 23:01:29 +01:00
|
|
|
assert base64.decode_str('aGk==') == ''
|
2021-02-26 07:22:12 +01:00
|
|
|
|
|
|
|
for i, p in pairs {
|
|
|
|
got := base64.decode_str(p.encoded)
|
2019-06-30 02:59:25 +02:00
|
|
|
if got != p.decoded {
|
2021-05-08 12:32:29 +02:00
|
|
|
eprintln('pairs[$i]: expected = $p.decoded, got = $got')
|
2019-06-30 02:59:25 +02:00
|
|
|
assert false
|
|
|
|
}
|
|
|
|
}
|
2019-06-25 15:31:56 +02:00
|
|
|
}
|
|
|
|
|
2019-06-27 20:06:00 +02:00
|
|
|
fn test_encode() {
|
2021-02-26 07:22:12 +01:00
|
|
|
assert base64.encode(man_pair.decoded.bytes()) == man_pair.encoded
|
|
|
|
|
|
|
|
for i, p in pairs {
|
|
|
|
got := base64.encode(p.decoded.bytes())
|
|
|
|
if got != p.encoded {
|
2021-05-08 12:32:29 +02:00
|
|
|
eprintln('pairs[$i]: expected = $p.encoded, got = $got')
|
2021-02-26 07:22:12 +01:00
|
|
|
assert false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_encode_str() {
|
|
|
|
assert base64.encode_str(man_pair.decoded) == man_pair.encoded
|
2019-06-30 02:59:25 +02:00
|
|
|
|
|
|
|
for i, p in pairs {
|
2021-02-26 07:22:12 +01:00
|
|
|
got := base64.encode_str(p.decoded)
|
2019-06-30 02:59:25 +02:00
|
|
|
if got != p.encoded {
|
2021-05-08 12:32:29 +02:00
|
|
|
eprintln('pairs[$i]: expected = $p.encoded, got = $got')
|
2019-06-30 02:59:25 +02:00
|
|
|
assert false
|
|
|
|
}
|
|
|
|
}
|
2019-06-27 20:06:00 +02:00
|
|
|
}
|
2020-10-15 16:04:06 +02:00
|
|
|
|
2021-02-26 07:22:12 +01:00
|
|
|
fn test_url_encode() {
|
|
|
|
test := base64.url_encode('Hello Base64Url encoding!'.bytes())
|
|
|
|
assert test == 'SGVsbG8gQmFzZTY0VXJsIGVuY29kaW5nIQ'
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_url_encode_str() {
|
|
|
|
test := base64.url_encode_str('Hello Base64Url encoding!')
|
2020-10-15 16:04:06 +02:00
|
|
|
assert test == 'SGVsbG8gQmFzZTY0VXJsIGVuY29kaW5nIQ'
|
|
|
|
}
|
|
|
|
|
2021-02-26 07:22:12 +01:00
|
|
|
fn test_url_decode() {
|
2021-05-08 12:32:29 +02:00
|
|
|
test := base64.url_decode('SGVsbG8gQmFzZTY0VXJsIGVuY29kaW5nIQ')
|
2021-02-26 07:22:12 +01:00
|
|
|
assert test == 'Hello Base64Url encoding!'.bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_url_decode_str() {
|
2021-05-08 12:32:29 +02:00
|
|
|
test := base64.url_decode_str('SGVsbG8gQmFzZTY0VXJsIGVuY29kaW5nIQ')
|
2020-10-15 16:04:06 +02:00
|
|
|
assert test == 'Hello Base64Url encoding!'
|
|
|
|
}
|
2021-02-26 07:22:12 +01:00
|
|
|
|
2022-04-15 13:58:56 +02:00
|
|
|
fn test_encode_null_u8() {
|
|
|
|
assert base64.encode([u8(`A`), 0, `C`]) == 'QQBD'
|
2021-02-26 07:22:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_encode_null_byte_str() {
|
|
|
|
// While this works, bytestr() does a memcpy
|
2022-04-15 13:58:56 +02:00
|
|
|
s := [u8(`A`), 0, `C`].bytestr()
|
2021-02-26 07:22:12 +01:00
|
|
|
assert base64.encode_str(s) == 'QQBD'
|
|
|
|
}
|
|
|
|
|
2022-04-15 13:58:56 +02:00
|
|
|
fn test_decode_null_u8() {
|
|
|
|
assert base64.decode('QQBD') == [u8(`A`), 0, `C`]
|
2021-02-26 07:22:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_decode_null_byte_str() {
|
|
|
|
// While this works, bytestr() does a memcpy
|
2022-04-15 13:58:56 +02:00
|
|
|
s := [u8(`A`), 0, `C`].bytestr()
|
2021-02-26 07:22:12 +01:00
|
|
|
assert base64.decode_str('QQBD') == s
|
|
|
|
}
|
2021-08-06 23:09:55 +02:00
|
|
|
|
|
|
|
fn test_decode_in_buffer_bytes() {
|
|
|
|
rfc4648_pairs := [
|
|
|
|
TestPair{'foob', 'Zm9vYg=='},
|
|
|
|
TestPair{'fooba', 'Zm9vYmE='},
|
|
|
|
TestPair{'foobar', 'Zm9vYmFy'},
|
|
|
|
]
|
2022-04-15 14:35:35 +02:00
|
|
|
mut src_dec_buf := []u8{len: 8}
|
|
|
|
mut src_enc_buf := []u8{len: 8}
|
|
|
|
mut out_buf := []u8{len: 8}
|
2021-08-06 23:09:55 +02:00
|
|
|
|
|
|
|
for p in rfc4648_pairs {
|
|
|
|
src_dec_buf = p.decoded.bytes()
|
|
|
|
src_enc_buf = p.encoded.bytes()
|
|
|
|
n := base64.decode_in_buffer_bytes(src_enc_buf, out_buf.data)
|
|
|
|
assert src_dec_buf == out_buf[..n]
|
|
|
|
}
|
|
|
|
}
|