1
0

protocol_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // Copyright (C) 2014 The Protocol Authors.
  2. package protocol
  3. import (
  4. "bytes"
  5. "encoding/hex"
  6. "encoding/json"
  7. "flag"
  8. "fmt"
  9. "io"
  10. "io/ioutil"
  11. "os"
  12. "reflect"
  13. "strings"
  14. "testing"
  15. "testing/quick"
  16. "github.com/calmh/xdr"
  17. )
  18. var (
  19. c0ID = NewDeviceID([]byte{1})
  20. c1ID = NewDeviceID([]byte{2})
  21. quickCfg = &quick.Config{}
  22. )
  23. func TestMain(m *testing.M) {
  24. flag.Parse()
  25. if testing.Short() {
  26. quickCfg.MaxCount = 10
  27. }
  28. os.Exit(m.Run())
  29. }
  30. func TestHeaderFunctions(t *testing.T) {
  31. f := func(ver, id, typ int) bool {
  32. ver = int(uint(ver) % 16)
  33. id = int(uint(id) % 4096)
  34. typ = int(uint(typ) % 256)
  35. h0 := header{version: ver, msgID: id, msgType: typ}
  36. h1 := decodeHeader(encodeHeader(h0))
  37. return h0 == h1
  38. }
  39. if err := quick.Check(f, nil); err != nil {
  40. t.Error(err)
  41. }
  42. }
  43. func TestHeaderLayout(t *testing.T) {
  44. var e, a uint32
  45. // Version are the first four bits
  46. e = 0xf0000000
  47. a = encodeHeader(header{version: 0xf})
  48. if a != e {
  49. t.Errorf("Header layout incorrect; %08x != %08x", a, e)
  50. }
  51. // Message ID are the following 12 bits
  52. e = 0x0fff0000
  53. a = encodeHeader(header{msgID: 0xfff})
  54. if a != e {
  55. t.Errorf("Header layout incorrect; %08x != %08x", a, e)
  56. }
  57. // Type are the last 8 bits before reserved
  58. e = 0x0000ff00
  59. a = encodeHeader(header{msgType: 0xff})
  60. if a != e {
  61. t.Errorf("Header layout incorrect; %08x != %08x", a, e)
  62. }
  63. }
  64. func TestPing(t *testing.T) {
  65. ar, aw := io.Pipe()
  66. br, bw := io.Pipe()
  67. c0 := NewConnection(c0ID, ar, bw, newTestModel(), "name", CompressAlways).(wireFormatConnection).next.(*rawConnection)
  68. c0.Start()
  69. c1 := NewConnection(c1ID, br, aw, newTestModel(), "name", CompressAlways).(wireFormatConnection).next.(*rawConnection)
  70. c1.Start()
  71. c0.ClusterConfig(ClusterConfigMessage{})
  72. c1.ClusterConfig(ClusterConfigMessage{})
  73. if ok := c0.ping(); !ok {
  74. t.Error("c0 ping failed")
  75. }
  76. if ok := c1.ping(); !ok {
  77. t.Error("c1 ping failed")
  78. }
  79. }
  80. func TestVersionErr(t *testing.T) {
  81. m0 := newTestModel()
  82. m1 := newTestModel()
  83. ar, aw := io.Pipe()
  84. br, bw := io.Pipe()
  85. c0 := NewConnection(c0ID, ar, bw, m0, "name", CompressAlways).(wireFormatConnection).next.(*rawConnection)
  86. c0.Start()
  87. c1 := NewConnection(c1ID, br, aw, m1, "name", CompressAlways)
  88. c1.Start()
  89. c0.ClusterConfig(ClusterConfigMessage{})
  90. c1.ClusterConfig(ClusterConfigMessage{})
  91. w := xdr.NewWriter(c0.cw)
  92. w.WriteUint32(encodeHeader(header{
  93. version: 2,
  94. msgID: 0,
  95. msgType: 0,
  96. }))
  97. w.WriteUint32(0) // Avoids reader closing due to EOF
  98. if !m1.isClosed() {
  99. t.Error("Connection should close due to unknown version")
  100. }
  101. }
  102. func TestTypeErr(t *testing.T) {
  103. m0 := newTestModel()
  104. m1 := newTestModel()
  105. ar, aw := io.Pipe()
  106. br, bw := io.Pipe()
  107. c0 := NewConnection(c0ID, ar, bw, m0, "name", CompressAlways).(wireFormatConnection).next.(*rawConnection)
  108. c0.Start()
  109. c1 := NewConnection(c1ID, br, aw, m1, "name", CompressAlways)
  110. c1.Start()
  111. c0.ClusterConfig(ClusterConfigMessage{})
  112. c1.ClusterConfig(ClusterConfigMessage{})
  113. w := xdr.NewWriter(c0.cw)
  114. w.WriteUint32(encodeHeader(header{
  115. version: 0,
  116. msgID: 0,
  117. msgType: 42,
  118. }))
  119. w.WriteUint32(0) // Avoids reader closing due to EOF
  120. if !m1.isClosed() {
  121. t.Error("Connection should close due to unknown message type")
  122. }
  123. }
  124. func TestClose(t *testing.T) {
  125. m0 := newTestModel()
  126. m1 := newTestModel()
  127. ar, aw := io.Pipe()
  128. br, bw := io.Pipe()
  129. c0 := NewConnection(c0ID, ar, bw, m0, "name", CompressAlways).(wireFormatConnection).next.(*rawConnection)
  130. c0.Start()
  131. c1 := NewConnection(c1ID, br, aw, m1, "name", CompressAlways)
  132. c1.Start()
  133. c0.ClusterConfig(ClusterConfigMessage{})
  134. c1.ClusterConfig(ClusterConfigMessage{})
  135. c0.close(nil)
  136. <-c0.closed
  137. if !m0.isClosed() {
  138. t.Fatal("Connection should be closed")
  139. }
  140. // None of these should panic, some should return an error
  141. if c0.ping() {
  142. t.Error("Ping should not return true")
  143. }
  144. c0.Index("default", nil, 0, nil)
  145. c0.Index("default", nil, 0, nil)
  146. if _, err := c0.Request("default", "foo", 0, 0, nil, 0, nil); err == nil {
  147. t.Error("Request should return an error")
  148. }
  149. }
  150. func TestElementSizeExceededNested(t *testing.T) {
  151. m := ClusterConfigMessage{
  152. ClientName: "longstringlongstringlongstringinglongstringlongstringlonlongstringlongstringlon",
  153. }
  154. _, err := m.EncodeXDR(ioutil.Discard)
  155. if err == nil {
  156. t.Errorf("ID length %d > max 64, but no error", len(m.Folders[0].ID))
  157. }
  158. }
  159. func TestMarshalIndexMessage(t *testing.T) {
  160. f := func(m1 IndexMessage) bool {
  161. for i, f := range m1.Files {
  162. m1.Files[i].CachedSize = 0
  163. for j := range f.Blocks {
  164. f.Blocks[j].Offset = 0
  165. if len(f.Blocks[j].Hash) == 0 {
  166. f.Blocks[j].Hash = nil
  167. }
  168. }
  169. }
  170. return testMarshal(t, "index", &m1, &IndexMessage{})
  171. }
  172. if err := quick.Check(f, quickCfg); err != nil {
  173. t.Error(err)
  174. }
  175. }
  176. func TestMarshalRequestMessage(t *testing.T) {
  177. f := func(m1 RequestMessage) bool {
  178. return testMarshal(t, "request", &m1, &RequestMessage{})
  179. }
  180. if err := quick.Check(f, quickCfg); err != nil {
  181. t.Error(err)
  182. }
  183. }
  184. func TestMarshalResponseMessage(t *testing.T) {
  185. f := func(m1 ResponseMessage) bool {
  186. if len(m1.Data) == 0 {
  187. m1.Data = nil
  188. }
  189. return testMarshal(t, "response", &m1, &ResponseMessage{})
  190. }
  191. if err := quick.Check(f, quickCfg); err != nil {
  192. t.Error(err)
  193. }
  194. }
  195. func TestMarshalClusterConfigMessage(t *testing.T) {
  196. f := func(m1 ClusterConfigMessage) bool {
  197. return testMarshal(t, "clusterconfig", &m1, &ClusterConfigMessage{})
  198. }
  199. if err := quick.Check(f, quickCfg); err != nil {
  200. t.Error(err)
  201. }
  202. }
  203. func TestMarshalCloseMessage(t *testing.T) {
  204. f := func(m1 CloseMessage) bool {
  205. return testMarshal(t, "close", &m1, &CloseMessage{})
  206. }
  207. if err := quick.Check(f, quickCfg); err != nil {
  208. t.Error(err)
  209. }
  210. }
  211. type message interface {
  212. EncodeXDR(io.Writer) (int, error)
  213. DecodeXDR(io.Reader) error
  214. }
  215. func testMarshal(t *testing.T, prefix string, m1, m2 message) bool {
  216. var buf bytes.Buffer
  217. failed := func(bc []byte) {
  218. bs, _ := json.MarshalIndent(m1, "", " ")
  219. ioutil.WriteFile(prefix+"-1.txt", bs, 0644)
  220. bs, _ = json.MarshalIndent(m2, "", " ")
  221. ioutil.WriteFile(prefix+"-2.txt", bs, 0644)
  222. if len(bc) > 0 {
  223. f, _ := os.Create(prefix + "-data.txt")
  224. fmt.Fprint(f, hex.Dump(bc))
  225. f.Close()
  226. }
  227. }
  228. _, err := m1.EncodeXDR(&buf)
  229. if err != nil && strings.Contains(err.Error(), "exceeds size") {
  230. return true
  231. }
  232. if err != nil {
  233. failed(nil)
  234. t.Fatal(err)
  235. }
  236. bc := make([]byte, len(buf.Bytes()))
  237. copy(bc, buf.Bytes())
  238. err = m2.DecodeXDR(&buf)
  239. if err != nil {
  240. failed(bc)
  241. t.Fatal(err)
  242. }
  243. ok := reflect.DeepEqual(m1, m2)
  244. if !ok {
  245. failed(bc)
  246. }
  247. return ok
  248. }