diff options
author | EuAndreh <eu@euandre.org> | 2024-08-05 16:51:41 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2024-08-05 16:51:41 -0300 |
commit | 27f3f26e8ffa52aed694a0d6d17efcdd8278ba6f (patch) | |
tree | 89a881998c208e46bf0b81c95361b2e0b363b12e /tests/gobang.go | |
parent | Makefile: Build with "go tool compile" and "go tool link" (diff) | |
download | gobang-27f3f26e8ffa52aed694a0d6d17efcdd8278ba6f.tar.gz gobang-27f3f26e8ffa52aed694a0d6d17efcdd8278ba6f.tar.xz |
Start using TestStart() and Testing()
Diffstat (limited to 'tests/gobang.go')
-rw-r--r-- | tests/gobang.go | 115 |
1 files changed, 62 insertions, 53 deletions
diff --git a/tests/gobang.go b/tests/gobang.go index 3abdf6f..61168d6 100644 --- a/tests/gobang.go +++ b/tests/gobang.go @@ -9,7 +9,6 @@ import ( "fmt" "hash" "log/slog" - "testing" ) @@ -141,30 +140,29 @@ var sha256TestVectors = []pbkdfTestVector { }, } -func testHash( - t *testing.T, - h func() hash.Hash, - hashName string, - vectors []pbkdfTestVector, -) { +func testHash(h func() hash.Hash, hashName string, vectors []pbkdfTestVector) { for i, v := range vectors { - out := PBKDF2Key( + out := _PBKDF2Key( []byte(v.password), []byte(v.salt), v.iter, len(v.output), h, ) - AssertEqualI(t, i, out, v.output) + AssertEqualI(i, out, v.output) } } -func TestWithHMACSHA1(t *testing.T) { - testHash(t, sha1.New, "SHA1", sha1TestVectors) -} +func test__PBKDF() { + TestStart("_PBKDF()") + + Testing("HMAC with SHA1", func() { + testHash(sha1.New, "SHA1", sha1TestVectors) + }) -func TestWithHMACSHA256(t *testing.T) { - testHash(t, sha256.New, "SHA256", sha256TestVectors) + Testing("HMAC with SHA256", func() { + testHash(sha256.New, "SHA256", sha256TestVectors) + }) } type scryptTestVector struct { @@ -284,7 +282,7 @@ var good = []scryptTestVector { // }, } -const halfMax = MaxInt / 2 +const halfMax = maxInt / 2 var bad = []scryptTestVector { {"p", "s", 0, 1, 1, nil}, // N == 0 {"p", "s", 1, 1, 1, nil}, // N == 1 @@ -292,46 +290,53 @@ var bad = []scryptTestVector { {"p", "s", 16, halfMax, halfMax, nil}, // p * r too large } -func TestKey(t *testing.T) { - for i, v := range good { - k, err := Scrypt( - []byte(v.password), - []byte(v.salt), - v.N, - v.r, - v.p, - len(v.output), - ) - ErrorIf(t, err) - AssertEqualI(t, i, k, v.output) - } - for _, v := range bad { - _, err := Scrypt( - []byte(v.password), - []byte(v.salt), - v.N, - v.r, - v.p, - 32, - ) - ErrorNil(t, err) - } -} +func test_scrypt() { + TestStart("scrypt()") -func TestExample(t *testing.T) { - const expected = "lGnMz8io0AUkfzn6Pls1qX20Vs7PGN6sbYQ2TQgY12M=" - // DO NOT use this salt value; generate your own random salt. 8 bytes is - // a good length. - salt := []byte{0xc8, 0x28, 0xf2, 0x58, 0xa7, 0x6a, 0xad, 0x7b} + Testing("good hashes", func() { + for i, v := range good { + k, err := scrypt( + []byte(v.password), + []byte(v.salt), + v.N, + v.r, + v.p, + len(v.output), + ) + ErrorIf(err) + AssertEqualI(i, k, v.output) + } + }) - dk, err := Scrypt([]byte("some password"), salt, 1<<15, 8, 1, 32) - ErrorIf(t, err) + Testing("bad hashes", func() { + for _, v := range bad { + _, err := scrypt( + []byte(v.password), + []byte(v.salt), + v.N, + v.r, + v.p, + 32, + ) + ErrorNil(err) + } + }) - given := base64.StdEncoding.EncodeToString(dk) - AssertEqual(t, given, expected) + Testing("example value", func() { + const expected = "lGnMz8io0AUkfzn6Pls1qX20Vs7PGN6sbYQ2TQgY12M=" + // DO NOT use this salt value; generate your own random salt. 8 bytes is + // a good length. + salt := []byte{0xc8, 0x28, 0xf2, 0x58, 0xa7, 0x6a, 0xad, 0x7b} + + dk, err := scrypt([]byte("some password"), salt, 1<<15, 8, 1, 32) + ErrorIf(err) + + given := base64.StdEncoding.EncodeToString(dk) + AssertEqual(given, expected) + }) } -func TestSetLoggerOutput(t *testing.T) { +func TestSetLoggerOutput() { return type entry struct { msg string `json:"msg"` @@ -346,13 +351,17 @@ func TestSetLoggerOutput(t *testing.T) { // fmt.Println(e) err := json.Unmarshal([]byte(s), &e) if err != nil { - t.Fail() + // t.Fail() // Mmain() - Main() } if e.msg != "the message" { - t.Fail() + // t.Fail() } fmt.Println(1) // fmt.Println(e) } + +func MainTest() { + test__PBKDF() + test_scrypt() +} |