2008-12-18 16:42:39 -07:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
2009-11-05 00:16:46 -07:00
|
|
|
"testing";
|
2008-12-18 16:42:39 -07:00
|
|
|
)
|
|
|
|
|
2009-05-08 11:14:55 -06:00
|
|
|
func isEqual(a, b IP) bool {
|
2008-12-19 18:11:54 -07:00
|
|
|
if a == nil && b == nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return true
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
|
|
|
if a == nil || b == nil || len(a) != len(b) {
|
2009-11-09 13:07:39 -07:00
|
|
|
return false
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
|
|
|
for i := 0; i < len(a); i++ {
|
|
|
|
if a[i] != b[i] {
|
2009-11-09 13:07:39 -07:00
|
|
|
return false
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
|
|
|
}
|
2009-11-05 00:16:46 -07:00
|
|
|
return true;
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
|
|
|
|
2009-01-16 15:16:31 -07:00
|
|
|
type parseIPTest struct {
|
2009-11-05 00:16:46 -07:00
|
|
|
in string;
|
|
|
|
out IP;
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
2009-11-05 00:16:46 -07:00
|
|
|
|
2009-03-03 09:39:12 -07:00
|
|
|
var parseiptests = []parseIPTest{
|
2009-05-08 11:14:55 -06:00
|
|
|
parseIPTest{"127.0.1.2", IPv4(127, 0, 1, 2)},
|
|
|
|
parseIPTest{"127.0.0.1", IPv4(127, 0, 0, 1)},
|
2009-03-03 09:39:12 -07:00
|
|
|
parseIPTest{"127.0.0.256", nil},
|
|
|
|
parseIPTest{"abc", nil},
|
2009-05-08 11:14:55 -06:00
|
|
|
parseIPTest{"::ffff:127.0.0.1", IPv4(127, 0, 0, 1)},
|
2009-03-03 09:39:12 -07:00
|
|
|
parseIPTest{"2001:4860:0:2001::68",
|
2009-11-05 00:16:46 -07:00
|
|
|
IP{0x20, 0x01, 0x48, 0x60, 0, 0, 0x20, 0x01,
|
|
|
|
0, 0, 0, 0, 0, 0, 0x00, 0x68,
|
|
|
|
},
|
|
|
|
},
|
2009-05-08 11:14:55 -06:00
|
|
|
parseIPTest{"::ffff:4a7d:1363", IPv4(74, 125, 19, 99)},
|
2009-03-03 09:39:12 -07:00
|
|
|
}
|
2008-12-18 16:42:39 -07:00
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func TestParseIP(t *testing.T) {
|
2008-12-18 16:42:39 -07:00
|
|
|
for i := 0; i < len(parseiptests); i++ {
|
|
|
|
tt := parseiptests[i];
|
2009-01-16 15:16:31 -07:00
|
|
|
if out := ParseIP(tt.in); !isEqual(out, tt.out) {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("ParseIP(%#q) = %v, want %v", tt.in, out, tt.out)
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|