-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
network/records: fix panic due to insufficient subnets length (#1804)
* network/records: fix panic due to insufficient subnets length * add tests * Revert "add tests" This reverts commit 832cc71. * Revert "network/records: fix panic due to insufficient subnets length" This reverts commit 9a1496d. * fail metadata encoding/decoding if subnets length is invalid * add tests * fix some cases in TestHandshakeTestData * fix TestHandshakeTestData * fix remaining unit tests * add recover to acceptConnection * remove redundant comment
- Loading branch information
1 parent
0e5fc56
commit 0bc6433
Showing
5 changed files
with
114 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package records | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNodeMetadata_Encode(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
subnets string | ||
errString string | ||
}{ | ||
{ | ||
name: "Subnets too short", | ||
subnets: strings.Repeat("0", 31), | ||
errString: "invalid subnets length 31", | ||
}, | ||
{ | ||
name: "Subnets too long", | ||
subnets: strings.Repeat("0", 33), | ||
errString: "invalid subnets length 33", | ||
}, | ||
{ | ||
name: "Subnets exact and valid", | ||
subnets: strings.Repeat("0", 32), | ||
errString: "", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
nodeMeta := NodeMetadata{ | ||
NodeVersion: "1.0.0", | ||
ExecutionNode: "geth/v1.10.8", | ||
ConsensusNode: "lighthouse/v1.5.0", | ||
Subnets: tt.subnets, | ||
} | ||
|
||
_, err := nodeMeta.Encode() | ||
if tt.errString != "" { | ||
require.EqualError(t, err, tt.errString) | ||
} else { | ||
require.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestNodeMetadata_Decode(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
encoded string | ||
errString string | ||
}{ | ||
{ | ||
name: "Subnets too short", | ||
encoded: `{"NodeVersion":"1.0.0","ExecutionNode":"geth/v1.10.8","ConsensusNode":"lighthouse/v1.5.0","Subnets":"1234123412341234123412341234123"}`, | ||
errString: "invalid subnets length 31", | ||
}, | ||
{ | ||
name: "Subnets too long", | ||
encoded: `{"NodeVersion":"1.0.0","ExecutionNode":"geth/v1.10.8","ConsensusNode":"lighthouse/v1.5.0","Subnets":"123412341234123412341234123412341"}`, | ||
errString: "invalid subnets length 33", | ||
}, | ||
{ | ||
name: "Subnets exact and valid", | ||
encoded: `{"NodeVersion":"1.0.0","ExecutionNode":"geth/v1.10.8","ConsensusNode":"lighthouse/v1.5.0","Subnets":"12341234123412341234123412341234"}`, | ||
errString: "", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
nodeMeta := NodeMetadata{} | ||
|
||
err := nodeMeta.Decode([]byte(tt.encoded)) | ||
if tt.errString != "" { | ||
require.EqualError(t, err, tt.errString) | ||
} else { | ||
require.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters