protocol_test.go 8.3 KB

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