Skip to content

Commit 4f28f8f

Browse files
committed
test(eth): eip712 more tests
1 parent bfac1e8 commit 4f28f8f

File tree

9 files changed

+235
-125
lines changed

9 files changed

+235
-125
lines changed

eth/crypto/hd/algorithm_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import (
1313
"github.com/cosmos/cosmos-sdk/codec/types"
1414
"github.com/cosmos/cosmos-sdk/crypto/keyring"
1515

16+
"github.com/NibiruChain/nibiru/eth"
1617
cryptocodec "github.com/NibiruChain/nibiru/eth/crypto/codec"
1718
enccodec "github.com/NibiruChain/nibiru/eth/encoding/codec"
18-
ethtypes "github.com/NibiruChain/nibiru/eth"
1919
)
2020

2121
var TestCodec amino.Codec
@@ -52,7 +52,7 @@ func TestKeyring(t *testing.T) {
5252
require.Nil(t, info)
5353

5454
mockIn.Reset("password\npassword\n")
55-
info, mnemonic, err := kr.NewMnemonic("foo", keyring.English, ethtypes.BIP44HDPath, keyring.DefaultBIP39Passphrase, EthSecp256k1)
55+
info, mnemonic, err := kr.NewMnemonic("foo", keyring.English, eth.BIP44HDPath, keyring.DefaultBIP39Passphrase, EthSecp256k1)
5656
require.NoError(t, err)
5757
require.NotEmpty(t, mnemonic)
5858
require.Equal(t, "foo", info.Name)
@@ -61,7 +61,7 @@ func TestKeyring(t *testing.T) {
6161
require.NoError(t, err)
6262
require.Equal(t, string(EthSecp256k1Type), pubKey.Type())
6363

64-
hdPath := ethtypes.BIP44HDPath
64+
hdPath := eth.BIP44HDPath
6565

6666
bz, err := EthSecp256k1.Derive()(mnemonic, keyring.DefaultBIP39Passphrase, hdPath)
6767
require.NoError(t, err)
@@ -87,7 +87,7 @@ func TestKeyring(t *testing.T) {
8787
}
8888

8989
func TestDerivation(t *testing.T) {
90-
bz, err := EthSecp256k1.Derive()(mnemonic, keyring.DefaultBIP39Passphrase, ethtypes.BIP44HDPath)
90+
bz, err := EthSecp256k1.Derive()(mnemonic, keyring.DefaultBIP39Passphrase, eth.BIP44HDPath)
9191
require.NoError(t, err)
9292
require.NotEmpty(t, bz)
9393

@@ -105,7 +105,7 @@ func TestDerivation(t *testing.T) {
105105
wallet, err := NewFromMnemonic(mnemonic)
106106
require.NoError(t, err)
107107

108-
path := MustParseDerivationPath(ethtypes.BIP44HDPath)
108+
path := MustParseDerivationPath(eth.BIP44HDPath)
109109
account, err := wallet.Derive(path, false)
110110
require.NoError(t, err)
111111

eth/eip712/eip712_fuzzer_test.go

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -54,102 +54,102 @@ var params = EIP712FuzzTestParams{
5454
// variable input sizes, types, and fields. While it may be possible to translate a single input into
5555
// a JSON object, it would require difficult parsing, and ultimately approximates our randomized unit
5656
// tests as they are.
57-
func (suite *EIP712TestSuite) TestRandomPayloadFlattening() {
57+
func (s *EIP712TestSuite) TestRandomPayloadFlattening() {
5858
// Re-seed rand generator
5959
rand.Seed(rand.Int64())
6060

6161
for i := 0; i < params.numTestObjects; i++ {
62-
suite.Run(fmt.Sprintf("%v%d", fuzzTestName, i), func() {
63-
payload := suite.generateRandomPayload(i)
62+
s.Run(fmt.Sprintf("%v%d", fuzzTestName, i), func() {
63+
payload := s.generateRandomPayload(i)
6464

6565
flattened, numMessages, err := eip712.FlattenPayloadMessages(payload)
6666

67-
suite.Require().NoError(err)
68-
suite.Require().Equal(numMessages, i)
67+
s.Require().NoError(err)
68+
s.Require().Equal(numMessages, i)
6969

70-
suite.verifyPayloadAgainstFlattened(payload, flattened)
70+
s.verifyPayloadAgainstFlattened(payload, flattened)
7171
})
7272
}
7373
}
7474

7575
// generateRandomPayload creates a random payload of the desired format, with random sub-objects.
76-
func (suite *EIP712TestSuite) generateRandomPayload(numMessages int) gjson.Result {
77-
payload := suite.createRandomJSONObject().Raw
76+
func (s *EIP712TestSuite) generateRandomPayload(numMessages int) gjson.Result {
77+
payload := s.createRandomJSONObject().Raw
7878
msgs := make([]gjson.Result, numMessages)
7979

8080
for i := 0; i < numMessages; i++ {
81-
msgs[i] = suite.createRandomJSONObject()
81+
msgs[i] = s.createRandomJSONObject()
8282
}
8383

8484
payload, err := sjson.Set(payload, msgsFieldName, msgs)
85-
suite.Require().NoError(err)
85+
s.Require().NoError(err)
8686

8787
return gjson.Parse(payload)
8888
}
8989

9090
// createRandomJSONObject creates a JSON object with random fields.
91-
func (suite *EIP712TestSuite) createRandomJSONObject() gjson.Result {
91+
func (s *EIP712TestSuite) createRandomJSONObject() gjson.Result {
9292
var err error
9393
payloadRaw := ""
9494

95-
numFields := suite.createRandomIntInRange(0, params.maxNumFieldsPerObject)
95+
numFields := s.createRandomIntInRange(0, params.maxNumFieldsPerObject)
9696
for i := 0; i < numFields; i++ {
97-
key := suite.createRandomString()
97+
key := s.createRandomString()
9898

99-
randField := suite.createRandomJSONField(i, 0)
99+
randField := s.createRandomJSONField(i, 0)
100100
payloadRaw, err = sjson.Set(payloadRaw, key, randField)
101-
suite.Require().NoError(err)
101+
s.Require().NoError(err)
102102
}
103103

104104
return gjson.Parse(payloadRaw)
105105
}
106106

107107
// createRandomJSONField creates a random field with a random JSON type, with the possibility of
108108
// nested fields up to depth objects.
109-
func (suite *EIP712TestSuite) createRandomJSONField(t int, depth int) interface{} {
109+
func (s *EIP712TestSuite) createRandomJSONField(t int, depth int) interface{} {
110110
switch t % numJSONTypes {
111111
case jsonBoolType:
112-
return suite.createRandomBoolean()
112+
return s.createRandomBoolean()
113113
case jsonStringType:
114-
return suite.createRandomString()
114+
return s.createRandomString()
115115
case jsonFloatType:
116-
return suite.createRandomFloat()
116+
return s.createRandomFloat()
117117
case jsonArrayType:
118-
return suite.createRandomJSONNestedArray(depth)
118+
return s.createRandomJSONNestedArray(depth)
119119
case jsonObjectType:
120-
return suite.createRandomJSONNestedObject(depth)
120+
return s.createRandomJSONNestedObject(depth)
121121
default:
122122
return nil
123123
}
124124
}
125125

126126
// createRandomJSONNestedArray creates an array of random nested JSON fields.
127-
func (suite *EIP712TestSuite) createRandomJSONNestedArray(depth int) []interface{} {
127+
func (s *EIP712TestSuite) createRandomJSONNestedArray(depth int) []interface{} {
128128
arr := make([]interface{}, rand.Intn(params.maxArrayLength))
129129
for i := range arr {
130-
arr[i] = suite.createRandomJSONNestedField(depth)
130+
arr[i] = s.createRandomJSONNestedField(depth)
131131
}
132132

133133
return arr
134134
}
135135

136136
// createRandomJSONNestedObject creates a key-value set of objects with random nested JSON fields.
137-
func (suite *EIP712TestSuite) createRandomJSONNestedObject(depth int) interface{} {
137+
func (s *EIP712TestSuite) createRandomJSONNestedObject(depth int) interface{} {
138138
numFields := rand.Intn(params.maxNumFieldsPerObject)
139139
obj := make(map[string]interface{})
140140

141141
for i := 0; i < numFields; i++ {
142-
subField := suite.createRandomJSONNestedField(depth)
142+
subField := s.createRandomJSONNestedField(depth)
143143

144-
obj[suite.createRandomString()] = subField
144+
obj[s.createRandomString()] = subField
145145
}
146146

147147
return obj
148148
}
149149

150150
// createRandomJSONNestedField serves as a helper for createRandomJSONField and returns a random
151151
// subfield to populate an array or object type.
152-
func (suite *EIP712TestSuite) createRandomJSONNestedField(depth int) interface{} {
152+
func (s *EIP712TestSuite) createRandomJSONNestedField(depth int) interface{} {
153153
var newFieldType int
154154

155155
if depth == params.maxObjectDepth {
@@ -158,23 +158,23 @@ func (suite *EIP712TestSuite) createRandomJSONNestedField(depth int) interface{}
158158
newFieldType = rand.Intn(numJSONTypes)
159159
}
160160

161-
return suite.createRandomJSONField(newFieldType, depth+1)
161+
return s.createRandomJSONField(newFieldType, depth+1)
162162
}
163163

164-
func (suite *EIP712TestSuite) createRandomBoolean() bool {
164+
func (s *EIP712TestSuite) createRandomBoolean() bool {
165165
return rand.Intn(2) == 0
166166
}
167167

168-
func (suite *EIP712TestSuite) createRandomFloat() float64 {
168+
func (s *EIP712TestSuite) createRandomFloat() float64 {
169169
return (rand.Float64() - 0.5) * params.randomFloatRange
170170
}
171171

172-
func (suite *EIP712TestSuite) createRandomString() string {
173-
bzLen := suite.createRandomIntInRange(params.minStringLength, params.maxStringLength)
172+
func (s *EIP712TestSuite) createRandomString() string {
173+
bzLen := s.createRandomIntInRange(params.minStringLength, params.maxStringLength)
174174
bz := make([]byte, bzLen)
175175

176176
for i := 0; i < bzLen; i++ {
177-
bz[i] = byte(suite.createRandomIntInRange(asciiRangeStart, asciiRangeEnd))
177+
bz[i] = byte(s.createRandomIntInRange(asciiRangeStart, asciiRangeEnd))
178178
}
179179

180180
str := string(bz)
@@ -189,6 +189,6 @@ func (suite *EIP712TestSuite) createRandomString() string {
189189
}
190190

191191
// createRandomIntInRange provides a random integer between [min, max)
192-
func (suite *EIP712TestSuite) createRandomIntInRange(min int, max int) int {
192+
func (s *EIP712TestSuite) createRandomIntInRange(min int, max int) int {
193193
return rand.Intn(max-min) + min
194194
}

0 commit comments

Comments
 (0)