diff options
| author | Zoltán Kéri <z@zolk3ri.name> | 2024-12-29 23:35:01 +0100 |
|---|---|---|
| committer | Zoltán Kéri <z@zolk3ri.name> | 2024-12-29 23:35:01 +0100 |
| commit | c9c59edc646082ad9a687a80ccfd4e421d7e15d4 (patch) | |
| tree | 94046cb8675ab491f728e995f539aca4d0737f28 /core/encoding | |
| parent | 490f52700533bdda725fd90fc19cb248d38b2ff5 (diff) | |
encoding/base32: Move tests to base32_test.odin
Move existing test procedures to a dedicated test file for better
code organization and maintainability.
Diffstat (limited to 'core/encoding')
| -rw-r--r-- | core/encoding/base32/base32.odin | 126 | ||||
| -rw-r--r-- | core/encoding/base32/base32_test.odin | 127 |
2 files changed, 127 insertions, 126 deletions
diff --git a/core/encoding/base32/base32.odin b/core/encoding/base32/base32.odin index 12b6a426b..60ece7b26 100644 --- a/core/encoding/base32/base32.odin +++ b/core/encoding/base32/base32.odin @@ -1,8 +1,5 @@ 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).
@@ -225,126 +222,3 @@ decode :: proc( return
}
-
-@(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)
- if output != nil {
- defer delete(output)
- }
- 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)
- output, err := decode(input)
- if output != nil {
- defer delete(output)
- }
- testing.expect_value(t, err, Error.Invalid_Character)
- }
- {
- // Lowercase not allowed
- input := "mzxq===="
- output, err := decode(input)
- if output != nil {
- defer delete(output)
- }
- testing.expect_value(t, err, Error.Invalid_Character)
- }
-
- // Section 4 - Padding requirements
- {
- // Padding must only be at end
- input := "MZ=Q===="
- output, err := decode(input)
- if output != nil {
- defer delete(output)
- }
- testing.expect_value(t, err, Error.Malformed_Input)
- }
- {
- // Missing padding
- input := "MZXQ" // Should be MZXQ====
- output, err := decode(input)
- if output != nil {
- defer delete(output)
- }
- testing.expect_value(t, err, Error.Malformed_Input)
- }
- {
- // Incorrect padding length
- input := "MZXQ=" // Needs 4 padding chars
- output, err := decode(input)
- if output != nil {
- defer delete(output)
- }
- testing.expect_value(t, err, Error.Malformed_Input)
- }
- {
- // Too much padding
- input := "MY=========" // Extra padding chars
- output, err := decode(input)
- if output != nil {
- defer delete(output)
- }
- testing.expect_value(t, err, Error.Malformed_Input)
- }
-
- // Section 6 - Block size requirements
- {
- // Single character (invalid block)
- input := "M"
- output, err := decode(input)
- if output != nil {
- defer delete(output)
- }
- testing.expect_value(t, err, Error.Invalid_Length)
- }
-}
diff --git a/core/encoding/base32/base32_test.odin b/core/encoding/base32/base32_test.odin new file mode 100644 index 000000000..e492f9a85 --- /dev/null +++ b/core/encoding/base32/base32_test.odin @@ -0,0 +1,127 @@ +package encoding_base32 + +import "core:testing" +import "core:bytes" + +@(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) + if output != nil { + defer delete(output) + } + 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) + output, err := decode(input) + if output != nil { + defer delete(output) + } + testing.expect_value(t, err, Error.Invalid_Character) + } + { + // Lowercase not allowed + input := "mzxq====" + output, err := decode(input) + if output != nil { + defer delete(output) + } + testing.expect_value(t, err, Error.Invalid_Character) + } + + // Section 4 - Padding requirements + { + // Padding must only be at end + input := "MZ=Q====" + output, err := decode(input) + if output != nil { + defer delete(output) + } + testing.expect_value(t, err, Error.Malformed_Input) + } + { + // Missing padding + input := "MZXQ" // Should be MZXQ==== + output, err := decode(input) + if output != nil { + defer delete(output) + } + testing.expect_value(t, err, Error.Malformed_Input) + } + { + // Incorrect padding length + input := "MZXQ=" // Needs 4 padding chars + output, err := decode(input) + if output != nil { + defer delete(output) + } + testing.expect_value(t, err, Error.Malformed_Input) + } + { + // Too much padding + input := "MY=========" // Extra padding chars + output, err := decode(input) + if output != nil { + defer delete(output) + } + testing.expect_value(t, err, Error.Malformed_Input) + } + + // Section 6 - Block size requirements + { + // Single character (invalid block) + input := "M" + output, err := decode(input) + if output != nil { + defer delete(output) + } + testing.expect_value(t, err, Error.Invalid_Length) + } +} |