listen_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright (c) 2016 Arista Networks, Inc.
  2. // Use of this source code is governed by the Apache License 2.0
  3. // that can be found in the COPYING file.
  4. package dscp_test
  5. import (
  6. "net"
  7. "testing"
  8. "notabug.org/themusicgod1/goarista/dscp"
  9. )
  10. func TestListenTCPWithTOS(t *testing.T) {
  11. testListenTCPWithTOS(t, "127.0.0.1")
  12. //testListenTCPWithTOS(t, "::1")
  13. }
  14. func testListenTCPWithTOS(t *testing.T, ip string) {
  15. // Note: This test doesn't actually verify that the connection uses the
  16. // desired TOS byte, because that's kinda hard to check, but at least it
  17. // verifies that we return a usable TCPListener.
  18. addr := &net.TCPAddr{IP: net.ParseIP(ip), Port: 0}
  19. listen, err := dscp.ListenTCPWithTOS(addr, 40)
  20. if err != nil {
  21. t.Fatal(err)
  22. }
  23. defer listen.Close()
  24. done := make(chan struct{})
  25. go func() {
  26. conn, err := listen.Accept()
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. defer conn.Close()
  31. buf := []byte{'!'}
  32. conn.Write(buf)
  33. n, err := conn.Read(buf)
  34. if n != 1 || err != nil {
  35. t.Fatalf("Read returned %d / %s", n, err)
  36. } else if buf[0] != '!' {
  37. t.Fatalf("Expected to read '!' but got %q", buf)
  38. }
  39. close(done)
  40. }()
  41. conn, err := net.Dial(listen.Addr().Network(), listen.Addr().String())
  42. if err != nil {
  43. t.Fatal("Connection failed:", err)
  44. }
  45. defer conn.Close()
  46. buf := make([]byte, 1)
  47. n, err := conn.Read(buf)
  48. if n != 1 || err != nil {
  49. t.Fatalf("Read returned %d / %s", n, err)
  50. } else if buf[0] != '!' {
  51. t.Fatalf("Expected to read '!' but got %q", buf)
  52. }
  53. conn.Write(buf)
  54. <-done
  55. }