Skip to content

Commit

Permalink
refactor using stretchr/testify
Browse files Browse the repository at this point in the history
  • Loading branch information
nitram509 committed Sep 26, 2024
1 parent 7690ba0 commit 7b24dba
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 216 deletions.
19 changes: 7 additions & 12 deletions common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keystore

import (
"crypto/rand"
"github.com/stretchr/testify/assert"

Check failure on line 5 in common_test.go

View workflow job for this annotation

GitHub Actions / Lint

File is not `gci`-ed with --skip-generated -s standard -s default (gci)
"reflect"
"testing"
)
Expand All @@ -11,9 +12,8 @@ func TestZeroing(t *testing.T) {

for i := 0; i < 20; i++ {
buf := make([]byte, 4096)
if _, err := rand.Read(buf); err != nil {
t.Errorf("read random bytes: %v", err)
}
_, err := rand.Read(buf)
assert.Nil(t, err)

table = append(table, buf)
}
Expand All @@ -22,9 +22,7 @@ func TestZeroing(t *testing.T) {
zeroing(tt)

for i := range tt {
if tt[i] != 0 {
t.Errorf("fill input with zeros '%v'", tt)
}
assert.Equalf(t, uint8(0), tt[i], "fill input with zeros '%v'", tt)
}
}
}
Expand All @@ -39,9 +37,8 @@ func TestPasswordBytes(t *testing.T) {

for i := 0; i < 20; i++ {
input := make([]byte, 1024)
if _, err := rand.Read(input); err != nil {
t.Errorf("read random bytes: %v", err)
}
_, err := rand.Read(input)
assert.Nil(t, err)

output := make([]byte, len(input)*2)

Expand All @@ -55,8 +52,6 @@ func TestPasswordBytes(t *testing.T) {

for _, tt := range table {
output := passwordBytes(tt.input)
if !reflect.DeepEqual(output, tt.output) {
t.Errorf("convert password bytes '%v', '%v'", output, tt.output)
}
assert.Truef(t, reflect.DeepEqual(output, tt.output), "convert password bytes '%v', '%v'", output, tt.output)
}
}
80 changes: 21 additions & 59 deletions decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/binary"
"errors"
"fmt"
"github.com/stretchr/testify/assert"
"io"
"reflect"
"testing"
Expand Down Expand Up @@ -69,20 +70,14 @@ func TestReadUint16(t *testing.T) {
}

number, err := d.readUint16()
if !reflect.DeepEqual(err, tt.err) {
t.Errorf("invalid error '%v' '%v'", err, tt.err)
}
assert.Truef(t, reflect.DeepEqual(err, tt.err), "invalid error '%v' '%v'", err, tt.err)

if err == nil {
if number != tt.number {
t.Errorf("invalid number '%v' '%v'", number, tt.number)
}
assert.Equal(t, tt.number, number)
}

hash := d.h.Sum(nil)
if !reflect.DeepEqual(hash, tt.hash[:]) {
t.Errorf("invalid hash '%v' '%v'", hash, tt.hash)
}
assert.Truef(t, reflect.DeepEqual(hash, tt.hash[:]), "invalid hash '%v' '%v'", hash, tt.hash)
}
}

Expand Down Expand Up @@ -143,20 +138,14 @@ func TestReadUint32(t *testing.T) {
}

number, err := d.readUint32()
if !reflect.DeepEqual(err, tt.err) {
t.Errorf("invalid error '%v' '%v'", err, tt.err)
}
assert.Truef(t, reflect.DeepEqual(err, tt.err), "invalid error '%v' '%v'", err, tt.err)

if err == nil {
if number != tt.number {
t.Errorf("invalid uint32 '%v' '%v'", number, tt.number)
}
assert.Equal(t, tt.number, number)
}

hash := d.h.Sum(nil)
if !reflect.DeepEqual(hash, tt.hash[:]) {
t.Errorf("invalid hash '%v' '%v'", hash, tt.hash)
}
assert.Truef(t, reflect.DeepEqual(hash, tt.hash[:]), "invalid hash '%v' '%v'", hash, tt.hash)
}
}

Expand Down Expand Up @@ -221,20 +210,14 @@ func TestReadUint64(t *testing.T) {
}

number, err := d.readUint64()
if !reflect.DeepEqual(err, tt.err) {
t.Errorf("invalid error '%v' '%v'", err, tt.err)
}
assert.Truef(t, reflect.DeepEqual(err, tt.err), "invalid error '%v' '%v'", err, tt.err)

if err == nil {
if number != tt.number {
t.Errorf("invalid uint64 '%v' '%v'", number, tt.number)
}
assert.Equal(t, tt.number, number)
}

hash := d.h.Sum(nil)
if !reflect.DeepEqual(hash, tt.hash[:]) {
t.Errorf("invalid hash '%v' '%v'", hash, tt.hash)
}
assert.Truef(t, reflect.DeepEqual(hash, tt.hash[:]), "invalid hash '%v' '%v'", hash, tt.hash)
}
}

Expand Down Expand Up @@ -268,9 +251,8 @@ func TestReadBytes(t *testing.T) {
})
buf := func() []byte {
buf := make([]byte, 10*1024)
if _, err := rand.Read(buf); err != nil {
t.Errorf("read random bytes: %v", err)
}
_, err := rand.Read(buf)
assert.Nil(t, err)

return buf
}()
Expand All @@ -292,18 +274,12 @@ func TestReadBytes(t *testing.T) {
}

bts, err := d.readBytes(tt.readLen)
if err != nil {
t.Errorf("got error '%v'", err)
}
assert.Nil(t, err)

if !reflect.DeepEqual(bts, tt.bytes) {
t.Errorf("invalid bytes '%v' '%v'", bts, tt.bytes)
}
assert.Truef(t, reflect.DeepEqual(bts, tt.bytes), "invalid bytes '%v' '%v'", bts, tt.bytes)

hash := d.h.Sum(nil)
if !reflect.DeepEqual(hash, tt.hash[:]) {
t.Errorf("invalid hash '%v' '%v'", hash, tt.hash)
}
assert.Truef(t, reflect.DeepEqual(hash, tt.hash[:]), "invalid hash '%v' '%v'", hash, tt.hash)
}
}

Expand Down Expand Up @@ -362,18 +338,11 @@ func TestReadString(t *testing.T) {
}

str, err := d.readString()
if !reflect.DeepEqual(err, tt.err) {
t.Errorf("invalid error '%v' '%v'", err, tt.err)
}

if str != tt.string {
t.Errorf("invalid string '%v' '%v'", str, tt.string)
}
assert.Truef(t, reflect.DeepEqual(err, tt.err), "invalid error '%v' '%v'", err, tt.err)
assert.Equal(t, tt.string, str)

hash := d.h.Sum(nil)
if !reflect.DeepEqual(hash, tt.hash[:]) {
t.Errorf("invalid hash '%v' '%v'", hash, tt.hash)
}
assert.Truef(t, reflect.DeepEqual(hash, tt.hash[:]), "invalid hash '%v' '%v'", hash, tt.hash)
}
}

Expand Down Expand Up @@ -468,17 +437,10 @@ func TestReadCertificate(t *testing.T) {
}

cert, err := d.readCertificate(tt.version)
if !reflect.DeepEqual(err, tt.err) {
t.Errorf("invalid error '%v' '%v'", err, tt.err)
}

if !reflect.DeepEqual(cert, tt.cert) {
t.Errorf("invalid certificate '%v' '%v'", cert, tt.cert)
}
assert.Truef(t, reflect.DeepEqual(err, tt.err), "invalid error '%v' '%v'", err, tt.err)
assert.Truef(t, reflect.DeepEqual(cert, tt.cert), "invalid certificate '%v' '%v'", cert, tt.cert)

hash := d.h.Sum(nil)
if !reflect.DeepEqual(hash, tt.hash[:]) {
t.Errorf("invalid hash '%v' '%v'", hash, tt.hash)
}
assert.Truef(t, reflect.DeepEqual(hash, tt.hash[:]), "invalid hash '%v' '%v'", hash, tt.hash)
}
}
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
module github.com/pavlo-v-chernykh/keystore-go/v4

go 1.22.7

require github.com/stretchr/testify v1.9.0

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading

0 comments on commit 7b24dba

Please sign in to comment.