protocol_test.go 4.5 KB

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