summaryrefslogtreecommitdiff
path: root/tests/lib_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'tests/lib_test.go')
-rw-r--r--tests/lib_test.go160
1 files changed, 78 insertions, 82 deletions
diff --git a/tests/lib_test.go b/tests/lib_test.go
index cac0884..701747b 100644
--- a/tests/lib_test.go
+++ b/tests/lib_test.go
@@ -8,54 +8,13 @@ import (
"encoding/json"
"fmt"
"hash"
- "log"
"log/slog"
- "reflect"
"testing"
"euandre.org/gobang/src"
)
-func errorIf(t *testing.T, err error) {
- if err != nil {
- t.Errorf("Unexpected error: %#v\n", err)
- }
-}
-
-func assertEqual(t *testing.T, given any, expected any) {
- if !reflect.DeepEqual(given, expected) {
- t.Errorf("given != expected")
- t.Errorf("given: %#v\n", given)
- t.Errorf("expected: %#v\n", expected)
- }
-}
-
-func TestSetLoggerOutput(t *testing.T) {
- return
- type entry struct {
- msg string `json:"msg"`
- aKey string `json:"a-key"`
- }
- var e entry
- var buf bytes.Buffer
- slog.Error("the message", "a-key", "a-value")
-
- s := buf.String()
- // fmt.Println(s)
- // fmt.Println(e)
- err := json.Unmarshal([]byte(s), &e)
- if err != nil {
- t.Fail()
- // gobang.Mmain()
- gobang.Main()
- }
- if e.msg != "the message" {
- t.Fail()
- }
- fmt.Println(1)
- // fmt.Println(e)
-}
type pbkdfTestVector struct {
password string
@@ -184,12 +143,21 @@ var sha256TestVectors = []pbkdfTestVector {
},
}
-func testHash(t *testing.T, h func() hash.Hash, hashName string, vectors []pbkdfTestVector) {
+func testHash(
+ t *testing.T,
+ h func() hash.Hash,
+ hashName string,
+ vectors []pbkdfTestVector,
+) {
for i, v := range vectors {
- o := gobang.PBKDF2Key([]byte(v.password), []byte(v.salt), v.iter, len(v.output), h)
- if !bytes.Equal(o, v.output) {
- t.Errorf("%s %d: expected %x, got %x", hashName, i, v.output, o)
- }
+ out := gobang.PBKDF2Key(
+ []byte(v.password),
+ []byte(v.salt),
+ v.iter,
+ len(v.output),
+ h,
+ )
+ gobang.AssertEqualI(t, i, out, v.output)
}
}
@@ -301,49 +269,54 @@ var good = []scryptTestVector {
0x87,
},
},
- /*
- // Disabled: needs 1 GiB RAM and takes too long for a simple test.
- {
- "pleaseletmein", "SodiumChloride",
- 1048576, 8, 1,
- []byte{
- 0x21, 0x01, 0xcb, 0x9b, 0x6a, 0x51, 0x1a, 0xae, 0xad,
- 0xdb, 0xbe, 0x09, 0xcf, 0x70, 0xf8, 0x81, 0xec, 0x56,
- 0x8d, 0x57, 0x4a, 0x2f, 0xfd, 0x4d, 0xab, 0xe5, 0xee,
- 0x98, 0x20, 0xad, 0xaa, 0x47, 0x8e, 0x56, 0xfd, 0x8f,
- 0x4b, 0xa5, 0xd0, 0x9f, 0xfa, 0x1c, 0x6d, 0x92, 0x7c,
- 0x40, 0xf4, 0xc3, 0x37, 0x30, 0x40, 0x49, 0xe8, 0xa9,
- 0x52, 0xfb, 0xcb, 0xf4, 0x5c, 0x6f, 0xa7, 0x7a, 0x41,
- 0xa4,
- },
- },
- */
+ // // Disabled: needs 1 GiB RAM and takes too long for a simple test.
+ // {
+ // "pleaseletmein", "SodiumChloride",
+ // 1048576, 8, 1,
+ // []byte{
+ // 0x21, 0x01, 0xcb, 0x9b, 0x6a, 0x51, 0x1a, 0xae, 0xad,
+ // 0xdb, 0xbe, 0x09, 0xcf, 0x70, 0xf8, 0x81, 0xec, 0x56,
+ // 0x8d, 0x57, 0x4a, 0x2f, 0xfd, 0x4d, 0xab, 0xe5, 0xee,
+ // 0x98, 0x20, 0xad, 0xaa, 0x47, 0x8e, 0x56, 0xfd, 0x8f,
+ // 0x4b, 0xa5, 0xd0, 0x9f, 0xfa, 0x1c, 0x6d, 0x92, 0x7c,
+ // 0x40, 0xf4, 0xc3, 0x37, 0x30, 0x40, 0x49, 0xe8, 0xa9,
+ // 0x52, 0xfb, 0xcb, 0xf4, 0x5c, 0x6f, 0xa7, 0x7a, 0x41,
+ // 0xa4,
+ // },
+ // },
}
-const maxInt = int(^uint(0) >> 1)
-
+const halfMax = gobang.MaxInt / 2
var bad = []scryptTestVector {
{"p", "s", 0, 1, 1, nil}, // N == 0
{"p", "s", 1, 1, 1, nil}, // N == 1
{"p", "s", 7, 8, 1, nil}, // N is not power of 2
- {"p", "s", 16, maxInt / 2, maxInt / 2, nil}, // p * r too large
+ {"p", "s", 16, halfMax, halfMax, nil}, // p * r too large
}
func TestKey(t *testing.T) {
for i, v := range good {
- k, err := gobang.Scrypt([]byte(v.password), []byte(v.salt), v.N, v.r, v.p, len(v.output))
- if err != nil {
- t.Errorf("%d: got unexpected error: %s", i, err)
- }
- if !bytes.Equal(k, v.output) {
- t.Errorf("%d: expected %x, got %x", i, v.output, k)
- }
+ k, err := gobang.Scrypt(
+ []byte(v.password),
+ []byte(v.salt),
+ v.N,
+ v.r,
+ v.p,
+ len(v.output),
+ )
+ gobang.ErrorIf(t, err)
+ gobang.AssertEqualI(t, i, k, v.output)
}
- for i, v := range bad {
- _, err := gobang.Scrypt([]byte(v.password), []byte(v.salt), v.N, v.r, v.p, 32)
- if err == nil {
- t.Errorf("%d: expected error, got nil", i)
- }
+ for _, v := range bad {
+ _, err := gobang.Scrypt(
+ []byte(v.password),
+ []byte(v.salt),
+ v.N,
+ v.r,
+ v.p,
+ 32,
+ )
+ gobang.ErrorNil(t, err)
}
}
@@ -354,11 +327,34 @@ func TestExample(t *testing.T) {
salt := []byte{0xc8, 0x28, 0xf2, 0x58, 0xa7, 0x6a, 0xad, 0x7b}
dk, err := gobang.Scrypt([]byte("some password"), salt, 1<<15, 8, 1, 32)
- if err != nil {
- log.Fatal(err)
- }
+ gobang.ErrorIf(t, err)
given := base64.StdEncoding.EncodeToString(dk)
+ gobang.AssertEqual(t, given, expected)
+}
+
+func TestSetLoggerOutput(t *testing.T) {
+ return
+ type entry struct {
+ msg string `json:"msg"`
+ aKey string `json:"a-key"`
+ }
+ var e entry
+ var buf bytes.Buffer
+ slog.Error("the message", "a-key", "a-value")
- assertEqual(t, given, expected)
+ s := buf.String()
+ // fmt.Println(s)
+ // fmt.Println(e)
+ err := json.Unmarshal([]byte(s), &e)
+ if err != nil {
+ t.Fail()
+ // gobang.Mmain()
+ gobang.Main()
+ }
+ if e.msg != "the message" {
+ t.Fail()
+ }
+ fmt.Println(1)
+ // fmt.Println(e)
}