Files
moon-i2p/encoding/encoding_test.mbt
T
Antigravity e527b84bc3
check / check (ubuntu-latest) (push) Failing after 30s
check / check (macos-latest) (push) Failing after 33s
check / check (windows-latest) (push) Failing after 32s
feat: bootstrap moon-i2p core protocol foundation
2026-11-27 15:37:42 +00:00

82 lines
2.1 KiB
MoonBit

///|
test "i2p base64 uses - and ~ instead of + and /" {
// 0xfb 0xff 0xfe -> 111110 111111 111111 111110 -> 62 63 63 62 -> "-~~-"
let raw = b"\xfb\xff\xfe"
let encoded = @encoding.base64_encode(raw)
assert_eq(encoded, "-~~-")
let decoded = @encoding.base64_decode(encoded)
assert_eq(decoded, raw)
}
///|
test "i2p base64 padding round-trip" {
let cases : Array[(Bytes, String)] = [
(b"", ""),
(b"f", "Zg=="),
(b"fo", "Zm8="),
(b"foo", "Zm9v"),
(b"foob", "Zm9vYg=="),
(b"fooba", "Zm9vYmE="),
(b"foobar", "Zm9vYmFy"),
]
for item in cases {
let (raw, expected) = item
assert_eq(@encoding.base64_encode(raw), expected)
assert_eq(@encoding.base64_decode(expected), raw)
}
}
///|
test "i2p base32 rfc4648 lowercase unpadded vectors" {
let cases : Array[(Bytes, String)] = [
(b"", ""),
(b"f", "my"),
(b"fo", "mzxq"),
(b"foo", "mzxw6"),
(b"foob", "mzxw6yq"),
(b"fooba", "mzxw6ytb"),
(b"foobar", "mzxw6ytboi"),
]
for item in cases {
let (raw, expected) = item
assert_eq(@encoding.base32_encode(raw), expected)
assert_eq(@encoding.base32_decode(expected), raw)
}
}
///|
test "i2p base32 32-byte hash produces 52 chars" {
let zeros = Bytes::make(32, b'\x00')
let b32 = @encoding.base32_encode(zeros)
assert_eq(b32.length(), 52)
assert_eq(@encoding.base32_decode(b32), zeros)
}
///|
test "binary reader and writer big-endian round-trip" {
let w = @encoding.Writer::new()
w.write_u8(b'\x42')
w.write_u16_be(0x1234U)
w.write_u32_be(0xDEADBEEFU)
w.write_u64_be(0x0102030405060708UL)
w.write_i2p_string("NTCP2")
w.write_bytes(b"\xAA\xBB")
let bytes = w.to_bytes()
let r = @encoding.Reader::new(bytes)
assert_eq(r.read_u8(), b'\x42')
assert_eq(r.read_u16_be(), 0x1234U)
assert_eq(r.read_u32_be(), 0xDEADBEEFU)
assert_eq(r.read_u64_be(), 0x0102030405060708UL)
assert_eq(r.read_i2p_string(), "NTCP2")
assert_eq(r.read_bytes(2), b"\xAA\xBB")
assert_true(r.is_eof())
}
///|
test "hex encode and decode" {
let raw = b"\x00\x0f\xa5\xff"
assert_eq(@encoding.hex_encode(raw), "000fa5ff")
assert_eq(@encoding.hex_decode("000F a5ff"), raw)
}