Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

packet_test.gno

6.24 Kb · 273 lines
  1package types_test
  2
  3import (
  4	"encoding/hex"
  5	"strings"
  6	"testing"
  7	"time"
  8
  9	"gno.land/p/aib/ibc/types"
 10	"gno.land/p/nt/ufmt/v0"
 11	"gno.land/p/nt/urequire/v0"
 12)
 13
 14func TestMsgSendPacketValidateBasic(t *testing.T) {
 15	var msg *types.MsgSendPacket
 16	var payload types.Payload
 17	testCases := []struct {
 18		name     string
 19		malleate func()
 20		expErr   string
 21	}{
 22		{
 23			name:     "success",
 24			malleate: func() {},
 25		},
 26		{
 27			name: "failure: multiple payloads",
 28			malleate: func() {
 29				msg.Payloads = append(msg.Payloads, payload)
 30			},
 31			expErr: "payloads must contain exactly one payload",
 32		},
 33		{
 34			name: "failure: empty source client",
 35			malleate: func() {
 36				msg.SourceClient = ""
 37			},
 38			expErr: "validate client identifier: identifier cannot be blank",
 39		},
 40		{
 41			name: "failure: invalid timestamp",
 42			malleate: func() {
 43				msg.TimeoutTimestamp = 0
 44			},
 45			expErr: "timeout must not be 0",
 46		},
 47		{
 48			name: "failure: invalid length for payload",
 49			malleate: func() {
 50				msg.Payloads = []types.Payload{}
 51			},
 52			expErr: "payloads must contain exactly one payload",
 53		},
 54		{
 55			name: "failure: invalid packetdata",
 56			malleate: func() {
 57				msg.Payloads = []types.Payload{{}}
 58			},
 59			expErr: "invalid payload #0: invalid source port",
 60		},
 61		{
 62			name: "failure: invalid payload",
 63			malleate: func() {
 64				msg.Payloads[0].DestinationPort = ""
 65			},
 66			expErr: "invalid payload #0: invalid destination port",
 67		},
 68	}
 69	for _, tc := range testCases {
 70		t.Run(tc.name, func(t *testing.T) {
 71			payload = types.Payload{
 72				SourcePort:      "sourcePort",
 73				DestinationPort: "destinationPort",
 74				Version:         "ics20-1",
 75				Encoding:        "encoding",
 76				Value:           []byte("packetData"),
 77			}
 78			msg = types.NewMsgSendPacket(
 79				"sourceClient", uint64(time.Now().Add(time.Hour).Unix()), payload,
 80			)
 81			tc.malleate()
 82
 83			err := msg.ValidateBasic()
 84
 85			if tc.expErr == "" && err != nil {
 86				t.Errorf("expected no error, got %s", err)
 87				return
 88			}
 89			if tc.expErr != "" {
 90				if err == nil || !strings.Contains(err.Error(), tc.expErr) {
 91					t.Errorf("expected error %s, got %s", tc.expErr, err)
 92				}
 93			}
 94		})
 95	}
 96}
 97
 98func TestPacketValidateBasic(t *testing.T) {
 99	var packet types.Packet
100	var payload types.Payload
101	testCases := []struct {
102		name     string
103		malleate func()
104		expErr   string
105	}{
106		{
107			"success",
108			func() {},
109			"",
110		},
111		{
112			"success, single payload just below MaxPayloadsSize",
113			func() {
114				packet.Payloads[0].Value = make([]byte, types.MaximumPayloadsSize-1)
115			},
116			"",
117		},
118		{
119			"failure: invalid single payloads size",
120			func() {
121				// bytes that are larger than MaxPayloadsSize
122				packet.Payloads[0].Value = make([]byte, types.MaximumPayloadsSize+1)
123			},
124			"packet data bytes cannot exceed",
125		},
126		{
127			"failure: multiple payloads",
128			func() {
129				packet.Payloads = append(packet.Payloads, payload)
130			},
131			"payloads must contain exactly one payload",
132		},
133		{
134			"failure: payloads is nil",
135			func() {
136				packet.Payloads = nil
137			},
138			"payloads must contain exactly one payload",
139		},
140		{
141			"failure: empty payload",
142			func() {
143				packet.Payloads = []types.Payload{}
144			},
145			"payloads must contain exactly one payload",
146		},
147		{
148			"failure: invalid payload source port ID",
149			func() {
150				packet.Payloads[0].SourcePort = ""
151			},
152			"invalid Payload #0: invalid source port",
153		},
154		{
155			"failure: invalid payload dest port ID",
156			func() {
157				packet.Payloads[0].DestinationPort = ""
158			},
159			"invalid Payload #0: invalid destination port",
160		},
161		{
162			"failure: invalid source ID",
163			func() {
164				packet.SourceClient = ""
165			},
166			"invalid source ID",
167		},
168		{
169			"failure: invalid dest ID",
170			func() {
171				packet.DestinationClient = ""
172			},
173			"invalid destination ID",
174		},
175		{
176			"failure: invalid sequence",
177			func() {
178				packet.Sequence = 0
179			},
180			"packet sequence cannot be 0",
181		},
182		{
183			"failure: invalid timestamp",
184			func() {
185				packet.TimeoutTimestamp = 0
186			},
187			"packet timeout timestamp cannot be 0",
188		},
189		{
190			"failure: empty version",
191			func() {
192				packet.Payloads[0].Version = ""
193			},
194			"payload version cannot be empty",
195		},
196		{
197			"failure: empty encoding",
198			func() {
199				packet.Payloads[0].Encoding = ""
200			},
201			"payload encoding cannot be empty",
202		},
203		{
204			"failure: empty value",
205			func() {
206				packet.Payloads[0].Value = []byte{}
207			},
208			"payload value cannot be empty",
209		},
210	}
211	for _, tc := range testCases {
212		t.Run(tc.name, func(t *testing.T) {
213			payload = types.Payload{
214				SourcePort:      "sourcePort",
215				DestinationPort: "destinationPort",
216				Version:         "ics20-v2",
217				Encoding:        "application/json",
218				Value:           []byte("{}"),
219			}
220			packet = types.NewPacket(1, "channel-1", "channel-2", uint64(time.Now().Unix()), payload)
221			tc.malleate()
222
223			err := packet.ValidateBasic()
224
225			if tc.expErr == "" {
226				urequire.NoError(t, err)
227				return
228			}
229			urequire.ErrorContains(t, err, tc.expErr)
230		})
231	}
232}
233
234func TestPacketProtoMarshal(t *testing.T) {
235	timeout, err := time.Parse(time.RFC3339Nano, "2018-07-01T00:00:00Z")
236	if err != nil {
237		panic(err)
238	}
239	packet := types.Packet{
240		SourceClient:      "sourceClient",
241		DestinationClient: "destClient",
242		Sequence:          42,
243		TimeoutTimestamp:  uint64(timeout.Unix()),
244		Payloads: []types.Payload{
245			{
246				SourcePort:      "sourcePort",
247				DestinationPort: "destinationPort",
248				Version:         "ics20-v2",
249				Encoding:        "application/json",
250				Value:           []byte("value"),
251			},
252			{
253				SourcePort:      "sourcePort",
254				DestinationPort: "destinationPort",
255				Version:         "ics20-v2",
256				Encoding:        "application/json",
257				Value:           []byte("value2"),
258			},
259		},
260	}
261
262	bz := packet.ProtoMarshal()
263
264	expected := "082a120c736f75726365436c69656e741a0a64657374436c69656e742080b3e0d9052a400a0a736f75726365506f7274120f64657374696e6174696f6e506f72741a0869637332302d763222106170706c69636174696f6e2f6a736f6e2a0576616c75652a410a0a736f75726365506f7274120f64657374696e6174696f6e506f72741a0869637332302d763222106170706c69636174696f6e2f6a736f6e2a0676616c756532"
265	if h := hex.EncodeToString(bz); h != expected {
266		t.Fatalf("expected %s got %s", expected, h)
267	}
268}
269
270func TestAcknowledgementProtoMarshal(t *testing.T) {
271	ack := types.Acknowledgement{AppAcknowledgements: [][]byte{{0x01, 0x02}, {0x03, 0x04}}}
272	ufmt.Printf("%q\n", string(ack.ProtoMarshal()))
273}