quic_misc_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright (C) 2019 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at http://mozilla.org/MPL/2.0/.
  6. // +build go1.12
  7. package connections
  8. import (
  9. "net"
  10. "testing"
  11. "time"
  12. )
  13. type mockPacketConn struct {
  14. addr mockedAddr
  15. }
  16. func (mockPacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) {
  17. panic("implement me")
  18. }
  19. func (mockPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
  20. panic("implement me")
  21. }
  22. func (mockPacketConn) Close() error {
  23. panic("implement me")
  24. }
  25. func (c *mockPacketConn) LocalAddr() net.Addr {
  26. return c.addr
  27. }
  28. func (mockPacketConn) SetDeadline(t time.Time) error {
  29. panic("implement me")
  30. }
  31. func (mockPacketConn) SetReadDeadline(t time.Time) error {
  32. panic("implement me")
  33. }
  34. func (mockPacketConn) SetWriteDeadline(t time.Time) error {
  35. panic("implement me")
  36. }
  37. type mockedAddr struct {
  38. network string
  39. addr string
  40. }
  41. func (a mockedAddr) Network() string {
  42. return a.network
  43. }
  44. func (a mockedAddr) String() string {
  45. return a.addr
  46. }
  47. func TestPacketConnLess(t *testing.T) {
  48. cases := []struct {
  49. netA string
  50. addrA string
  51. netB string
  52. addrB string
  53. }{
  54. // B is assumed the winner.
  55. {"tcp", "127.0.0.1:1234", "tcp", ":1235"},
  56. {"tcp", "127.0.0.1:1234", "tcp", "0.0.0.0:1235"},
  57. {"tcp4", "0.0.0.0:1234", "tcp", "0.0.0.0:1235"}, // tcp4 on the first one
  58. }
  59. for i, testCase := range cases {
  60. conns := []*mockPacketConn{
  61. {mockedAddr{testCase.netA, testCase.addrA}},
  62. {mockedAddr{testCase.netB, testCase.addrB}},
  63. }
  64. if packetConnLess(conns[0], conns[1]) {
  65. t.Error(i, "unexpected")
  66. }
  67. if !packetConnLess(conns[1], conns[0]) {
  68. t.Error(i, "unexpected")
  69. }
  70. if packetConnLess(conns[0], conns[0]) || packetConnLess(conns[1], conns[1]) {
  71. t.Error(i, "unexpected")
  72. }
  73. }
  74. }