Repository navigation
Expand file tree
/
Copy pathconn_http_errors_fuzz_test.go
More file actions
59 lines (53 loc) · 2.4 KB
/
Copy pathconn_http_errors_fuzz_test.go
File metadata and controls
59 lines (53 loc) · 2.4 KB
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
package clickhouse
import (
"testing"
)
// Both parsers consume whatever a server or proxy hands us. The invariants
// under arbitrary input: never panic, and never produce a typed Exception
// with a non-positive code.
func FuzzParseHTTPException(f *testing.F) {
f.Add("Code: 60. DB::Exception: Unknown table 'foo'. (UNKNOWN_TABLE) (version 25.1.5.31 (official build))", "", "")
f.Add("Code: 62. DB::Exception: Syntax error:\nmultiline. (SYNTAX_ERROR)", "62", "SYNTAX_ERROR")
f.Add("<html><body>502 Bad Gateway</body></html>", "", "")
f.Add("", "60", "UNKNOWN_TABLE")
f.Add("Code: 0. DB::Exception: zero", "0", "not-a-symbol")
f.Add("Code: 999999999999999999999. DB::Exception: overflow", "-1", "")
f.Add("Code: 210. DB::NetException: Connection refused", "abc", "a b")
f.Add("Code: 60.", "", "X")
f.Fuzz(func(t *testing.T, text, headerCode, headerName string) {
ex := parseHTTPException(text, headerCode, headerName)
if ex == nil {
return
}
if ex.Code <= 0 {
t.Errorf("parsed exception with non-positive code %d from text=%q header=%q", ex.Code, text, headerCode)
}
if ex.CodeName != "" && !exceptionCodeNameValueRe.MatchString(ex.CodeName) {
t.Errorf("parsed exception with malformed CodeName %q from text=%q headerName=%q", ex.CodeName, text, headerName)
}
if ex.Error() == "" {
t.Errorf("parsed exception with empty Error() from text=%q header=%q", text, headerCode)
}
})
}
func FuzzParseExceptionFromBytes(f *testing.F) {
f.Add([]byte("\r\n__exception__\r\n1234567890123456\r\nCode: 395. DB::Exception: boom. (FUNCTION_THROW_IF_VALUE_IS_NON_ZERO)\n42 1234567890123456\r\n__exception__\r\n"))
f.Add([]byte("\r\n__exception__\r\n1234567890123456\r\nDB::Exception: no code prefix"))
f.Add([]byte("__exception__"))
f.Add([]byte("__exception__\r\n\r\n\r\n__exception__"))
f.Add([]byte("no marker at all"))
f.Add([]byte{})
f.Add([]byte("\r\n__exception__\r\n\x00\x01\x02binary\xff\r\n__exception__\r\n"))
f.Add([]byte("__exception__\r\nCode: 395. DB::Exception: fake Code: 60. DB::Exception: injected. (X)\n"))
f.Add([]byte("__exception__\r\nCode: 395. DB::Exception: truncat\nexception__\nCode: 395. DB::Exception: full. (Y)\n"))
f.Fuzz(func(t *testing.T, data []byte) {
err := parseExceptionFromBytes(data)
if err == nil {
t.Errorf("parseExceptionFromBytes returned nil error for %q", data)
return
}
if err.Error() == "" {
t.Errorf("empty error message for %q", data)
}
})
}