protocol_test.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. // Copyright (C) 2014 The Protocol Authors.
  2. package protocol
  3. import (
  4. "bytes"
  5. "context"
  6. "crypto/sha256"
  7. "encoding/hex"
  8. "encoding/json"
  9. "errors"
  10. "io"
  11. "io/ioutil"
  12. "runtime"
  13. "sync"
  14. "testing"
  15. "testing/quick"
  16. "time"
  17. "github.com/syncthing/syncthing/lib/rand"
  18. "github.com/syncthing/syncthing/lib/testutils"
  19. )
  20. var (
  21. c0ID = NewDeviceID([]byte{1})
  22. c1ID = NewDeviceID([]byte{2})
  23. quickCfg = &quick.Config{}
  24. )
  25. func TestPing(t *testing.T) {
  26. ar, aw := io.Pipe()
  27. br, bw := io.Pipe()
  28. c0 := NewConnection(c0ID, ar, bw, newTestModel(), "name", CompressionAlways).(wireFormatConnection).Connection.(*rawConnection)
  29. c0.Start()
  30. c1 := NewConnection(c1ID, br, aw, newTestModel(), "name", CompressionAlways).(wireFormatConnection).Connection.(*rawConnection)
  31. c1.Start()
  32. c0.ClusterConfig(ClusterConfig{})
  33. c1.ClusterConfig(ClusterConfig{})
  34. if ok := c0.ping(); !ok {
  35. t.Error("c0 ping failed")
  36. }
  37. if ok := c1.ping(); !ok {
  38. t.Error("c1 ping failed")
  39. }
  40. }
  41. var errManual = errors.New("manual close")
  42. func TestClose(t *testing.T) {
  43. m0 := newTestModel()
  44. m1 := newTestModel()
  45. ar, aw := io.Pipe()
  46. br, bw := io.Pipe()
  47. c0 := NewConnection(c0ID, ar, bw, m0, "name", CompressionAlways).(wireFormatConnection).Connection.(*rawConnection)
  48. c0.Start()
  49. c1 := NewConnection(c1ID, br, aw, m1, "name", CompressionAlways)
  50. c1.Start()
  51. c0.ClusterConfig(ClusterConfig{})
  52. c1.ClusterConfig(ClusterConfig{})
  53. c0.internalClose(errManual)
  54. <-c0.closed
  55. if err := m0.closedError(); err != errManual {
  56. t.Fatal("Connection should be closed")
  57. }
  58. // None of these should panic, some should return an error
  59. if c0.ping() {
  60. t.Error("Ping should not return true")
  61. }
  62. ctx := context.Background()
  63. c0.Index(ctx, "default", nil)
  64. c0.Index(ctx, "default", nil)
  65. if _, err := c0.Request(ctx, "default", "foo", 0, 0, nil, 0, false); err == nil {
  66. t.Error("Request should return an error")
  67. }
  68. }
  69. // TestCloseOnBlockingSend checks that the connection does not deadlock when
  70. // Close is called while the underlying connection is broken (send blocks).
  71. // https://github.com/syncthing/syncthing/pull/5442
  72. func TestCloseOnBlockingSend(t *testing.T) {
  73. oldCloseTimeout := CloseTimeout
  74. CloseTimeout = 100 * time.Millisecond
  75. defer func() {
  76. CloseTimeout = oldCloseTimeout
  77. }()
  78. m := newTestModel()
  79. c := NewConnection(c0ID, &testutils.BlockingRW{}, &testutils.BlockingRW{}, m, "name", CompressionAlways).(wireFormatConnection).Connection.(*rawConnection)
  80. c.Start()
  81. wg := sync.WaitGroup{}
  82. wg.Add(1)
  83. go func() {
  84. c.ClusterConfig(ClusterConfig{})
  85. wg.Done()
  86. }()
  87. wg.Add(1)
  88. go func() {
  89. c.Close(errManual)
  90. wg.Done()
  91. }()
  92. // This simulates an error from ping timeout
  93. wg.Add(1)
  94. go func() {
  95. c.internalClose(ErrTimeout)
  96. wg.Done()
  97. }()
  98. done := make(chan struct{})
  99. go func() {
  100. wg.Wait()
  101. close(done)
  102. }()
  103. select {
  104. case <-done:
  105. case <-time.After(time.Second):
  106. t.Fatal("timed out before all functions returned")
  107. }
  108. }
  109. func TestCloseRace(t *testing.T) {
  110. indexReceived := make(chan struct{})
  111. unblockIndex := make(chan struct{})
  112. m0 := newTestModel()
  113. m0.indexFn = func(_ DeviceID, _ string, _ []FileInfo) {
  114. close(indexReceived)
  115. <-unblockIndex
  116. }
  117. m1 := newTestModel()
  118. ar, aw := io.Pipe()
  119. br, bw := io.Pipe()
  120. c0 := NewConnection(c0ID, ar, bw, m0, "c0", CompressionNever).(wireFormatConnection).Connection.(*rawConnection)
  121. c0.Start()
  122. c1 := NewConnection(c1ID, br, aw, m1, "c1", CompressionNever)
  123. c1.Start()
  124. c0.ClusterConfig(ClusterConfig{})
  125. c1.ClusterConfig(ClusterConfig{})
  126. c1.Index(context.Background(), "default", nil)
  127. select {
  128. case <-indexReceived:
  129. case <-time.After(time.Second):
  130. t.Fatal("timed out before receiving index")
  131. }
  132. go c0.internalClose(errManual)
  133. select {
  134. case <-c0.closed:
  135. case <-time.After(time.Second):
  136. t.Fatal("timed out before c0.closed was closed")
  137. }
  138. select {
  139. case <-m0.closedCh:
  140. t.Errorf("receiver.Closed called before receiver.Index")
  141. default:
  142. }
  143. close(unblockIndex)
  144. if err := m0.closedError(); err != errManual {
  145. t.Fatal("Connection should be closed")
  146. }
  147. }
  148. func TestClusterConfigFirst(t *testing.T) {
  149. m := newTestModel()
  150. c := NewConnection(c0ID, &testutils.BlockingRW{}, &testutils.NoopRW{}, m, "name", CompressionAlways).(wireFormatConnection).Connection.(*rawConnection)
  151. c.Start()
  152. select {
  153. case c.outbox <- asyncMessage{&Ping{}, nil}:
  154. t.Fatal("able to send ping before cluster config")
  155. case <-time.After(100 * time.Millisecond):
  156. // Allow some time for c.writerLoop to setup after c.Start
  157. }
  158. c.ClusterConfig(ClusterConfig{})
  159. done := make(chan struct{})
  160. if ok := c.send(context.Background(), &Ping{}, done); !ok {
  161. t.Fatal("send ping after cluster config returned false")
  162. }
  163. select {
  164. case <-done:
  165. case <-time.After(time.Second):
  166. t.Fatal("timed out before ping was sent")
  167. }
  168. done = make(chan struct{})
  169. go func() {
  170. c.internalClose(errManual)
  171. close(done)
  172. }()
  173. select {
  174. case <-done:
  175. case <-time.After(5 * time.Second):
  176. t.Fatal("Close didn't return before timeout")
  177. }
  178. if err := m.closedError(); err != errManual {
  179. t.Fatal("Connection should be closed")
  180. }
  181. }
  182. // TestCloseTimeout checks that calling Close times out and proceeds, if sending
  183. // the close message does not succeed.
  184. func TestCloseTimeout(t *testing.T) {
  185. oldCloseTimeout := CloseTimeout
  186. CloseTimeout = 100 * time.Millisecond
  187. defer func() {
  188. CloseTimeout = oldCloseTimeout
  189. }()
  190. m := newTestModel()
  191. c := NewConnection(c0ID, &testutils.BlockingRW{}, &testutils.BlockingRW{}, m, "name", CompressionAlways).(wireFormatConnection).Connection.(*rawConnection)
  192. c.Start()
  193. done := make(chan struct{})
  194. go func() {
  195. c.Close(errManual)
  196. close(done)
  197. }()
  198. select {
  199. case <-done:
  200. case <-time.After(5 * CloseTimeout):
  201. t.Fatal("timed out before Close returned")
  202. }
  203. }
  204. func TestMarshalIndexMessage(t *testing.T) {
  205. if testing.Short() {
  206. quickCfg.MaxCount = 10
  207. }
  208. f := func(m1 Index) bool {
  209. if len(m1.Files) == 0 {
  210. m1.Files = nil
  211. }
  212. for i, f := range m1.Files {
  213. if len(f.BlocksHash) == 0 {
  214. m1.Files[i].BlocksHash = nil
  215. }
  216. if len(f.VersionHash) == 0 {
  217. m1.Files[i].VersionHash = nil
  218. }
  219. if len(f.Blocks) == 0 {
  220. m1.Files[i].Blocks = nil
  221. } else {
  222. for j := range f.Blocks {
  223. f.Blocks[j].Offset = 0
  224. if len(f.Blocks[j].Hash) == 0 {
  225. f.Blocks[j].Hash = nil
  226. }
  227. }
  228. }
  229. if len(f.Version.Counters) == 0 {
  230. m1.Files[i].Version.Counters = nil
  231. }
  232. }
  233. return testMarshal(t, "index", &m1, &Index{})
  234. }
  235. if err := quick.Check(f, quickCfg); err != nil {
  236. t.Error(err)
  237. }
  238. }
  239. func TestMarshalRequestMessage(t *testing.T) {
  240. if testing.Short() {
  241. quickCfg.MaxCount = 10
  242. }
  243. f := func(m1 Request) bool {
  244. if len(m1.Hash) == 0 {
  245. m1.Hash = nil
  246. }
  247. return testMarshal(t, "request", &m1, &Request{})
  248. }
  249. if err := quick.Check(f, quickCfg); err != nil {
  250. t.Error(err)
  251. }
  252. }
  253. func TestMarshalResponseMessage(t *testing.T) {
  254. if testing.Short() {
  255. quickCfg.MaxCount = 10
  256. }
  257. f := func(m1 Response) bool {
  258. if len(m1.Data) == 0 {
  259. m1.Data = nil
  260. }
  261. return testMarshal(t, "response", &m1, &Response{})
  262. }
  263. if err := quick.Check(f, quickCfg); err != nil {
  264. t.Error(err)
  265. }
  266. }
  267. func TestMarshalClusterConfigMessage(t *testing.T) {
  268. if testing.Short() {
  269. quickCfg.MaxCount = 10
  270. }
  271. f := func(m1 ClusterConfig) bool {
  272. if len(m1.Folders) == 0 {
  273. m1.Folders = nil
  274. }
  275. for i := range m1.Folders {
  276. if len(m1.Folders[i].Devices) == 0 {
  277. m1.Folders[i].Devices = nil
  278. }
  279. for j := range m1.Folders[i].Devices {
  280. if len(m1.Folders[i].Devices[j].Addresses) == 0 {
  281. m1.Folders[i].Devices[j].Addresses = nil
  282. }
  283. }
  284. }
  285. return testMarshal(t, "clusterconfig", &m1, &ClusterConfig{})
  286. }
  287. if err := quick.Check(f, quickCfg); err != nil {
  288. t.Error(err)
  289. }
  290. }
  291. func TestMarshalCloseMessage(t *testing.T) {
  292. if testing.Short() {
  293. quickCfg.MaxCount = 10
  294. }
  295. f := func(m1 Close) bool {
  296. return testMarshal(t, "close", &m1, &Close{})
  297. }
  298. if err := quick.Check(f, quickCfg); err != nil {
  299. t.Error(err)
  300. }
  301. }
  302. func TestMarshalFDPU(t *testing.T) {
  303. if testing.Short() {
  304. quickCfg.MaxCount = 10
  305. }
  306. f := func(m1 FileDownloadProgressUpdate) bool {
  307. if len(m1.Version.Counters) == 0 {
  308. m1.Version.Counters = nil
  309. }
  310. if len(m1.BlockIndexes) == 0 {
  311. m1.BlockIndexes = nil
  312. }
  313. return testMarshal(t, "fdpu", &m1, &FileDownloadProgressUpdate{})
  314. }
  315. if err := quick.Check(f, quickCfg); err != nil {
  316. t.Error(err)
  317. }
  318. }
  319. func TestUnmarshalFDPUv16v17(t *testing.T) {
  320. var fdpu FileDownloadProgressUpdate
  321. m0, _ := hex.DecodeString("08cda1e2e3011278f3918787f3b89b8af2958887f0aa9389f3a08588f3aa8f96f39aa8a5f48b9188f19286a0f3848da4f3aba799f3beb489f0a285b9f487b684f2a3bda2f48598b4f2938a89f2a28badf187a0a2f2aebdbdf4849494f4808fbbf2b3a2adf2bb95bff0a6ada4f198ab9af29a9c8bf1abb793f3baabb2f188a6ba1a0020bb9390f60220f6d9e42220b0c7e2b2fdffffffff0120fdb2dfcdfbffffffff0120cedab1d50120bd8784c0feffffffff0120ace99591fdffffffff0120eed7d09af9ffffffff01")
  322. if err := fdpu.Unmarshal(m0); err != nil {
  323. t.Fatal("Unmarshalling message from v0.14.16:", err)
  324. }
  325. m1, _ := hex.DecodeString("0880f1969905128401f099b192f0abb1b9f3b280aff19e9aa2f3b89e84f484b39df1a7a6b0f1aea4b1f0adac94f3b39caaf1939281f1928a8af0abb1b0f0a8b3b3f3a88e94f2bd85acf29c97a9f2969da6f0b7a188f1908ea2f09a9c9bf19d86a6f29aada8f389bb95f0bf9d88f1a09d89f1b1a4b5f29b9eabf298a59df1b2a589f2979ebdf0b69880f18986b21a440a1508c7d8fb8897ca93d90910e8c4d8e8f2f8f0ccee010a1508afa8ffd8c085b393c50110e5bdedc3bddefe9b0b0a1408a1bedddba4cac5da3c10b8e5d9958ca7e3ec19225ae2f88cb2f8ffffffff018ceda99cfbffffffff01b9c298a407e295e8e9fcffffffff01f3b9ade5fcffffffff01c08bfea9fdffffffff01a2c2e5e1ffffffffff0186dcc5dafdffffffff01e9ffc7e507c9d89db8fdffffffff01")
  326. if err := fdpu.Unmarshal(m1); err != nil {
  327. t.Fatal("Unmarshalling message from v0.14.16:", err)
  328. }
  329. }
  330. func testMarshal(t *testing.T, prefix string, m1, m2 message) bool {
  331. buf, err := m1.Marshal()
  332. if err != nil {
  333. t.Fatal(err)
  334. }
  335. err = m2.Unmarshal(buf)
  336. if err != nil {
  337. t.Fatal(err)
  338. }
  339. bs1, _ := json.MarshalIndent(m1, "", " ")
  340. bs2, _ := json.MarshalIndent(m2, "", " ")
  341. if !bytes.Equal(bs1, bs2) {
  342. ioutil.WriteFile(prefix+"-1.txt", bs1, 0644)
  343. ioutil.WriteFile(prefix+"-2.txt", bs2, 0644)
  344. return false
  345. }
  346. return true
  347. }
  348. func TestLZ4Compression(t *testing.T) {
  349. c := new(rawConnection)
  350. for i := 0; i < 10; i++ {
  351. dataLen := 150 + rand.Intn(150)
  352. data := make([]byte, dataLen)
  353. _, err := io.ReadFull(rand.Reader, data[100:])
  354. if err != nil {
  355. t.Fatal(err)
  356. }
  357. comp, err := c.lz4Compress(data)
  358. if err != nil {
  359. t.Errorf("compressing %d bytes: %v", dataLen, err)
  360. continue
  361. }
  362. res, err := c.lz4Decompress(comp)
  363. if err != nil {
  364. t.Errorf("decompressing %d bytes to %d: %v", len(comp), dataLen, err)
  365. continue
  366. }
  367. if len(res) != len(data) {
  368. t.Errorf("Incorrect len %d != expected %d", len(res), len(data))
  369. }
  370. if !bytes.Equal(data, res) {
  371. t.Error("Incorrect decompressed data")
  372. }
  373. t.Logf("OK #%d, %d -> %d -> %d", i, dataLen, len(comp), dataLen)
  374. }
  375. }
  376. func TestStressLZ4CompressGrows(t *testing.T) {
  377. c := new(rawConnection)
  378. success := 0
  379. for i := 0; i < 100; i++ {
  380. // Create a slize that is precisely one min block size, fill it with
  381. // random data. This shouldn't compress at all, so will in fact
  382. // become larger when LZ4 does its thing.
  383. data := make([]byte, MinBlockSize)
  384. if _, err := rand.Reader.Read(data); err != nil {
  385. t.Fatal("randomness failure")
  386. }
  387. comp, err := c.lz4Compress(data)
  388. if err != nil {
  389. t.Fatal("unexpected compression error: ", err)
  390. }
  391. if len(comp) < len(data) {
  392. // data size should grow. We must have been really unlucky in
  393. // the random generation, try again.
  394. continue
  395. }
  396. // Putting it into the buffer pool shouldn't panic because the block
  397. // should come from there to begin with.
  398. BufferPool.Put(comp)
  399. success++
  400. }
  401. if success == 0 {
  402. t.Fatal("unable to find data that grows when compressed")
  403. }
  404. }
  405. func TestCheckFilename(t *testing.T) {
  406. cases := []struct {
  407. name string
  408. ok bool
  409. }{
  410. // Valid filenames
  411. {"foo", true},
  412. {"foo/bar/baz", true},
  413. {"foo/bar:baz", true}, // colon is ok in general, will be filtered on windows
  414. {`\`, true}, // path separator on the wire is forward slash, so as above
  415. {`\.`, true},
  416. {`\..`, true},
  417. {".foo", true},
  418. {"foo..", true},
  419. // Invalid filenames
  420. {"foo/..", false},
  421. {"foo/../bar", false},
  422. {"../foo/../bar", false},
  423. {"", false},
  424. {".", false},
  425. {"..", false},
  426. {"/", false},
  427. {"/.", false},
  428. {"/..", false},
  429. {"/foo", false},
  430. {"./foo", false},
  431. {"foo./", false},
  432. {"foo/.", false},
  433. {"foo/", false},
  434. }
  435. for _, tc := range cases {
  436. err := checkFilename(tc.name)
  437. if (err == nil) != tc.ok {
  438. t.Errorf("Unexpected result for checkFilename(%q): %v", tc.name, err)
  439. }
  440. }
  441. }
  442. func TestCheckConsistency(t *testing.T) {
  443. cases := []struct {
  444. fi FileInfo
  445. ok bool
  446. }{
  447. {
  448. // valid
  449. fi: FileInfo{
  450. Name: "foo",
  451. Type: FileInfoTypeFile,
  452. Blocks: []BlockInfo{{Size: 1234, Offset: 0, Hash: []byte{1, 2, 3, 4}}},
  453. },
  454. ok: true,
  455. },
  456. {
  457. // deleted with blocks
  458. fi: FileInfo{
  459. Name: "foo",
  460. Deleted: true,
  461. Type: FileInfoTypeFile,
  462. Blocks: []BlockInfo{{Size: 1234, Offset: 0, Hash: []byte{1, 2, 3, 4}}},
  463. },
  464. ok: false,
  465. },
  466. {
  467. // no blocks
  468. fi: FileInfo{
  469. Name: "foo",
  470. Type: FileInfoTypeFile,
  471. },
  472. ok: false,
  473. },
  474. {
  475. // directory with blocks
  476. fi: FileInfo{
  477. Name: "foo",
  478. Type: FileInfoTypeDirectory,
  479. Blocks: []BlockInfo{{Size: 1234, Offset: 0, Hash: []byte{1, 2, 3, 4}}},
  480. },
  481. ok: false,
  482. },
  483. }
  484. for _, tc := range cases {
  485. err := checkFileInfoConsistency(tc.fi)
  486. if tc.ok && err != nil {
  487. t.Errorf("Unexpected error %v (want nil) for %v", err, tc.fi)
  488. }
  489. if !tc.ok && err == nil {
  490. t.Errorf("Unexpected nil error for %v", tc.fi)
  491. }
  492. }
  493. }
  494. func TestBlockSize(t *testing.T) {
  495. cases := []struct {
  496. fileSize int64
  497. blockSize int
  498. }{
  499. {1 << KiB, 128 << KiB},
  500. {1 << MiB, 128 << KiB},
  501. {499 << MiB, 256 << KiB},
  502. {500 << MiB, 512 << KiB},
  503. {501 << MiB, 512 << KiB},
  504. {1 << GiB, 1 << MiB},
  505. {2 << GiB, 2 << MiB},
  506. {3 << GiB, 2 << MiB},
  507. {500 << GiB, 16 << MiB},
  508. {50000 << GiB, 16 << MiB},
  509. }
  510. for _, tc := range cases {
  511. size := BlockSize(tc.fileSize)
  512. if size != tc.blockSize {
  513. t.Errorf("BlockSize(%d), size=%d, expected %d", tc.fileSize, size, tc.blockSize)
  514. }
  515. }
  516. }
  517. var blockSize int
  518. func BenchmarkBlockSize(b *testing.B) {
  519. for i := 0; i < b.N; i++ {
  520. blockSize = BlockSize(16 << 30)
  521. }
  522. }
  523. func TestLocalFlagBits(t *testing.T) {
  524. var f FileInfo
  525. if f.IsIgnored() || f.MustRescan() || f.IsInvalid() {
  526. t.Error("file should have no weird bits set by default")
  527. }
  528. f.SetIgnored(42)
  529. if !f.IsIgnored() || f.MustRescan() || !f.IsInvalid() {
  530. t.Error("file should be ignored and invalid")
  531. }
  532. f.SetMustRescan(42)
  533. if f.IsIgnored() || !f.MustRescan() || !f.IsInvalid() {
  534. t.Error("file should be must-rescan and invalid")
  535. }
  536. f.SetUnsupported(42)
  537. if f.IsIgnored() || f.MustRescan() || !f.IsInvalid() {
  538. t.Error("file should be invalid")
  539. }
  540. }
  541. func TestIsEquivalent(t *testing.T) {
  542. b := func(v bool) *bool {
  543. return &v
  544. }
  545. type testCase struct {
  546. a FileInfo
  547. b FileInfo
  548. ignPerms *bool // nil means should not matter, we'll test both variants
  549. ignBlocks *bool
  550. ignFlags uint32
  551. eq bool
  552. }
  553. cases := []testCase{
  554. // Empty FileInfos are equivalent
  555. {eq: true},
  556. // Various basic attributes, all of which cause ineqality when
  557. // they differ
  558. {
  559. a: FileInfo{Name: "foo"},
  560. b: FileInfo{Name: "bar"},
  561. eq: false,
  562. },
  563. {
  564. a: FileInfo{Type: FileInfoTypeFile},
  565. b: FileInfo{Type: FileInfoTypeDirectory},
  566. eq: false,
  567. },
  568. {
  569. a: FileInfo{Size: 1234},
  570. b: FileInfo{Size: 2345},
  571. eq: false,
  572. },
  573. {
  574. a: FileInfo{Deleted: false},
  575. b: FileInfo{Deleted: true},
  576. eq: false,
  577. },
  578. {
  579. a: FileInfo{RawInvalid: false},
  580. b: FileInfo{RawInvalid: true},
  581. eq: false,
  582. },
  583. {
  584. a: FileInfo{ModifiedS: 1234},
  585. b: FileInfo{ModifiedS: 2345},
  586. eq: false,
  587. },
  588. {
  589. a: FileInfo{ModifiedNs: 1234},
  590. b: FileInfo{ModifiedNs: 2345},
  591. eq: false,
  592. },
  593. // Special handling of local flags and invalidity. "MustRescan"
  594. // files are never equivalent to each other. Otherwise, equivalence
  595. // is based just on whether the file becomes IsInvalid() or not, not
  596. // the specific reason or flag bits.
  597. {
  598. a: FileInfo{LocalFlags: FlagLocalMustRescan},
  599. b: FileInfo{LocalFlags: FlagLocalMustRescan},
  600. eq: false,
  601. },
  602. {
  603. a: FileInfo{RawInvalid: true},
  604. b: FileInfo{RawInvalid: true},
  605. eq: true,
  606. },
  607. {
  608. a: FileInfo{LocalFlags: FlagLocalUnsupported},
  609. b: FileInfo{LocalFlags: FlagLocalUnsupported},
  610. eq: true,
  611. },
  612. {
  613. a: FileInfo{RawInvalid: true},
  614. b: FileInfo{LocalFlags: FlagLocalUnsupported},
  615. eq: true,
  616. },
  617. {
  618. a: FileInfo{LocalFlags: 0},
  619. b: FileInfo{LocalFlags: FlagLocalReceiveOnly},
  620. eq: false,
  621. },
  622. {
  623. a: FileInfo{LocalFlags: 0},
  624. b: FileInfo{LocalFlags: FlagLocalReceiveOnly},
  625. ignFlags: FlagLocalReceiveOnly,
  626. eq: true,
  627. },
  628. // Difference in blocks is not OK
  629. {
  630. a: FileInfo{Blocks: []BlockInfo{{Hash: []byte{1, 2, 3, 4}}}},
  631. b: FileInfo{Blocks: []BlockInfo{{Hash: []byte{2, 3, 4, 5}}}},
  632. ignBlocks: b(false),
  633. eq: false,
  634. },
  635. // ... unless we say it is
  636. {
  637. a: FileInfo{Blocks: []BlockInfo{{Hash: []byte{1, 2, 3, 4}}}},
  638. b: FileInfo{Blocks: []BlockInfo{{Hash: []byte{2, 3, 4, 5}}}},
  639. ignBlocks: b(true),
  640. eq: true,
  641. },
  642. // Difference in permissions is not OK.
  643. {
  644. a: FileInfo{Permissions: 0444},
  645. b: FileInfo{Permissions: 0666},
  646. ignPerms: b(false),
  647. eq: false,
  648. },
  649. // ... unless we say it is
  650. {
  651. a: FileInfo{Permissions: 0666},
  652. b: FileInfo{Permissions: 0444},
  653. ignPerms: b(true),
  654. eq: true,
  655. },
  656. // These attributes are not checked at all
  657. {
  658. a: FileInfo{NoPermissions: false},
  659. b: FileInfo{NoPermissions: true},
  660. eq: true,
  661. },
  662. {
  663. a: FileInfo{Version: Vector{Counters: []Counter{{ID: 1, Value: 42}}}},
  664. b: FileInfo{Version: Vector{Counters: []Counter{{ID: 42, Value: 1}}}},
  665. eq: true,
  666. },
  667. {
  668. a: FileInfo{Sequence: 1},
  669. b: FileInfo{Sequence: 2},
  670. eq: true,
  671. },
  672. // The block size is not checked (but this would fail the blocks
  673. // check in real world)
  674. {
  675. a: FileInfo{RawBlockSize: 1},
  676. b: FileInfo{RawBlockSize: 2},
  677. eq: true,
  678. },
  679. // The symlink target is checked for symlinks
  680. {
  681. a: FileInfo{Type: FileInfoTypeSymlink, SymlinkTarget: "a"},
  682. b: FileInfo{Type: FileInfoTypeSymlink, SymlinkTarget: "b"},
  683. eq: false,
  684. },
  685. // ... but not for non-symlinks
  686. {
  687. a: FileInfo{Type: FileInfoTypeFile, SymlinkTarget: "a"},
  688. b: FileInfo{Type: FileInfoTypeFile, SymlinkTarget: "b"},
  689. eq: true,
  690. },
  691. }
  692. if runtime.GOOS == "windows" {
  693. // On windows we only check the user writable bit of the permission
  694. // set, so these are equivalent.
  695. cases = append(cases, testCase{
  696. a: FileInfo{Permissions: 0777},
  697. b: FileInfo{Permissions: 0600},
  698. ignPerms: b(false),
  699. eq: true,
  700. })
  701. }
  702. for i, tc := range cases {
  703. // Check the standard attributes with all permutations of the
  704. // special ignore flags, unless the value of those flags are given
  705. // in the tests.
  706. for _, ignPerms := range []bool{true, false} {
  707. for _, ignBlocks := range []bool{true, false} {
  708. if tc.ignPerms != nil && *tc.ignPerms != ignPerms {
  709. continue
  710. }
  711. if tc.ignBlocks != nil && *tc.ignBlocks != ignBlocks {
  712. continue
  713. }
  714. if res := tc.a.isEquivalent(tc.b, 0, ignPerms, ignBlocks, tc.ignFlags); res != tc.eq {
  715. t.Errorf("Case %d:\na: %v\nb: %v\na.IsEquivalent(b, %v, %v) => %v, expected %v", i, tc.a, tc.b, ignPerms, ignBlocks, res, tc.eq)
  716. }
  717. if res := tc.b.isEquivalent(tc.a, 0, ignPerms, ignBlocks, tc.ignFlags); res != tc.eq {
  718. t.Errorf("Case %d:\na: %v\nb: %v\nb.IsEquivalent(a, %v, %v) => %v, expected %v", i, tc.a, tc.b, ignPerms, ignBlocks, res, tc.eq)
  719. }
  720. }
  721. }
  722. }
  723. }
  724. func TestSha256OfEmptyBlock(t *testing.T) {
  725. // every block size should have a correct entry in sha256OfEmptyBlock
  726. for blockSize := MinBlockSize; blockSize <= MaxBlockSize; blockSize *= 2 {
  727. expected := sha256.Sum256(make([]byte, blockSize))
  728. if sha256OfEmptyBlock[blockSize] != expected {
  729. t.Error("missing or wrong hash for block of size", blockSize)
  730. }
  731. }
  732. }
  733. // TestClusterConfigAfterClose checks that ClusterConfig does not deadlock when
  734. // ClusterConfig is called on a closed connection.
  735. func TestClusterConfigAfterClose(t *testing.T) {
  736. m := newTestModel()
  737. c := NewConnection(c0ID, &testutils.BlockingRW{}, &testutils.BlockingRW{}, m, "name", CompressionAlways).(wireFormatConnection).Connection.(*rawConnection)
  738. c.Start()
  739. c.internalClose(errManual)
  740. done := make(chan struct{})
  741. go func() {
  742. c.ClusterConfig(ClusterConfig{})
  743. close(done)
  744. }()
  745. select {
  746. case <-done:
  747. case <-time.After(time.Second):
  748. t.Fatal("timed out before Cluster Config returned")
  749. }
  750. }
  751. func TestDispatcherToCloseDeadlock(t *testing.T) {
  752. // Verify that we don't deadlock when calling Close() from within one of
  753. // the model callbacks (ClusterConfig).
  754. m := newTestModel()
  755. c := NewConnection(c0ID, &testutils.BlockingRW{}, &testutils.NoopRW{}, m, "name", CompressionAlways).(wireFormatConnection).Connection.(*rawConnection)
  756. m.ccFn = func(devID DeviceID, cc ClusterConfig) {
  757. c.Close(errManual)
  758. }
  759. c.Start()
  760. c.inbox <- &ClusterConfig{}
  761. select {
  762. case <-c.dispatcherLoopStopped:
  763. case <-time.After(time.Second):
  764. t.Fatal("timed out before dispatcher loop terminated")
  765. }
  766. }
  767. func TestBlocksEqual(t *testing.T) {
  768. blocksOne := []BlockInfo{{Hash: []byte{1, 2, 3, 4}}}
  769. blocksTwo := []BlockInfo{{Hash: []byte{5, 6, 7, 8}}}
  770. hashOne := []byte{42, 42, 42, 42}
  771. hashTwo := []byte{29, 29, 29, 29}
  772. cases := []struct {
  773. b1 []BlockInfo
  774. h1 []byte
  775. b2 []BlockInfo
  776. h2 []byte
  777. eq bool
  778. }{
  779. {blocksOne, hashOne, blocksOne, hashOne, true}, // everything equal
  780. {blocksOne, hashOne, blocksTwo, hashTwo, false}, // nothing equal
  781. {blocksOne, hashOne, blocksOne, nil, true}, // blocks compared
  782. {blocksOne, nil, blocksOne, nil, true}, // blocks compared
  783. {blocksOne, nil, blocksTwo, nil, false}, // blocks compared
  784. {blocksOne, hashOne, blocksTwo, hashOne, true}, // hashes equal, blocks not looked at
  785. {blocksOne, hashOne, blocksOne, hashTwo, true}, // hashes different, blocks compared
  786. {blocksOne, hashOne, blocksTwo, hashTwo, false}, // hashes different, blocks compared
  787. {blocksOne, hashOne, nil, nil, false}, // blocks is different from no blocks
  788. {blocksOne, nil, nil, nil, false}, // blocks is different from no blocks
  789. {nil, hashOne, nil, nil, true}, // nil blocks are equal, even of one side has a hash
  790. }
  791. for _, tc := range cases {
  792. f1 := FileInfo{Blocks: tc.b1, BlocksHash: tc.h1}
  793. f2 := FileInfo{Blocks: tc.b2, BlocksHash: tc.h2}
  794. if !f1.BlocksEqual(f1) {
  795. t.Error("f1 is always equal to itself", f1)
  796. }
  797. if !f2.BlocksEqual(f2) {
  798. t.Error("f2 is always equal to itself", f2)
  799. }
  800. if res := f1.BlocksEqual(f2); res != tc.eq {
  801. t.Log("f1", f1.BlocksHash, f1.Blocks)
  802. t.Log("f2", f2.BlocksHash, f2.Blocks)
  803. t.Errorf("f1.BlocksEqual(f2) == %v but should be %v", res, tc.eq)
  804. }
  805. if res := f2.BlocksEqual(f1); res != tc.eq {
  806. t.Log("f1", f1.BlocksHash, f1.Blocks)
  807. t.Log("f2", f2.BlocksHash, f2.Blocks)
  808. t.Errorf("f2.BlocksEqual(f1) == %v but should be %v", res, tc.eq)
  809. }
  810. }
  811. }
  812. func TestIndexIDString(t *testing.T) {
  813. // Index ID is a 64 bit, zero padded hex integer.
  814. var i IndexID = 42
  815. if i.String() != "0x000000000000002A" {
  816. t.Error(i.String())
  817. }
  818. }