| 1 |
package utils |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"fmt" |
| 6 |
"net/http" |
| 7 |
"net/netip" |
| 8 |
"net/url" |
| 9 |
"strings" |
| 10 |
"testing" |
| 11 |
"time" |
| 12 |
"unicode/utf8" |
| 13 |
|
| 14 |
"github.com/stretchr/testify/require" |
| 15 |
) |
| 16 |
|
| 17 |
// Differential edge-case suites for the helpers that promise byte-for-byte |
| 18 |
// stdlib parity. Rather than hand-picking expectations, these enumerate |
| 19 |
// structured input spaces — positional mutations, word-boundary offsets, |
| 20 |
// malformed UTF-8, calendar boundaries — and require agreement with the |
| 21 |
// stdlib reference on every input. |
| 22 |
|
| 23 |
func Test_ParseHTTPDate_AllMonthsAndWeekdays(t *testing.T) { |
| 24 |
t.Parallel() |
| 25 |
// Twelve months exercise every httpMonthNum case; seven consecutive |
| 26 |
// days exercise every weekday name. |
| 27 |
for month := time.January; month <= time.December; month++ { |
| 28 |
tm := time.Date(2021, month, 15, 1, 2, 3, 0, time.UTC) |
| 29 |
got, err := ParseHTTPDate(FormatHTTPDate(tm)) |
| 30 |
require.NoError(t, err, "month %v", month) |
| 31 |
require.True(t, got.Equal(tm), "month %v", month) |
| 32 |
} |
| 33 |
for day := 1; day <= 7; day++ { |
| 34 |
tm := time.Date(2021, time.August, day, 23, 59, 59, 0, time.UTC) |
| 35 |
got, err := ParseHTTPDate(FormatHTTPDate(tm)) |
| 36 |
require.NoError(t, err, "day %d", day) |
| 37 |
require.True(t, got.Equal(tm), "day %d", day) |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
func Test_ParseHTTPDate_PositionalMutations(t *testing.T) { |
| 42 |
t.Parallel() |
| 43 |
base := "Sun, 06 Nov 1994 08:49:37 GMT" |
| 44 |
replacements := []byte{' ', ':', ',', '-', '.', '0', '5', '9', 'A', 'M', 'a', 'z', 0x00, 0x7F, 0xFF} |
| 45 |
for pos := range len(base) { |
| 46 |
for _, r := range replacements { |
| 47 |
mutated := base[:pos] + string(r) + base[pos+1:] |
| 48 |
assertHTTPDateParity(t, mutated) |
| 49 |
} |
| 50 |
} |
| 51 |
// Length mutations: truncations and extensions. |
| 52 |
for cut := range len(base) { |
| 53 |
assertHTTPDateParity(t, base[:cut]) |
| 54 |
} |
| 55 |
assertHTTPDateParity(t, base+" ") |
| 56 |
assertHTTPDateParity(t, base+"X") |
| 57 |
assertHTTPDateParity(t, " "+base) |
| 58 |
assertHTTPDateParity(t, "\t"+base+"\r\n") |
| 59 |
} |
| 60 |
|
| 61 |
func Test_ParseHTTPDate_CalendarBoundaries(t *testing.T) { |
| 62 |
t.Parallel() |
| 63 |
months := []string{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"} |
| 64 |
years := []int{0, 4, 100, 400, 1900, 1996, 2000, 2020, 2023, 9999} |
| 65 |
days := []int{0, 1, 28, 29, 30, 31, 32, 99} |
| 66 |
for _, year := range years { |
| 67 |
for _, month := range months { |
| 68 |
for _, day := range days { |
| 69 |
in := fmt.Sprintf("Fri, %02d %s %04d 12:00:00 GMT", day, month, year) |
| 70 |
assertHTTPDateParity(t, in) |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
// Clock-field boundaries. |
| 75 |
for _, clock := range []string{"00:00:00", "23:59:59", "24:00:00", "23:60:00", "23:59:60", "99:99:99"} { |
| 76 |
assertHTTPDateParity(t, "Sat, 15 Jul 2023 "+clock+" GMT") |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
// assertHTTPDateParity requires ParseHTTPDate to agree with the |
| 81 |
// net/http.ParseTime reference on outcome, instant, and location. |
| 82 |
func assertHTTPDateParity(t *testing.T, in string) { |
| 83 |
t.Helper() |
| 84 |
want, wantErr := refParseHTTPDate(in) |
| 85 |
got, err := ParseHTTPDate(in) |
| 86 |
if wantErr != nil { |
| 87 |
require.Error(t, err, "input %q", in) |
| 88 |
return |
| 89 |
} |
| 90 |
require.NoError(t, err, "input %q", in) |
| 91 |
require.True(t, got.Equal(want), "input %q: got %v, want %v", in, got, want) |
| 92 |
require.Equal(t, want.Location().String(), got.Location().String(), "input %q", in) |
| 93 |
} |
| 94 |
|
| 95 |
func Test_AppendUnescape_LongAndBoundaryInputs(t *testing.T) { |
| 96 |
t.Parallel() |
| 97 |
long := strings.Repeat("abcdefgh", 8) // 64B, engages the vectorized scans |
| 98 |
inputs := []string{ |
| 99 |
long, |
| 100 |
long + "%", |
| 101 |
long + "%4", |
| 102 |
long + "%41", |
| 103 |
long + "%zz", |
| 104 |
"%41" + long, |
| 105 |
long[:31] + "%" + long[:1], // '%' exactly at index 31 |
| 106 |
strings.Repeat("+", 64), |
| 107 |
strings.Repeat("%25", 32), |
| 108 |
strings.Repeat("a+", 32), |
| 109 |
"%", |
| 110 |
"+", |
| 111 |
"%%%", |
| 112 |
"%+1", |
| 113 |
"+%41+", |
| 114 |
} |
| 115 |
for _, in := range inputs { |
| 116 |
assertUnescapeParity(t, in) |
| 117 |
} |
| 118 |
|
| 119 |
// A late error must still return the original dst content untouched. |
| 120 |
got, err := AppendQueryUnescape([]byte("keep"), long+"%zz") |
| 121 |
require.Error(t, err) |
| 122 |
require.Equal(t, "keep", string(got)) |
| 123 |
gotPath, err := AppendPathUnescape([]byte("keep"), long+"%") |
| 124 |
require.Error(t, err) |
| 125 |
require.Equal(t, "keep", string(gotPath)) |
| 126 |
} |
| 127 |
|
| 128 |
func Test_AppendUnescape_EveryEscapeByte(t *testing.T) { |
| 129 |
t.Parallel() |
| 130 |
// Every byte in both positions of an escape pair, plus that byte alone |
| 131 |
// after '%', pinned to net/url's accept set and error values. |
| 132 |
for i := range 256 { |
| 133 |
c := string([]byte{byte(i)}) |
| 134 |
assertUnescapeParity(t, "%"+c+"0ab") |
| 135 |
assertUnescapeParity(t, "%4"+c+"ab") |
| 136 |
assertUnescapeParity(t, "ab%"+c) |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
// assertUnescapeParity requires the two unescape helpers to agree with |
| 141 |
// net/url on output and exact error values, for both instantiations. |
| 142 |
func assertUnescapeParity(t *testing.T, in string) { |
| 143 |
t.Helper() |
| 144 |
wantQ, wantQErr := url.QueryUnescape(in) |
| 145 |
gotQ, errQ := AppendQueryUnescape(nil, in) |
| 146 |
gotQB, errQB := AppendQueryUnescape([]byte(nil), []byte(in)) |
| 147 |
wantP, wantPErr := url.PathUnescape(in) |
| 148 |
gotP, errP := AppendPathUnescape(nil, in) |
| 149 |
if wantQErr != nil { |
| 150 |
require.Equal(t, wantQErr, errQ, "query input %q", in) |
| 151 |
require.Equal(t, wantQErr, errQB, "query input %q", in) |
| 152 |
} else { |
| 153 |
require.NoError(t, errQ, "query input %q", in) |
| 154 |
require.Equal(t, wantQ, string(gotQ), "query input %q", in) |
| 155 |
require.Equal(t, wantQ, string(gotQB), "query input %q", in) |
| 156 |
} |
| 157 |
if wantPErr != nil { |
| 158 |
require.Equal(t, wantPErr, errP, "path input %q", in) |
| 159 |
} else { |
| 160 |
require.NoError(t, errP, "path input %q", in) |
| 161 |
require.Equal(t, wantP, string(gotP), "path input %q", in) |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
func Test_AppendEscape_EdgeCases(t *testing.T) { |
| 166 |
t.Parallel() |
| 167 |
all := make([]byte, 256) |
| 168 |
for i := range all { |
| 169 |
all[i] = byte(i) |
| 170 |
} |
| 171 |
inputs := []string{ |
| 172 |
string(all), |
| 173 |
strings.Repeat(" ", 64), |
| 174 |
strings.Repeat("%", 64), |
| 175 |
strings.Repeat("a ", 32), |
| 176 |
strings.Repeat("unreserved-only.~_", 16), |
| 177 |
"ends with unsafe/", |
| 178 |
"/starts with unsafe", |
| 179 |
} |
| 180 |
for _, in := range inputs { |
| 181 |
require.Equal(t, url.QueryEscape(in), string(AppendQueryEscape(nil, in)), "input %q", in) |
| 182 |
require.Equal(t, url.PathEscape(in), string(AppendPathEscape(nil, in)), "input %q", in) |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
func Test_AppendJSONString_BoundaryOffsets(t *testing.T) { |
| 187 |
t.Parallel() |
| 188 |
// Each special byte or sequence at every offset around the SWAR word |
| 189 |
// width, so escapes land in every lane and on both sides of the |
| 190 |
// word-loop/tail split. |
| 191 |
specials := []string{ |
| 192 |
"\"", "\\", "\n", "\r", "\t", "\b", "\f", "\x00", "\x1f", "\x7f", |
| 193 |
"<", ">", "&", "\xff", "\xc3\xa9", "\u2028", "\u2029", "\xf0\x9f\x98\x80", "\xc3", |
| 194 |
} |
| 195 |
for _, sp := range specials { |
| 196 |
for pad := range 18 { |
| 197 |
in := strings.Repeat("a", pad) + sp + strings.Repeat("b", 17-pad) |
| 198 |
assertJSONStringParity(t, in) |
| 199 |
// And with the special at the very end, where the tail paths run. |
| 200 |
assertJSONStringParity(t, strings.Repeat("a", pad)+sp) |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
func Test_AppendJSONString_UTF8EdgeCases(t *testing.T) { |
| 206 |
t.Parallel() |
| 207 |
inputs := []string{ |
| 208 |
"\xc0\x80", // overlong NUL |
| 209 |
"\xc1\xbf", // overlong |
| 210 |
"\xe0\x80\xaf", // overlong 3-byte |
| 211 |
"\xed\xa0\x80", // UTF-16 surrogate half |
| 212 |
"\xed\xbf\xbf", // UTF-16 surrogate half |
| 213 |
"\xf4\x8f\xbf\xbf", // U+10FFFF, highest valid rune |
| 214 |
"\xf4\x90\x80\x80", // just above U+10FFFF |
| 215 |
"\xf8\x88\x80\x80\x80", // 5-byte form |
| 216 |
"\x80", // lone continuation |
| 217 |
"\xbf\xbf", // lone continuations |
| 218 |
"\xc3", // truncated 2-byte |
| 219 |
"\xe2\x80", // truncated 3-byte (prefix of U+2028) |
| 220 |
"\xf0\x9f\x98", // truncated 4-byte |
| 221 |
"\xef\xbb\xbfBOM", // BOM prefix |
| 222 |
"ok\xc3\xa9\xc3", // valid then truncated |
| 223 |
"\xe2\x80\xa8\xe2\x80\xa9\xe2\x80\xaa", // U+2028, U+2029, then U+202A (unescaped) |
| 224 |
strings.Repeat("\xff", 32), |
| 225 |
strings.Repeat("\xc3\xa9", 16), |
| 226 |
} |
| 227 |
for _, in := range inputs { |
| 228 |
assertJSONStringParity(t, in) |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
// assertJSONStringParity requires AppendJSONString to agree with |
| 233 |
// encoding/json.Marshal byte for byte, for both instantiations. Go 1.27 made |
| 234 |
// json/v2 the default, which writes a raw U+FFFD where v1 escaped it, so for |
| 235 |
// invalid UTF-8 the encodings differ and only the decoded string can agree. |
| 236 |
func assertJSONStringParity(t *testing.T, in string) { |
| 237 |
t.Helper() |
| 238 |
want, err := json.Marshal(in) |
| 239 |
require.NoError(t, err) |
| 240 |
for _, got := range [][]byte{AppendJSONString(nil, in), AppendJSONString(nil, []byte(in))} { |
| 241 |
if utf8.ValidString(in) { |
| 242 |
require.Equal(t, string(want), string(got), "input %q", in) |
| 243 |
continue |
| 244 |
} |
| 245 |
var gotStr, wantStr string |
| 246 |
require.NoError(t, json.Unmarshal(got, &gotStr), "input %q", in) |
| 247 |
require.NoError(t, json.Unmarshal(want, &wantStr), "input %q", in) |
| 248 |
require.Equal(t, wantStr, gotStr, "input %q", in) |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
func Test_ParseIP_PositionalMutations(t *testing.T) { |
| 253 |
t.Parallel() |
| 254 |
bases := []string{ |
| 255 |
"203.0.113.195", |
| 256 |
"::1", |
| 257 |
"2001:db8::8a2e:370:7334", |
| 258 |
"1:2:3:4:5:6:7:8", |
| 259 |
"::ffff:1.2.3.4", |
| 260 |
"1:2:3:4:5:6:1.2.3.4", |
| 261 |
"fe80::1%eth0", |
| 262 |
} |
| 263 |
replacements := []byte{'.', ':', '%', '0', '9', 'a', 'f', 'g', 'G', '-', ' ', 0x00, 0xFF} |
| 264 |
for _, base := range bases { |
| 265 |
for pos := range len(base) { |
| 266 |
for _, r := range replacements { |
| 267 |
assertParseIPParity(t, base[:pos]+string(r)+base[pos+1:]) |
| 268 |
} |
| 269 |
assertParseIPParity(t, base[:pos]) // every truncation |
| 270 |
} |
| 271 |
assertParseIPParity(t, base+":") |
| 272 |
assertParseIPParity(t, base+".") |
| 273 |
assertParseIPParity(t, base+"0") |
| 274 |
assertParseIPParity(t, base+"%") |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
func Test_ParseIP_MoreEdgeCases(t *testing.T) { |
| 279 |
t.Parallel() |
| 280 |
inputs := []string{ |
| 281 |
"1;2", // field followed by a non-separator byte |
| 282 |
"1:2;3", // same, after a valid group |
| 283 |
"fe80::1%%", // zone containing '%' |
| 284 |
"1::2%", // empty zone after valid address |
| 285 |
"::%25eth0", |
| 286 |
"1:2:3:4:5:6:7:8%eth0", // full address with zone |
| 287 |
"0.0.0.256", |
| 288 |
"0.0.0.00", |
| 289 |
"255.255.255.2555", |
| 290 |
"::0.0.0.0", |
| 291 |
"0::00:0:00:0.0.0.0", |
| 292 |
"1:2:3:4:5:1.2.3.4", // v4 tail too early without ellipsis |
| 293 |
"1:2:3:4:5:6:7:1.2.3.4", // v4 tail too late |
| 294 |
"::ffff:016.1.2.3", |
| 295 |
"ABCD:EF01:2345:6789:abcd:ef01:2345:6789", |
| 296 |
"0000:0000:0000:0000:0000:0000:0000:0000", |
| 297 |
"00000::", |
| 298 |
"::00000", |
| 299 |
"2:2:2:2:2:2:2:2:", |
| 300 |
"\x00::", |
| 301 |
"1.2.3.4%eth0", // zones are IPv6-only |
| 302 |
} |
| 303 |
for _, in := range inputs { |
| 304 |
assertParseIPParity(t, in) |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
// assertParseIPParity requires ParseIPv4/ParseIPv6 to accept exactly the |
| 309 |
// per-family netip.ParseAddr inputs and produce identical addresses, for |
| 310 |
// both instantiations. |
| 311 |
func assertParseIPParity(t *testing.T, in string) { |
| 312 |
t.Helper() |
| 313 |
want, wantErr := netip.ParseAddr(in) |
| 314 |
hasColon := strings.ContainsRune(in, ':') |
| 315 |
|
| 316 |
got4, ok4 := ParseIPv4(in) |
| 317 |
_, ok4B := ParseIPv4([]byte(in)) |
| 318 |
require.Equal(t, wantErr == nil && !hasColon, ok4, "ParseIPv4 input %q (netip err: %v)", in, wantErr) |
| 319 |
require.Equal(t, ok4, ok4B, "ParseIPv4 bytes input %q", in) |
| 320 |
if ok4 { |
| 321 |
require.Equal(t, want, got4, "ParseIPv4 input %q", in) |
| 322 |
} else { |
| 323 |
require.Equal(t, netip.Addr{}, got4, "ParseIPv4 input %q", in) |
| 324 |
} |
| 325 |
|
| 326 |
got6, ok6 := ParseIPv6(in) |
| 327 |
_, ok6B := ParseIPv6([]byte(in)) |
| 328 |
require.Equal(t, wantErr == nil && hasColon, ok6, "ParseIPv6 input %q (netip err: %v)", in, wantErr) |
| 329 |
require.Equal(t, ok6, ok6B, "ParseIPv6 bytes input %q", in) |
| 330 |
if ok6 { |
| 331 |
require.Equal(t, want, got6, "ParseIPv6 input %q", in) |
| 332 |
} else { |
| 333 |
require.Equal(t, netip.Addr{}, got6, "ParseIPv6 input %q", in) |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
func Test_CanonicalHeaderKey_PositionalMutations(t *testing.T) { |
| 338 |
t.Parallel() |
| 339 |
bases := []string{"content-type", "X-Request-Id", "a", "-", "--", "a-", "-a", "A--b"} |
| 340 |
for _, base := range bases { |
| 341 |
for pos := range len(base) { |
| 342 |
for c := range 256 { |
| 343 |
in := base[:pos] + string(byte(c)) + base[pos+1:] |
| 344 |
want := http.CanonicalHeaderKey(in) |
| 345 |
require.Equal(t, want, CanonicalHeaderKey(in), "input %q", in) |
| 346 |
require.Equal(t, want, string(CanonicalHeaderKey([]byte(in))), "input %q", in) |
| 347 |
} |
| 348 |
} |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
func Test_CanonicalHeaderKey_InputImmutability(t *testing.T) { |
| 353 |
t.Parallel() |
| 354 |
// The rewrite path must copy, never mutate the caller's bytes. |
| 355 |
in := []byte("content-TYPE") |
| 356 |
out := CanonicalHeaderKey(in) |
| 357 |
require.Equal(t, "content-TYPE", string(in)) |
| 358 |
require.Equal(t, "Content-Type", string(out)) |
| 359 |
} |