forked from mtusi2026/mtusi2026lab2
50 lines
1.2 KiB
Go
50 lines
1.2 KiB
Go
package common
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func TestCursorRoundTrip(t *testing.T) {
|
|
want := CursorKeyCtx{
|
|
Time: time.Date(2026, 10, 20, 12, 30, 45, 123456789, time.UTC),
|
|
ID: uuid.MustParse("11111111-1111-4111-8111-111111111111"),
|
|
ContextID: uuid.MustParse("22222222-2222-4222-8222-222222222222"),
|
|
}
|
|
|
|
encoded := EncodeCursor(want)
|
|
got, ok := DecodeCursor(encoded)
|
|
if !ok {
|
|
t.Fatal("cursor did not decode")
|
|
}
|
|
if !got.Time.Equal(want.Time) || got.ID != want.ID || got.ContextID != want.ContextID {
|
|
t.Fatalf("cursor round trip changed the key: %+v", got)
|
|
}
|
|
|
|
for _, invalid := range []string{"not-base64!", "", "aGVsbG8"} {
|
|
if _, ok := DecodeCursor(invalid); ok {
|
|
t.Fatalf("invalid cursor %q was accepted", invalid)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestValidatePasswordRange(t *testing.T) {
|
|
cases := []struct {
|
|
password string
|
|
valid bool
|
|
}{
|
|
{"short", false},
|
|
{"12345678901", false},
|
|
{"123456789012", true},
|
|
{"пароль-из-двенадцати", true},
|
|
{"😀", false},
|
|
}
|
|
for _, tc := range cases {
|
|
if got := ValidatePassword(tc.password) == ""; got != tc.valid {
|
|
t.Fatalf("ValidatePassword(%q) valid=%v, want %v", tc.password, got, tc.valid)
|
|
}
|
|
}
|
|
}
|