protocol_test.go 6.6 KB

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