Permalink
Cannot retrieve contributors at this time
97 lines (84 sloc)
3.2 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "bytes" | |
| "log" | |
| "net" | |
| "strconv" | |
| "testing" | |
| ) | |
| func TestClientAddr(t *testing.T) { | |
| // good tests | |
| for _, test := range []struct { | |
| input string | |
| expected net.IP | |
| }{ | |
| {"1.2.3.4", net.ParseIP("1.2.3.4")}, | |
| {"1:2::3:4", net.ParseIP("1:2::3:4")}, | |
| } { | |
| useraddr := clientAddr(test.input) | |
| host, port, err := net.SplitHostPort(useraddr) | |
| if err != nil { | |
| t.Errorf("clientAddr(%q) → SplitHostPort error %v", test.input, err) | |
| continue | |
| } | |
| if !test.expected.Equal(net.ParseIP(host)) { | |
| t.Errorf("clientAddr(%q) → host %q, not %v", test.input, host, test.expected) | |
| } | |
| portNo, err := strconv.Atoi(port) | |
| if err != nil { | |
| t.Errorf("clientAddr(%q) → port %q", test.input, port) | |
| continue | |
| } | |
| if portNo == 0 { | |
| t.Errorf("clientAddr(%q) → port %d", test.input, portNo) | |
| } | |
| } | |
| // bad tests | |
| for _, input := range []string{ | |
| "", | |
| "abc", | |
| "1.2.3.4.5", | |
| "[12::34]", | |
| } { | |
| useraddr := clientAddr(input) | |
| if useraddr != "" { | |
| t.Errorf("clientAddr(%q) → %q, not %q", input, useraddr, "") | |
| } | |
| } | |
| } | |
| func TestLogScrubber(t *testing.T) { | |
| var buff bytes.Buffer | |
| scrubber := &logScrubber{&buff} | |
| log.SetFlags(0) //remove all extra log output for test comparisons | |
| log.SetOutput(scrubber) | |
| log.Printf("%s", "http: TLS handshake error from 129.97.208.23:38310:") | |
| //Example IPv4 address that ended up in log | |
| if bytes.Compare(buff.Bytes(), []byte("http: TLS handshake error from X.X.X.X:38310:\n")) != 0 { | |
| t.Errorf("log scrubber didn't scrub IPv4 address. Output: %s", string(buff.Bytes())) | |
| } | |
| buff.Reset() | |
| log.Printf("%s", "http2: panic serving [2620:101:f000:780:9097:75b1:519f:dbb8]:58344: interface conversion: *http2.responseWriter is not http.Hijacker: missing method Hijack") | |
| //Example IPv6 address that ended up in log | |
| if bytes.Compare(buff.Bytes(), []byte("http2: panic serving [X:X:X:X:X:X:X:X]:58344: interface conversion: *http2.responseWriter is not http.Hijacker: missing method Hijack\n")) != 0 { | |
| t.Errorf("log scrubber didn't scrub IPv6 address. Output: %s", string(buff.Bytes())) | |
| } | |
| buff.Reset() | |
| //Testing IPv6 edge cases | |
| log.Printf("%s", "[1::]:58344") | |
| log.Printf("%s", "[1:2:3:4:5:6::8]:58344") | |
| log.Printf("%s", "[1::7:8]:58344") | |
| log.Printf("%s", "[::4:5:6:7:8]:58344") | |
| log.Printf("%s", "[::255.255.255.255]:58344") | |
| log.Printf("%s", "[::ffff:0:255.255.255.255]:58344") | |
| log.Printf("%s", "[2001:db8:3:4::192.0.2.33]:58344") | |
| if bytes.Compare(buff.Bytes(), []byte("[X:X:X:X:X:X:X:X]:58344\n[X:X:X:X:X:X:X:X]:58344\n[X:X:X:X:X:X:X:X]:58344\n[X:X:X:X:X:X:X:X]:58344\n[X:X:X:X:X:X:X:XX.X.X.X]:58344\n[X:X:X:X:X:X:X:XX.X.X.X]:58344\n[X:X:X:X:X:X:X:XX.X.X.X]:58344\n")) != 0 { | |
| t.Errorf("log scrubber didn't scrub IPv6 address. Output: %s", string(buff.Bytes())) | |
| } | |
| buff.Reset() | |
| //Make sure it doesn't scrub fingerprint | |
| log.Printf("%s", "a=fingerprint:sha-256 33:B6:FA:F6:94:CA:74:61:45:4A:D2:1F:2C:2F:75:8A:D9:EB:23:34:B2:30:E9:1B:2A:A6:A9:E0:44:72:CC:74") | |
| if bytes.Compare(buff.Bytes(), []byte("a=fingerprint:sha-256 33:B6:FA:F6:94:CA:74:61:45:4A:D2:1F:2C:2F:75:8A:D9:EB:23:34:B2:30:E9:1B:2A:A6:A9:E0:44:72:CC:74\n")) != 0 { | |
| t.Errorf("log scrubber scrubbed fingerprint: %s", string(buff.Bytes())) | |
| } | |
| buff.Reset() | |
| } |