protocol_test.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. // Copyright (C) 2014 The Protocol Authors.
  2. package protocol
  3. import (
  4. "bytes"
  5. "encoding/json"
  6. "errors"
  7. "io"
  8. "io/ioutil"
  9. "strings"
  10. "testing"
  11. "testing/quick"
  12. "encoding/hex"
  13. "github.com/syncthing/syncthing/lib/rand"
  14. )
  15. var (
  16. c0ID = NewDeviceID([]byte{1})
  17. c1ID = NewDeviceID([]byte{2})
  18. quickCfg = &quick.Config{}
  19. )
  20. func TestPing(t *testing.T) {
  21. ar, aw := io.Pipe()
  22. br, bw := io.Pipe()
  23. c0 := NewConnection(c0ID, ar, bw, newTestModel(), "name", CompressAlways).(wireFormatConnection).Connection.(*rawConnection)
  24. c0.Start()
  25. c1 := NewConnection(c1ID, br, aw, newTestModel(), "name", CompressAlways).(wireFormatConnection).Connection.(*rawConnection)
  26. c1.Start()
  27. c0.ClusterConfig(ClusterConfig{})
  28. c1.ClusterConfig(ClusterConfig{})
  29. if ok := c0.ping(); !ok {
  30. t.Error("c0 ping failed")
  31. }
  32. if ok := c1.ping(); !ok {
  33. t.Error("c1 ping failed")
  34. }
  35. }
  36. func TestClose(t *testing.T) {
  37. m0 := newTestModel()
  38. m1 := newTestModel()
  39. ar, aw := io.Pipe()
  40. br, bw := io.Pipe()
  41. c0 := NewConnection(c0ID, ar, bw, m0, "name", CompressAlways).(wireFormatConnection).Connection.(*rawConnection)
  42. c0.Start()
  43. c1 := NewConnection(c1ID, br, aw, m1, "name", CompressAlways)
  44. c1.Start()
  45. c0.ClusterConfig(ClusterConfig{})
  46. c1.ClusterConfig(ClusterConfig{})
  47. c0.close(errors.New("manual close"))
  48. <-c0.closed
  49. if err := m0.closedError(); err == nil || !strings.Contains(err.Error(), "manual close") {
  50. t.Fatal("Connection should be closed")
  51. }
  52. // None of these should panic, some should return an error
  53. if c0.ping() {
  54. t.Error("Ping should not return true")
  55. }
  56. c0.Index("default", nil)
  57. c0.Index("default", nil)
  58. if _, err := c0.Request("default", "foo", 0, 0, nil, 0, false); err == nil {
  59. t.Error("Request should return an error")
  60. }
  61. }
  62. func TestMarshalIndexMessage(t *testing.T) {
  63. if testing.Short() {
  64. quickCfg.MaxCount = 10
  65. }
  66. f := func(m1 Index) bool {
  67. if len(m1.Files) == 0 {
  68. m1.Files = nil
  69. }
  70. for i, f := range m1.Files {
  71. if len(f.Blocks) == 0 {
  72. m1.Files[i].Blocks = nil
  73. } else {
  74. for j := range f.Blocks {
  75. f.Blocks[j].Offset = 0
  76. if len(f.Blocks[j].Hash) == 0 {
  77. f.Blocks[j].Hash = nil
  78. }
  79. }
  80. }
  81. if len(f.Version.Counters) == 0 {
  82. m1.Files[i].Version.Counters = nil
  83. }
  84. }
  85. return testMarshal(t, "index", &m1, &Index{})
  86. }
  87. if err := quick.Check(f, quickCfg); err != nil {
  88. t.Error(err)
  89. }
  90. }
  91. func TestMarshalRequestMessage(t *testing.T) {
  92. if testing.Short() {
  93. quickCfg.MaxCount = 10
  94. }
  95. f := func(m1 Request) bool {
  96. if len(m1.Hash) == 0 {
  97. m1.Hash = nil
  98. }
  99. return testMarshal(t, "request", &m1, &Request{})
  100. }
  101. if err := quick.Check(f, quickCfg); err != nil {
  102. t.Error(err)
  103. }
  104. }
  105. func TestMarshalResponseMessage(t *testing.T) {
  106. if testing.Short() {
  107. quickCfg.MaxCount = 10
  108. }
  109. f := func(m1 Response) bool {
  110. if len(m1.Data) == 0 {
  111. m1.Data = nil
  112. }
  113. return testMarshal(t, "response", &m1, &Response{})
  114. }
  115. if err := quick.Check(f, quickCfg); err != nil {
  116. t.Error(err)
  117. }
  118. }
  119. func TestMarshalClusterConfigMessage(t *testing.T) {
  120. if testing.Short() {
  121. quickCfg.MaxCount = 10
  122. }
  123. f := func(m1 ClusterConfig) bool {
  124. if len(m1.Folders) == 0 {
  125. m1.Folders = nil
  126. }
  127. for i := range m1.Folders {
  128. if len(m1.Folders[i].Devices) == 0 {
  129. m1.Folders[i].Devices = nil
  130. }
  131. }
  132. return testMarshal(t, "clusterconfig", &m1, &ClusterConfig{})
  133. }
  134. if err := quick.Check(f, quickCfg); err != nil {
  135. t.Error(err)
  136. }
  137. }
  138. func TestMarshalCloseMessage(t *testing.T) {
  139. if testing.Short() {
  140. quickCfg.MaxCount = 10
  141. }
  142. f := func(m1 Close) bool {
  143. return testMarshal(t, "close", &m1, &Close{})
  144. }
  145. if err := quick.Check(f, quickCfg); err != nil {
  146. t.Error(err)
  147. }
  148. }
  149. func TestMarshalFDPU(t *testing.T) {
  150. if testing.Short() {
  151. quickCfg.MaxCount = 10
  152. }
  153. f := func(m1 FileDownloadProgressUpdate) bool {
  154. if len(m1.Version.Counters) == 0 {
  155. m1.Version.Counters = nil
  156. }
  157. return testMarshal(t, "close", &m1, &FileDownloadProgressUpdate{})
  158. }
  159. if err := quick.Check(f, quickCfg); err != nil {
  160. t.Error(err)
  161. }
  162. }
  163. func TestUnmarshalFDPUv16v17(t *testing.T) {
  164. var fdpu FileDownloadProgressUpdate
  165. m0, _ := hex.DecodeString("08cda1e2e3011278f3918787f3b89b8af2958887f0aa9389f3a08588f3aa8f96f39aa8a5f48b9188f19286a0f3848da4f3aba799f3beb489f0a285b9f487b684f2a3bda2f48598b4f2938a89f2a28badf187a0a2f2aebdbdf4849494f4808fbbf2b3a2adf2bb95bff0a6ada4f198ab9af29a9c8bf1abb793f3baabb2f188a6ba1a0020bb9390f60220f6d9e42220b0c7e2b2fdffffffff0120fdb2dfcdfbffffffff0120cedab1d50120bd8784c0feffffffff0120ace99591fdffffffff0120eed7d09af9ffffffff01")
  166. if err := fdpu.Unmarshal(m0); err != nil {
  167. t.Fatal("Unmarshalling message from v0.14.16:", err)
  168. }
  169. m1, _ := hex.DecodeString("0880f1969905128401f099b192f0abb1b9f3b280aff19e9aa2f3b89e84f484b39df1a7a6b0f1aea4b1f0adac94f3b39caaf1939281f1928a8af0abb1b0f0a8b3b3f3a88e94f2bd85acf29c97a9f2969da6f0b7a188f1908ea2f09a9c9bf19d86a6f29aada8f389bb95f0bf9d88f1a09d89f1b1a4b5f29b9eabf298a59df1b2a589f2979ebdf0b69880f18986b21a440a1508c7d8fb8897ca93d90910e8c4d8e8f2f8f0ccee010a1508afa8ffd8c085b393c50110e5bdedc3bddefe9b0b0a1408a1bedddba4cac5da3c10b8e5d9958ca7e3ec19225ae2f88cb2f8ffffffff018ceda99cfbffffffff01b9c298a407e295e8e9fcffffffff01f3b9ade5fcffffffff01c08bfea9fdffffffff01a2c2e5e1ffffffffff0186dcc5dafdffffffff01e9ffc7e507c9d89db8fdffffffff01")
  170. if err := fdpu.Unmarshal(m1); err != nil {
  171. t.Fatal("Unmarshalling message from v0.14.16:", err)
  172. }
  173. }
  174. func testMarshal(t *testing.T, prefix string, m1, m2 message) bool {
  175. buf, err := m1.Marshal()
  176. if err != nil {
  177. t.Fatal(err)
  178. }
  179. err = m2.Unmarshal(buf)
  180. if err != nil {
  181. t.Fatal(err)
  182. }
  183. bs1, _ := json.MarshalIndent(m1, "", " ")
  184. bs2, _ := json.MarshalIndent(m2, "", " ")
  185. if !bytes.Equal(bs1, bs2) {
  186. ioutil.WriteFile(prefix+"-1.txt", bs1, 0644)
  187. ioutil.WriteFile(prefix+"-2.txt", bs2, 0644)
  188. return false
  189. }
  190. return true
  191. }
  192. func TestLZ4Compression(t *testing.T) {
  193. c := new(rawConnection)
  194. for i := 0; i < 10; i++ {
  195. dataLen := 150 + rand.Intn(150)
  196. data := make([]byte, dataLen)
  197. _, err := io.ReadFull(rand.Reader, data[100:])
  198. if err != nil {
  199. t.Fatal(err)
  200. }
  201. comp, err := c.lz4Compress(data)
  202. if err != nil {
  203. t.Errorf("compressing %d bytes: %v", dataLen, err)
  204. continue
  205. }
  206. res, err := c.lz4Decompress(comp)
  207. if err != nil {
  208. t.Errorf("decompressing %d bytes to %d: %v", len(comp), dataLen, err)
  209. continue
  210. }
  211. if len(res) != len(data) {
  212. t.Errorf("Incorrect len %d != expected %d", len(res), len(data))
  213. }
  214. if !bytes.Equal(data, res) {
  215. t.Error("Incorrect decompressed data")
  216. }
  217. t.Logf("OK #%d, %d -> %d -> %d", i, dataLen, len(comp), dataLen)
  218. }
  219. }
  220. func TestCheckFilename(t *testing.T) {
  221. cases := []struct {
  222. name string
  223. ok bool
  224. }{
  225. // Valid filenames
  226. {"foo", true},
  227. {"foo/bar/baz", true},
  228. {"foo/bar:baz", true}, // colon is ok in general, will be filtered on windows
  229. {`\`, true}, // path separator on the wire is forward slash, so as above
  230. {`\.`, true},
  231. {`\..`, true},
  232. {".foo", true},
  233. {"foo..", true},
  234. // Invalid filenames
  235. {"foo/..", false},
  236. {"foo/../bar", false},
  237. {"../foo/../bar", false},
  238. {"", false},
  239. {".", false},
  240. {"..", false},
  241. {"/", false},
  242. {"/.", false},
  243. {"/..", false},
  244. {"/foo", false},
  245. {"./foo", false},
  246. {"foo./", false},
  247. {"foo/.", false},
  248. {"foo/", false},
  249. }
  250. for _, tc := range cases {
  251. err := checkFilename(tc.name)
  252. if (err == nil) != tc.ok {
  253. t.Errorf("Unexpected result for checkFilename(%q): %v", tc.name, err)
  254. }
  255. }
  256. }
  257. func TestCheckConsistency(t *testing.T) {
  258. cases := []struct {
  259. fi FileInfo
  260. ok bool
  261. }{
  262. {
  263. // valid
  264. fi: FileInfo{
  265. Name: "foo",
  266. Type: FileInfoTypeFile,
  267. Blocks: []BlockInfo{{Size: 1234, Offset: 0, Hash: []byte{1, 2, 3, 4}}},
  268. },
  269. ok: true,
  270. },
  271. {
  272. // deleted with blocks
  273. fi: FileInfo{
  274. Name: "foo",
  275. Deleted: true,
  276. Type: FileInfoTypeFile,
  277. Blocks: []BlockInfo{{Size: 1234, Offset: 0, Hash: []byte{1, 2, 3, 4}}},
  278. },
  279. ok: false,
  280. },
  281. {
  282. // no blocks
  283. fi: FileInfo{
  284. Name: "foo",
  285. Type: FileInfoTypeFile,
  286. },
  287. ok: false,
  288. },
  289. {
  290. // directory with blocks
  291. fi: FileInfo{
  292. Name: "foo",
  293. Type: FileInfoTypeDirectory,
  294. Blocks: []BlockInfo{{Size: 1234, Offset: 0, Hash: []byte{1, 2, 3, 4}}},
  295. },
  296. ok: false,
  297. },
  298. }
  299. for _, tc := range cases {
  300. err := checkFileInfoConsistency(tc.fi)
  301. if tc.ok && err != nil {
  302. t.Errorf("Unexpected error %v (want nil) for %v", err, tc.fi)
  303. }
  304. if !tc.ok && err == nil {
  305. t.Errorf("Unexpected nil error for %v", tc.fi)
  306. }
  307. }
  308. }
  309. func TestBlockSize(t *testing.T) {
  310. cases := []struct {
  311. fileSize int64
  312. blockSize int
  313. }{
  314. {1 << KiB, 128 << KiB},
  315. {1 << MiB, 128 << KiB},
  316. {499 << MiB, 256 << KiB},
  317. {500 << MiB, 512 << KiB},
  318. {501 << MiB, 512 << KiB},
  319. {1 << GiB, 1 << MiB},
  320. {2 << GiB, 2 << MiB},
  321. {3 << GiB, 2 << MiB},
  322. {500 << GiB, 16 << MiB},
  323. {50000 << GiB, 16 << MiB},
  324. }
  325. for _, tc := range cases {
  326. size := BlockSize(tc.fileSize)
  327. if size != tc.blockSize {
  328. t.Errorf("BlockSize(%d), size=%d, expected %d", tc.fileSize, size, tc.blockSize)
  329. }
  330. }
  331. }
  332. var blockSize int
  333. func BenchmarkBlockSize(b *testing.B) {
  334. for i := 0; i < b.N; i++ {
  335. blockSize = BlockSize(16 << 30)
  336. }
  337. }