You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

126 lines
2.8 KiB

  1. /*
  2. *
  3. * Copyright 2018 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. // Package testutil include useful test utilities for the handshaker.
  19. package testutil
  20. import (
  21. "bytes"
  22. "encoding/binary"
  23. "io"
  24. "net"
  25. "sync"
  26. "google.golang.org/grpc/credentials/alts/internal/conn"
  27. )
  28. // Stats is used to collect statistics about concurrent handshake calls.
  29. type Stats struct {
  30. mu sync.Mutex
  31. calls int
  32. MaxConcurrentCalls int
  33. }
  34. // Update updates the statistics by adding one call.
  35. func (s *Stats) Update() func() {
  36. s.mu.Lock()
  37. s.calls++
  38. if s.calls > s.MaxConcurrentCalls {
  39. s.MaxConcurrentCalls = s.calls
  40. }
  41. s.mu.Unlock()
  42. return func() {
  43. s.mu.Lock()
  44. s.calls--
  45. s.mu.Unlock()
  46. }
  47. }
  48. // Reset resets the statistics.
  49. func (s *Stats) Reset() {
  50. s.mu.Lock()
  51. defer s.mu.Unlock()
  52. s.calls = 0
  53. s.MaxConcurrentCalls = 0
  54. }
  55. // testConn mimics a net.Conn to the peer.
  56. type testConn struct {
  57. net.Conn
  58. in *bytes.Buffer
  59. out *bytes.Buffer
  60. }
  61. // NewTestConn creates a new instance of testConn object.
  62. func NewTestConn(in *bytes.Buffer, out *bytes.Buffer) net.Conn {
  63. return &testConn{
  64. in: in,
  65. out: out,
  66. }
  67. }
  68. // Read reads from the in buffer.
  69. func (c *testConn) Read(b []byte) (n int, err error) {
  70. return c.in.Read(b)
  71. }
  72. // Write writes to the out buffer.
  73. func (c *testConn) Write(b []byte) (n int, err error) {
  74. return c.out.Write(b)
  75. }
  76. // Close closes the testConn object.
  77. func (c *testConn) Close() error {
  78. return nil
  79. }
  80. // unresponsiveTestConn mimics a net.Conn for an unresponsive peer. It is used
  81. // for testing the PeerNotResponding case.
  82. type unresponsiveTestConn struct {
  83. net.Conn
  84. }
  85. // NewUnresponsiveTestConn creates a new instance of unresponsiveTestConn object.
  86. func NewUnresponsiveTestConn() net.Conn {
  87. return &unresponsiveTestConn{}
  88. }
  89. // Read reads from the in buffer.
  90. func (c *unresponsiveTestConn) Read(b []byte) (n int, err error) {
  91. return 0, io.EOF
  92. }
  93. // Write writes to the out buffer.
  94. func (c *unresponsiveTestConn) Write(b []byte) (n int, err error) {
  95. return 0, nil
  96. }
  97. // Close closes the TestConn object.
  98. func (c *unresponsiveTestConn) Close() error {
  99. return nil
  100. }
  101. // MakeFrame creates a handshake frame.
  102. func MakeFrame(pl string) []byte {
  103. f := make([]byte, len(pl)+conn.MsgLenFieldSize)
  104. binary.LittleEndian.PutUint32(f, uint32(len(pl)))
  105. copy(f[conn.MsgLenFieldSize:], []byte(pl))
  106. return f
  107. }