Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion x/tron/types/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,18 @@ func ValidateTronAddress(address string) error {
return errors.New("empty")
}
if len(address) != tronaddress.AddressLengthBase58 {
return errors.New("wrong length")
return fmt.Errorf("invalid address length: expected %d chars, got %d", tronaddress.AddressLengthBase58, len(address))
}
tronAddr, err := common.DecodeCheck(address)
if err != nil {
return errors.New("doesn't pass format validation")
}
if len(tronAddr) != tronaddress.AddressLength {
return fmt.Errorf("invalid address length: expected decoded %d bytes, got %d", tronaddress.AddressLength, len(tronAddr))
}
Comment thread
NikYak228 marked this conversation as resolved.
if tronAddr[0] != tronaddress.TronBytePrefix {
return errors.New("invalid tron prefix")
}
Comment thread
NikYak228 marked this conversation as resolved.
expectAddress := common.EncodeCheck(tronAddr)
if expectAddress != address {
return fmt.Errorf("mismatch expected: %s, got: %s", expectAddress, address)
Expand Down
23 changes: 20 additions & 3 deletions x/tron/types/address_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types_test

import (
"bytes"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -9,6 +10,10 @@
)

func TestValidateTronAddress(t *testing.T) {
nonTronPrefix := byte(0x00)
nonTronPayload := append([]byte{nonTronPrefix}, bytes.Repeat([]byte{0x11}, tronaddress.AddressLength-1)...)

Check failure on line 14 in x/tron/types/address_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: tronaddress

Check failure on line 14 in x/tron/types/address_test.go

View workflow job for this annotation

GitHub Actions / test

undefined: tronaddress
shortTronPayload := append([]byte{tronaddress.TronBytePrefix}, bytes.Repeat([]byte{0x11}, tronaddress.AddressLength-2)...)

Check failure on line 15 in x/tron/types/address_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: tronaddress

Check failure on line 15 in x/tron/types/address_test.go

View workflow job for this annotation

GitHub Actions / test

undefined: tronaddress

testCases := []struct {
testName string
value string
Expand All @@ -25,13 +30,13 @@
testName: "address length not match",
value: "abcdddddd",
expectPass: false,
errStr: "wrong length",
errStr: "invalid address length",
},
{
testName: "address length great than tron address",
value: "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t6666",
expectPass: false,
errStr: "wrong length",
errStr: "invalid address length",
},
{
testName: "lowercase address",
Expand All @@ -45,6 +50,18 @@
expectPass: false,
errStr: "doesn't pass format validation",
},
{
testName: "base58check address with non-tron prefix",
value: troncommon.EncodeCheck(nonTronPayload),

Check failure on line 55 in x/tron/types/address_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: troncommon

Check failure on line 55 in x/tron/types/address_test.go

View workflow job for this annotation

GitHub Actions / test

undefined: troncommon
expectPass: false,
errStr: "invalid tron prefix",
},
Comment thread
NikYak228 marked this conversation as resolved.
{
testName: "base58check address with invalid decoded length",
value: troncommon.EncodeCheck(shortTronPayload),

Check failure on line 61 in x/tron/types/address_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: troncommon (typecheck)

Check failure on line 61 in x/tron/types/address_test.go

View workflow job for this annotation

GitHub Actions / test

undefined: troncommon
expectPass: false,
errStr: "invalid address length",
},
Comment on lines +62 to +66

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Make the decoded-length regression case branch-specific.

Line 63 ("invalid address length") plus Line 79 (require.Contains) can pass via the earlier char-length guard, so this case does not reliably prove the new decoded-length check is exercised.

Suggested patch
@@
 		{
 			testName:   "base58check address with invalid decoded length",
 			value:      troncommon.EncodeCheck(shortTronPayload),
 			expectPass: false,
-			errStr:     "invalid address length",
+			errStr:     "expected decoded",
 		},
@@
-			require.Contains(t, err.Error(), testCase.errStr, testCase.value)
+			require.Error(t, err)
+			require.Contains(t, err.Error(), testCase.errStr, testCase.value)

As per path instructions, "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request".

Also applies to: 79-79

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@x/tron/types/address_test.go` around lines 60 - 64, The base58check invalid
decoded-length test in address_test.go is too generic and can succeed on the
earlier character-length validation instead of the new decoded-length branch.
Update the test case around the address decoding checks in the relevant test
function to use input that bypasses the char-length guard and only fails at the
decoded-length validation, and tighten the assertion in the corresponding
require.Contains check so it proves the branch-specific error path is exercised.

Source: Path instructions

{
testName: "normal address",
value: "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t",
Expand All @@ -59,7 +76,7 @@
require.NoError(t, err)
return
}
require.EqualValues(t, testCase.errStr, err.Error(), testCase.value)
require.Contains(t, err.Error(), testCase.errStr, testCase.value)
})
}
}
Loading