-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcip20_test.go
257 lines (239 loc) · 7.94 KB
/
cip20_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package models
import (
"encoding/hex"
"encoding/json"
"reflect"
"strings"
"testing"
"github.com/fxamacker/cbor/v2"
)
func TestValidCip20Metadata(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
cborHex string
expectedObj Cip20Metadata
jsonData string
expectCBORUnmarshalError bool
expectJSONUnmarshalError bool
expectValidationError bool
expectCBORDeepEqualError bool
expectJSONDeepEqualError bool
}{
{
name: "Valid - Single Message",
expectedObj: Cip20Metadata{
Num674: Num674{
Msg: []string{"This is a comment for the transaction xyz, thank you very much!"},
},
},
cborHex: "A163363734A1636D736781783F54686973206973206120636F6D6D656E7420666F7220746865207472616E73616374696F6E2078797A2C207468616E6B20796F752076657279206D75636821",
jsonData: `{
"674":
{
"msg":
[
"This is a comment for the transaction xyz, thank you very much!"
]
}
}`,
expectCBORUnmarshalError: false,
expectJSONUnmarshalError: false,
expectValidationError: false,
expectCBORDeepEqualError: false,
expectJSONDeepEqualError: false,
},
{
name: "Valid - Multiple Messages",
expectedObj: Cip20Metadata{
Num674: Num674{
Msg: []string{
"Invoice-No: 1234567890",
"Customer-No: 555-1234",
"P.S.: i will shop again at your store :-)",
},
},
},
jsonData: `{
"674":
{
"msg":
[
"Invoice-No: 1234567890",
"Customer-No: 555-1234",
"P.S.: i will shop again at your store :-)"
]
}
}`,
cborHex: "A163363734A1636D73678376496E766F6963652D4E6F3A203132333435363738393075437573746F6D65722D4E6F3A203535352D313233347829502E532E3A20692077696C6C2073686F7020616761696E20617420796F75722073746F7265203A2D29",
expectCBORUnmarshalError: false,
expectJSONUnmarshalError: false,
expectValidationError: false,
expectCBORDeepEqualError: false,
expectJSONDeepEqualError: false,
},
{
name: "Invalid - Message Exceeds 64 Bytes",
expectedObj: Cip20Metadata{
Num674: Num674{
Msg: []string{strings.Repeat("a", 65)}, // 65 'a's to exceed the limit
},
},
jsonData: `{
"674": {
"msg": ["aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"]
}
}`,
cborHex: "A163363734A1636D73678178416161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161",
expectCBORUnmarshalError: false,
expectJSONUnmarshalError: false,
expectValidationError: true,
expectCBORDeepEqualError: false,
expectJSONDeepEqualError: false,
},
{
name: "Invalid - Message Deep error",
expectedObj: Cip20Metadata{
Num674: Num674{
Msg: []string{strings.Repeat("b", 65)},
},
},
jsonData: `{
"674": {
"msg": ["aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"]
}
}`,
cborHex: "A163363734A1636D73678178416161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161",
expectCBORUnmarshalError: false,
expectJSONUnmarshalError: false,
expectValidationError: true,
expectCBORDeepEqualError: true,
expectJSONDeepEqualError: true,
},
{
name: "Invalid - Message Deep error 675",
expectedObj: Cip20Metadata{
Num674: Num674{
Msg: []string{strings.Repeat("b", 65)},
},
},
jsonData: `{
"675": {
"msg": ["aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"]
}
}`,
cborHex: "A163363734A1636D73678178416161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161",
expectCBORUnmarshalError: false,
expectJSONUnmarshalError: false,
expectValidationError: true,
expectCBORDeepEqualError: true,
expectJSONDeepEqualError: true,
},
}
for _, tc := range testCases {
tc := tc // capture range variable for goroutines
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
// Decode the CBOR hex string
cborData, err := hex.DecodeString(tc.cborHex)
if err != nil {
t.Errorf("failed to decode CBOR hex string for %s, error: %v", tc.name, err)
}
// CBOR Unmarshal
var decodedMetadata Cip20Metadata
err = cbor.Unmarshal(cborData, &decodedMetadata)
if tc.expectCBORUnmarshalError {
if err == nil {
t.Errorf("expected CBOR unmarshal error but got none for test: %s", tc.name)
}
} else {
if err != nil {
t.Errorf("did not expect CBOR unmarshal error but got one for test: %s, error: %v", tc.name, err)
}
}
// CBOR Validate
err = decodedMetadata.Validate()
if tc.expectValidationError {
if err == nil {
t.Errorf("expected validation error but got none for test: %s", tc.name)
}
} else {
if err != nil {
t.Errorf("did not expect validation error but got one for test: %s, error: %v", tc.name, err)
}
}
// Deep Equality Check for CBOR
deepEqual := reflect.DeepEqual(decodedMetadata, tc.expectedObj)
if tc.expectCBORDeepEqualError && deepEqual {
t.Errorf("Cbor expected deep equal error but objects are identical for test: %s", tc.name)
}
if !tc.expectCBORDeepEqualError && !deepEqual {
t.Errorf("Cbor expected objects to be identical but they are not for test: %s, expected: %v, got: %v", tc.name, tc.expectedObj, decodedMetadata)
}
// Reset the object for JSON testing
decodedMetadata = Cip20Metadata{}
// Decode the JSON string
err = json.Unmarshal([]byte(tc.jsonData), &decodedMetadata)
if err != nil {
t.Errorf("unexpected result unmarshaling JSON to Cip20Metadata for test %s, error: %v", tc.name, err)
}
if tc.expectJSONUnmarshalError {
if err == nil {
t.Errorf("expected JSON unmarshal error but got none for test: %s", tc.name)
}
} else {
if err != nil {
t.Errorf("did not expect JSON unmarshal error but got one for test: %s, error: %v", tc.name, err)
}
}
// JSON Validate
err = decodedMetadata.Validate()
if tc.expectValidationError {
if err == nil {
t.Errorf("expected validation error but got none for test: %s", tc.name)
}
} else {
if err != nil {
t.Errorf("did not expect validation error but got one for test: %s, error: %v", tc.name, err)
}
}
// Deep Equality Check for JSON
deepEqual = reflect.DeepEqual(decodedMetadata, tc.expectedObj)
if tc.expectJSONDeepEqualError && deepEqual {
t.Errorf("Json expected deep equal error but objects are identical for test: %s", tc.name)
}
if !tc.expectJSONDeepEqualError && !deepEqual {
t.Errorf("Json expected objects to be identical but they are not for test: %s, expected: %v, got: %v", tc.name, tc.expectedObj, decodedMetadata)
}
})
}
}
func TestNewCip20Metadata(t *testing.T) {
t.Parallel()
// Test case: Non-empty messages
messages := []string{"message1", "message2"}
expectedJSON := `{"674":{"msg":["message1","message2"]}}`
metadata, err := NewCip20Metadata(messages)
if err != nil {
t.Errorf("Expected no error, but got: %v", err)
}
actualJSON, err := json.Marshal(metadata)
if err != nil {
t.Errorf("Failed to marshal metadata to JSON: %v", err)
}
if string(actualJSON) != expectedJSON {
t.Errorf("Expected JSON: %s, but got: %s", expectedJSON, string(actualJSON))
}
// Test case: Invalid metadata
messages = []string{}
expectedErr := "Key: 'Cip20Metadata.Num674.Msg' Error:Field validation for 'Msg' failed on the 'gt' tag"
_, err = NewCip20Metadata(messages)
if err == nil {
t.Errorf("Expected validation error, but got no error")
} else {
actualErr := err.Error()
if actualErr != expectedErr {
t.Errorf("Expected error: %s, but got: %s", expectedErr, actualErr)
}
}
}