ping_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright 2014 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 icmp_test
  5. import (
  6. "errors"
  7. "fmt"
  8. "net"
  9. "os"
  10. "runtime"
  11. "testing"
  12. "time"
  13. "golang.org/x/net/icmp"
  14. "golang.org/x/net/internal/iana"
  15. "golang.org/x/net/internal/nettest"
  16. "golang.org/x/net/ipv4"
  17. "golang.org/x/net/ipv6"
  18. )
  19. func googleAddr(c *icmp.PacketConn, protocol int) (net.Addr, error) {
  20. const host = "www.google.com"
  21. ips, err := net.LookupIP(host)
  22. if err != nil {
  23. return nil, err
  24. }
  25. netaddr := func(ip net.IP) (net.Addr, error) {
  26. switch c.LocalAddr().(type) {
  27. case *net.UDPAddr:
  28. return &net.UDPAddr{IP: ip}, nil
  29. case *net.IPAddr:
  30. return &net.IPAddr{IP: ip}, nil
  31. default:
  32. return nil, errors.New("neither UDPAddr nor IPAddr")
  33. }
  34. }
  35. for _, ip := range ips {
  36. switch protocol {
  37. case iana.ProtocolICMP:
  38. if ip.To4() != nil {
  39. return netaddr(ip)
  40. }
  41. case iana.ProtocolIPv6ICMP:
  42. if ip.To16() != nil && ip.To4() == nil {
  43. return netaddr(ip)
  44. }
  45. }
  46. }
  47. return nil, errors.New("no A or AAAA record")
  48. }
  49. type pingTest struct {
  50. network, address string
  51. protocol int
  52. mtype icmp.Type
  53. }
  54. var nonPrivilegedPingTests = []pingTest{
  55. {"udp4", "0.0.0.0", iana.ProtocolICMP, ipv4.ICMPTypeEcho},
  56. {"udp6", "::", iana.ProtocolIPv6ICMP, ipv6.ICMPTypeEchoRequest},
  57. }
  58. func TestNonPrivilegedPing(t *testing.T) {
  59. if testing.Short() {
  60. t.Skip("avoid external network")
  61. }
  62. switch runtime.GOOS {
  63. case "darwin":
  64. case "linux":
  65. t.Log("you may need to adjust the net.ipv4.ping_group_range kernel state")
  66. default:
  67. t.Skipf("not supported on %s", runtime.GOOS)
  68. }
  69. for i, tt := range nonPrivilegedPingTests {
  70. if err := doPing(tt, i); err != nil {
  71. t.Error(err)
  72. }
  73. }
  74. }
  75. var privilegedPingTests = []pingTest{
  76. {"ip4:icmp", "0.0.0.0", iana.ProtocolICMP, ipv4.ICMPTypeEcho},
  77. {"ip6:ipv6-icmp", "::", iana.ProtocolIPv6ICMP, ipv6.ICMPTypeEchoRequest},
  78. }
  79. func TestPrivilegedPing(t *testing.T) {
  80. if testing.Short() {
  81. t.Skip("avoid external network")
  82. }
  83. if m, ok := nettest.SupportsRawIPSocket(); !ok {
  84. t.Skip(m)
  85. }
  86. for i, tt := range privilegedPingTests {
  87. if err := doPing(tt, i); err != nil {
  88. t.Error(err)
  89. }
  90. }
  91. }
  92. func doPing(tt pingTest, seq int) error {
  93. c, err := icmp.ListenPacket(tt.network, tt.address)
  94. if err != nil {
  95. return err
  96. }
  97. defer c.Close()
  98. dst, err := googleAddr(c, tt.protocol)
  99. if err != nil {
  100. return err
  101. }
  102. if tt.network != "udp6" && tt.protocol == iana.ProtocolIPv6ICMP {
  103. var f ipv6.ICMPFilter
  104. f.SetAll(true)
  105. f.Accept(ipv6.ICMPTypeDestinationUnreachable)
  106. f.Accept(ipv6.ICMPTypePacketTooBig)
  107. f.Accept(ipv6.ICMPTypeTimeExceeded)
  108. f.Accept(ipv6.ICMPTypeParameterProblem)
  109. f.Accept(ipv6.ICMPTypeEchoReply)
  110. if err := c.IPv6PacketConn().SetICMPFilter(&f); err != nil {
  111. return err
  112. }
  113. }
  114. wm := icmp.Message{
  115. Type: tt.mtype, Code: 0,
  116. Body: &icmp.Echo{
  117. ID: os.Getpid() & 0xffff, Seq: 1 << uint(seq),
  118. Data: []byte("HELLO-R-U-THERE"),
  119. },
  120. }
  121. wb, err := wm.Marshal(nil)
  122. if err != nil {
  123. return err
  124. }
  125. if n, err := c.WriteTo(wb, dst); err != nil {
  126. return err
  127. } else if n != len(wb) {
  128. return fmt.Errorf("got %v; want %v", n, len(wb))
  129. }
  130. rb := make([]byte, 1500)
  131. if err := c.SetReadDeadline(time.Now().Add(3 * time.Second)); err != nil {
  132. return err
  133. }
  134. n, peer, err := c.ReadFrom(rb)
  135. if err != nil {
  136. return err
  137. }
  138. rm, err := icmp.ParseMessage(tt.protocol, rb[:n])
  139. if err != nil {
  140. return err
  141. }
  142. switch rm.Type {
  143. case ipv4.ICMPTypeEchoReply, ipv6.ICMPTypeEchoReply:
  144. return nil
  145. default:
  146. return fmt.Errorf("got %+v from %v; want echo reply", rm, peer)
  147. }
  148. }