tlsutil_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. "crypto/tls"
  12. "io"
  13. "net"
  14. "testing"
  15. "time"
  16. )
  17. func TestUnionedConnection(t *testing.T) {
  18. cases := []struct {
  19. data []byte
  20. isTLS bool
  21. }{
  22. {[]byte{0}, false},
  23. {[]byte{0x16}, true},
  24. {[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, false},
  25. {[]byte{0x16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, true},
  26. }
  27. for i, tc := range cases {
  28. fc := &fakeAccepter{tc.data}
  29. dl := DowngradingListener{fc, nil}
  30. conn, isTLS, err := dl.AcceptNoWrapTLS()
  31. if err != nil {
  32. t.Fatalf("%d: %v", i, err)
  33. }
  34. if conn == nil {
  35. t.Fatalf("%d: unexpected nil conn", i)
  36. }
  37. if isTLS != tc.isTLS {
  38. t.Errorf("%d: isTLS=%v, expected %v", i, isTLS, tc.isTLS)
  39. }
  40. // Read all the data, check it's the same
  41. var bs []byte
  42. buf := make([]byte, 128)
  43. for {
  44. n, err := conn.Read(buf)
  45. if err == io.EOF {
  46. break
  47. }
  48. if err != nil {
  49. t.Fatalf("%d: read error: %v", i, err)
  50. }
  51. if len(bs) == 0 {
  52. // first read; should return just one byte
  53. if n != 1 {
  54. t.Errorf("%d: first read returned %d bytes, not 1", i, n)
  55. }
  56. // Check that we've nilled out the "first" thing
  57. if conn.(*UnionedConnection).first != nil {
  58. t.Errorf("%d: expected first read to clear out the `first` attribute", i)
  59. }
  60. }
  61. bs = append(bs, buf[:n]...)
  62. }
  63. if !bytes.Equal(bs, tc.data) {
  64. t.Errorf("%d: got wrong data", i)
  65. }
  66. t.Logf("%d: %v, %x", i, isTLS, bs)
  67. }
  68. }
  69. func TestCheckCipherSuites(t *testing.T) {
  70. // This is the set of cipher suites we expect - only the order should
  71. // differ.
  72. allSuites := []uint16{
  73. tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
  74. tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
  75. tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  76. tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
  77. tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
  78. tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
  79. tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
  80. tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
  81. tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
  82. tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
  83. tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
  84. tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
  85. tls.TLS_RSA_WITH_AES_128_GCM_SHA256,
  86. tls.TLS_RSA_WITH_AES_256_GCM_SHA384,
  87. tls.TLS_RSA_WITH_AES_128_CBC_SHA256,
  88. tls.TLS_RSA_WITH_AES_128_CBC_SHA,
  89. tls.TLS_RSA_WITH_AES_256_CBC_SHA,
  90. }
  91. suites := buildCipherSuites()
  92. if len(suites) != len(allSuites) {
  93. t.Fatal("should get a list representing all suites")
  94. }
  95. // Check that the returned list of suites doesn't contain anything
  96. // unexpecteds and is free from duplicates.
  97. seen := make(map[uint16]struct{})
  98. nextSuite:
  99. for _, s0 := range suites {
  100. if _, ok := seen[s0]; ok {
  101. t.Fatal("duplicate suite", s0)
  102. }
  103. for _, s1 := range allSuites {
  104. if s0 == s1 {
  105. seen[s0] = struct{}{}
  106. continue nextSuite
  107. }
  108. }
  109. t.Fatal("got unknown suite", s0)
  110. }
  111. }
  112. type fakeAccepter struct {
  113. data []byte
  114. }
  115. func (f *fakeAccepter) Accept() (net.Conn, error) {
  116. return &fakeConn{f.data}, nil
  117. }
  118. func (f *fakeAccepter) Addr() net.Addr { return nil }
  119. func (f *fakeAccepter) Close() error { return nil }
  120. type fakeConn struct {
  121. data []byte
  122. }
  123. func (f *fakeConn) Read(b []byte) (int, error) {
  124. if len(f.data) == 0 {
  125. return 0, io.EOF
  126. }
  127. n := copy(b, f.data)
  128. f.data = f.data[n:]
  129. return n, nil
  130. }
  131. func (f *fakeConn) Write(b []byte) (int, error) {
  132. return len(b), nil
  133. }
  134. func (f *fakeConn) Close() error { return nil }
  135. func (f *fakeConn) LocalAddr() net.Addr { return nil }
  136. func (f *fakeConn) RemoteAddr() net.Addr { return nil }
  137. func (f *fakeConn) SetDeadline(time.Time) error { return nil }
  138. func (f *fakeConn) SetReadDeadline(time.Time) error { return nil }
  139. func (f *fakeConn) SetWriteDeadline(time.Time) error { return nil }