Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bznein committed Oct 3, 2024
1 parent c391827 commit d4d84a9
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 0 deletions.
20 changes: 20 additions & 0 deletions modules/core/04-channel/v2/types/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,26 @@ import (
host "github.com/cosmos/ibc-go/v9/modules/core/24-host"
)

// NewPacket constructs a new packet.
func NewPacket(sequence uint64, sourceID, destinationID string, timeoutTimestamp uint64, data ...PacketData) Packet {
return Packet{
Sequence: sequence,
SourceId: sourceID,
DestinationId: destinationID,
TimeoutTimestamp: timeoutTimestamp,
Data: data,
}
}

// NewPayload constructs a new Payload
func NewPayload(version, encoding string, value []byte) Payload {
return Payload{
Version: version,
Encoding: encoding,
Value: value,
}
}

// ValidateBasic validates that a Packet satisfies the basic requirements.
func (p Packet) ValidateBasic() error {
if len(p.Data) == 0 {
Expand Down
137 changes: 137 additions & 0 deletions modules/core/04-channel/v2/types/packet_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package types

import (
"testing"
"time"

host "github.com/cosmos/ibc-go/v9/modules/core/24-host"
"github.com/cosmos/ibc-go/v9/testing/mock"
"github.com/stretchr/testify/require"
)

// TestValidate tests the Validate function of Packet
func TestValidate(t *testing.T) {
testCases := []struct {
name string
payload Payload
expErr error
}{
{
"success",
NewPayload("ics20-v1", "json", mock.MockPacketData),
nil,
},
{
"failure: empty version",
NewPayload("", "json", mock.MockPacketData),
ErrInvalidPayload,
},
{
"failure: empty encoding",
NewPayload("ics20-v2", "", mock.MockPacketData),
ErrInvalidPayload,
},
{
"failure: empty value",
NewPayload("ics20-v1", "json", []byte{}),
ErrInvalidPayload,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := tc.payload.Validate()
if tc.expErr == nil {
require.NoError(t, err)
} else {
require.ErrorIs(t, err, tc.expErr)
}
})
}
}

// TestValidateBasic tests the ValidateBasic functio of Packet
func TestValidateBasic(t *testing.T) {
var packet Packet
testCases := []struct {
name string
malleate func()
expErr error
}{
{
"success",
func() {},
nil,
},
{
"failure: empty data",
func() {
packet.Data = []PacketData{}
},
ErrInvalidPacket,
},
{
"failure: invalid data source port ID",
func() {
packet.Data[0].SourcePort = ""
},
host.ErrInvalidID,
},
{
"failure: invalid data dest port ID",
func() {
packet.Data[0].DestinationPort = ""
},
host.ErrInvalidID,
},
{
"failure: invalid source channel ID",
func() {
packet.SourceId = ""
},
host.ErrInvalidID,
},
{
"failure: invalid dest channel ID",
func() {
packet.DestinationId = ""
},
host.ErrInvalidID,
},
{
"failure: invalid sequence",
func() {
packet.Sequence = 0
},
ErrInvalidPacket,
},
{
"failure: invalid timestamp",
func() {
packet.TimeoutTimestamp = 0
},
ErrInvalidPacket,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
packet = NewPacket(1, "sourceChannelID", "destChannelID", uint64(time.Now().Unix()), PacketData{
SourcePort: "sourcePort",
DestinationPort: "destPort",
Payload: Payload{
Version: "ics20-v2",
Encoding: "encoding",
Value: mock.MockPacketData,
},
})

tc.malleate()

err := packet.ValidateBasic()
if tc.expErr == nil {
require.NoError(t, err)
} else {
require.ErrorIs(t, err, tc.expErr)
}
})
}
}

0 comments on commit d4d84a9

Please sign in to comment.