v/vlib/encoding/base64/base64_test.v

70 lines
2.0 KiB
V
Raw Normal View History

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 (
pairs = [
// 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
]
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() {
assert base64.decode(man_pair.encoded) == man_pair.decoded
// Test for incorrect padding.
assert base64.decode('aGk') == 'hi'
assert base64.decode('aGk=') == 'hi'
assert base64.decode('aGk==') == 'hi'
2019-06-30 02:59:25 +02:00
for i, p in pairs {
got := base64.decode(p.encoded)
if got != p.decoded {
eprintln('pairs[${i}]: expected = ${p.decoded}, got = ${got}')
assert false
}
}
2019-06-25 15:31:56 +02:00
}
fn test_encode() {
assert base64.encode(man_pair.decoded) == man_pair.encoded
2019-06-30 02:59:25 +02:00
for i, p in pairs {
got := base64.encode(p.decoded)
if got != p.encoded {
eprintln('pairs[${i}]: expected = ${p.encoded}, got = ${got}')
assert false
}
}
}