protocol_test.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  2. // All rights reserved. Use of this source code is governed by an MIT-style
  3. // license that can be found in the LICENSE file.
  4. package protocol
  5. import (
  6. "bytes"
  7. "encoding/hex"
  8. "encoding/json"
  9. "errors"
  10. "fmt"
  11. "io"
  12. "io/ioutil"
  13. "os"
  14. "reflect"
  15. "testing"
  16. "testing/quick"
  17. "github.com/calmh/xdr"
  18. )
  19. var (
  20. c0ID = NewNodeID([]byte{1})
  21. c1ID = NewNodeID([]byte{2})
  22. )
  23. func TestHeaderFunctions(t *testing.T) {
  24. f := func(ver, id, typ int) bool {
  25. ver = int(uint(ver) % 16)
  26. id = int(uint(id) % 4096)
  27. typ = int(uint(typ) % 256)
  28. h0 := header{version: ver, msgID: id, msgType: typ}
  29. h1 := decodeHeader(encodeHeader(h0))
  30. return h0 == h1
  31. }
  32. if err := quick.Check(f, nil); err != nil {
  33. t.Error(err)
  34. }
  35. }
  36. func TestHeaderLayout(t *testing.T) {
  37. var e, a uint32
  38. // Version are the first four bits
  39. e = 0xf0000000
  40. a = encodeHeader(header{version: 0xf})
  41. if a != e {
  42. t.Errorf("Header layout incorrect; %08x != %08x", a, e)
  43. }
  44. // Message ID are the following 12 bits
  45. e = 0x0fff0000
  46. a = encodeHeader(header{msgID: 0xfff})
  47. if a != e {
  48. t.Errorf("Header layout incorrect; %08x != %08x", a, e)
  49. }
  50. // Type are the last 8 bits before reserved
  51. e = 0x0000ff00
  52. a = encodeHeader(header{msgType: 0xff})
  53. if a != e {
  54. t.Errorf("Header layout incorrect; %08x != %08x", a, e)
  55. }
  56. }
  57. func TestPing(t *testing.T) {
  58. ar, aw := io.Pipe()
  59. br, bw := io.Pipe()
  60. c0 := NewConnection(c0ID, ar, bw, nil, "name", true).(wireFormatConnection).next.(*rawConnection)
  61. c1 := NewConnection(c1ID, br, aw, nil, "name", true).(wireFormatConnection).next.(*rawConnection)
  62. if ok := c0.ping(); !ok {
  63. t.Error("c0 ping failed")
  64. }
  65. if ok := c1.ping(); !ok {
  66. t.Error("c1 ping failed")
  67. }
  68. }
  69. func TestPingErr(t *testing.T) {
  70. e := errors.New("something broke")
  71. for i := 0; i < 16; i++ {
  72. for j := 0; j < 16; j++ {
  73. m0 := newTestModel()
  74. m1 := newTestModel()
  75. ar, aw := io.Pipe()
  76. br, bw := io.Pipe()
  77. eaw := &ErrPipe{PipeWriter: *aw, max: i, err: e}
  78. ebw := &ErrPipe{PipeWriter: *bw, max: j, err: e}
  79. c0 := NewConnection(c0ID, ar, ebw, m0, "name", true).(wireFormatConnection).next.(*rawConnection)
  80. NewConnection(c1ID, br, eaw, m1, "name", true)
  81. res := c0.ping()
  82. if (i < 8 || j < 8) && res {
  83. t.Errorf("Unexpected ping success; i=%d, j=%d", i, j)
  84. } else if (i >= 12 && j >= 12) && !res {
  85. t.Errorf("Unexpected ping fail; i=%d, j=%d", i, j)
  86. }
  87. }
  88. }
  89. }
  90. // func TestRequestResponseErr(t *testing.T) {
  91. // e := errors.New("something broke")
  92. // var pass bool
  93. // for i := 0; i < 48; i++ {
  94. // for j := 0; j < 38; j++ {
  95. // m0 := newTestModel()
  96. // m0.data = []byte("response data")
  97. // m1 := newTestModel()
  98. // ar, aw := io.Pipe()
  99. // br, bw := io.Pipe()
  100. // eaw := &ErrPipe{PipeWriter: *aw, max: i, err: e}
  101. // ebw := &ErrPipe{PipeWriter: *bw, max: j, err: e}
  102. // NewConnection(c0ID, ar, ebw, m0, nil)
  103. // c1 := NewConnection(c1ID, br, eaw, m1, nil).(wireFormatConnection).next.(*rawConnection)
  104. // d, err := c1.Request("default", "tn", 1234, 5678)
  105. // if err == e || err == ErrClosed {
  106. // t.Logf("Error at %d+%d bytes", i, j)
  107. // if !m1.isClosed() {
  108. // t.Fatal("c1 not closed")
  109. // }
  110. // if !m0.isClosed() {
  111. // t.Fatal("c0 not closed")
  112. // }
  113. // continue
  114. // }
  115. // if err != nil {
  116. // t.Fatal(err)
  117. // }
  118. // if string(d) != "response data" {
  119. // t.Fatalf("Incorrect response data %q", string(d))
  120. // }
  121. // if m0.repo != "default" {
  122. // t.Fatalf("Incorrect repo %q", m0.repo)
  123. // }
  124. // if m0.name != "tn" {
  125. // t.Fatalf("Incorrect name %q", m0.name)
  126. // }
  127. // if m0.offset != 1234 {
  128. // t.Fatalf("Incorrect offset %d", m0.offset)
  129. // }
  130. // if m0.size != 5678 {
  131. // t.Fatalf("Incorrect size %d", m0.size)
  132. // }
  133. // t.Logf("Pass at %d+%d bytes", i, j)
  134. // pass = true
  135. // }
  136. // }
  137. // if !pass {
  138. // t.Fatal("Never passed")
  139. // }
  140. // }
  141. func TestVersionErr(t *testing.T) {
  142. m0 := newTestModel()
  143. m1 := newTestModel()
  144. ar, aw := io.Pipe()
  145. br, bw := io.Pipe()
  146. c0 := NewConnection(c0ID, ar, bw, m0, "name", true).(wireFormatConnection).next.(*rawConnection)
  147. NewConnection(c1ID, br, aw, m1, "name", true)
  148. w := xdr.NewWriter(c0.cw)
  149. w.WriteUint32(encodeHeader(header{
  150. version: 2,
  151. msgID: 0,
  152. msgType: 0,
  153. }))
  154. w.WriteUint32(0)
  155. if !m1.isClosed() {
  156. t.Error("Connection should close due to unknown version")
  157. }
  158. }
  159. func TestTypeErr(t *testing.T) {
  160. m0 := newTestModel()
  161. m1 := newTestModel()
  162. ar, aw := io.Pipe()
  163. br, bw := io.Pipe()
  164. c0 := NewConnection(c0ID, ar, bw, m0, "name", true).(wireFormatConnection).next.(*rawConnection)
  165. NewConnection(c1ID, br, aw, m1, "name", true)
  166. w := xdr.NewWriter(c0.cw)
  167. w.WriteUint32(encodeHeader(header{
  168. version: 0,
  169. msgID: 0,
  170. msgType: 42,
  171. }))
  172. w.WriteUint32(0)
  173. if !m1.isClosed() {
  174. t.Error("Connection should close due to unknown message type")
  175. }
  176. }
  177. func TestClose(t *testing.T) {
  178. m0 := newTestModel()
  179. m1 := newTestModel()
  180. ar, aw := io.Pipe()
  181. br, bw := io.Pipe()
  182. c0 := NewConnection(c0ID, ar, bw, m0, "name", true).(wireFormatConnection).next.(*rawConnection)
  183. NewConnection(c1ID, br, aw, m1, "name", true)
  184. c0.close(nil)
  185. <-c0.closed
  186. if !m0.isClosed() {
  187. t.Fatal("Connection should be closed")
  188. }
  189. // None of these should panic, some should return an error
  190. if c0.ping() {
  191. t.Error("Ping should not return true")
  192. }
  193. c0.Index("default", nil)
  194. c0.Index("default", nil)
  195. if _, err := c0.Request("default", "foo", 0, 0); err == nil {
  196. t.Error("Request should return an error")
  197. }
  198. }
  199. func TestElementSizeExceededNested(t *testing.T) {
  200. m := ClusterConfigMessage{
  201. Repositories: []Repository{
  202. {ID: "longstringlongstringlongstringinglongstringlongstringlonlongstringlongstringlon"},
  203. },
  204. }
  205. _, err := m.EncodeXDR(ioutil.Discard)
  206. if err == nil {
  207. t.Errorf("ID length %d > max 64, but no error", len(m.Repositories[0].ID))
  208. }
  209. }
  210. func TestMarshalIndexMessage(t *testing.T) {
  211. var quickCfg = &quick.Config{MaxCountScale: 10}
  212. if testing.Short() {
  213. quickCfg = nil
  214. }
  215. f := func(m1 IndexMessage) bool {
  216. for _, f := range m1.Files {
  217. for i := range f.Blocks {
  218. f.Blocks[i].Offset = 0
  219. if len(f.Blocks[i].Hash) == 0 {
  220. f.Blocks[i].Hash = nil
  221. }
  222. }
  223. }
  224. return testMarshal(t, "index", &m1, &IndexMessage{})
  225. }
  226. if err := quick.Check(f, quickCfg); err != nil {
  227. t.Error(err)
  228. }
  229. }
  230. func TestMarshalRequestMessage(t *testing.T) {
  231. var quickCfg = &quick.Config{MaxCountScale: 10}
  232. if testing.Short() {
  233. quickCfg = nil
  234. }
  235. f := func(m1 RequestMessage) bool {
  236. return testMarshal(t, "request", &m1, &RequestMessage{})
  237. }
  238. if err := quick.Check(f, quickCfg); err != nil {
  239. t.Error(err)
  240. }
  241. }
  242. func TestMarshalResponseMessage(t *testing.T) {
  243. var quickCfg = &quick.Config{MaxCountScale: 10}
  244. if testing.Short() {
  245. quickCfg = nil
  246. }
  247. f := func(m1 ResponseMessage) bool {
  248. if len(m1.Data) == 0 {
  249. m1.Data = nil
  250. }
  251. return testMarshal(t, "response", &m1, &ResponseMessage{})
  252. }
  253. if err := quick.Check(f, quickCfg); err != nil {
  254. t.Error(err)
  255. }
  256. }
  257. func TestMarshalClusterConfigMessage(t *testing.T) {
  258. var quickCfg = &quick.Config{MaxCountScale: 10}
  259. if testing.Short() {
  260. quickCfg = nil
  261. }
  262. f := func(m1 ClusterConfigMessage) bool {
  263. return testMarshal(t, "clusterconfig", &m1, &ClusterConfigMessage{})
  264. }
  265. if err := quick.Check(f, quickCfg); err != nil {
  266. t.Error(err)
  267. }
  268. }
  269. func TestMarshalCloseMessage(t *testing.T) {
  270. var quickCfg = &quick.Config{MaxCountScale: 10}
  271. if testing.Short() {
  272. quickCfg = nil
  273. }
  274. f := func(m1 CloseMessage) bool {
  275. return testMarshal(t, "close", &m1, &CloseMessage{})
  276. }
  277. if err := quick.Check(f, quickCfg); err != nil {
  278. t.Error(err)
  279. }
  280. }
  281. type message interface {
  282. EncodeXDR(io.Writer) (int, error)
  283. DecodeXDR(io.Reader) error
  284. }
  285. func testMarshal(t *testing.T, prefix string, m1, m2 message) bool {
  286. var buf bytes.Buffer
  287. failed := func(bc []byte) {
  288. bs, _ := json.MarshalIndent(m1, "", " ")
  289. ioutil.WriteFile(prefix+"-1.txt", bs, 0644)
  290. bs, _ = json.MarshalIndent(m2, "", " ")
  291. ioutil.WriteFile(prefix+"-2.txt", bs, 0644)
  292. if len(bc) > 0 {
  293. f, _ := os.Create(prefix + "-data.txt")
  294. fmt.Fprint(f, hex.Dump(bc))
  295. f.Close()
  296. }
  297. }
  298. _, err := m1.EncodeXDR(&buf)
  299. if err == xdr.ErrElementSizeExceeded {
  300. return true
  301. }
  302. if err != nil {
  303. failed(nil)
  304. t.Fatal(err)
  305. }
  306. bc := make([]byte, len(buf.Bytes()))
  307. copy(bc, buf.Bytes())
  308. err = m2.DecodeXDR(&buf)
  309. if err != nil {
  310. failed(bc)
  311. t.Fatal(err)
  312. }
  313. ok := reflect.DeepEqual(m1, m2)
  314. if !ok {
  315. failed(bc)
  316. }
  317. return ok
  318. }