Files
cms-server/internal/cryptography/base64_test.go
Asai Neko 210b8b08ce
All checks were successful
Server Check Build (NixCN CMS) TeamCity build finished
Add test for all components
Signed-off-by: Asai Neko <sugar@sne.moe>
2026-03-26 18:19:26 +08:00

48 lines
1.3 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package cryptography
import (
"encoding/base64"
"testing"
"github.com/stretchr/testify/assert"
)
func TestIsBase64StdValid(t *testing.T) {
cases := []struct {
name string
input string
}{
{"non-empty", base64.StdEncoding.EncodeToString([]byte("hello"))},
{"longer", base64.StdEncoding.EncodeToString([]byte("hello, world!"))},
// Note: empty string encodes to "" which IsBase64Std("") returns false (early exit)
}
for _, tc := range cases {
assert.True(t, IsBase64Std(tc.input), "expected %q to be valid base64", tc.input)
}
}
func TestIsBase64StdEmptyString(t *testing.T) {
assert.False(t, IsBase64Std(""))
}
func TestIsBase64StdInvalidChars(t *testing.T) {
assert.False(t, IsBase64Std("not-base64!!!"))
}
func TestIsBase64StdWrongPadding(t *testing.T) {
// "aGVsbG8" is base64url without padding standard decoder requires padding
assert.False(t, IsBase64Std("aGVsbG8"))
}
func TestIsBase64StdWhitespaceOnly(t *testing.T) {
// " " → TrimSpace → "" → len % 4 == 0 → attempts decode → succeeds → true
// IsBase64Std trims leading/trailing whitespace, so all-whitespace passes
// because empty string decodes without error. Document actual behaviour:
assert.True(t, IsBase64Std(" "))
}
func TestIsBase64StdPaddedBase64(t *testing.T) {
// Standard base64 with padding
assert.True(t, IsBase64Std("aGVsbG8="))
}