aboutsummaryrefslogtreecommitdiff
path: root/core/encoding
diff options
context:
space:
mode:
authorZoltán Kéri <z@zolk3ri.name>2024-12-30 15:18:38 +0100
committerZoltán Kéri <z@zolk3ri.name>2024-12-30 15:18:38 +0100
commit82925097699c389475c5e2d12286a447165ffa65 (patch)
tree8ecd7a8498298893ff2ad59c59c4c6693f0418dc /core/encoding
parent591dd8765adb04ff5e0ba66c4c583ea49dbbcc87 (diff)
encoding/base32: Add custom alphabet test case
Add test case to verify custom alphabet support. The test uses a decimal-uppercase alphabet (0-9, A-V) to test both encoding and decoding with custom tables, including validation. This ensures the encode and decode functions work correctly with custom encoding tables and validation functions as documented.
Diffstat (limited to 'core/encoding')
-rw-r--r--core/encoding/base32/base32_test.odin75
1 files changed, 75 insertions, 0 deletions
diff --git a/core/encoding/base32/base32_test.odin b/core/encoding/base32/base32_test.odin
index 6589abc42..968c6c4df 100644
--- a/core/encoding/base32/base32_test.odin
+++ b/core/encoding/base32/base32_test.odin
@@ -148,3 +148,78 @@ test_base32_roundtrip :: proc(t: ^testing.T) {
testing.expect(t, bytes.equal(decoded, transmute([]byte)input))
}
}
+
+@(test)
+
+test_base32_custom_alphabet :: proc(t: ^testing.T) {
+ custom_enc_table := [32]byte{
+ '0', '1', '2', '3', '4', '5', '6', '7',
+ '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
+ 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
+ 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
+ }
+
+ custom_dec_table: [256]u8
+ for i := 0; i < len(custom_enc_table); i += 1 {
+ custom_dec_table[custom_enc_table[i]] = u8(i)
+ }
+
+ /*
+ custom_dec_table := [256]u8{
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x00-0x0f
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x10-0x1f
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x20-0x2f
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, // 0x30-0x3f ('0'-'9')
+ 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, // 0x40-0x4f ('A'-'O')
+ 25, 26, 27, 28, 29, 30, 31, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x50-0x5f ('P'-'V')
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x60-0x6f
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x70-0x7f
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x80-0x8f
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x90-0x9f
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xa0-0xaf
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xb0-0xbf
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xc0-0xcf
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xd0-0xdf
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xe0-0xef
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xf0-0xff
+ }
+ */
+
+ custom_validate :: proc(c: byte) -> bool {
+ return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'V') || c == byte(PADDING)
+ }
+
+ cases := [?]struct {
+ input: string,
+ enc_expected: string,
+ }{
+ {"f", "CO======"},
+ {"fo", "CPNG===="},
+ {"foo", "CPNMU==="},
+ }
+
+ for c in cases {
+ // Test encoding
+ encoded := encode(transmute([]byte)c.input, custom_enc_table)
+ testing.expect(t, encoded == c.enc_expected)
+
+ // Test decoding
+ decoded, err := decode(encoded, custom_dec_table, custom_validate)
+ defer if decoded != nil {
+ delete(decoded)
+ }
+
+ testing.expect_value(t, err, Error.None)
+ testing.expect(t, bytes.equal(decoded, transmute([]byte)c.input))
+ }
+
+ // Test invalid character detection
+ {
+ input := "WXY=====" // Contains chars not in our alphabet
+ output, err := decode(input, custom_dec_table, custom_validate)
+ if output != nil {
+ delete(output)
+ }
+ testing.expect_value(t, err, Error.Invalid_Character)
+ }
+}