forked from santhosh-tekuri/jsonschema
-
Notifications
You must be signed in to change notification settings - Fork 6
/
formats_test.go
236 lines (219 loc) · 7.17 KB
/
formats_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
// Copyright 2017 Santhosh Kumar Tekuri. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package jsonschema
import (
"strings"
"testing"
)
type test struct {
str string
valid bool
}
func TestIsDateTime(t *testing.T) {
tests := []test{
{"1985-04-12T23:20:50.52Z", true},
{"1996-12-19T16:39:57-08:00", true},
{"1990-12-31T23:59:59Z", true},
{"1990-12-31T15:59:59-08:00", true},
{"1937-01-01T12:00:27.87+00:20", true},
{"1963-06-19T08:30:06.283185Z", true},
{"06/19/1963 08:30:06 PST", false},
{"2013-350T01:01:01", false},
}
for i, test := range tests {
if test.valid != isDateTime(test.str) {
t.Errorf("#%d: %q, valid %t, got valid %t", i, test.str, test.valid, !test.valid)
}
}
}
func TestIsDate(t *testing.T) {
tests := []test{
{"1963-06-19", true},
{"06/19/1963", false},
{"2013-350", false}, // only RFC3339 not all of ISO 8601 are valid
}
for i, test := range tests {
if test.valid != isDate(test.str) {
t.Errorf("#%d: %q, valid %t, got valid %t", i, test.str, test.valid, !test.valid)
}
}
}
func TestIsTime(t *testing.T) {
tests := []test{
{"08:30:06.283185Z", true},
{"08:30:06 PST", false},
{"01:01:01,1111", false}, // only RFC3339 not all of ISO 8601 are valid
}
for i, test := range tests {
if test.valid != isTime(test.str) {
t.Errorf("#%d: %q, valid %t, got valid %t", i, test.str, test.valid, !test.valid)
}
}
}
func TestIsHostname(t *testing.T) {
tests := []test{
{"www.example.com", true},
{strings.Repeat("a", 63) + "." + strings.Repeat("a", 63) + "." + strings.Repeat("a", 63) + "." + strings.Repeat("a", 61), true},
{strings.Repeat("a", 63) + "." + strings.Repeat("a", 63) + "." + strings.Repeat("a", 63) + "." + strings.Repeat("a", 61) + ".", true},
{strings.Repeat("a", 63) + "." + strings.Repeat("a", 63) + "." + strings.Repeat("a", 63) + "." + strings.Repeat("a", 62) + ".", false}, // length more than 253 characters long
{"www..com", false}, // empty label
{"-a-host-name-that-starts-with--", false},
{"not_a_valid_host_name", false},
{"a-vvvvvvvvvvvvvvvveeeeeeeeeeeeeeeerrrrrrrrrrrrrrrryyyyyyyyyyyyyyyy-long-host-name-component", false},
{"www.example-.com", false}, // label ends with a hyphen
}
for i, test := range tests {
if test.valid != isHostname(test.str) {
t.Errorf("#%d: %q, valid %t, got valid %t", i, test.str, test.valid, !test.valid)
}
}
}
func TestIsEmail(t *testing.T) {
tests := []test{
{"joe.bloggs@example.com", true},
{"2962", false}, // no "@" character
{strings.Repeat("a", 244) + "@google.com", false}, // more than 254 characters long
{strings.Repeat("a", 65) + "@google.com", false}, // local part more than 64 characters long
{"santhosh@-google.com", false}, // invalid domain name
}
for i, test := range tests {
if test.valid != isEmail(test.str) {
t.Errorf("#%d: %q, valid %t, got valid %t", i, test.str, test.valid, !test.valid)
}
}
}
func TestIsPhone(t *testing.T) {
tests := []test{
{"+380634872774", true},
{"+442087599036", true},
{"+18004444444", true},
{"1800801920", false},
{"0634872774", false},
{"+38-063-482-723-77", false}, // more than 17 characters long
}
for i, test := range tests {
if test.valid != isPhone(test.str) {
t.Errorf("#%d: %q, valid %t, got valid %t", i, test.str, test.valid, !test.valid)
}
}
}
func TestIsIPV4(t *testing.T) {
tests := []test{
{"192.168.0.1", true},
{"192.168.0.test", false}, // non-integer component
{"127.0.0.0.1", false}, // too many components
{"256.256.256.256", false}, // out-of-range values
{"127.0", false}, // without 4 components
{"0x7f000001", false}, // an integer
}
for i, test := range tests {
if test.valid != isIPV4(test.str) {
t.Errorf("#%d: %q, valid %t, got valid %t", i, test.str, test.valid, !test.valid)
}
}
}
func TestIsIPV6(t *testing.T) {
tests := []test{
{"::1", true},
{"192.168.0.1", false}, // is IPV4
{"12345::", false}, // out-of-range values
{"1:1:1:1:1:1:1:1:1:1:1:1:1:1:1:1", false}, // too many components
{"::laptop", false}, // containing illegal characters
}
for i, test := range tests {
if test.valid != isIPV6(test.str) {
t.Errorf("#%d: %q, valid %t, got valid %t", i, test.str, test.valid, !test.valid)
}
}
}
func TestIsURI(t *testing.T) {
tests := []test{
{"http://foo.bar/?baz=qux#quux", true},
{"//foo.bar/?baz=qux#quux", false}, // an invalid protocol-relative URI Reference
{"\\\\WINDOWS\\fileshare", false}, // an invalid URI
{"abc", false}, // an invalid URI though valid URI reference
}
for i, test := range tests {
if test.valid != isURI(test.str) {
t.Errorf("#%d: %q, valid %t, got valid %t", i, test.str, test.valid, !test.valid)
}
}
}
func TestIsURITemplate(t *testing.T) {
tests := []test{
{"http://example.com/dictionary/{term:1}/{term}", true},
{"http://example.com/dictionary/{term:1}/{term", false},
{"http://example.com/dictionary", true}, // without variables
{"dictionary/{term:1}/{term}", true}, // relative url-template
}
for i, test := range tests {
if test.valid != isURITemplate(test.str) {
t.Errorf("#%d: %q, valid %t, got valid %t", i, test.str, test.valid, !test.valid)
}
}
}
func TestIsRegex(t *testing.T) {
tests := []test{
{"([abc])+\\s+$", true},
{"^(abc]", false}, // unclosed parenthesis
}
for i, test := range tests {
if test.valid != isRegex(test.str) {
t.Errorf("#%d: %q, valid %t, got valid %t", i, test.str, test.valid, !test.valid)
}
}
}
func TestIsJSONPointer(t *testing.T) {
tests := []test{
{"", true}, // empty
{"/ ", true},
{"/foo/baz", true},
{"/foo/bar~0/baz~1/%a", true},
{"/g|h", true},
{"/i\\j", true},
{"/k\"l", true},
{"/foo//bar", true}, // empty segment
{"/foo/bar/", true}, // last empty segment
{"/foo/-", true}, // last array position
{"/foo/-/bar", true}, // - used as object member
{"/~1~0~0~1~1", true}, // multiple escape characters
{"/foo/baz~", false}, // ~ not escaped
{"/~-1", false}, // wrong escape character
{"/~~", false}, // multiple characters not escaped
// escaped with fractional part
{"/~1.1", true},
{"/~0.1", true},
// uri fragment identifier
{"#", false},
{"#/", false},
{"#a", false},
// some escaped, but not all
{"/~0~", false},
{"/~0/~", false},
{"/~0/~", false},
// isn't empty nor starts with /
{"a", false},
{"0", false},
{"a/a", false},
}
for i, test := range tests {
if test.valid != isJSONPointer(test.str) {
t.Errorf("#%d: %q, valid %t, got valid %t", i, test.str, test.valid, !test.valid)
}
}
}
func TestRelativeJSONPointer(t *testing.T) {
tests := []test{
{"1", true}, // upwards RJP
{"0/foo/bar", true}, // downwards RJP
{"2/0/baz/1/zip", true}, // up and then down RJP, with array index
{"0#", true}, // taking the member or index name
{"/foo/bar", false}, // valid json-pointer, but invalid RJP
}
for i, test := range tests {
if test.valid != isRelativeJSONPointer(test.str) {
t.Errorf("#%d: %q, valid %t, got valid %t", i, test.str, test.valid, !test.valid)
}
}
}