@@ -6,46 +6,51 @@ 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
+ "all optional fields 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
+ TransactionVersion : 7 ,
48
+ },
37
49
encoding : []byte {
38
50
0x4 , 0x1 , 0x4 , 0x2 , 0x3 , 0x0 , 0x0 , 0x0 , 0x4 , 0x0 , 0x0 , 0x0 ,
39
51
0x5 , 0x0 , 0x0 , 0x0 , 0x4 , 0x1 , 0x2 , 0x3 , 0x4 , 0x5 , 0x6 , 0x7 ,
40
52
0x8 , 0x6 , 0x0 , 0x0 , 0x0 , 0x7 , 0x0 , 0x0 , 0x0 },
41
53
},
42
- "legacy" : {
43
- version : someVersion .WithLegacy (),
44
- encoding : []byte {
45
- 0x4 , 0x1 , 0x4 , 0x2 , 0x3 , 0x0 , 0x0 , 0x0 , 0x4 , 0x0 , 0x0 , 0x0 ,
46
- 0x5 , 0x0 , 0x0 , 0x0 , 0x4 , 0x1 , 0x2 , 0x3 , 0x4 , 0x5 , 0x6 , 0x7 ,
47
- 0x8 , 0x6 , 0x0 , 0x0 , 0x0 },
48
- },
49
54
}
50
55
51
56
for name , testCase := range testCases {
@@ -73,11 +78,35 @@ func Test_DecodeVersion(t *testing.T) {
73
78
errWrapped error
74
79
errMessage string
75
80
}{
76
- "unmarshal success" : {
81
+ "required field decode error" : {
82
+ encoded : concatBytes ([][]byte {
83
+ scaleEncode (t , []byte {1 , 2 }),
84
+ {255 , 255 }, // error
85
+ }),
86
+ errWrapped : ErrDecodingVersionField ,
87
+ errMessage : "decoding version field impl name: could not decode invalid integer" ,
88
+ },
89
+ // TODO add transaction version decode error once
90
+ // https://github.com/ChainSafe/gossamer/pull/2683
91
+ // is merged.
92
+ // "transaction version decode error": {
93
+ // encoded: concatBytes([][]byte{
94
+ // scaleEncode(t, []byte("a")), // spec name
95
+ // scaleEncode(t, []byte("b")), // impl name
96
+ // scaleEncode(t, uint32(1)), // authoring version
97
+ // scaleEncode(t, uint32(2)), // spec version
98
+ // scaleEncode(t, uint32(3)), // impl version
99
+ // scaleEncode(t, []APIItem{{}}), // api items
100
+ // {1, 2, 3}, // transaction version
101
+ // }),
102
+ // errWrapped: ErrDecoding,
103
+ // errMessage: "decoding transaction version: could not decode invalid integer",
104
+ // },
105
+ "no optional field set" : {
77
106
encoded : []byte {
78
107
0x4 , 0x1 , 0x4 , 0x2 , 0x3 , 0x0 , 0x0 , 0x0 , 0x4 , 0x0 , 0x0 , 0x0 ,
79
108
0x5 , 0x0 , 0x0 , 0x0 , 0x4 , 0x1 , 0x2 , 0x3 , 0x4 , 0x5 , 0x6 , 0x7 ,
80
- 0x8 , 0x6 , 0x0 , 0x0 , 0x0 , 0x7 , 0x0 , 0x0 , 0x0 },
109
+ 0x8 , 0x6 , 0x0 , 0x0 , 0x0 },
81
110
version : Version {
82
111
SpecName : []byte {1 },
83
112
ImplName : []byte {2 },
@@ -88,24 +117,13 @@ func Test_DecodeVersion(t *testing.T) {
88
117
Name : [8 ]byte {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 },
89
118
Ver : 6 ,
90
119
}},
91
- TransactionVersion : 7 ,
92
120
},
93
121
},
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" : {
122
+ "transaction version set" : {
105
123
encoded : []byte {
106
124
0x4 , 0x1 , 0x4 , 0x2 , 0x3 , 0x0 , 0x0 , 0x0 , 0x4 , 0x0 , 0x0 , 0x0 ,
107
125
0x5 , 0x0 , 0x0 , 0x0 , 0x4 , 0x1 , 0x2 , 0x3 , 0x4 , 0x5 , 0x6 , 0x7 ,
108
- 0x8 , 0x6 , 0x0 , 0x0 , 0x0 },
126
+ 0x8 , 0x6 , 0x0 , 0x0 , 0x0 , 0x7 , 0x0 , 0x0 , 0x0 },
109
127
version : Version {
110
128
SpecName : []byte {1 },
111
129
ImplName : []byte {2 },
@@ -116,7 +134,7 @@ func Test_DecodeVersion(t *testing.T) {
116
134
Name : [8 ]byte {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 },
117
135
Ver : 6 ,
118
136
}},
119
- legacy : true ,
137
+ TransactionVersion : 7 ,
120
138
},
121
139
},
122
140
}
@@ -145,7 +163,7 @@ func Test_Version_Scale(t *testing.T) {
145
163
encoding []byte
146
164
decoded Version
147
165
}{
148
- "current version " : {
166
+ "all fields set " : {
149
167
version : Version {
150
168
SpecName : []byte {1 },
151
169
ImplName : []byte {2 },
@@ -175,37 +193,6 @@ func Test_Version_Scale(t *testing.T) {
175
193
TransactionVersion : 7 ,
176
194
},
177
195
},
178
- "legacy version" : {
179
- version : Version {
180
- SpecName : []byte {1 },
181
- ImplName : []byte {2 },
182
- AuthoringVersion : 3 ,
183
- SpecVersion : 4 ,
184
- ImplVersion : 5 ,
185
- APIItems : []APIItem {{
186
- Name : [8 ]byte {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 },
187
- Ver : 6 ,
188
- }},
189
- TransactionVersion : 7 ,
190
- legacy : true ,
191
- },
192
- encoding : []byte {
193
- 0x4 , 0x1 , 0x4 , 0x2 , 0x3 , 0x0 , 0x0 , 0x0 , 0x4 , 0x0 , 0x0 , 0x0 ,
194
- 0x5 , 0x0 , 0x0 , 0x0 , 0x4 , 0x1 , 0x2 , 0x3 , 0x4 , 0x5 , 0x6 , 0x7 ,
195
- 0x8 , 0x6 , 0x0 , 0x0 , 0x0 },
196
- decoded : Version {
197
- SpecName : []byte {1 },
198
- ImplName : []byte {2 },
199
- AuthoringVersion : 3 ,
200
- SpecVersion : 4 ,
201
- ImplVersion : 5 ,
202
- APIItems : []APIItem {{
203
- Name : [8 ]byte {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 },
204
- Ver : 6 ,
205
- }},
206
- legacy : true ,
207
- },
208
- },
209
196
}
210
197
211
198
for name , testCase := range testCases {
0 commit comments