Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(types/address): add unit tests for the file types/address.go #20237

Merged
merged 15 commits into from
Jun 13, 2024
Prev Previous commit
Next Next commit
fix comments
  • Loading branch information
EmilGeorgiev committed May 10, 2024
commit 2c488cf89de72ea6dd6f23ae940657be6e96290d
45 changes: 27 additions & 18 deletions types/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"crypto/rand"
"encoding/hex"
"fmt"
"github.com/stretchr/testify/assert"
mathrand "math/rand"
"strings"
"testing"
Expand Down Expand Up @@ -61,11 +62,12 @@
s.Require().Equal(original, res)
}

func (s *addressTestSuite) testMarshalYAML(original, res interface{}, marshal func() (interface{}, error), unmarshal func([]byte) error) {
func testMarshalYAML(t *testing.T, original, res interface{}, marshal func() (interface{}, error), unmarshal func([]byte) error) {

Check failure on line 65 in types/address_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

test helper function should start from t.Helper() (thelper)
bz, err := marshal()
s.Require().NoError(err)
s.Require().NoError(unmarshal([]byte(bz.(string))))
s.Require().Equal(original, res)

assert.NoError(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: here and others: use require.NoError instead so that your test can stop early.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

assert.NoError(t, unmarshal([]byte(bz.(string))))
assert.Equal(t, original, res)
}

func (s *addressTestSuite) TestEmptyAddresses() {
Expand Down Expand Up @@ -143,29 +145,36 @@
s.Require().Equal(types.ErrEmptyHexAddress, err)
}

func (s *addressTestSuite) TestRandBech32AccAddrConsistencyYAML() {
pubBz := make([]byte, ed25519.PubKeySize)
pub := &ed25519.PubKey{Key: pubBz}
func FuzzBech32AccAddrConsistencyYAML(f *testing.F) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please move fuzzer tests to to a address_fuzz_test.go file. I always find it confusing to see them with others.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

if testing.Short() {
f.Skip("running in -short mode")
}

for i := 0; i < 1000; i++ {
_, err := rand.Read(pub.Key)
s.Require().NoError(err)
acc := types.AccAddress(pub.Address())
f.Add([]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19})
f.Add([]byte{16, 1, 2, 3, 4, 5, 16, 27, 58, 9, 51, 11, 12, 13, 14, 15, 16, 17, 20, 21})
f.Add([]byte{19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0})

f.Fuzz(func(t *testing.T, input []byte) {
acc := types.AccAddress(input)
res := &types.AccAddress{}

s.testMarshalYAML(&acc, res, acc.MarshalYAML, res.UnmarshalYAML)
s.testMarshalYAML(&acc, res, acc.MarshalYAML, res.UnmarshalYAML)
testMarshalYAML(t, &acc, res, acc.MarshalYAML, res.UnmarshalYAML)
testMarshalYAML(t, &acc, res, acc.MarshalYAML, res.UnmarshalYAML)

str := acc.String()
var err error
*res, err = types.AccAddressFromBech32(str)
s.Require().NoError(err)
s.Require().Equal(acc, *res)
assert.NoError(t, err)
assert.Equal(t, acc, *res)

str = hex.EncodeToString(acc)
*res, err = types.AccAddressFromHexUnsafe(str)
s.Require().NoError(err)
s.Require().Equal(acc, *res)
}
assert.NoError(t, err)
assert.Equal(t, acc, *res)
})
}

func (s *addressTestSuite) TestUnmarshalYAMLWithInvalidInput() {

for _, str := range invalidStrs {
_, err := types.AccAddressFromHexUnsafe(str)
Expand Down Expand Up @@ -670,7 +679,7 @@

func (s *addressTestSuite) TestFormatAccAddressWhenVerbIsDifferentFromSOrP() {
myAddr := types.AccAddress([]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19})
exp := "000102030405060708090A0B0C0D0E0F10111213"

Check failure on line 682 in types/address_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

string `000102030405060708090A0B0C0D0E0F10111213` has 3 occurrences, make it a constant (goconst)
spec := []string{"%v", "%#v", "%t", "%b", "%c", "%d", "%o", "%O", "%x", "%X", "%U", "%e", "%E", "%f", "%F", "%g", "%G"}
for _, v := range spec {
s.Require().Equal(exp, fmt.Sprintf(v, myAddr), v)
Expand Down
Loading