diff options
| author | Zoltán Kéri <z@zolk3ri.name> | 2024-12-24 15:28:34 +0100 |
|---|---|---|
| committer | Zoltán Kéri <z@zolk3ri.name> | 2024-12-24 15:28:34 +0100 |
| commit | 7672ac945a7b8e35b600a9d2a2421caf16a5a364 (patch) | |
| tree | f5469b7b7ea0933dc5f1f6f1b3e7f4f6f8ccf1bc /core/encoding/base32 | |
| parent | b9338777e34006b40b9315e795232e0608caa499 (diff) | |
encoding/base32: Add RFC 4648 test suite
Add test suite based on RFC 4648 test vectors and validation rules:
- Add section 10 test vectors for valid encoding/decoding
- Add test cases for invalid character handling (section 3.2)
- Add test cases for padding validation (section 4)
- Add test cases for length requirements (section 6)
The test vectors verify that:
- Empty string encodes/decodes correctly
- Standard cases like "foo" -> "MZXW6===" work
- Invalid characters are rejected
- Missing or malformed padding is detected
- Invalid lengths are caught
Diffstat (limited to 'core/encoding/base32')
| -rw-r--r-- | core/encoding/base32/base32.odin | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/core/encoding/base32/base32.odin b/core/encoding/base32/base32.odin index 8e3499dce..54737c9ce 100644 --- a/core/encoding/base32/base32.odin +++ b/core/encoding/base32/base32.odin @@ -1,5 +1,8 @@ package encoding_base32
+import "core:testing"
+import "core:bytes"
+
// @note(zh): Encoding utility for Base32
// A secondary param can be used to supply a custom alphabet to
// @link(encode) and a matching decoding table to @link(decode).
@@ -186,3 +189,104 @@ decode :: proc(data: string, DEC_TBL := DEC_TABLE, allocator := context.allocato return out, .None
}
+
+@(test)
+test_base32_decode_valid :: proc(t: ^testing.T) {
+ // RFC 4648 Section 10 - Test vectors
+ cases := [?]struct {
+ input, expected: string,
+ }{
+ {"", ""},
+ {"MY======", "f"},
+ {"MZXQ====", "fo"},
+ {"MZXW6===", "foo"},
+ {"MZXW6YQ=", "foob"},
+ {"MZXW6YTB", "fooba"},
+ {"MZXW6YTBOI======", "foobar"},
+ }
+
+
+ for c in cases {
+ output, err := decode(c.input)
+ testing.expect_value(t, err, Error.None)
+ expected := transmute([]u8)c.expected
+
+ if output != nil {
+ testing.expect(t, bytes.equal(output, expected))
+ } else {
+ testing.expect(t, len(c.expected) == 0)
+ }
+ }
+}
+
+@(test)
+test_base32_encode :: proc(t: ^testing.T) {
+ // RFC 4648 Section 10 - Test vectors
+ cases := [?]struct {
+ input, expected: string,
+ }{
+ {"", ""},
+ {"f", "MY======"},
+ {"fo", "MZXQ===="},
+ {"foo", "MZXW6==="},
+ {"foob", "MZXW6YQ="},
+ {"fooba", "MZXW6YTB"},
+ {"foobar", "MZXW6YTBOI======"},
+ }
+
+ for c in cases {
+ output := encode(transmute([]byte)c.input)
+ testing.expect(t, output == c.expected)
+ }
+}
+
+@(test)
+test_base32_decode_invalid :: proc(t: ^testing.T) {
+ // Section 3.2 - Alphabet check
+ {
+ // Characters outside alphabet
+ input := "MZ1W6YTB" // '1' not in alphabet (A-Z, 2-7)
+ _, err := decode(input)
+ testing.expect_value(t, err, Error.Invalid_Character)
+ }
+ {
+ // Lowercase not allowed
+ input := "mzxq===="
+ _, err := decode(input)
+ testing.expect_value(t, err, Error.Invalid_Character)
+ }
+
+ // Section 4 - Padding requirements
+ {
+ // Padding must only be at end
+ input := "MZ=Q===="
+ _, err := decode(input)
+ testing.expect_value(t, err, Error.Malformed_Input)
+ }
+ {
+ // Missing padding
+ input := "MZXQ" // Should be MZXQ====
+ _, err := decode(input)
+ testing.expect_value(t, err, Error.Malformed_Input)
+ }
+ {
+ // Incorrect padding length
+ input := "MZXQ=" // Needs 4 padding chars
+ _, err := decode(input)
+ testing.expect_value(t, err, Error.Malformed_Input)
+ }
+ {
+ // Too much padding
+ input := "MY=========" // Extra padding chars
+ _, err := decode(input)
+ testing.expect_value(t, err, Error.Malformed_Input)
+ }
+
+ // Section 6 - Block size requirements
+ {
+ // Single character (invalid block)
+ input := "M"
+ _, err := decode(input)
+ testing.expect_value(t, err, Error.Invalid_Length)
+ }
+}
|