protocol_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // Copyright (C) 2014 Jakob Borg and other contributors. All rights reserved.
  2. // Use of this source code is governed by an MIT-style license that can be
  3. // found in the LICENSE file.
  4. package protocol
  5. import (
  6. "errors"
  7. "io"
  8. "testing"
  9. "testing/quick"
  10. )
  11. func TestHeaderFunctions(t *testing.T) {
  12. f := func(ver, id, typ int) bool {
  13. ver = int(uint(ver) % 16)
  14. id = int(uint(id) % 4096)
  15. typ = int(uint(typ) % 256)
  16. h0 := header{ver, id, typ}
  17. h1 := decodeHeader(encodeHeader(h0))
  18. return h0 == h1
  19. }
  20. if err := quick.Check(f, nil); err != nil {
  21. t.Error(err)
  22. }
  23. }
  24. func TestHeaderLayout(t *testing.T) {
  25. var e, a uint32
  26. // Version are the first four bits
  27. e = 0xf0000000
  28. a = encodeHeader(header{0xf, 0, 0})
  29. if a != e {
  30. t.Errorf("Header layout incorrect; %08x != %08x", a, e)
  31. }
  32. // Message ID are the following 12 bits
  33. e = 0x0fff0000
  34. a = encodeHeader(header{0, 0xfff, 0})
  35. if a != e {
  36. t.Errorf("Header layout incorrect; %08x != %08x", a, e)
  37. }
  38. // Type are the last 8 bits before reserved
  39. e = 0x0000ff00
  40. a = encodeHeader(header{0, 0, 0xff})
  41. if a != e {
  42. t.Errorf("Header layout incorrect; %08x != %08x", a, e)
  43. }
  44. }
  45. func TestPing(t *testing.T) {
  46. ar, aw := io.Pipe()
  47. br, bw := io.Pipe()
  48. c0 := NewConnection("c0", ar, bw, nil).(wireFormatConnection).next.(*rawConnection)
  49. c1 := NewConnection("c1", br, aw, nil).(wireFormatConnection).next.(*rawConnection)
  50. if ok := c0.ping(); !ok {
  51. t.Error("c0 ping failed")
  52. }
  53. if ok := c1.ping(); !ok {
  54. t.Error("c1 ping failed")
  55. }
  56. }
  57. func TestPingErr(t *testing.T) {
  58. e := errors.New("something broke")
  59. for i := 0; i < 12; i++ {
  60. for j := 0; j < 12; j++ {
  61. m0 := newTestModel()
  62. m1 := newTestModel()
  63. ar, aw := io.Pipe()
  64. br, bw := io.Pipe()
  65. eaw := &ErrPipe{PipeWriter: *aw, max: i, err: e}
  66. ebw := &ErrPipe{PipeWriter: *bw, max: j, err: e}
  67. c0 := NewConnection("c0", ar, ebw, m0).(wireFormatConnection).next.(*rawConnection)
  68. NewConnection("c1", br, eaw, m1)
  69. res := c0.ping()
  70. if (i < 4 || j < 4) && res {
  71. t.Errorf("Unexpected ping success; i=%d, j=%d", i, j)
  72. } else if (i >= 8 && j >= 8) && !res {
  73. t.Errorf("Unexpected ping fail; i=%d, j=%d", i, j)
  74. }
  75. }
  76. }
  77. }
  78. // func TestRequestResponseErr(t *testing.T) {
  79. // e := errors.New("something broke")
  80. // var pass bool
  81. // for i := 0; i < 48; i++ {
  82. // for j := 0; j < 38; j++ {
  83. // m0 := newTestModel()
  84. // m0.data = []byte("response data")
  85. // m1 := newTestModel()
  86. // ar, aw := io.Pipe()
  87. // br, bw := io.Pipe()
  88. // eaw := &ErrPipe{PipeWriter: *aw, max: i, err: e}
  89. // ebw := &ErrPipe{PipeWriter: *bw, max: j, err: e}
  90. // NewConnection("c0", ar, ebw, m0, nil)
  91. // c1 := NewConnection("c1", br, eaw, m1, nil).(wireFormatConnection).next.(*rawConnection)
  92. // d, err := c1.Request("default", "tn", 1234, 5678)
  93. // if err == e || err == ErrClosed {
  94. // t.Logf("Error at %d+%d bytes", i, j)
  95. // if !m1.isClosed() {
  96. // t.Fatal("c1 not closed")
  97. // }
  98. // if !m0.isClosed() {
  99. // t.Fatal("c0 not closed")
  100. // }
  101. // continue
  102. // }
  103. // if err != nil {
  104. // t.Fatal(err)
  105. // }
  106. // if string(d) != "response data" {
  107. // t.Fatalf("Incorrect response data %q", string(d))
  108. // }
  109. // if m0.repo != "default" {
  110. // t.Fatalf("Incorrect repo %q", m0.repo)
  111. // }
  112. // if m0.name != "tn" {
  113. // t.Fatalf("Incorrect name %q", m0.name)
  114. // }
  115. // if m0.offset != 1234 {
  116. // t.Fatalf("Incorrect offset %d", m0.offset)
  117. // }
  118. // if m0.size != 5678 {
  119. // t.Fatalf("Incorrect size %d", m0.size)
  120. // }
  121. // t.Logf("Pass at %d+%d bytes", i, j)
  122. // pass = true
  123. // }
  124. // }
  125. // if !pass {
  126. // t.Fatal("Never passed")
  127. // }
  128. // }
  129. func TestVersionErr(t *testing.T) {
  130. m0 := newTestModel()
  131. m1 := newTestModel()
  132. ar, aw := io.Pipe()
  133. br, bw := io.Pipe()
  134. c0 := NewConnection("c0", ar, bw, m0).(wireFormatConnection).next.(*rawConnection)
  135. NewConnection("c1", br, aw, m1)
  136. c0.xw.WriteUint32(encodeHeader(header{
  137. version: 2,
  138. msgID: 0,
  139. msgType: 0,
  140. }))
  141. c0.flush()
  142. if !m1.isClosed() {
  143. t.Error("Connection should close due to unknown version")
  144. }
  145. }
  146. func TestTypeErr(t *testing.T) {
  147. m0 := newTestModel()
  148. m1 := newTestModel()
  149. ar, aw := io.Pipe()
  150. br, bw := io.Pipe()
  151. c0 := NewConnection("c0", ar, bw, m0).(wireFormatConnection).next.(*rawConnection)
  152. NewConnection("c1", br, aw, m1)
  153. c0.xw.WriteUint32(encodeHeader(header{
  154. version: 0,
  155. msgID: 0,
  156. msgType: 42,
  157. }))
  158. c0.flush()
  159. if !m1.isClosed() {
  160. t.Error("Connection should close due to unknown message type")
  161. }
  162. }
  163. func TestClose(t *testing.T) {
  164. m0 := newTestModel()
  165. m1 := newTestModel()
  166. ar, aw := io.Pipe()
  167. br, bw := io.Pipe()
  168. c0 := NewConnection("c0", ar, bw, m0).(wireFormatConnection).next.(*rawConnection)
  169. NewConnection("c1", br, aw, m1)
  170. c0.close(nil)
  171. <-c0.closed
  172. if !m0.isClosed() {
  173. t.Fatal("Connection should be closed")
  174. }
  175. // None of these should panic, some should return an error
  176. if c0.ping() {
  177. t.Error("Ping should not return true")
  178. }
  179. c0.Index("default", nil)
  180. c0.Index("default", nil)
  181. if _, err := c0.Request("default", "foo", 0, 0); err == nil {
  182. t.Error("Request should return an error")
  183. }
  184. }