Debian Codegraph @

pkg/internal/third_party/go-json-experiment/json/arshal_time_test.go

raw
1 // Copyright 2023 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package json
6
7 import (
8 "errors"
9 "fmt"
10 "math"
11 "strconv"
12 "testing"
13 "time"
14
15 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json/internal/jsonwire"
16 )
17
18 func baseLabel(base uint64) string {
19 if log10 := math.Log10(float64(base)); log10 == float64(int64(log10)) {
20 return fmt.Sprintf("1e%d", int(log10))
21 }
22 return fmt.Sprint(base)
23 }
24
25 var formatDurationTestdata = []struct {
26 td time.Duration
27 base10Sec string
28 base10Milli string
29 base10Micro string
30 base10Nano string
31 iso8601 string
32 }{
33 {math.MaxInt64, "9223372036.854775807", "9223372036854.775807", "9223372036854775.807", "9223372036854775807", "PT2562047H47M16.854775807S"},
34 {123*time.Hour + 4*time.Minute + 56*time.Second, "443096", "443096000", "443096000000", "443096000000000", "PT123H4M56S"},
35 {time.Hour, "3600", "3600000", "3600000000", "3600000000000", "PT1H"},
36 {time.Minute, "60", "60000", "60000000", "60000000000", "PT1M"},
37 {1e12 + 1e12, "2000", "2000000", "2000000000", "2000000000000", "PT33M20S"},
38 {1e12 + 1e11, "1100", "1100000", "1100000000", "1100000000000", "PT18M20S"},
39 {1e12 + 1e10, "1010", "1010000", "1010000000", "1010000000000", "PT16M50S"},
40 {1e12 + 1e9, "1001", "1001000", "1001000000", "1001000000000", "PT16M41S"},
41 {1e12 + 1e8, "1000.1", "1000100", "1000100000", "1000100000000", "PT16M40.1S"},
42 {1e12 + 1e7, "1000.01", "1000010", "1000010000", "1000010000000", "PT16M40.01S"},
43 {1e12 + 1e6, "1000.001", "1000001", "1000001000", "1000001000000", "PT16M40.001S"},
44 {1e12 + 1e5, "1000.0001", "1000000.1", "1000000100", "1000000100000", "PT16M40.0001S"},
45 {1e12 + 1e4, "1000.00001", "1000000.01", "1000000010", "1000000010000", "PT16M40.00001S"},
46 {1e12 + 1e3, "1000.000001", "1000000.001", "1000000001", "1000000001000", "PT16M40.000001S"},
47 {1e12 + 1e2, "1000.0000001", "1000000.0001", "1000000000.1", "1000000000100", "PT16M40.0000001S"},
48 {1e12 + 1e1, "1000.00000001", "1000000.00001", "1000000000.01", "1000000000010", "PT16M40.00000001S"},
49 {1e12 + 1e0, "1000.000000001", "1000000.000001", "1000000000.001", "1000000000001", "PT16M40.000000001S"},
50 {+(1e9 + 1), "1.000000001", "1000.000001", "1000000.001", "1000000001", "PT1.000000001S"},
51 {+(1e9), "1", "1000", "1000000", "1000000000", "PT1S"},
52 {+(1e9 - 1), "0.999999999", "999.999999", "999999.999", "999999999", "PT0.999999999S"},
53 {+100000000, "0.1", "100", "100000", "100000000", "PT0.1S"},
54 {+120000000, "0.12", "120", "120000", "120000000", "PT0.12S"},
55 {+123000000, "0.123", "123", "123000", "123000000", "PT0.123S"},
56 {+123400000, "0.1234", "123.4", "123400", "123400000", "PT0.1234S"},
57 {+123450000, "0.12345", "123.45", "123450", "123450000", "PT0.12345S"},
58 {+123456000, "0.123456", "123.456", "123456", "123456000", "PT0.123456S"},
59 {+123456700, "0.1234567", "123.4567", "123456.7", "123456700", "PT0.1234567S"},
60 {+123456780, "0.12345678", "123.45678", "123456.78", "123456780", "PT0.12345678S"},
61 {+123456789, "0.123456789", "123.456789", "123456.789", "123456789", "PT0.123456789S"},
62 {+12345678, "0.012345678", "12.345678", "12345.678", "12345678", "PT0.012345678S"},
63 {+1234567, "0.001234567", "1.234567", "1234.567", "1234567", "PT0.001234567S"},
64 {+123456, "0.000123456", "0.123456", "123.456", "123456", "PT0.000123456S"},
65 {+12345, "0.000012345", "0.012345", "12.345", "12345", "PT0.000012345S"},
66 {+1234, "0.000001234", "0.001234", "1.234", "1234", "PT0.000001234S"},
67 {+123, "0.000000123", "0.000123", "0.123", "123", "PT0.000000123S"},
68 {+12, "0.000000012", "0.000012", "0.012", "12", "PT0.000000012S"},
69 {+1, "0.000000001", "0.000001", "0.001", "1", "PT0.000000001S"},
70 {0, "0", "0", "0", "0", "PT0S"},
71 {-1, "-0.000000001", "-0.000001", "-0.001", "-1", "-PT0.000000001S"},
72 {-12, "-0.000000012", "-0.000012", "-0.012", "-12", "-PT0.000000012S"},
73 {-123, "-0.000000123", "-0.000123", "-0.123", "-123", "-PT0.000000123S"},
74 {-1234, "-0.000001234", "-0.001234", "-1.234", "-1234", "-PT0.000001234S"},
75 {-12345, "-0.000012345", "-0.012345", "-12.345", "-12345", "-PT0.000012345S"},
76 {-123456, "-0.000123456", "-0.123456", "-123.456", "-123456", "-PT0.000123456S"},
77 {-1234567, "-0.001234567", "-1.234567", "-1234.567", "-1234567", "-PT0.001234567S"},
78 {-12345678, "-0.012345678", "-12.345678", "-12345.678", "-12345678", "-PT0.012345678S"},
79 {-123456789, "-0.123456789", "-123.456789", "-123456.789", "-123456789", "-PT0.123456789S"},
80 {-123456780, "-0.12345678", "-123.45678", "-123456.78", "-123456780", "-PT0.12345678S"},
81 {-123456700, "-0.1234567", "-123.4567", "-123456.7", "-123456700", "-PT0.1234567S"},
82 {-123456000, "-0.123456", "-123.456", "-123456", "-123456000", "-PT0.123456S"},
83 {-123450000, "-0.12345", "-123.45", "-123450", "-123450000", "-PT0.12345S"},
84 {-123400000, "-0.1234", "-123.4", "-123400", "-123400000", "-PT0.1234S"},
85 {-123000000, "-0.123", "-123", "-123000", "-123000000", "-PT0.123S"},
86 {-120000000, "-0.12", "-120", "-120000", "-120000000", "-PT0.12S"},
87 {-100000000, "-0.1", "-100", "-100000", "-100000000", "-PT0.1S"},
88 {-(1e9 - 1), "-0.999999999", "-999.999999", "-999999.999", "-999999999", "-PT0.999999999S"},
89 {-(1e9), "-1", "-1000", "-1000000", "-1000000000", "-PT1S"},
90 {-(1e9 + 1), "-1.000000001", "-1000.000001", "-1000000.001", "-1000000001", "-PT1.000000001S"},
91 {math.MinInt64, "-9223372036.854775808", "-9223372036854.775808", "-9223372036854775.808", "-9223372036854775808", "-PT2562047H47M16.854775808S"},
92 }
93
94 func TestFormatDuration(t *testing.T) {
95 var gotBuf []byte
96 check := func(td time.Duration, s string, base uint64) {
97 a := durationArshaler{td, base}
98 gotBuf, _ = a.appendMarshal(gotBuf[:0])
99 if string(gotBuf) != s {
100 t.Errorf("formatDuration(%d, %s) = %q, want %q", td, baseLabel(base), string(gotBuf), s)
101 }
102 if err := a.unmarshal(gotBuf); err != nil {
103 t.Errorf("parseDuration(%q, %s) error: %v", gotBuf, baseLabel(base), err)
104 }
105 if a.td != td {
106 t.Errorf("parseDuration(%q, %s) = %d, want %d", gotBuf, baseLabel(base), a.td, td)
107 }
108 }
109 for _, tt := range formatDurationTestdata {
110 check(tt.td, tt.base10Sec, 1e9)
111 check(tt.td, tt.base10Milli, 1e6)
112 check(tt.td, tt.base10Micro, 1e3)
113 check(tt.td, tt.base10Nano, 1e0)
114 check(tt.td, tt.iso8601, 8601)
115 }
116 }
117
118 var parseDurationTestdata = []struct {
119 in string
120 base uint64
121 want time.Duration
122 wantErr error
123 }{
124 {"0", 1e0, 0, nil},
125 {"0.", 1e0, 0, strconv.ErrSyntax},
126 {"0.0", 1e0, 0, nil},
127 {"0.00", 1e0, 0, nil},
128 {"00.0", 1e0, 0, strconv.ErrSyntax},
129 {"+0", 1e0, 0, strconv.ErrSyntax},
130 {"1e0", 1e0, 0, strconv.ErrSyntax},
131 {"1.000000000x", 1e9, 0, strconv.ErrSyntax},
132 {"1.000000x", 1e6, 0, strconv.ErrSyntax},
133 {"1.000x", 1e3, 0, strconv.ErrSyntax},
134 {"1.x", 1e0, 0, strconv.ErrSyntax},
135 {"1.0000000009", 1e9, +time.Second, nil},
136 {"1.0000009", 1e6, +time.Millisecond, nil},
137 {"1.0009", 1e3, +time.Microsecond, nil},
138 {"1.9", 1e0, +time.Nanosecond, nil},
139 {"-9223372036854775809", 1e0, 0, strconv.ErrRange},
140 {"9223372036854775.808", 1e3, 0, strconv.ErrRange},
141 {"-9223372036854.775809", 1e6, 0, strconv.ErrRange},
142 {"9223372036.854775808", 1e9, 0, strconv.ErrRange},
143 {"-1.9", 1e0, -time.Nanosecond, nil},
144 {"-1.0009", 1e3, -time.Microsecond, nil},
145 {"-1.0000009", 1e6, -time.Millisecond, nil},
146 {"-1.0000000009", 1e9, -time.Second, nil},
147 {"", 8601, 0, strconv.ErrSyntax},
148 {"P", 8601, 0, strconv.ErrSyntax},
149 {"PT", 8601, 0, strconv.ErrSyntax},
150 {"PT0", 8601, 0, strconv.ErrSyntax},
151 {"DT0S", 8601, 0, strconv.ErrSyntax},
152 {"PT0S", 8601, 0, nil},
153 {" PT0S", 8601, 0, strconv.ErrSyntax},
154 {"PT0S ", 8601, 0, strconv.ErrSyntax},
155 {"+PT0S", 8601, 0, nil},
156 {"PT0.M", 8601, 0, strconv.ErrSyntax},
157 {"PT0.S", 8601, 0, strconv.ErrSyntax},
158 {"PT0.0S", 8601, 0, nil},
159 {"PT0.0_0H", 8601, 0, strconv.ErrSyntax},
160 {"PT0.0_0M", 8601, 0, strconv.ErrSyntax},
161 {"PT0.0_0S", 8601, 0, strconv.ErrSyntax},
162 {"PT.0S", 8601, 0, strconv.ErrSyntax},
163 {"PT00.0S", 8601, 0, nil},
164 {"PT0S", 8601, 0, nil},
165 {"PT1,5S", 8601, time.Second + 500*time.Millisecond, nil},
166 {"PT1H", 8601, time.Hour, nil},
167 {"PT1H0S", 8601, time.Hour, nil},
168 {"PT0S", 8601, 0, nil},
169 {"PT00S", 8601, 0, nil},
170 {"PT000S", 8601, 0, nil},
171 {"PTS", 8601, 0, strconv.ErrSyntax},
172 {"PT1M", 8601, time.Minute, nil},
173 {"PT01M", 8601, time.Minute, nil},
174 {"PT001M", 8601, time.Minute, nil},
175 {"PT1H59S", 8601, time.Hour + 59*time.Second, nil},
176 {"PT123H4M56.789S", 8601, 123*time.Hour + 4*time.Minute + 56*time.Second + 789*time.Millisecond, nil},
177 {"-PT123H4M56.789S", 8601, -123*time.Hour - 4*time.Minute - 56*time.Second - 789*time.Millisecond, nil},
178 {"PT0H0S", 8601, 0, nil},
179 {"PT0H", 8601, 0, nil},
180 {"PT0M", 8601, 0, nil},
181 {"-PT0S", 8601, 0, nil},
182 {"PT1M0S", 8601, time.Minute, nil},
183 {"PT0H1M0S", 8601, time.Minute, nil},
184 {"PT01H02M03S", 8601, 1*time.Hour + 2*time.Minute + 3*time.Second, nil},
185 {"PT0,123S", 8601, 123 * time.Millisecond, nil},
186 {"PT1.S", 8601, 0, strconv.ErrSyntax},
187 {"PT1.000S", 8601, time.Second, nil},
188 {"PT0.025H", 8601, time.Minute + 30*time.Second, nil},
189 {"PT0.025H0M", 8601, 0, strconv.ErrSyntax},
190 {"PT1.5M", 8601, time.Minute + 30*time.Second, nil},
191 {"PT1.5M0S", 8601, 0, strconv.ErrSyntax},
192 {"PT60M", 8601, time.Hour, nil},
193 {"PT3600S", 8601, time.Hour, nil},
194 {"PT1H2M3.0S", 8601, 1*time.Hour + 2*time.Minute + 3*time.Second, nil},
195 {"pt1h2m3,0s", 8601, 1*time.Hour + 2*time.Minute + 3*time.Second, nil},
196 {"PT-1H-2M-3S", 8601, 0, strconv.ErrSyntax},
197 {"P1Y", 8601, time.Duration(daysPerYear * 24 * 60 * 60 * 1e9), errInaccurateDateUnits},
198 {"P1.0Y", 8601, 0, strconv.ErrSyntax},
199 {"P1M", 8601, time.Duration(daysPerYear / 12 * 24 * 60 * 60 * 1e9), errInaccurateDateUnits},
200 {"P1.0M", 8601, 0, strconv.ErrSyntax},
201 {"P1W", 8601, 7 * 24 * time.Hour, errInaccurateDateUnits},
202 {"P1.0W", 8601, 0, strconv.ErrSyntax},
203 {"P1D", 8601, 24 * time.Hour, errInaccurateDateUnits},
204 {"P1.0D", 8601, 0, strconv.ErrSyntax},
205 {"P1W1S", 8601, 0, strconv.ErrSyntax},
206 {"-P1Y2M3W4DT5H6M7.8S", 8601, -(time.Duration(14*daysPerYear/12*24*60*60*1e9) + time.Duration((3*7+4)*24*60*60*1e9) + 5*time.Hour + 6*time.Minute + 7*time.Second + 800*time.Millisecond), errInaccurateDateUnits},
207 {"-p1y2m3w4dt5h6m7.8s", 8601, -(time.Duration(14*daysPerYear/12*24*60*60*1e9) + time.Duration((3*7+4)*24*60*60*1e9) + 5*time.Hour + 6*time.Minute + 7*time.Second + 800*time.Millisecond), errInaccurateDateUnits},
208 {"P0Y0M0DT1H2M3S", 8601, 1*time.Hour + 2*time.Minute + 3*time.Second, errInaccurateDateUnits},
209 {"PT0.0000000001S", 8601, 0, nil},
210 {"PT0.0000000005S", 8601, 0, nil},
211 {"PT0.000000000500000000S", 8601, 0, nil},
212 {"PT0.000000000499999999S", 8601, 0, nil},
213 {"PT2562047H47M16.854775808S", 8601, 0, strconv.ErrRange},
214 {"-PT2562047H47M16.854775809S", 8601, 0, strconv.ErrRange},
215 {"PT9223372036.854775807S", 8601, math.MaxInt64, nil},
216 {"PT9223372036.854775808S", 8601, 0, strconv.ErrRange},
217 {"-PT9223372036.854775808S", 8601, math.MinInt64, nil},
218 {"-PT9223372036.854775809S", 8601, 0, strconv.ErrRange},
219 {"PT18446744073709551616S", 8601, 0, strconv.ErrRange},
220 {"PT5124096H", 8601, 0, strconv.ErrRange},
221 {"PT2562047.7880152155019444H", 8601, math.MaxInt64, nil},
222 {"PT2562047.7880152155022222H", 8601, 0, strconv.ErrRange},
223 {"PT5124094H94M33.709551616S", 8601, 0, strconv.ErrRange},
224 }
225
226 func TestParseDuration(t *testing.T) {
227 for _, tt := range parseDurationTestdata {
228 a := durationArshaler{base: tt.base}
229 switch err := a.unmarshal([]byte(tt.in)); {
230 case a.td != tt.want:
231 t.Errorf("parseDuration(%q, %s) = %v, want %v", tt.in, baseLabel(tt.base), a.td, tt.want)
232 case !errors.Is(err, tt.wantErr):
233 t.Errorf("parseDuration(%q, %s) error = %v, want %v", tt.in, baseLabel(tt.base), err, tt.wantErr)
234 }
235 }
236 }
237
238 func FuzzFormatDuration(f *testing.F) {
239 for _, tt := range formatDurationTestdata {
240 f.Add(int64(tt.td))
241 }
242 f.Fuzz(func(t *testing.T, want int64) {
243 var buf []byte
244 for _, base := range [...]uint64{1e0, 1e3, 1e6, 1e9, 8601} {
245 a := durationArshaler{td: time.Duration(want), base: base}
246 buf, _ = a.appendMarshal(buf[:0])
247 switch err := a.unmarshal(buf); {
248 case err != nil:
249 t.Fatalf("parseDuration(%q, %s) error: %v", buf, baseLabel(base), err)
250 case a.td != time.Duration(want):
251 t.Fatalf("parseDuration(%q, %s) = %v, want %v", buf, baseLabel(base), a.td, time.Duration(want))
252 }
253 }
254 })
255 }
256
257 func FuzzParseDuration(f *testing.F) {
258 for _, tt := range parseDurationTestdata {
259 f.Add([]byte(tt.in))
260 }
261 f.Fuzz(func(t *testing.T, in []byte) {
262 for _, base := range [...]uint64{1e0, 1e3, 1e6, 1e9, 8601} {
263 a := durationArshaler{base: base}
264 switch err := a.unmarshal(in); {
265 case err != nil: // nothing else to check
266 case base != 8601:
267 if n, err := jsonwire.ConsumeNumber(in); err != nil || n != len(in) {
268 t.Fatalf("parseDuration(%q) error is nil for invalid JSON number", in)
269 }
270 }
271 }
272 })
273 }
274
275 type formatTimeTestdataEntry struct {
276 ts time.Time
277 unixSec string
278 unixMilli string
279 unixMicro string
280 unixNano string
281 }
282
283 var formatTimeTestdata = func() []formatTimeTestdataEntry {
284 out := []formatTimeTestdataEntry{
285 {time.Unix(math.MaxInt64/int64(1e0), 1e9-1).UTC(), "9223372036854775807.999999999", "9223372036854775807999.999999", "9223372036854775807999999.999", "9223372036854775807999999999"},
286 {time.Unix(math.MaxInt64/int64(1e1), 1e9-1).UTC(), "922337203685477580.999999999", "922337203685477580999.999999", "922337203685477580999999.999", "922337203685477580999999999"},
287 {time.Unix(math.MaxInt64/int64(1e2), 1e9-1).UTC(), "92233720368547758.999999999", "92233720368547758999.999999", "92233720368547758999999.999", "92233720368547758999999999"},
288 {time.Unix(math.MinInt64, 1).UTC(), "-9223372036854775807.999999999", "-9223372036854775807999.999999", "-9223372036854775807999999.999", "-9223372036854775807999999999"},
289 {time.Unix(math.MinInt64, 0).UTC(), "-9223372036854775808", "-9223372036854775808000", "-9223372036854775808000000", "-9223372036854775808000000000"},
290 }
291 for _, tt := range formatDurationTestdata {
292 out = append(out, formatTimeTestdataEntry{time.Unix(0, int64(tt.td)).UTC(), tt.base10Sec, tt.base10Milli, tt.base10Micro, tt.base10Nano})
293 }
294 return out
295 }()
296
297 func TestFormatTime(t *testing.T) {
298 var gotBuf []byte
299 check := func(ts time.Time, s string, pow10 uint64) {
300 gotBuf = appendTimeUnix(gotBuf[:0], ts, pow10)
301 if string(gotBuf) != s {
302 t.Errorf("formatTime(time.Unix(%d, %d), %s) = %q, want %q", ts.Unix(), ts.Nanosecond(), baseLabel(pow10), string(gotBuf), s)
303 }
304 gotTS, err := parseTimeUnix(gotBuf, pow10)
305 if err != nil {
306 t.Errorf("parseTime(%q, %s) error: %v", gotBuf, baseLabel(pow10), err)
307 }
308 if !gotTS.Equal(ts) {
309 t.Errorf("parseTime(%q, %s) = time.Unix(%d, %d), want time.Unix(%d, %d)", gotBuf, baseLabel(pow10), gotTS.Unix(), gotTS.Nanosecond(), ts.Unix(), ts.Nanosecond())
310 }
311 }
312 for _, tt := range formatTimeTestdata {
313 check(tt.ts, tt.unixSec, 1e0)
314 check(tt.ts, tt.unixMilli, 1e3)
315 check(tt.ts, tt.unixMicro, 1e6)
316 check(tt.ts, tt.unixNano, 1e9)
317 }
318 }
319
320 var parseTimeTestdata = []struct {
321 in string
322 base uint64
323 want time.Time
324 wantErr error
325 }{
326 {"0", 1e0, time.Unix(0, 0).UTC(), nil},
327 {"0.", 1e0, time.Time{}, strconv.ErrSyntax},
328 {"0.0", 1e0, time.Unix(0, 0).UTC(), nil},
329 {"0.00", 1e0, time.Unix(0, 0).UTC(), nil},
330 {"00.0", 1e0, time.Time{}, strconv.ErrSyntax},
331 {"+0", 1e0, time.Time{}, strconv.ErrSyntax},
332 {"1e0", 1e0, time.Time{}, strconv.ErrSyntax},
333 {"1234567890123456789012345678901234567890", 1e0, time.Time{}, strconv.ErrRange},
334 {"9223372036854775808000.000000", 1e3, time.Time{}, strconv.ErrRange},
335 {"9223372036854775807999999.9999", 1e6, time.Unix(math.MaxInt64, 1e9-1).UTC(), nil},
336 {"9223372036854775807999999999.9", 1e9, time.Unix(math.MaxInt64, 1e9-1).UTC(), nil},
337 {"9223372036854775807.999999999x", 1e0, time.Time{}, strconv.ErrSyntax},
338 {"9223372036854775807000000000", 1e9, time.Unix(math.MaxInt64, 0).UTC(), nil},
339 {"-9223372036854775808", 1e0, time.Unix(math.MinInt64, 0).UTC(), nil},
340 {"-9223372036854775808000.000001", 1e3, time.Time{}, strconv.ErrRange},
341 {"-9223372036854775808000000.0001", 1e6, time.Unix(math.MinInt64, 0).UTC(), nil},
342 {"-9223372036854775808000000000.x", 1e9, time.Time{}, strconv.ErrSyntax},
343 {"-1234567890123456789012345678901234567890", 1e9, time.Time{}, strconv.ErrRange},
344 }
345
346 func TestParseTime(t *testing.T) {
347 for _, tt := range parseTimeTestdata {
348 a := timeArshaler{base: tt.base}
349 switch err := a.unmarshal([]byte(tt.in)); {
350 case a.tt != tt.want:
351 t.Errorf("parseTime(%q, %s) = time.Unix(%d, %d), want time.Unix(%d, %d)", tt.in, baseLabel(tt.base), a.tt.Unix(), a.tt.Nanosecond(), tt.want.Unix(), tt.want.Nanosecond())
352 case !errors.Is(err, tt.wantErr):
353 t.Errorf("parseTime(%q, %s) error = %v, want %v", tt.in, baseLabel(tt.base), err, tt.wantErr)
354 }
355 }
356 }
357
358 func FuzzFormatTime(f *testing.F) {
359 for _, tt := range formatTimeTestdata {
360 f.Add(tt.ts.Unix(), int64(tt.ts.Nanosecond()))
361 }
362 f.Fuzz(func(t *testing.T, wantSec, wantNano int64) {
363 want := time.Unix(wantSec, int64(uint64(wantNano)%1e9)).UTC()
364 var buf []byte
365 for _, base := range [...]uint64{1e0, 1e3, 1e6, 1e9} {
366 a := timeArshaler{tt: want, base: base}
367 buf, _ = a.appendMarshal(buf[:0])
368 switch err := a.unmarshal(buf); {
369 case err != nil:
370 t.Fatalf("parseTime(%q, %s) error: %v", buf, baseLabel(base), err)
371 case a.tt != want:
372 t.Fatalf("parseTime(%q, %s) = time.Unix(%d, %d), want time.Unix(%d, %d)", buf, baseLabel(base), a.tt.Unix(), a.tt.Nanosecond(), want.Unix(), want.Nanosecond())
373 }
374 }
375 })
376 }
377
378 func FuzzParseTime(f *testing.F) {
379 for _, tt := range parseTimeTestdata {
380 f.Add([]byte(tt.in))
381 }
382 f.Fuzz(func(t *testing.T, in []byte) {
383 for _, base := range [...]uint64{1e0, 1e3, 1e6, 1e9} {
384 a := timeArshaler{base: base}
385 if err := a.unmarshal(in); err == nil {
386 if n, err := jsonwire.ConsumeNumber(in); err != nil || n != len(in) {
387 t.Fatalf("parseTime(%q) error is nil for invalid JSON number", in)
388 }
389 }
390 }
391 })
392 }