///| 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) } ///| test "crc32, gzip dynamic Huffman inflate, deflate/gzip round-trip, I2CP mux, and ZIP extraction" { // Known-answer dynamic Huffman gzip from Python zlib/gzip (BTYPE=2) let gz_dyn = @encoding.hex_decode( "1f8b08000000000000ff85d53b6a03411084e11359a8aaf45a161dc0a923a7c69670a415927c7fcb513185a1266b9a3ffbe8b92d3f8fd3edf5725e5ed6cf77fc5eee8f2326aed62bae305f97db73fcdbcc9f1fd7fbf1fdfc365f4e8fd7af23e7db90624ce9142de598ca295baa31dd38554b3763ba75ba69e9764c774eb72ddd8de9dee9aea5fb313d38ddb7f430a693d3434ba720b1763b9516c9c99ed03c213cc1a0d0402140c1a2d0442144c1a4d0482148c1a6d04c214cc1a8d0502150c1aad0542154c1acd0582158c1aed05c215cd1aed05c315cd1aed85c31ef945db1b962b8a25db1b962b8a25db1b962b8a25db1b962b8a25db1b962b8a25db1b962b8a25db1b962b8a25db1b962b8925db1b952b8925da9b952b8925da9b9527e8076a5e64ae14a76a5e64ae14a76a5e64ae14a76a5e64ae14a76a5e64ae14a76a5e64ae14a76a5e64ae16a6357fac7d52f616a009557090000", ) let plain_dyn = @encoding.gzip_decompress(gz_dyn) assert_eq(plain_dyn.length(), 2391) assert_true( @encoding.utf8_bytes_to_string(plain_dyn[:43].to_owned()).has_prefix( "routerInfo-0000=host=192.0.2.1;port=10000;", ), ) // Compress & decompress round-trip + I2CP port/protocol multiplexing let recompressed = @encoding.gzip_compress(plain_dyn) assert_true(recompressed.length() < plain_dyn.length()) assert_eq(@encoding.gzip_decompress(recompressed), plain_dyn) let i2cp_pkt = @encoding.i2cp_gzip_wrap( b"GET / HTTP/1.1\r\nHost: stats.i2p\r\n\r\n", from_port=12345U, to_port=80U, protocol=b'\x06', ) let (hdr, unwrapped) = @encoding.i2cp_gzip_unwrap(i2cp_pkt) assert_eq(hdr.from_port, 12345U) assert_eq(hdr.to_port, 80U) assert_eq(hdr.protocol, b'\x06') assert_eq(unwrapped, b"GET / HTTP/1.1\r\nHost: stats.i2p\r\n\r\n") // Real Python-generated ZIP archive with STORED ("a.txt") + DEFLATED ("b.dat") entries let zip_bytes = @encoding.hex_decode( "504b030414000000000046ae7b5dee81b1340c0000000c00000005000000612e74787473746f7265642d68656c6c6f504b030414000000080046ae7b5d53b7ea5194000000ac03000005000000622e646174f3340a5008ca2f2d492df2cc4bcb575053f0494d2c4e0d4e2d3152484ecccbcfcb4c4ecc5128492d2e512848acccc94f4c5128cf2cc950284a2d484d2cc9cc4b5728c828026a2856f01c3568d4a05183f019c4c0c8c4ccc2cac6cec1c9c5cdc3cbc72f2028242c222a262e2129252d232b27afa0a8a4aca2aaa6aea1a9a5ada3aba76f6068646c626a666e6169656d636b670f00504b0102140314000000000046ae7b5dee81b1340c0000000c000000050000000000000000000000800100000000612e747874504b0102140314000000080046ae7b5d53b7ea5194000000ac03000005000000000000000000000080012f000000622e646174504b0506000000000200020066000000e60000000000", ) let zip_entries = @encoding.zip_extract_entries(zip_bytes) assert_eq(zip_entries.length(), 2) assert_eq(zip_entries[0], ("a.txt", b"stored-hello")) assert_eq(zip_entries[1].0, "b.dat") assert_eq(zip_entries[1].1.length(), 940) assert_eq(@encoding.crc32(zip_entries[1].1), 0x51eab753U) }