tlsutil_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright (C) 2016 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 https://mozilla.org/MPL/2.0/.
  6. // The existence of this file means we get 0% test coverage rather than no
  7. // test coverage at all. Remove when implementing an actual test.
  8. package tlsutil
  9. import (
  10. "bytes"
  11. "io"
  12. "net"
  13. "testing"
  14. "time"
  15. )
  16. func TestUnionedConnection(t *testing.T) {
  17. cases := []struct {
  18. data []byte
  19. isTLS bool
  20. }{
  21. {[]byte{0}, false},
  22. {[]byte{0x16}, true},
  23. {[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, false},
  24. {[]byte{0x16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, true},
  25. }
  26. for i, tc := range cases {
  27. fc := &fakeAccepter{tc.data}
  28. dl := DowngradingListener{fc, nil}
  29. conn, isTLS, err := dl.AcceptNoWrapTLS()
  30. if err != nil {
  31. t.Fatalf("%d: %v", i, err)
  32. }
  33. if conn == nil {
  34. t.Fatalf("%d: unexpected nil conn", i)
  35. }
  36. if isTLS != tc.isTLS {
  37. t.Errorf("%d: isTLS=%v, expected %v", i, isTLS, tc.isTLS)
  38. }
  39. // Read all the data, check it's the same
  40. var bs []byte
  41. buf := make([]byte, 128)
  42. for {
  43. n, err := conn.Read(buf)
  44. if err == io.EOF {
  45. break
  46. }
  47. if err != nil {
  48. t.Fatalf("%d: read error: %v", i, err)
  49. }
  50. if len(bs) == 0 {
  51. // first read; should return just one byte
  52. if n != 1 {
  53. t.Errorf("%d: first read returned %d bytes, not 1", i, n)
  54. }
  55. // Check that we've nilled out the "first" thing
  56. if conn.(*UnionedConnection).first != nil {
  57. t.Errorf("%d: expected first read to clear out the `first` attribute", i)
  58. }
  59. }
  60. bs = append(bs, buf[:n]...)
  61. }
  62. if !bytes.Equal(bs, tc.data) {
  63. t.Errorf("%d: got wrong data", i)
  64. }
  65. t.Logf("%d: %v, %x", i, isTLS, bs)
  66. }
  67. }
  68. type fakeAccepter struct {
  69. data []byte
  70. }
  71. func (f *fakeAccepter) Accept() (net.Conn, error) {
  72. return &fakeConn{f.data}, nil
  73. }
  74. func (f *fakeAccepter) Addr() net.Addr { return nil }
  75. func (f *fakeAccepter) Close() error { return nil }
  76. type fakeConn struct {
  77. data []byte
  78. }
  79. func (f *fakeConn) Read(b []byte) (int, error) {
  80. if len(f.data) == 0 {
  81. return 0, io.EOF
  82. }
  83. n := copy(b, f.data)
  84. f.data = f.data[n:]
  85. return n, nil
  86. }
  87. func (f *fakeConn) Write(b []byte) (int, error) {
  88. return len(b), nil
  89. }
  90. func (f *fakeConn) Close() error { return nil }
  91. func (f *fakeConn) LocalAddr() net.Addr { return nil }
  92. func (f *fakeConn) RemoteAddr() net.Addr { return nil }
  93. func (f *fakeConn) SetDeadline(time.Time) error { return nil }
  94. func (f *fakeConn) SetReadDeadline(time.Time) error { return nil }
  95. func (f *fakeConn) SetWriteDeadline(time.Time) error { return nil }