protocol_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package protocol
  2. import (
  3. "errors"
  4. "io"
  5. "testing"
  6. "testing/quick"
  7. "time"
  8. )
  9. func TestHeaderFunctions(t *testing.T) {
  10. f := func(ver, id, typ int) bool {
  11. ver = int(uint(ver) % 16)
  12. id = int(uint(id) % 4096)
  13. typ = int(uint(typ) % 256)
  14. h0 := header{ver, id, typ}
  15. h1 := decodeHeader(encodeHeader(h0))
  16. return h0 == h1
  17. }
  18. if err := quick.Check(f, nil); err != nil {
  19. t.Error(err)
  20. }
  21. }
  22. func TestPad(t *testing.T) {
  23. tests := [][]int{
  24. {0, 0},
  25. {1, 3},
  26. {2, 2},
  27. {3, 1},
  28. {4, 0},
  29. {32, 0},
  30. {33, 3},
  31. }
  32. for _, tc := range tests {
  33. if p := pad(tc[0]); p != tc[1] {
  34. t.Errorf("Incorrect padding for %d bytes, %d != %d", tc[0], p, tc[1])
  35. }
  36. }
  37. }
  38. func TestPing(t *testing.T) {
  39. ar, aw := io.Pipe()
  40. br, bw := io.Pipe()
  41. c0 := NewConnection("c0", ar, bw, nil, nil)
  42. c1 := NewConnection("c1", br, aw, nil, nil)
  43. if ok := c0.ping(); !ok {
  44. t.Error("c0 ping failed")
  45. }
  46. if ok := c1.ping(); !ok {
  47. t.Error("c1 ping failed")
  48. }
  49. }
  50. func TestPingErr(t *testing.T) {
  51. e := errors.New("Something broke")
  52. for i := 0; i < 12; i++ {
  53. for j := 0; j < 12; j++ {
  54. m0 := &TestModel{}
  55. m1 := &TestModel{}
  56. ar, aw := io.Pipe()
  57. br, bw := io.Pipe()
  58. eaw := &ErrPipe{PipeWriter: *aw, max: i, err: e}
  59. ebw := &ErrPipe{PipeWriter: *bw, max: j, err: e}
  60. c0 := NewConnection("c0", ar, ebw, m0, nil)
  61. NewConnection("c1", br, eaw, m1, nil)
  62. res := c0.ping()
  63. if (i < 4 || j < 4) && res {
  64. t.Errorf("Unexpected ping success; i=%d, j=%d", i, j)
  65. } else if (i >= 8 && j >= 8) && !res {
  66. t.Errorf("Unexpected ping fail; i=%d, j=%d", i, j)
  67. }
  68. }
  69. }
  70. }
  71. func TestRequestResponseErr(t *testing.T) {
  72. e := errors.New("Something broke")
  73. var pass bool
  74. for i := 0; i < 48; i++ {
  75. for j := 0; j < 38; j++ {
  76. m0 := &TestModel{data: []byte("response data")}
  77. m1 := &TestModel{}
  78. ar, aw := io.Pipe()
  79. br, bw := io.Pipe()
  80. eaw := &ErrPipe{PipeWriter: *aw, max: i, err: e}
  81. ebw := &ErrPipe{PipeWriter: *bw, max: j, err: e}
  82. NewConnection("c0", ar, ebw, m0, nil)
  83. c1 := NewConnection("c1", br, eaw, m1, nil)
  84. d, err := c1.Request("default", "tn", 1234, 3456, []byte("hashbytes"))
  85. if err == e || err == ErrClosed {
  86. t.Logf("Error at %d+%d bytes", i, j)
  87. if !m1.closed {
  88. t.Error("c1 not closed")
  89. }
  90. time.Sleep(1 * time.Millisecond)
  91. if !m0.closed {
  92. t.Error("c0 not closed")
  93. }
  94. continue
  95. }
  96. if err != nil {
  97. t.Error(err)
  98. }
  99. if string(d) != "response data" {
  100. t.Errorf("Incorrect response data %q", string(d))
  101. }
  102. if m0.repo != "default" {
  103. t.Error("Incorrect repo %q", m0.repo)
  104. }
  105. if m0.name != "tn" {
  106. t.Error("Incorrect name %q", m0.name)
  107. }
  108. if m0.offset != 1234 {
  109. t.Error("Incorrect offset %d", m0.offset)
  110. }
  111. if m0.size != 3456 {
  112. t.Error("Incorrect size %d", m0.size)
  113. }
  114. if string(m0.hash) != "hashbytes" {
  115. t.Error("Incorrect hash %q", m0.hash)
  116. }
  117. t.Logf("Pass at %d+%d bytes", i, j)
  118. pass = true
  119. }
  120. }
  121. if !pass {
  122. t.Error("Never passed")
  123. }
  124. }
  125. func TestVersionErr(t *testing.T) {
  126. m0 := &TestModel{}
  127. m1 := &TestModel{}
  128. ar, aw := io.Pipe()
  129. br, bw := io.Pipe()
  130. c0 := NewConnection("c0", ar, bw, m0, nil)
  131. NewConnection("c1", br, aw, m1, nil)
  132. c0.mwriter.writeHeader(header{
  133. version: 2,
  134. msgID: 0,
  135. msgType: 0,
  136. })
  137. c0.flush()
  138. if !m1.closed {
  139. t.Error("Connection should close due to unknown version")
  140. }
  141. }
  142. func TestTypeErr(t *testing.T) {
  143. m0 := &TestModel{}
  144. m1 := &TestModel{}
  145. ar, aw := io.Pipe()
  146. br, bw := io.Pipe()
  147. c0 := NewConnection("c0", ar, bw, m0, nil)
  148. NewConnection("c1", br, aw, m1, nil)
  149. c0.mwriter.writeHeader(header{
  150. version: 0,
  151. msgID: 0,
  152. msgType: 42,
  153. })
  154. c0.flush()
  155. if !m1.closed {
  156. t.Error("Connection should close due to unknown message type")
  157. }
  158. }
  159. func TestClose(t *testing.T) {
  160. m0 := &TestModel{}
  161. m1 := &TestModel{}
  162. ar, aw := io.Pipe()
  163. br, bw := io.Pipe()
  164. c0 := NewConnection("c0", ar, bw, m0, nil)
  165. NewConnection("c1", br, aw, m1, nil)
  166. c0.close(nil)
  167. ok := c0.isClosed()
  168. if !ok {
  169. t.Fatal("Connection should be closed")
  170. }
  171. // None of these should panic, some should return an error
  172. ok = c0.ping()
  173. if ok {
  174. t.Error("Ping should not return true")
  175. }
  176. c0.Index("default", nil)
  177. c0.Index("default", nil)
  178. _, err := c0.Request("default", "foo", 0, 0, nil)
  179. if err == nil {
  180. t.Error("Request should return an error")
  181. }
  182. }