diff options
author | Aliaksandr Valialkin <valyala@gmail.com> | 2015-12-11 13:11:15 +0200 |
---|---|---|
committer | Aliaksandr Valialkin <valyala@gmail.com> | 2015-12-11 13:11:15 +0200 |
commit | 3222ab8c8c892740571936ad9e9c3edb25796664 (patch) | |
tree | 686cf15ea40627a35485e151ea77e3703a9893f6 /bytesconv_test.go | |
parent | Refer to PathRewriteFunc as net/http.StripPrefix substitution (diff) | |
download | fasthttp-3222ab8c8c892740571936ad9e9c3edb25796664.tar.gz fasthttp-3222ab8c8c892740571936ad9e9c3edb25796664.tar.bz2 fasthttp-3222ab8c8c892740571936ad9e9c3edb25796664.zip |
Added AppendIPv4 and ParseIPv4 helper functions
Diffstat (limited to 'bytesconv_test.go')
-rw-r--r-- | bytesconv_test.go | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/bytesconv_test.go b/bytesconv_test.go index 89c735e..24458db 100644 --- a/bytesconv_test.go +++ b/bytesconv_test.go @@ -4,10 +4,70 @@ import ( "bufio" "bytes" "fmt" + "net" "testing" "time" ) +func TestParseIPv4(t *testing.T) { + testParseIPv4(t, "0.0.0.0", true) + testParseIPv4(t, "255.255.255.255", true) + testParseIPv4(t, "123.45.67.89", true) + + // ipv6 shouldn't work + testParseIPv4(t, "2001:4860:0:2001::68", false) + + // invalid ip + testParseIPv4(t, "foobar", false) + testParseIPv4(t, "1.2.3", false) + testParseIPv4(t, "123.456.789.11", false) +} + +func testParseIPv4(t *testing.T, ipStr string, isValid bool) { + ip, err := ParseIPv4(nil, []byte(ipStr)) + if isValid { + if err != nil { + t.Fatalf("unexpected error when parsing ip %q: %s", ipStr, err) + } + s := string(AppendIPv4(nil, ip)) + if s != ipStr { + t.Fatalf("unexpected ip parsed %q. Expecting %q", s, ipStr) + } + } else { + if err == nil { + t.Fatalf("expecting error when parsing ip %q", ipStr) + } + } +} + +func TestAppendIPv4(t *testing.T) { + testAppendIPv4(t, "0.0.0.0", true) + testAppendIPv4(t, "127.0.0.1", true) + testAppendIPv4(t, "8.8.8.8", true) + testAppendIPv4(t, "123.45.67.89", true) + + // ipv6 shouldn't work + testAppendIPv4(t, "2001:4860:0:2001::68", false) +} + +func testAppendIPv4(t *testing.T, ipStr string, isValid bool) { + ip := net.ParseIP(ipStr) + if ip == nil { + t.Fatalf("cannot parse ip %q", ipStr) + } + s := string(AppendIPv4(nil, ip)) + if isValid { + if s != ipStr { + t.Fatalf("unepxected ip %q. Expecting %q", s, ipStr) + } + } else { + ipStr = "non-v4 ip passed to AppendIPv4" + if s != ipStr { + t.Fatalf("unexpected ip %q. Expecting %q", s, ipStr) + } + } +} + func testAppendUint(t *testing.T, n int) { expectedS := fmt.Sprintf("%d", n) s := AppendUint(nil, n) |