-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathidkey_test.tmpl
211 lines (200 loc) · 6.33 KB
/
idkey_test.tmpl
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
// MIT License
//
// Copyright 2018 Canonical Ledgers, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
// Code generated DO NOT EDIT
package factom
var (
// Test id/sk key pairs with all zeros.
// OBVIOUSLY NEVER USE THESE FOR ANYTHING!
{{range . -}}
id{{.ID}}KeyStr = "{{.IDStr}}"
{{end}}
{{range . -}}
sk{{.ID}}KeyStr = "{{.SKStr}}"
{{end}}
)
type idKeyUnmarshalJSONTest struct {
Name string
ID interface{}
ExpID interface{}
Data string
Err string
}
var idKeyUnmarshalJSONTests = []idKeyUnmarshalJSONTest{ {
{{ range . -}}
Name: "valid/ID{{.ID}}",
Data: fmt.Sprintf("%q", id{{.ID}}KeyStr),
ID: new(ID{{.ID}}Key),
ExpID: func() *ID{{.ID}}Key {
sk, _ := NewSK{{.ID}}Key(sk{{.ID}}KeyStr)
id := sk.ID{{.ID}}Key()
return &id
}(),
}, {
Name: "invalid type/ID{{.ID}}",
Data: `{}`,
Err: "json: cannot unmarshal object into Go value of type *factom.ID{{.ID}}Key",
ID: new(ID{{.ID}}Key),
}, {
Name: "invalid type/ID{{.ID}}",
Data: `5.5`,
Err: "json: cannot unmarshal number into Go value of type *factom.ID{{.ID}}Key",
ID: new(ID{{.ID}}Key),
}, {
Name: "invalid type/ID{{.ID}}",
Data: `["hello"]`,
Err: "json: cannot unmarshal array into Go value of type *factom.ID{{.ID}}Key",
ID: new(ID{{.ID}}Key),
}, {
{{ end }}
{{ range . -}}
Name: "valid/SK{{.ID}}",
Data: fmt.Sprintf("%q", sk{{.ID}}KeyStr),
ID: new(SK{{.ID}}Key),
ExpID: func() *SK{{.ID}}Key {
key, _ := NewSK{{.ID}}Key(sk{{.ID}}KeyStr)
return &key
}(),
}, {
Name: "invalid type/SK{{.ID}}",
Data: `{}`,
Err: "json: cannot unmarshal object into Go value of type *factom.SK{{.ID}}Key",
ID: new(SK{{.ID}}Key),
}, {
Name: "invalid type/SK{{.ID}}",
Data: `5.5`,
Err: "json: cannot unmarshal number into Go value of type *factom.SK{{.ID}}Key",
ID: new(SK{{.ID}}Key),
}, {
Name: "invalid type/SK{{.ID}}",
Data: `["hello"]`,
Err: "json: cannot unmarshal array into Go value of type *factom.SK{{.ID}}Key",
ID: new(SK{{.ID}}Key),
}, {
{{end}}
Name: "invalid length",
Data: fmt.Sprintf("%q", id1KeyStr[0:len(id1KeyStr)-1]),
Err: "invalid length",
}, {
Name: "invalid length",
Data: fmt.Sprintf("%q", id1KeyStr+"Q"),
Err: "invalid length",
}, {
Name: "invalid prefix",
Data: fmt.Sprintf("%q", func() string {
key, _ := NewSK1Key(sk1KeyStr)
return key.payload().StringWithPrefix([]byte{0x50, 0x50, 0x50})
}()),
Err: "invalid prefix",
{{ range . -}}
}, {
Name: "invalid symbol/ID{{.ID}}",
Data: fmt.Sprintf("%q", id{{.ID}}KeyStr[0:len(id{{.ID}}KeyStr)-1]+"0"),
Err: "invalid format: version and/or checksum bytes missing",
ID: new(ID{{.ID}}Key),
ExpID: new(ID{{.ID}}Key),
}, {
Name: "invalid symbol/SK{{.ID}}",
Data: fmt.Sprintf("%q", sk{{.ID}}KeyStr[0:len(sk{{.ID}}KeyStr)-1]+"0"),
Err: "invalid format: version and/or checksum bytes missing",
ID: new(SK{{.ID}}Key),
ExpID: new(SK{{.ID}}Key),
}, {
Name: "invalid checksum",
Data: fmt.Sprintf("%q", id{{.ID}}KeyStr[0:len(id{{.ID}}KeyStr)-1]+"e"),
Err: "checksum error",
ID: new(ID{{.ID}}Key),
ExpID: new(ID{{.ID}}Key),
}, {
Name: "invalid checksum",
Data: fmt.Sprintf("%q", sk{{.ID}}KeyStr[0:len(sk{{.ID}}KeyStr)-1]+"e"),
Err: "checksum error",
ID: new(SK{{.ID}}Key),
ExpID: new(SK{{.ID}}Key),
{{end}}
} }
func testIDKeyUnmarshalJSON(t *testing.T, test idKeyUnmarshalJSONTest) {
err := json.Unmarshal([]byte(test.Data), test.ID)
assert := assert.New(t)
if len(test.Err) > 0 {
assert.EqualError(err, test.Err)
return
}
assert.NoError(err)
assert.Equal(test.ExpID, test.ID)
}
func TestIDKey(t *testing.T) {
for _, test := range idKeyUnmarshalJSONTests {
if test.ID != nil {
t.Run("UnmarshalJSON/"+test.Name, func(t *testing.T) {
testIDKeyUnmarshalJSON(t, test)
})
continue
}
{{range .}}
test.ExpID, test.ID = new(ID{{.ID}}Key), new(ID{{.ID}}Key)
t.Run("UnmarshalJSON/"+test.Name+"/ID{{.ID}}Key", func(t *testing.T) {
testIDKeyUnmarshalJSON(t, test)
})
test.ExpID, test.ID = new(SK{{.ID}}Key), new(SK{{.ID}}Key)
t.Run("UnmarshalJSON/"+test.Name+"/SK{{.ID}}Key", func(t *testing.T) {
testIDKeyUnmarshalJSON(t, test)
})
{{end}}
}
{{range . -}}
id{{.ID}}, _ := NewID{{.ID}}Key(id{{.ID}}KeyStr)
sk{{.ID}}, _ := NewSK{{.ID}}Key(sk{{.ID}}KeyStr)
{{end}}
strToKey := map[string]interface{
PrefixString() string
String() string
}{
{{range . -}}
id{{.ID}}KeyStr: id{{.ID}}, sk{{.ID}}KeyStr: sk{{.ID}},
{{end}}
}
for keyStr, key := range strToKey {
t.Run("MarshalJSON/"+key.PrefixString(), func(t *testing.T) {
data, err := json.Marshal(key)
assert := assert.New(t)
assert.NoError(err)
assert.Equal(fmt.Sprintf("%q", keyStr), string(data))
})
t.Run("String/"+key.PrefixString(), func(t *testing.T) {
assert.Equal(t, keyStr, key.String())
})
}
{{range . -}}
t.Run("SKKey/SK{{.ID}}", func(t *testing.T) {
id, _ := NewID{{.ID}}Key(id{{.ID}}KeyStr)
sk, _ := NewSK{{.ID}}Key(sk{{.ID}}KeyStr)
assert := assert.New(t)
assert.Equal(id, sk.ID{{.ID}}Key())
})
{{end}}
{{range . -}}
t.Run("Generate/SK{{.ID}}", func(t *testing.T) {
_, err := GenerateSK{{.ID}}Key()
assert.NoError(t, err)
})
{{end}}
}