protocol_test.go 8.8 KB

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