diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/gobang.go | 126 |
1 files changed, 123 insertions, 3 deletions
diff --git a/tests/gobang.go b/tests/gobang.go index 61168d6..46cc9c5 100644 --- a/tests/gobang.go +++ b/tests/gobang.go @@ -5,10 +5,12 @@ import ( "crypto/sha1" "crypto/sha256" "encoding/base64" + "encoding/hex" "encoding/json" "fmt" "hash" "log/slog" + "time" ) @@ -324,11 +326,18 @@ func test_scrypt() { Testing("example value", func() { const expected = "lGnMz8io0AUkfzn6Pls1qX20Vs7PGN6sbYQ2TQgY12M=" - // DO NOT use this salt value; generate your own random salt. 8 bytes is - // a good length. + // 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) + dk, err := scrypt( + []byte("some password"), + salt, + 1<<15, + 8, + 1, + 32, + ) ErrorIf(err) given := base64.StdEncoding.EncodeToString(dk) @@ -336,6 +345,113 @@ func test_scrypt() { }) } +func test_newUUIDFrom() { + TestStart("newUUIDFrom()") + + Testing("same UUID from same input", func() { + for i := 0; i < 100; i++ { + var b [uuidByteCount]byte + buffer := Random(uuidByteCount) + now := time.Now().UnixNano() + + lastV7Time = now + 1 + copy(b[:], buffer) + uuid1 := newUUIDFrom(b, now) + + lastV7Time = now + 1 + copy(b[:], buffer) + uuid2 := newUUIDFrom(b, now) + + AssertEqual(uuid1, uuid2) + } + }) +} + +func test_NewUUID() { + TestStart("NewUUID()") + + Testing("we can generate UUID values", func() { + var uuid UUID = NewUUID() + AssertEqual(len(uuid.bytes), uuidByteCount) + }) +} + +func test_UUIDString() { + TestStart("UUID.String()") + + Testing("simple example values", func() { + uuids := []UUID { + UUID { + bytes: [uuidByteCount]byte { + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + }, + }, + UUID { + bytes: [uuidByteCount]byte { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, + }, + }, + UUID { + bytes: [uuidByteCount]byte { + 222, 222, 222, 222, 222, 222, 222, 222, + 222, 222, 222, 222, 222, 222, 222, 222, + }, + }, + UUID { + bytes: [uuidByteCount]byte { + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + }, + }, + } + + strs := []string { + "00000000-0000-0000-0000-000000000000", + "00010203-0405-0607-0809-0a0b0c0d0e0f", + "dededede-dede-dede-dede-dededededede", + "ffffffff-ffff-ffff-ffff-ffffffffffff", + } + + for i := range uuids { + AssertEqual(uuids[i].String(), strs[i]) + } + }) +} + +func test_ParseUUID() { + TestStart("ParseUUID()") + + Testing("UUID -> string -> UUID round trip", func() { + for i := 0; i < 100; i++ { + uuid0 := NewUUID() + uuid1, err := ParseUUID(uuid0.String()) + ErrorIf(err) + AssertEqual(uuid1, uuid0) + } + }) + + Testing("errors we detect", func() { + var err error + + _, err = ParseUUID("") + AssertEqual(err, badUUIDLengthError) + + _, err = ParseUUID("---000000000000000000000000000000000") + AssertEqual(err, badUUIDDashCountError) + + _, err = ParseUUID("----00000000000000000000000000000000") + AssertEqual(err, badUUIDDashPositionError) + + _, err = ParseUUID("00000000-0000-0000-0000-00000000000g") + AssertEqual(err, hex.InvalidByteError('g')) + + _, err = ParseUUID("00000000-0000-0000-0000-000000000000") + ErrorIf(err) + }) +} + func TestSetLoggerOutput() { return type entry struct { @@ -364,4 +480,8 @@ func TestSetLoggerOutput() { func MainTest() { test__PBKDF() test_scrypt() + test_newUUIDFrom() + test_NewUUID() + test_UUIDString() + test_ParseUUID() } |