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