forked from mtusi2026/mtusi2026lab6
ЛР 6 — БПИ2302 — Нурлыгаянова — на 3
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
package token
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNew_FormatAndHash(t *testing.T) {
|
||||
plain, hash, err := New()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(plain) != EncodedLen || len(plain) < 32 {
|
||||
t.Errorf("len(token) = %d, want %d", len(plain), EncodedLen)
|
||||
}
|
||||
if len(hash) != 32 {
|
||||
t.Errorf("len(hash) = %d, want 32", len(hash))
|
||||
}
|
||||
if !bytes.Equal(hash, Hash(plain)) {
|
||||
t.Error("hash from New differs from Hash(token)")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNew_Unique(t *testing.T) {
|
||||
seen := make(map[string]struct{}, 1000)
|
||||
for i := 0; i < 1000; i++ {
|
||||
plain, _, err := New()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, ok := seen[plain]; ok {
|
||||
t.Fatalf("duplicate token %q", plain)
|
||||
}
|
||||
seen[plain] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParse(t *testing.T) {
|
||||
plain, hash, err := New()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := Parse(plain)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(got, hash) {
|
||||
t.Error("Parse hash differs from New hash")
|
||||
}
|
||||
|
||||
bad := []string{
|
||||
"",
|
||||
"short",
|
||||
plain[:EncodedLen-1],
|
||||
plain + "A",
|
||||
"+" + plain[1:],
|
||||
"00000000-0000-0000-0000-000000000000",
|
||||
}
|
||||
for _, s := range bad {
|
||||
if _, err := Parse(s); !errors.Is(err, ErrMalformed) {
|
||||
t.Errorf("Parse(%q) err = %v, want ErrMalformed", s, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user