diff options
author | EuAndreh <eu@euandre.org> | 2024-08-06 11:00:32 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2024-08-06 11:00:32 -0300 |
commit | 8dc3d5485c8de6d0398d5df5a6fc4682687f4c9b (patch) | |
tree | db20c1e3982d2077ca08e1a7777b353fc675af98 | |
parent | Makefile: Separate dependencies and definition of $(tests.bin-check) (diff) | |
download | gobang-8dc3d5485c8de6d0398d5df5a6fc4682687f4c9b.tar.gz gobang-8dc3d5485c8de6d0398d5df5a6fc4682687f4c9b.tar.xz |
tests/gobang.go: Add tests for Random(), Salt() and Hash()
-rw-r--r-- | src/gobang.go | 6 | ||||
-rw-r--r-- | tests/gobang.go | 40 |
2 files changed, 44 insertions, 2 deletions
diff --git a/src/gobang.go b/src/gobang.go index dc0d9bc..ff8f68b 100644 --- a/src/gobang.go +++ b/src/gobang.go @@ -57,6 +57,7 @@ type CopyResult struct { const maxInt = int((^uint(0)) >> 1) +const MinimumPasswordLength = 16 const ( scrypt_N = 1 << 15 @@ -410,6 +411,7 @@ func Hash(password []byte, salt []byte) []byte{ } // FIXME: finish rewriting +// FIXME: add tests // // getV7Time returns the time in milliseconds and nanoseconds / 256. // The returned (milli << (12 + seq)) is guaranteed to be greater than @@ -419,8 +421,8 @@ func getV7Time(nano int64) (int64, int64) { const nanoPerMilli = 1000 * 1000 milli := nano / nanoPerMilli - seq := (nano - (milli * nanoPerMilli)) >> 8 - now := milli << (12 + seq) + seq := (nano - (milli * nanoPerMilli)) >> 8 + now := milli << (12 + seq) timeMutex.Lock() defer timeMutex.Unlock() diff --git a/tests/gobang.go b/tests/gobang.go index 46cc9c5..324a155 100644 --- a/tests/gobang.go +++ b/tests/gobang.go @@ -10,6 +10,7 @@ import ( "fmt" "hash" "log/slog" + "os" "time" ) @@ -345,6 +346,42 @@ func test_scrypt() { }) } +func test_Random() { + TestStart("Random()") + + Testing("we get the desired output size", func() { + for i := 0; i < 100; i++ { + buffer := Random(i) + AssertEqual(len(buffer), i) + } + }) +} + +func test_Salt() { + TestStart("Salt()") + + Testing("we generate a random salt of a fixed size", func() { + salt := Salt() + AssertEqual(len(salt), scryptSaltMinLength) + + var buffer [scryptSaltMinLength * 2]byte + hex.Encode(buffer[:], salt) + fmt.Fprintf(os.Stderr, "%s ", string(buffer[:])) + }) +} + +func test_Hash() { + TestStart("Hash()") + + Testing("same input, same output", func() { + password := Random(16) + salt := Salt() + hash1 := Hash(password, salt) + hash2 := Hash(password, salt) + AssertEqual(hash1, hash2) + }) +} + func test_newUUIDFrom() { TestStart("newUUIDFrom()") @@ -480,6 +517,9 @@ func TestSetLoggerOutput() { func MainTest() { test__PBKDF() test_scrypt() + test_Hash() + test_Random() + test_Salt() test_newUUIDFrom() test_NewUUID() test_UUIDString() |