PageRenderTime 40ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/libgo/go/net/iprawsock_test.go

https://gitlab.com/4144/gcc
Go | 193 lines | 163 code | 21 blank | 9 comment | 43 complexity | 7cd1b291592446506c82c1c52d97bc84 MD5 | raw file
  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.
  4. // +build !js
  5. package net
  6. import (
  7. "reflect"
  8. "testing"
  9. )
  10. // The full stack test cases for IPConn have been moved to the
  11. // following:
  12. // golang.org/x/net/ipv4
  13. // golang.org/x/net/ipv6
  14. // golang.org/x/net/icmp
  15. type resolveIPAddrTest struct {
  16. network string
  17. litAddrOrName string
  18. addr *IPAddr
  19. err error
  20. }
  21. var resolveIPAddrTests = []resolveIPAddrTest{
  22. {"ip", "127.0.0.1", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil},
  23. {"ip4", "127.0.0.1", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil},
  24. {"ip4:icmp", "127.0.0.1", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil},
  25. {"ip", "::1", &IPAddr{IP: ParseIP("::1")}, nil},
  26. {"ip6", "::1", &IPAddr{IP: ParseIP("::1")}, nil},
  27. {"ip6:ipv6-icmp", "::1", &IPAddr{IP: ParseIP("::1")}, nil},
  28. {"ip6:IPv6-ICMP", "::1", &IPAddr{IP: ParseIP("::1")}, nil},
  29. {"ip", "::1%en0", &IPAddr{IP: ParseIP("::1"), Zone: "en0"}, nil},
  30. {"ip6", "::1%911", &IPAddr{IP: ParseIP("::1"), Zone: "911"}, nil},
  31. {"", "127.0.0.1", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil}, // Go 1.0 behavior
  32. {"", "::1", &IPAddr{IP: ParseIP("::1")}, nil}, // Go 1.0 behavior
  33. {"ip4:icmp", "", &IPAddr{}, nil},
  34. {"l2tp", "127.0.0.1", nil, UnknownNetworkError("l2tp")},
  35. {"l2tp:gre", "127.0.0.1", nil, UnknownNetworkError("l2tp:gre")},
  36. {"tcp", "1.2.3.4:123", nil, UnknownNetworkError("tcp")},
  37. {"ip4", "2001:db8::1", nil, &AddrError{Err: errNoSuitableAddress.Error(), Addr: "2001:db8::1"}},
  38. {"ip4:icmp", "2001:db8::1", nil, &AddrError{Err: errNoSuitableAddress.Error(), Addr: "2001:db8::1"}},
  39. {"ip6", "127.0.0.1", nil, &AddrError{Err: errNoSuitableAddress.Error(), Addr: "127.0.0.1"}},
  40. {"ip6", "::ffff:127.0.0.1", nil, &AddrError{Err: errNoSuitableAddress.Error(), Addr: "::ffff:127.0.0.1"}},
  41. {"ip6:ipv6-icmp", "127.0.0.1", nil, &AddrError{Err: errNoSuitableAddress.Error(), Addr: "127.0.0.1"}},
  42. {"ip6:ipv6-icmp", "::ffff:127.0.0.1", nil, &AddrError{Err: errNoSuitableAddress.Error(), Addr: "::ffff:127.0.0.1"}},
  43. }
  44. func TestResolveIPAddr(t *testing.T) {
  45. if !testableNetwork("ip+nopriv") {
  46. t.Skip("ip+nopriv test")
  47. }
  48. origTestHookLookupIP := testHookLookupIP
  49. defer func() { testHookLookupIP = origTestHookLookupIP }()
  50. testHookLookupIP = lookupLocalhost
  51. for _, tt := range resolveIPAddrTests {
  52. addr, err := ResolveIPAddr(tt.network, tt.litAddrOrName)
  53. if !reflect.DeepEqual(addr, tt.addr) || !reflect.DeepEqual(err, tt.err) {
  54. t.Errorf("ResolveIPAddr(%q, %q) = %#v, %v, want %#v, %v", tt.network, tt.litAddrOrName, addr, err, tt.addr, tt.err)
  55. continue
  56. }
  57. if err == nil {
  58. addr2, err := ResolveIPAddr(addr.Network(), addr.String())
  59. if !reflect.DeepEqual(addr2, tt.addr) || err != tt.err {
  60. t.Errorf("(%q, %q): ResolveIPAddr(%q, %q) = %#v, %v, want %#v, %v", tt.network, tt.litAddrOrName, addr.Network(), addr.String(), addr2, err, tt.addr, tt.err)
  61. }
  62. }
  63. }
  64. }
  65. var ipConnLocalNameTests = []struct {
  66. net string
  67. laddr *IPAddr
  68. }{
  69. {"ip4:icmp", &IPAddr{IP: IPv4(127, 0, 0, 1)}},
  70. {"ip4:icmp", &IPAddr{}},
  71. {"ip4:icmp", nil},
  72. }
  73. func TestIPConnLocalName(t *testing.T) {
  74. for _, tt := range ipConnLocalNameTests {
  75. if !testableNetwork(tt.net) {
  76. t.Logf("skipping %s test", tt.net)
  77. continue
  78. }
  79. c, err := ListenIP(tt.net, tt.laddr)
  80. if err != nil {
  81. t.Fatal(err)
  82. }
  83. defer c.Close()
  84. if la := c.LocalAddr(); la == nil {
  85. t.Fatal("should not fail")
  86. }
  87. }
  88. }
  89. func TestIPConnRemoteName(t *testing.T) {
  90. if !testableNetwork("ip:tcp") {
  91. t.Skip("ip:tcp test")
  92. }
  93. raddr := &IPAddr{IP: IPv4(127, 0, 0, 1).To4()}
  94. c, err := DialIP("ip:tcp", &IPAddr{IP: IPv4(127, 0, 0, 1)}, raddr)
  95. if err != nil {
  96. t.Fatal(err)
  97. }
  98. defer c.Close()
  99. if !reflect.DeepEqual(raddr, c.RemoteAddr()) {
  100. t.Fatalf("got %#v; want %#v", c.RemoteAddr(), raddr)
  101. }
  102. }
  103. func TestDialListenIPArgs(t *testing.T) {
  104. type test struct {
  105. argLists [][2]string
  106. shouldFail bool
  107. }
  108. tests := []test{
  109. {
  110. argLists: [][2]string{
  111. {"ip", "127.0.0.1"},
  112. {"ip:", "127.0.0.1"},
  113. {"ip::", "127.0.0.1"},
  114. {"ip", "::1"},
  115. {"ip:", "::1"},
  116. {"ip::", "::1"},
  117. {"ip4", "127.0.0.1"},
  118. {"ip4:", "127.0.0.1"},
  119. {"ip4::", "127.0.0.1"},
  120. {"ip6", "::1"},
  121. {"ip6:", "::1"},
  122. {"ip6::", "::1"},
  123. },
  124. shouldFail: true,
  125. },
  126. }
  127. if testableNetwork("ip") {
  128. priv := test{shouldFail: false}
  129. for _, tt := range []struct {
  130. network, address string
  131. args [2]string
  132. }{
  133. {"ip4:47", "127.0.0.1", [2]string{"ip4:47", "127.0.0.1"}},
  134. {"ip6:47", "::1", [2]string{"ip6:47", "::1"}},
  135. } {
  136. c, err := ListenPacket(tt.network, tt.address)
  137. if err != nil {
  138. continue
  139. }
  140. c.Close()
  141. priv.argLists = append(priv.argLists, tt.args)
  142. }
  143. if len(priv.argLists) > 0 {
  144. tests = append(tests, priv)
  145. }
  146. }
  147. for _, tt := range tests {
  148. for _, args := range tt.argLists {
  149. _, err := Dial(args[0], args[1])
  150. if tt.shouldFail != (err != nil) {
  151. t.Errorf("Dial(%q, %q) = %v; want (err != nil) is %t", args[0], args[1], err, tt.shouldFail)
  152. }
  153. _, err = ListenPacket(args[0], args[1])
  154. if tt.shouldFail != (err != nil) {
  155. t.Errorf("ListenPacket(%q, %q) = %v; want (err != nil) is %t", args[0], args[1], err, tt.shouldFail)
  156. }
  157. a, err := ResolveIPAddr("ip", args[1])
  158. if err != nil {
  159. t.Errorf("ResolveIPAddr(\"ip\", %q) = %v", args[1], err)
  160. continue
  161. }
  162. _, err = DialIP(args[0], nil, a)
  163. if tt.shouldFail != (err != nil) {
  164. t.Errorf("DialIP(%q, %v) = %v; want (err != nil) is %t", args[0], a, err, tt.shouldFail)
  165. }
  166. _, err = ListenIP(args[0], a)
  167. if tt.shouldFail != (err != nil) {
  168. t.Errorf("ListenIP(%q, %v) = %v; want (err != nil) is %t", args[0], a, err, tt.shouldFail)
  169. }
  170. }
  171. }
  172. }