protocol_test.go 6.7 KB

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