socket_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2016 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 netreflect_test
  5. import (
  6. "net"
  7. "os"
  8. "runtime"
  9. "testing"
  10. "golang.org/x/net/internal/netreflect"
  11. "golang.org/x/net/internal/nettest"
  12. )
  13. func TestSocketOf(t *testing.T) {
  14. for _, network := range []string{"tcp", "unix", "unixpacket"} {
  15. switch runtime.GOOS {
  16. case "darwin":
  17. if network == "unixpacket" {
  18. continue
  19. }
  20. case "nacl", "plan9":
  21. continue
  22. case "windows":
  23. if network == "unix" || network == "unixpacket" {
  24. continue
  25. }
  26. }
  27. ln, err := nettest.NewLocalListener(network)
  28. if err != nil {
  29. t.Error(err)
  30. continue
  31. }
  32. defer func() {
  33. path := ln.Addr().String()
  34. ln.Close()
  35. if network == "unix" || network == "unixpacket" {
  36. os.Remove(path)
  37. }
  38. }()
  39. c, err := net.Dial(ln.Addr().Network(), ln.Addr().String())
  40. if err != nil {
  41. t.Error(err)
  42. continue
  43. }
  44. defer c.Close()
  45. if _, err := netreflect.SocketOf(c); err != nil {
  46. t.Error(err)
  47. continue
  48. }
  49. }
  50. }
  51. func TestPacketSocketOf(t *testing.T) {
  52. for _, network := range []string{"udp", "unixgram"} {
  53. switch runtime.GOOS {
  54. case "nacl", "plan9":
  55. continue
  56. case "windows":
  57. if network == "unixgram" {
  58. continue
  59. }
  60. }
  61. c, err := nettest.NewLocalPacketListener(network)
  62. if err != nil {
  63. t.Error(err)
  64. continue
  65. }
  66. defer c.Close()
  67. if _, err := netreflect.PacketSocketOf(c); err != nil {
  68. t.Error(err)
  69. continue
  70. }
  71. }
  72. }