Skip to content

Commit

Permalink
Add gomonkey unit tests.
Browse files Browse the repository at this point in the history
Signed-off-by: txaty <[email protected]>
  • Loading branch information
txaty committed Aug 23, 2022
1 parent a3e9599 commit d6074ec
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
uses: golangci/[email protected]

- name: Run coverage
run: go test -race -coverprofile=coverage.txt -covermode=atomic
run: go test -race go test -gcflags=all=-l -coverprofile=coverage.txt -covermode=atomic

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/txaty/go-merkletree

go 1.19

require golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4
require (
github.com/agiledragon/gomonkey/v2 v2.8.0
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4
)
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
github.com/agiledragon/gomonkey/v2 v2.8.0 h1:u2K2nNGyk0ippzklz1CWalllEB9ptD+DtSXeCX5O000=
github.com/agiledragon/gomonkey/v2 v2.8.0/go.mod h1:ap1AmDzcVOAz1YpeJ3TCzIgstoaWLA6jbbgxfB4w2iY=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
66 changes: 66 additions & 0 deletions merkle_tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ package merkletree
import (
"bytes"
"crypto/sha256"
"errors"
"fmt"
"github.com/agiledragon/gomonkey/v2"
"math/rand"
"reflect"
"runtime"
Expand Down Expand Up @@ -441,6 +443,24 @@ func TestMerkleTreeNew_proofGenAndTreeBuild(t *testing.T) {
},
wantErr: true,
},
{
name: "test_tree_build_hash_func_error",
args: args{
blocks: genTestDataBlocks(100),
config: &Config{
HashFunc: func(block []byte) ([]byte, error) {
if len(block) == 64 {
return nil, fmt.Errorf("hash func error")
}
sha256Func := sha256.New()
sha256Func.Write(block)
return sha256Func.Sum(nil), nil
},
Mode: ModeProofGenAndTreeBuild,
},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -825,3 +845,49 @@ func TestMerkleTree_GenerateProof(t *testing.T) {
})
}
}

func TestMerkleTree_proofGen(t *testing.T) {
type args struct {
config *Config
blocks []DataBlock
}
tests := []struct {
name string
args args
mock func()
wantErr bool
}{
{
name: "test_fix_odd_err",
args: args{
config: &Config{
NoDuplicates: true,
},
blocks: genTestDataBlocks(5),
},
mock: func() {
patches := gomonkey.ApplyFunc(rand.Read,
func(b []byte) (n int, err error) {
return 0, errors.New("test_rand_read_err")
})
defer patches.Reset()
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.mock != nil {
tt.mock()
}
m, err := New(tt.args.config, tt.args.blocks)
if err != nil {
t.Errorf("New() error = %v", err)
return
}
if err := m.proofGen(); (err != nil) != tt.wantErr {
t.Errorf("proofGen() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

0 comments on commit d6074ec

Please sign in to comment.