protocol_test.go 3.9 KB

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