socket_test.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Copyright 2017 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 darwin dragonfly freebsd linux netbsd openbsd solaris windows
  5. package socket_test
  6. import (
  7. "net"
  8. "runtime"
  9. "syscall"
  10. "testing"
  11. "golang.org/x/net/internal/nettest"
  12. "golang.org/x/net/internal/socket"
  13. )
  14. func TestSocket(t *testing.T) {
  15. t.Run("Option", func(t *testing.T) {
  16. testSocketOption(t, &socket.Option{Level: syscall.SOL_SOCKET, Name: syscall.SO_RCVBUF, Len: 4})
  17. })
  18. }
  19. func testSocketOption(t *testing.T, so *socket.Option) {
  20. c, err := nettest.NewLocalPacketListener("udp")
  21. if err != nil {
  22. t.Skipf("not supported on %s/%s: %v", runtime.GOOS, runtime.GOARCH, err)
  23. }
  24. defer c.Close()
  25. cc, err := socket.NewConn(c.(net.Conn))
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. const N = 2048
  30. if err := so.SetInt(cc, N); err != nil {
  31. t.Fatal(err)
  32. }
  33. n, err := so.GetInt(cc)
  34. if err != nil {
  35. t.Fatal(err)
  36. }
  37. if n < N {
  38. t.Fatalf("got %d; want greater than or equal to %d", n, N)
  39. }
  40. }