Merge from mainline (167278:168000).
[official-gcc/graphite-test-results.git] / libgo / go / net / ip_test.go
blobe29c3021da9e82942869e5d9d028929480407e84
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 package net
7 import (
8 "testing"
11 func isEqual(a, b IP) bool {
12 if a == nil && b == nil {
13 return true
15 if a == nil || b == nil || len(a) != len(b) {
16 return false
18 for i := 0; i < len(a); i++ {
19 if a[i] != b[i] {
20 return false
23 return true
26 type parseIPTest struct {
27 in string
28 out IP
31 var parseiptests = []parseIPTest{
32 {"127.0.1.2", IPv4(127, 0, 1, 2)},
33 {"127.0.0.1", IPv4(127, 0, 0, 1)},
34 {"127.0.0.256", nil},
35 {"abc", nil},
36 {"::ffff:127.0.0.1", IPv4(127, 0, 0, 1)},
37 {"2001:4860:0:2001::68",
38 IP{0x20, 0x01, 0x48, 0x60, 0, 0, 0x20, 0x01,
39 0, 0, 0, 0, 0, 0, 0x00, 0x68,
42 {"::ffff:4a7d:1363", IPv4(74, 125, 19, 99)},
45 func TestParseIP(t *testing.T) {
46 for i := 0; i < len(parseiptests); i++ {
47 tt := parseiptests[i]
48 if out := ParseIP(tt.in); !isEqual(out, tt.out) {
49 t.Errorf("ParseIP(%#q) = %v, want %v", tt.in, out, tt.out)
54 type ipStringTest struct {
55 in IP
56 out string
59 var ipstringtests = []ipStringTest{
60 // cf. RFC 5952 (A Recommendation for IPv6 Address Text Representation)
61 {IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0,
62 0, 0, 0x1, 0x23, 0, 0x12, 0, 0x1},
63 "2001:db8::123:12:1"},
64 {IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0,
65 0, 0, 0, 0, 0, 0, 0, 0x1},
66 "2001:db8::1"},
67 {IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0x1,
68 0, 0, 0, 0x1, 0, 0, 0, 0x1},
69 "2001:db8:0:1:0:1:0:1"},
70 {IP{0x20, 0x1, 0xd, 0xb8, 0, 0x1, 0, 0,
71 0, 0x1, 0, 0, 0, 0x1, 0, 0},
72 "2001:db8:1:0:1:0:1:0"},
73 {IP{0x20, 0x1, 0, 0, 0, 0, 0, 0,
74 0, 0x1, 0, 0, 0, 0, 0, 0x1},
75 "2001::1:0:0:1"},
76 {IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0,
77 0, 0x1, 0, 0, 0, 0, 0, 0},
78 "2001:db8:0:0:1::"},
79 {IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0,
80 0, 0x1, 0, 0, 0, 0, 0, 0x1},
81 "2001:db8::1:0:0:1"},
82 {IP{0x20, 0x1, 0xD, 0xB8, 0, 0, 0, 0,
83 0, 0xA, 0, 0xB, 0, 0xC, 0, 0xD},
84 "2001:db8::a:b:c:d"},
87 func TestIPString(t *testing.T) {
88 for i := 0; i < len(ipstringtests); i++ {
89 tt := ipstringtests[i]
90 if out := tt.in.String(); out != tt.out {
91 t.Errorf("IP.String(%v) = %#q, want %#q", tt.in, out, tt.out)