]> Cypherpunks repositories - gostls13.git/commitdiff
net: make IPv6 String method standards compliant
authorMikio Hara <mikioh.mikioh@gmail.com>
Thu, 12 Aug 2010 07:03:01 +0000 (00:03 -0700)
committerRuss Cox <rsc@golang.org>
Thu, 12 Aug 2010 07:03:01 +0000 (00:03 -0700)
draft-ietf-6man-text-addr-representation-07 will introduce
a canonical textual representation format for IPv6 address.

R=rsc
CC=golang-dev
https://golang.org/cl/1856047

src/pkg/net/ip.go
src/pkg/net/ip_test.go

index bd0c75de69697ed24ea50771c80805397a732414..e82224a28364274e55fb51b410481b430053ac18 100644 (file)
@@ -222,6 +222,11 @@ func (ip IP) String() string {
                        e1 = j
                }
        }
+       // The symbol "::" MUST NOT be used to shorten just one 16 bit 0 field.
+       if e1-e0 <= 2 {
+               e0 = -1
+               e1 = -1
+       }
 
        // Print with possible :: in place of run of zeros
        var s string
index 0ea1d926057a7b07b1dc2d5ea4a669fbde0ed823..9f30f14767ba3dea253c028a3a77b023021ac19b 100644 (file)
@@ -50,3 +50,46 @@ func TestParseIP(t *testing.T) {
                }
        }
 }
+
+type ipStringTest struct {
+       in  IP
+       out string
+}
+
+var ipstringtests = []ipStringTest{
+       // cf. draft-ietf-6man-text-addr-representation-07
+       // (A Recommendation for IPv6 Address Text Representation)
+       ipStringTest{IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0,
+               0, 0, 0x1, 0x23, 0, 0x12, 0, 0x1},
+               "2001:db8::123:12:1"},
+       ipStringTest{IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0,
+               0, 0, 0, 0, 0, 0, 0, 0x1},
+               "2001:db8::1"},
+       ipStringTest{IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0x1,
+               0, 0, 0, 0x1, 0, 0, 0, 0x1},
+               "2001:db8:0:1:0:1:0:1"},
+       ipStringTest{IP{0x20, 0x1, 0xd, 0xb8, 0, 0x1, 0, 0,
+               0, 0x1, 0, 0, 0, 0x1, 0, 0},
+               "2001:db8:1:0:1:0:1:0"},
+       ipStringTest{IP{0x20, 0x1, 0, 0, 0, 0, 0, 0,
+               0, 0x1, 0, 0, 0, 0, 0, 0x1},
+               "2001::1:0:0:1"},
+       ipStringTest{IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0,
+               0, 0x1, 0, 0, 0, 0, 0, 0},
+               "2001:db8:0:0:1::"},
+       ipStringTest{IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0,
+               0, 0x1, 0, 0, 0, 0, 0, 0x1},
+               "2001:db8::1:0:0:1"},
+       ipStringTest{IP{0x20, 0x1, 0xD, 0xB8, 0, 0, 0, 0,
+               0, 0xA, 0, 0xB, 0, 0xC, 0, 0xD},
+               "2001:db8::a:b:c:d"},
+}
+
+func TestIPString(t *testing.T) {
+       for i := 0; i < len(ipstringtests); i++ {
+               tt := ipstringtests[i]
+               if out := tt.in.String(); out != tt.out {
+                       t.Errorf("IP.String(%v) = %#q, want %#q", tt.in, out, tt.out)
+               }
+       }
+}