The reconcile package is used for DOM reconcilation in Isomorphic Go web applications.

unicast_test.go 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // Copyright 2012 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. package ipv4_test
  5. import (
  6. "bytes"
  7. "net"
  8. "os"
  9. "runtime"
  10. "testing"
  11. "time"
  12. "golang.org/x/net/icmp"
  13. "golang.org/x/net/internal/iana"
  14. "golang.org/x/net/internal/nettest"
  15. "golang.org/x/net/ipv4"
  16. )
  17. func TestPacketConnReadWriteUnicastUDP(t *testing.T) {
  18. switch runtime.GOOS {
  19. case "nacl", "plan9", "windows":
  20. t.Skipf("not supported on %s", runtime.GOOS)
  21. }
  22. ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback)
  23. if ifi == nil {
  24. t.Skipf("not available on %s", runtime.GOOS)
  25. }
  26. c, err := nettest.NewLocalPacketListener("udp4")
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. defer c.Close()
  31. p := ipv4.NewPacketConn(c)
  32. defer p.Close()
  33. dst := c.LocalAddr()
  34. cf := ipv4.FlagTTL | ipv4.FlagDst | ipv4.FlagInterface
  35. wb := []byte("HELLO-R-U-THERE")
  36. for i, toggle := range []bool{true, false, true} {
  37. if err := p.SetControlMessage(cf, toggle); err != nil {
  38. if nettest.ProtocolNotSupported(err) {
  39. t.Logf("not supported on %s", runtime.GOOS)
  40. continue
  41. }
  42. t.Fatal(err)
  43. }
  44. p.SetTTL(i + 1)
  45. if err := p.SetWriteDeadline(time.Now().Add(100 * time.Millisecond)); err != nil {
  46. t.Fatal(err)
  47. }
  48. if n, err := p.WriteTo(wb, nil, dst); err != nil {
  49. t.Fatal(err)
  50. } else if n != len(wb) {
  51. t.Fatalf("got %v; want %v", n, len(wb))
  52. }
  53. rb := make([]byte, 128)
  54. if err := p.SetReadDeadline(time.Now().Add(100 * time.Millisecond)); err != nil {
  55. t.Fatal(err)
  56. }
  57. if n, _, _, err := p.ReadFrom(rb); err != nil {
  58. t.Fatal(err)
  59. } else if !bytes.Equal(rb[:n], wb) {
  60. t.Fatalf("got %v; want %v", rb[:n], wb)
  61. }
  62. }
  63. }
  64. func TestPacketConnReadWriteUnicastICMP(t *testing.T) {
  65. switch runtime.GOOS {
  66. case "nacl", "plan9", "windows":
  67. t.Skipf("not supported on %s", runtime.GOOS)
  68. }
  69. if m, ok := nettest.SupportsRawIPSocket(); !ok {
  70. t.Skip(m)
  71. }
  72. ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback)
  73. if ifi == nil {
  74. t.Skipf("not available on %s", runtime.GOOS)
  75. }
  76. c, err := net.ListenPacket("ip4:icmp", "0.0.0.0")
  77. if err != nil {
  78. t.Fatal(err)
  79. }
  80. defer c.Close()
  81. dst, err := net.ResolveIPAddr("ip4", "127.0.0.1")
  82. if err != nil {
  83. t.Fatal(err)
  84. }
  85. p := ipv4.NewPacketConn(c)
  86. defer p.Close()
  87. cf := ipv4.FlagDst | ipv4.FlagInterface
  88. if runtime.GOOS != "solaris" {
  89. // Solaris never allows to modify ICMP properties.
  90. cf |= ipv4.FlagTTL
  91. }
  92. for i, toggle := range []bool{true, false, true} {
  93. wb, err := (&icmp.Message{
  94. Type: ipv4.ICMPTypeEcho, Code: 0,
  95. Body: &icmp.Echo{
  96. ID: os.Getpid() & 0xffff, Seq: i + 1,
  97. Data: []byte("HELLO-R-U-THERE"),
  98. },
  99. }).Marshal(nil)
  100. if err != nil {
  101. t.Fatal(err)
  102. }
  103. if err := p.SetControlMessage(cf, toggle); err != nil {
  104. if nettest.ProtocolNotSupported(err) {
  105. t.Logf("not supported on %s", runtime.GOOS)
  106. continue
  107. }
  108. t.Fatal(err)
  109. }
  110. p.SetTTL(i + 1)
  111. if err := p.SetWriteDeadline(time.Now().Add(100 * time.Millisecond)); err != nil {
  112. t.Fatal(err)
  113. }
  114. if n, err := p.WriteTo(wb, nil, dst); err != nil {
  115. t.Fatal(err)
  116. } else if n != len(wb) {
  117. t.Fatalf("got %v; want %v", n, len(wb))
  118. }
  119. rb := make([]byte, 128)
  120. loop:
  121. if err := p.SetReadDeadline(time.Now().Add(100 * time.Millisecond)); err != nil {
  122. t.Fatal(err)
  123. }
  124. if n, _, _, err := p.ReadFrom(rb); err != nil {
  125. switch runtime.GOOS {
  126. case "darwin": // older darwin kernels have some limitation on receiving icmp packet through raw socket
  127. t.Logf("not supported on %s", runtime.GOOS)
  128. continue
  129. }
  130. t.Fatal(err)
  131. } else {
  132. m, err := icmp.ParseMessage(iana.ProtocolICMP, rb[:n])
  133. if err != nil {
  134. t.Fatal(err)
  135. }
  136. if runtime.GOOS == "linux" && m.Type == ipv4.ICMPTypeEcho {
  137. // On Linux we must handle own sent packets.
  138. goto loop
  139. }
  140. if m.Type != ipv4.ICMPTypeEchoReply || m.Code != 0 {
  141. t.Fatalf("got type=%v, code=%v; want type=%v, code=%v", m.Type, m.Code, ipv4.ICMPTypeEchoReply, 0)
  142. }
  143. }
  144. }
  145. }
  146. func TestRawConnReadWriteUnicastICMP(t *testing.T) {
  147. switch runtime.GOOS {
  148. case "nacl", "plan9", "windows":
  149. t.Skipf("not supported on %s", runtime.GOOS)
  150. }
  151. if m, ok := nettest.SupportsRawIPSocket(); !ok {
  152. t.Skip(m)
  153. }
  154. ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback)
  155. if ifi == nil {
  156. t.Skipf("not available on %s", runtime.GOOS)
  157. }
  158. c, err := net.ListenPacket("ip4:icmp", "0.0.0.0")
  159. if err != nil {
  160. t.Fatal(err)
  161. }
  162. defer c.Close()
  163. dst, err := net.ResolveIPAddr("ip4", "127.0.0.1")
  164. if err != nil {
  165. t.Fatal(err)
  166. }
  167. r, err := ipv4.NewRawConn(c)
  168. if err != nil {
  169. t.Fatal(err)
  170. }
  171. defer r.Close()
  172. cf := ipv4.FlagTTL | ipv4.FlagDst | ipv4.FlagInterface
  173. for i, toggle := range []bool{true, false, true} {
  174. wb, err := (&icmp.Message{
  175. Type: ipv4.ICMPTypeEcho, Code: 0,
  176. Body: &icmp.Echo{
  177. ID: os.Getpid() & 0xffff, Seq: i + 1,
  178. Data: []byte("HELLO-R-U-THERE"),
  179. },
  180. }).Marshal(nil)
  181. if err != nil {
  182. t.Fatal(err)
  183. }
  184. wh := &ipv4.Header{
  185. Version: ipv4.Version,
  186. Len: ipv4.HeaderLen,
  187. TOS: i + 1,
  188. TotalLen: ipv4.HeaderLen + len(wb),
  189. TTL: i + 1,
  190. Protocol: 1,
  191. Dst: dst.IP,
  192. }
  193. if err := r.SetControlMessage(cf, toggle); err != nil {
  194. if nettest.ProtocolNotSupported(err) {
  195. t.Logf("not supported on %s", runtime.GOOS)
  196. continue
  197. }
  198. t.Fatal(err)
  199. }
  200. if err := r.SetWriteDeadline(time.Now().Add(100 * time.Millisecond)); err != nil {
  201. t.Fatal(err)
  202. }
  203. if err := r.WriteTo(wh, wb, nil); err != nil {
  204. t.Fatal(err)
  205. }
  206. rb := make([]byte, ipv4.HeaderLen+128)
  207. loop:
  208. if err := r.SetReadDeadline(time.Now().Add(100 * time.Millisecond)); err != nil {
  209. t.Fatal(err)
  210. }
  211. if _, b, _, err := r.ReadFrom(rb); err != nil {
  212. switch runtime.GOOS {
  213. case "darwin": // older darwin kernels have some limitation on receiving icmp packet through raw socket
  214. t.Logf("not supported on %s", runtime.GOOS)
  215. continue
  216. }
  217. t.Fatal(err)
  218. } else {
  219. m, err := icmp.ParseMessage(iana.ProtocolICMP, b)
  220. if err != nil {
  221. t.Fatal(err)
  222. }
  223. if runtime.GOOS == "linux" && m.Type == ipv4.ICMPTypeEcho {
  224. // On Linux we must handle own sent packets.
  225. goto loop
  226. }
  227. if m.Type != ipv4.ICMPTypeEchoReply || m.Code != 0 {
  228. t.Fatalf("got type=%v, code=%v; want type=%v, code=%v", m.Type, m.Code, ipv4.ICMPTypeEchoReply, 0)
  229. }
  230. }
  231. }
  232. }