protocol_test.go 8.8 KB

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