1
0

protocol_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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)
  42. c1 := NewConnection("c1", br, aw, 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)
  61. NewConnection("c1", br, eaw, m1)
  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 < 36; i++ {
  75. for j := 0; j < 26; 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)
  83. c1 := NewConnection("c1", br, eaw, m1)
  84. d, err := c1.Request("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.name != "tn" {
  103. t.Error("Incorrect name %q", m0.name)
  104. }
  105. if m0.offset != 1234 {
  106. t.Error("Incorrect offset %d", m0.offset)
  107. }
  108. if m0.size != 3456 {
  109. t.Error("Incorrect size %d", m0.size)
  110. }
  111. if string(m0.hash) != "hashbytes" {
  112. t.Error("Incorrect hash %q", m0.hash)
  113. }
  114. t.Logf("Pass at %d+%d bytes", i, j)
  115. pass = true
  116. }
  117. }
  118. if !pass {
  119. t.Error("Never passed")
  120. }
  121. }
  122. func TestVersionErr(t *testing.T) {
  123. m0 := &TestModel{}
  124. m1 := &TestModel{}
  125. ar, aw := io.Pipe()
  126. br, bw := io.Pipe()
  127. c0 := NewConnection("c0", ar, bw, m0)
  128. NewConnection("c1", br, aw, m1)
  129. c0.mwriter.writeHeader(header{
  130. version: 2,
  131. msgID: 0,
  132. msgType: 0,
  133. })
  134. c0.flush()
  135. if !m1.closed {
  136. t.Error("Connection should close due to unknown version")
  137. }
  138. }
  139. func TestTypeErr(t *testing.T) {
  140. m0 := &TestModel{}
  141. m1 := &TestModel{}
  142. ar, aw := io.Pipe()
  143. br, bw := io.Pipe()
  144. c0 := NewConnection("c0", ar, bw, m0)
  145. NewConnection("c1", br, aw, m1)
  146. c0.mwriter.writeHeader(header{
  147. version: 0,
  148. msgID: 0,
  149. msgType: 42,
  150. })
  151. c0.flush()
  152. if !m1.closed {
  153. t.Error("Connection should close due to unknown message type")
  154. }
  155. }
  156. func TestClose(t *testing.T) {
  157. m0 := &TestModel{}
  158. m1 := &TestModel{}
  159. ar, aw := io.Pipe()
  160. br, bw := io.Pipe()
  161. c0 := NewConnection("c0", ar, bw, m0)
  162. NewConnection("c1", br, aw, m1)
  163. c0.close(nil)
  164. ok := c0.isClosed()
  165. if !ok {
  166. t.Fatal("Connection should be closed")
  167. }
  168. // None of these should panic, some should return an error
  169. ok = c0.ping()
  170. if ok {
  171. t.Error("Ping should not return true")
  172. }
  173. c0.Index(nil)
  174. c0.Index(nil)
  175. _, err := c0.Request("foo", 0, 0, nil)
  176. if err == nil {
  177. t.Error("Request should return an error")
  178. }
  179. }