@@ -6,45 +6,67 @@ package runtime
6
6
import (
7
7
"testing"
8
8
9
+ "github.com/ChainSafe/gossamer/pkg/scale"
9
10
"github.com/stretchr/testify/assert"
10
11
"github.com/stretchr/testify/require"
11
12
)
12
13
13
- func Test_Version_Encode (t * testing.T ) {
14
- t .Parallel ()
14
+ func scaleEncode (t * testing.T , x any ) []byte {
15
+ encoded , err := scale .Marshal (x )
16
+ require .NoError (t , err )
17
+ return encoded
18
+ }
15
19
16
- someVersion := Version {
17
- SpecName : []byte {1 },
18
- ImplName : []byte {2 },
19
- AuthoringVersion : 3 ,
20
- SpecVersion : 4 ,
21
- ImplVersion : 5 ,
22
- APIItems : []APIItem {{
23
- Name : [8 ]byte {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 },
24
- Ver : 6 ,
25
- }},
26
- TransactionVersion : 7 ,
20
+ func concatBytes (slices [][]byte ) (concatenated []byte ) {
21
+ for _ , slice := range slices {
22
+ concatenated = append (concatenated , slice ... )
27
23
}
24
+ return concatenated
25
+ }
26
+
27
+ func Test_Version_Encode (t * testing.T ) {
28
+ t .Parallel ()
28
29
29
30
testCases := map [string ]struct {
30
31
version Version
31
32
encoding []byte
32
33
errWrapped error
33
34
errMessage string
34
35
}{
35
- "not legacy" : {
36
- version : someVersion ,
36
+ "no optional field set" : {
37
+ version : Version {
38
+ SpecName : []byte {1 },
39
+ ImplName : []byte {2 },
40
+ AuthoringVersion : 3 ,
41
+ SpecVersion : 4 ,
42
+ ImplVersion : 5 ,
43
+ APIItems : []APIItem {{
44
+ Name : [8 ]byte {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 },
45
+ Ver : 6 ,
46
+ }},
47
+ },
37
48
encoding : []byte {
38
49
0x4 , 0x1 , 0x4 , 0x2 , 0x3 , 0x0 , 0x0 , 0x0 , 0x4 , 0x0 , 0x0 , 0x0 ,
39
50
0x5 , 0x0 , 0x0 , 0x0 , 0x4 , 0x1 , 0x2 , 0x3 , 0x4 , 0x5 , 0x6 , 0x7 ,
40
- 0x8 , 0x6 , 0x0 , 0x0 , 0x0 , 0x7 , 0x0 , 0x0 , 0x0 },
51
+ 0x8 , 0x6 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 },
41
52
},
42
- "legacy" : {
43
- version : someVersion .WithLegacy (),
53
+ "all optional fields set" : {
54
+ version : Version {
55
+ SpecName : []byte {1 },
56
+ ImplName : []byte {2 },
57
+ AuthoringVersion : 3 ,
58
+ SpecVersion : 4 ,
59
+ ImplVersion : 5 ,
60
+ APIItems : []APIItem {{
61
+ Name : [8 ]byte {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 },
62
+ Ver : 6 ,
63
+ }},
64
+ TransactionVersion : 7 ,
65
+ },
44
66
encoding : []byte {
45
67
0x4 , 0x1 , 0x4 , 0x2 , 0x3 , 0x0 , 0x0 , 0x0 , 0x4 , 0x0 , 0x0 , 0x0 ,
46
68
0x5 , 0x0 , 0x0 , 0x0 , 0x4 , 0x1 , 0x2 , 0x3 , 0x4 , 0x5 , 0x6 , 0x7 ,
47
- 0x8 , 0x6 , 0x0 , 0x0 , 0x0 },
69
+ 0x8 , 0x6 , 0x0 , 0x0 , 0x0 , 0x7 , 0x0 , 0x0 , 0x0 },
48
70
},
49
71
}
50
72
@@ -73,11 +95,35 @@ func Test_DecodeVersion(t *testing.T) {
73
95
errWrapped error
74
96
errMessage string
75
97
}{
76
- "unmarshal success" : {
98
+ "required field decode error" : {
99
+ encoded : concatBytes ([][]byte {
100
+ scaleEncode (t , []byte {1 , 2 }),
101
+ {255 , 255 }, // error
102
+ }),
103
+ errWrapped : ErrDecodingVersionField ,
104
+ errMessage : "decoding version field impl name: could not decode invalid integer" ,
105
+ },
106
+ // TODO add transaction version decode error once
107
+ // https://github.com/ChainSafe/gossamer/pull/2683
108
+ // is merged.
109
+ // "transaction version decode error": {
110
+ // encoded: concatBytes([][]byte{
111
+ // scaleEncode(t, []byte("a")), // spec name
112
+ // scaleEncode(t, []byte("b")), // impl name
113
+ // scaleEncode(t, uint32(1)), // authoring version
114
+ // scaleEncode(t, uint32(2)), // spec version
115
+ // scaleEncode(t, uint32(3)), // impl version
116
+ // scaleEncode(t, []APIItem{{}}), // api items
117
+ // {1, 2, 3}, // transaction version
118
+ // }),
119
+ // errWrapped: ErrDecoding,
120
+ // errMessage: "decoding transaction version: could not decode invalid integer",
121
+ // },
122
+ "no optional field set" : {
77
123
encoded : []byte {
78
124
0x4 , 0x1 , 0x4 , 0x2 , 0x3 , 0x0 , 0x0 , 0x0 , 0x4 , 0x0 , 0x0 , 0x0 ,
79
125
0x5 , 0x0 , 0x0 , 0x0 , 0x4 , 0x1 , 0x2 , 0x3 , 0x4 , 0x5 , 0x6 , 0x7 ,
80
- 0x8 , 0x6 , 0x0 , 0x0 , 0x0 , 0x7 , 0x0 , 0x0 , 0x0 },
126
+ 0x8 , 0x6 , 0x0 , 0x0 , 0x0 },
81
127
version : Version {
82
128
SpecName : []byte {1 },
83
129
ImplName : []byte {2 },
@@ -88,24 +134,13 @@ func Test_DecodeVersion(t *testing.T) {
88
134
Name : [8 ]byte {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 },
89
135
Ver : 6 ,
90
136
}},
91
- TransactionVersion : 7 ,
92
137
},
93
138
},
94
- "unmarshal error" : {
95
- encoded : []byte {255 , 255 },
96
- errWrapped : ErrDecodingVersion ,
97
- errMessage : "decoding version: could not decode invalid integer, field: []" ,
98
- },
99
- "legacy unmarshal error" : {
100
- encoded : []byte {0 },
101
- errWrapped : ErrDecodingLegacyVersion ,
102
- errMessage : "decoding legacy version: EOF, field: []" ,
103
- },
104
- "legacy unmarshal success" : {
139
+ "transaction version set" : {
105
140
encoded : []byte {
106
141
0x4 , 0x1 , 0x4 , 0x2 , 0x3 , 0x0 , 0x0 , 0x0 , 0x4 , 0x0 , 0x0 , 0x0 ,
107
142
0x5 , 0x0 , 0x0 , 0x0 , 0x4 , 0x1 , 0x2 , 0x3 , 0x4 , 0x5 , 0x6 , 0x7 ,
108
- 0x8 , 0x6 , 0x0 , 0x0 , 0x0 },
143
+ 0x8 , 0x6 , 0x0 , 0x0 , 0x0 , 0x7 , 0x0 , 0x0 , 0x0 },
109
144
version : Version {
110
145
SpecName : []byte {1 },
111
146
ImplName : []byte {2 },
@@ -116,7 +151,7 @@ func Test_DecodeVersion(t *testing.T) {
116
151
Name : [8 ]byte {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 },
117
152
Ver : 6 ,
118
153
}},
119
- legacy : true ,
154
+ TransactionVersion : 7 ,
120
155
},
121
156
},
122
157
}
@@ -145,7 +180,7 @@ func Test_Version_Scale(t *testing.T) {
145
180
encoding []byte
146
181
decoded Version
147
182
}{
148
- "current version " : {
183
+ "no optional field set " : {
149
184
version : Version {
150
185
SpecName : []byte {1 },
151
186
ImplName : []byte {2 },
@@ -156,12 +191,11 @@ func Test_Version_Scale(t *testing.T) {
156
191
Name : [8 ]byte {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 },
157
192
Ver : 6 ,
158
193
}},
159
- TransactionVersion : 7 ,
160
194
},
161
195
encoding : []byte {
162
196
0x4 , 0x1 , 0x4 , 0x2 , 0x3 , 0x0 , 0x0 , 0x0 , 0x4 , 0x0 , 0x0 , 0x0 ,
163
197
0x5 , 0x0 , 0x0 , 0x0 , 0x4 , 0x1 , 0x2 , 0x3 , 0x4 , 0x5 , 0x6 , 0x7 ,
164
- 0x8 , 0x6 , 0x0 , 0x0 , 0x0 , 0x7 , 0x0 , 0x0 , 0x0 },
198
+ 0x8 , 0x6 , 0x0 , 0x0 , 0x0 },
165
199
decoded : Version {
166
200
SpecName : []byte {1 },
167
201
ImplName : []byte {2 },
@@ -172,10 +206,9 @@ func Test_Version_Scale(t *testing.T) {
172
206
Name : [8 ]byte {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 },
173
207
Ver : 6 ,
174
208
}},
175
- TransactionVersion : 7 ,
176
209
},
177
210
},
178
- "legacy version" : {
211
+ "transaction version set " : {
179
212
version : Version {
180
213
SpecName : []byte {1 },
181
214
ImplName : []byte {2 },
@@ -187,12 +220,11 @@ func Test_Version_Scale(t *testing.T) {
187
220
Ver : 6 ,
188
221
}},
189
222
TransactionVersion : 7 ,
190
- legacy : true ,
191
223
},
192
224
encoding : []byte {
193
225
0x4 , 0x1 , 0x4 , 0x2 , 0x3 , 0x0 , 0x0 , 0x0 , 0x4 , 0x0 , 0x0 , 0x0 ,
194
226
0x5 , 0x0 , 0x0 , 0x0 , 0x4 , 0x1 , 0x2 , 0x3 , 0x4 , 0x5 , 0x6 , 0x7 ,
195
- 0x8 , 0x6 , 0x0 , 0x0 , 0x0 },
227
+ 0x8 , 0x6 , 0x0 , 0x0 , 0x0 , 0x7 , 0x0 , 0x0 , 0x0 },
196
228
decoded : Version {
197
229
SpecName : []byte {1 },
198
230
ImplName : []byte {2 },
@@ -203,7 +235,7 @@ func Test_Version_Scale(t *testing.T) {
203
235
Name : [8 ]byte {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 },
204
236
Ver : 6 ,
205
237
}},
206
- legacy : true ,
238
+ TransactionVersion : 7 ,
207
239
},
208
240
},
209
241
}
0 commit comments