protocol_test.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  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", CompressAlways).(wireFormatConnection).Connection.(*rawConnection)
  29. c0.Start()
  30. c1 := NewConnection(c1ID, br, aw, newTestModel(), "name", CompressAlways).(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", CompressAlways).(wireFormatConnection).Connection.(*rawConnection)
  48. c0.Start()
  49. c1 := NewConnection(c1ID, br, aw, m1, "name", CompressAlways)
  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", CompressAlways).(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", CompressNever).(wireFormatConnection).Connection.(*rawConnection)
  121. c0.Start()
  122. c1 := NewConnection(c1ID, br, aw, m1, "c1", CompressNever)
  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", CompressAlways).(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", CompressAlways).(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.Blocks) == 0 {
  214. m1.Files[i].Blocks = nil
  215. } else {
  216. for j := range f.Blocks {
  217. f.Blocks[j].Offset = 0
  218. if len(f.Blocks[j].Hash) == 0 {
  219. f.Blocks[j].Hash = nil
  220. }
  221. }
  222. }
  223. if len(f.Version.Counters) == 0 {
  224. m1.Files[i].Version.Counters = nil
  225. }
  226. }
  227. return testMarshal(t, "index", &m1, &Index{})
  228. }
  229. if err := quick.Check(f, quickCfg); err != nil {
  230. t.Error(err)
  231. }
  232. }
  233. func TestMarshalRequestMessage(t *testing.T) {
  234. if testing.Short() {
  235. quickCfg.MaxCount = 10
  236. }
  237. f := func(m1 Request) bool {
  238. if len(m1.Hash) == 0 {
  239. m1.Hash = nil
  240. }
  241. return testMarshal(t, "request", &m1, &Request{})
  242. }
  243. if err := quick.Check(f, quickCfg); err != nil {
  244. t.Error(err)
  245. }
  246. }
  247. func TestMarshalResponseMessage(t *testing.T) {
  248. if testing.Short() {
  249. quickCfg.MaxCount = 10
  250. }
  251. f := func(m1 Response) bool {
  252. if len(m1.Data) == 0 {
  253. m1.Data = nil
  254. }
  255. return testMarshal(t, "response", &m1, &Response{})
  256. }
  257. if err := quick.Check(f, quickCfg); err != nil {
  258. t.Error(err)
  259. }
  260. }
  261. func TestMarshalClusterConfigMessage(t *testing.T) {
  262. if testing.Short() {
  263. quickCfg.MaxCount = 10
  264. }
  265. f := func(m1 ClusterConfig) bool {
  266. if len(m1.Folders) == 0 {
  267. m1.Folders = nil
  268. }
  269. for i := range m1.Folders {
  270. if len(m1.Folders[i].Devices) == 0 {
  271. m1.Folders[i].Devices = nil
  272. }
  273. }
  274. return testMarshal(t, "clusterconfig", &m1, &ClusterConfig{})
  275. }
  276. if err := quick.Check(f, quickCfg); err != nil {
  277. t.Error(err)
  278. }
  279. }
  280. func TestMarshalCloseMessage(t *testing.T) {
  281. if testing.Short() {
  282. quickCfg.MaxCount = 10
  283. }
  284. f := func(m1 Close) bool {
  285. return testMarshal(t, "close", &m1, &Close{})
  286. }
  287. if err := quick.Check(f, quickCfg); err != nil {
  288. t.Error(err)
  289. }
  290. }
  291. func TestMarshalFDPU(t *testing.T) {
  292. if testing.Short() {
  293. quickCfg.MaxCount = 10
  294. }
  295. f := func(m1 FileDownloadProgressUpdate) bool {
  296. if len(m1.Version.Counters) == 0 {
  297. m1.Version.Counters = nil
  298. }
  299. return testMarshal(t, "close", &m1, &FileDownloadProgressUpdate{})
  300. }
  301. if err := quick.Check(f, quickCfg); err != nil {
  302. t.Error(err)
  303. }
  304. }
  305. func TestUnmarshalFDPUv16v17(t *testing.T) {
  306. var fdpu FileDownloadProgressUpdate
  307. m0, _ := hex.DecodeString("08cda1e2e3011278f3918787f3b89b8af2958887f0aa9389f3a08588f3aa8f96f39aa8a5f48b9188f19286a0f3848da4f3aba799f3beb489f0a285b9f487b684f2a3bda2f48598b4f2938a89f2a28badf187a0a2f2aebdbdf4849494f4808fbbf2b3a2adf2bb95bff0a6ada4f198ab9af29a9c8bf1abb793f3baabb2f188a6ba1a0020bb9390f60220f6d9e42220b0c7e2b2fdffffffff0120fdb2dfcdfbffffffff0120cedab1d50120bd8784c0feffffffff0120ace99591fdffffffff0120eed7d09af9ffffffff01")
  308. if err := fdpu.Unmarshal(m0); err != nil {
  309. t.Fatal("Unmarshalling message from v0.14.16:", err)
  310. }
  311. m1, _ := hex.DecodeString("0880f1969905128401f099b192f0abb1b9f3b280aff19e9aa2f3b89e84f484b39df1a7a6b0f1aea4b1f0adac94f3b39caaf1939281f1928a8af0abb1b0f0a8b3b3f3a88e94f2bd85acf29c97a9f2969da6f0b7a188f1908ea2f09a9c9bf19d86a6f29aada8f389bb95f0bf9d88f1a09d89f1b1a4b5f29b9eabf298a59df1b2a589f2979ebdf0b69880f18986b21a440a1508c7d8fb8897ca93d90910e8c4d8e8f2f8f0ccee010a1508afa8ffd8c085b393c50110e5bdedc3bddefe9b0b0a1408a1bedddba4cac5da3c10b8e5d9958ca7e3ec19225ae2f88cb2f8ffffffff018ceda99cfbffffffff01b9c298a407e295e8e9fcffffffff01f3b9ade5fcffffffff01c08bfea9fdffffffff01a2c2e5e1ffffffffff0186dcc5dafdffffffff01e9ffc7e507c9d89db8fdffffffff01")
  312. if err := fdpu.Unmarshal(m1); err != nil {
  313. t.Fatal("Unmarshalling message from v0.14.16:", err)
  314. }
  315. }
  316. func testMarshal(t *testing.T, prefix string, m1, m2 message) bool {
  317. buf, err := m1.Marshal()
  318. if err != nil {
  319. t.Fatal(err)
  320. }
  321. err = m2.Unmarshal(buf)
  322. if err != nil {
  323. t.Fatal(err)
  324. }
  325. bs1, _ := json.MarshalIndent(m1, "", " ")
  326. bs2, _ := json.MarshalIndent(m2, "", " ")
  327. if !bytes.Equal(bs1, bs2) {
  328. ioutil.WriteFile(prefix+"-1.txt", bs1, 0644)
  329. ioutil.WriteFile(prefix+"-2.txt", bs2, 0644)
  330. return false
  331. }
  332. return true
  333. }
  334. func TestLZ4Compression(t *testing.T) {
  335. c := new(rawConnection)
  336. for i := 0; i < 10; i++ {
  337. dataLen := 150 + rand.Intn(150)
  338. data := make([]byte, dataLen)
  339. _, err := io.ReadFull(rand.Reader, data[100:])
  340. if err != nil {
  341. t.Fatal(err)
  342. }
  343. comp, err := c.lz4Compress(data)
  344. if err != nil {
  345. t.Errorf("compressing %d bytes: %v", dataLen, err)
  346. continue
  347. }
  348. res, err := c.lz4Decompress(comp)
  349. if err != nil {
  350. t.Errorf("decompressing %d bytes to %d: %v", len(comp), dataLen, err)
  351. continue
  352. }
  353. if len(res) != len(data) {
  354. t.Errorf("Incorrect len %d != expected %d", len(res), len(data))
  355. }
  356. if !bytes.Equal(data, res) {
  357. t.Error("Incorrect decompressed data")
  358. }
  359. t.Logf("OK #%d, %d -> %d -> %d", i, dataLen, len(comp), dataLen)
  360. }
  361. }
  362. func TestStressLZ4CompressGrows(t *testing.T) {
  363. c := new(rawConnection)
  364. success := 0
  365. for i := 0; i < 100; i++ {
  366. // Create a slize that is precisely one min block size, fill it with
  367. // random data. This shouldn't compress at all, so will in fact
  368. // become larger when LZ4 does its thing.
  369. data := make([]byte, MinBlockSize)
  370. if _, err := rand.Reader.Read(data); err != nil {
  371. t.Fatal("randomness failure")
  372. }
  373. comp, err := c.lz4Compress(data)
  374. if err != nil {
  375. t.Fatal("unexpected compression error: ", err)
  376. }
  377. if len(comp) < len(data) {
  378. // data size should grow. We must have been really unlucky in
  379. // the random generation, try again.
  380. continue
  381. }
  382. // Putting it into the buffer pool shouldn't panic because the block
  383. // should come from there to begin with.
  384. BufferPool.Put(comp)
  385. success++
  386. }
  387. if success == 0 {
  388. t.Fatal("unable to find data that grows when compressed")
  389. }
  390. }
  391. func TestCheckFilename(t *testing.T) {
  392. cases := []struct {
  393. name string
  394. ok bool
  395. }{
  396. // Valid filenames
  397. {"foo", true},
  398. {"foo/bar/baz", true},
  399. {"foo/bar:baz", true}, // colon is ok in general, will be filtered on windows
  400. {`\`, true}, // path separator on the wire is forward slash, so as above
  401. {`\.`, true},
  402. {`\..`, true},
  403. {".foo", true},
  404. {"foo..", true},
  405. // Invalid filenames
  406. {"foo/..", false},
  407. {"foo/../bar", false},
  408. {"../foo/../bar", false},
  409. {"", false},
  410. {".", false},
  411. {"..", false},
  412. {"/", false},
  413. {"/.", false},
  414. {"/..", false},
  415. {"/foo", false},
  416. {"./foo", false},
  417. {"foo./", false},
  418. {"foo/.", false},
  419. {"foo/", false},
  420. }
  421. for _, tc := range cases {
  422. err := checkFilename(tc.name)
  423. if (err == nil) != tc.ok {
  424. t.Errorf("Unexpected result for checkFilename(%q): %v", tc.name, err)
  425. }
  426. }
  427. }
  428. func TestCheckConsistency(t *testing.T) {
  429. cases := []struct {
  430. fi FileInfo
  431. ok bool
  432. }{
  433. {
  434. // valid
  435. fi: FileInfo{
  436. Name: "foo",
  437. Type: FileInfoTypeFile,
  438. Blocks: []BlockInfo{{Size: 1234, Offset: 0, Hash: []byte{1, 2, 3, 4}}},
  439. },
  440. ok: true,
  441. },
  442. {
  443. // deleted with blocks
  444. fi: FileInfo{
  445. Name: "foo",
  446. Deleted: true,
  447. Type: FileInfoTypeFile,
  448. Blocks: []BlockInfo{{Size: 1234, Offset: 0, Hash: []byte{1, 2, 3, 4}}},
  449. },
  450. ok: false,
  451. },
  452. {
  453. // no blocks
  454. fi: FileInfo{
  455. Name: "foo",
  456. Type: FileInfoTypeFile,
  457. },
  458. ok: false,
  459. },
  460. {
  461. // directory with blocks
  462. fi: FileInfo{
  463. Name: "foo",
  464. Type: FileInfoTypeDirectory,
  465. Blocks: []BlockInfo{{Size: 1234, Offset: 0, Hash: []byte{1, 2, 3, 4}}},
  466. },
  467. ok: false,
  468. },
  469. }
  470. for _, tc := range cases {
  471. err := checkFileInfoConsistency(tc.fi)
  472. if tc.ok && err != nil {
  473. t.Errorf("Unexpected error %v (want nil) for %v", err, tc.fi)
  474. }
  475. if !tc.ok && err == nil {
  476. t.Errorf("Unexpected nil error for %v", tc.fi)
  477. }
  478. }
  479. }
  480. func TestBlockSize(t *testing.T) {
  481. cases := []struct {
  482. fileSize int64
  483. blockSize int
  484. }{
  485. {1 << KiB, 128 << KiB},
  486. {1 << MiB, 128 << KiB},
  487. {499 << MiB, 256 << KiB},
  488. {500 << MiB, 512 << KiB},
  489. {501 << MiB, 512 << KiB},
  490. {1 << GiB, 1 << MiB},
  491. {2 << GiB, 2 << MiB},
  492. {3 << GiB, 2 << MiB},
  493. {500 << GiB, 16 << MiB},
  494. {50000 << GiB, 16 << MiB},
  495. }
  496. for _, tc := range cases {
  497. size := BlockSize(tc.fileSize)
  498. if size != tc.blockSize {
  499. t.Errorf("BlockSize(%d), size=%d, expected %d", tc.fileSize, size, tc.blockSize)
  500. }
  501. }
  502. }
  503. var blockSize int
  504. func BenchmarkBlockSize(b *testing.B) {
  505. for i := 0; i < b.N; i++ {
  506. blockSize = BlockSize(16 << 30)
  507. }
  508. }
  509. func TestLocalFlagBits(t *testing.T) {
  510. var f FileInfo
  511. if f.IsIgnored() || f.MustRescan() || f.IsInvalid() {
  512. t.Error("file should have no weird bits set by default")
  513. }
  514. f.SetIgnored(42)
  515. if !f.IsIgnored() || f.MustRescan() || !f.IsInvalid() {
  516. t.Error("file should be ignored and invalid")
  517. }
  518. f.SetMustRescan(42)
  519. if f.IsIgnored() || !f.MustRescan() || !f.IsInvalid() {
  520. t.Error("file should be must-rescan and invalid")
  521. }
  522. f.SetUnsupported(42)
  523. if f.IsIgnored() || f.MustRescan() || !f.IsInvalid() {
  524. t.Error("file should be invalid")
  525. }
  526. }
  527. func TestIsEquivalent(t *testing.T) {
  528. b := func(v bool) *bool {
  529. return &v
  530. }
  531. type testCase struct {
  532. a FileInfo
  533. b FileInfo
  534. ignPerms *bool // nil means should not matter, we'll test both variants
  535. ignBlocks *bool
  536. ignFlags uint32
  537. eq bool
  538. }
  539. cases := []testCase{
  540. // Empty FileInfos are equivalent
  541. {eq: true},
  542. // Various basic attributes, all of which cause ineqality when
  543. // they differ
  544. {
  545. a: FileInfo{Name: "foo"},
  546. b: FileInfo{Name: "bar"},
  547. eq: false,
  548. },
  549. {
  550. a: FileInfo{Type: FileInfoTypeFile},
  551. b: FileInfo{Type: FileInfoTypeDirectory},
  552. eq: false,
  553. },
  554. {
  555. a: FileInfo{Size: 1234},
  556. b: FileInfo{Size: 2345},
  557. eq: false,
  558. },
  559. {
  560. a: FileInfo{Deleted: false},
  561. b: FileInfo{Deleted: true},
  562. eq: false,
  563. },
  564. {
  565. a: FileInfo{RawInvalid: false},
  566. b: FileInfo{RawInvalid: true},
  567. eq: false,
  568. },
  569. {
  570. a: FileInfo{ModifiedS: 1234},
  571. b: FileInfo{ModifiedS: 2345},
  572. eq: false,
  573. },
  574. {
  575. a: FileInfo{ModifiedNs: 1234},
  576. b: FileInfo{ModifiedNs: 2345},
  577. eq: false,
  578. },
  579. // Special handling of local flags and invalidity. "MustRescan"
  580. // files are never equivalent to each other. Otherwise, equivalence
  581. // is based just on whether the file becomes IsInvalid() or not, not
  582. // the specific reason or flag bits.
  583. {
  584. a: FileInfo{LocalFlags: FlagLocalMustRescan},
  585. b: FileInfo{LocalFlags: FlagLocalMustRescan},
  586. eq: false,
  587. },
  588. {
  589. a: FileInfo{RawInvalid: true},
  590. b: FileInfo{RawInvalid: true},
  591. eq: true,
  592. },
  593. {
  594. a: FileInfo{LocalFlags: FlagLocalUnsupported},
  595. b: FileInfo{LocalFlags: FlagLocalUnsupported},
  596. eq: true,
  597. },
  598. {
  599. a: FileInfo{RawInvalid: true},
  600. b: FileInfo{LocalFlags: FlagLocalUnsupported},
  601. eq: true,
  602. },
  603. {
  604. a: FileInfo{LocalFlags: 0},
  605. b: FileInfo{LocalFlags: FlagLocalReceiveOnly},
  606. eq: false,
  607. },
  608. {
  609. a: FileInfo{LocalFlags: 0},
  610. b: FileInfo{LocalFlags: FlagLocalReceiveOnly},
  611. ignFlags: FlagLocalReceiveOnly,
  612. eq: true,
  613. },
  614. // Difference in blocks is not OK
  615. {
  616. a: FileInfo{Blocks: []BlockInfo{{Hash: []byte{1, 2, 3, 4}}}},
  617. b: FileInfo{Blocks: []BlockInfo{{Hash: []byte{2, 3, 4, 5}}}},
  618. ignBlocks: b(false),
  619. eq: false,
  620. },
  621. // ... unless we say it is
  622. {
  623. a: FileInfo{Blocks: []BlockInfo{{Hash: []byte{1, 2, 3, 4}}}},
  624. b: FileInfo{Blocks: []BlockInfo{{Hash: []byte{2, 3, 4, 5}}}},
  625. ignBlocks: b(true),
  626. eq: true,
  627. },
  628. // Difference in permissions is not OK.
  629. {
  630. a: FileInfo{Permissions: 0444},
  631. b: FileInfo{Permissions: 0666},
  632. ignPerms: b(false),
  633. eq: false,
  634. },
  635. // ... unless we say it is
  636. {
  637. a: FileInfo{Permissions: 0666},
  638. b: FileInfo{Permissions: 0444},
  639. ignPerms: b(true),
  640. eq: true,
  641. },
  642. // These attributes are not checked at all
  643. {
  644. a: FileInfo{NoPermissions: false},
  645. b: FileInfo{NoPermissions: true},
  646. eq: true,
  647. },
  648. {
  649. a: FileInfo{Version: Vector{Counters: []Counter{{ID: 1, Value: 42}}}},
  650. b: FileInfo{Version: Vector{Counters: []Counter{{ID: 42, Value: 1}}}},
  651. eq: true,
  652. },
  653. {
  654. a: FileInfo{Sequence: 1},
  655. b: FileInfo{Sequence: 2},
  656. eq: true,
  657. },
  658. // The block size is not checked (but this would fail the blocks
  659. // check in real world)
  660. {
  661. a: FileInfo{RawBlockSize: 1},
  662. b: FileInfo{RawBlockSize: 2},
  663. eq: true,
  664. },
  665. // The symlink target is checked for symlinks
  666. {
  667. a: FileInfo{Type: FileInfoTypeSymlink, SymlinkTarget: "a"},
  668. b: FileInfo{Type: FileInfoTypeSymlink, SymlinkTarget: "b"},
  669. eq: false,
  670. },
  671. // ... but not for non-symlinks
  672. {
  673. a: FileInfo{Type: FileInfoTypeFile, SymlinkTarget: "a"},
  674. b: FileInfo{Type: FileInfoTypeFile, SymlinkTarget: "b"},
  675. eq: true,
  676. },
  677. }
  678. if runtime.GOOS == "windows" {
  679. // On windows we only check the user writable bit of the permission
  680. // set, so these are equivalent.
  681. cases = append(cases, testCase{
  682. a: FileInfo{Permissions: 0777},
  683. b: FileInfo{Permissions: 0600},
  684. ignPerms: b(false),
  685. eq: true,
  686. })
  687. }
  688. for i, tc := range cases {
  689. // Check the standard attributes with all permutations of the
  690. // special ignore flags, unless the value of those flags are given
  691. // in the tests.
  692. for _, ignPerms := range []bool{true, false} {
  693. for _, ignBlocks := range []bool{true, false} {
  694. if tc.ignPerms != nil && *tc.ignPerms != ignPerms {
  695. continue
  696. }
  697. if tc.ignBlocks != nil && *tc.ignBlocks != ignBlocks {
  698. continue
  699. }
  700. if res := tc.a.isEquivalent(tc.b, 0, ignPerms, ignBlocks, tc.ignFlags); res != tc.eq {
  701. 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)
  702. }
  703. if res := tc.b.isEquivalent(tc.a, 0, ignPerms, ignBlocks, tc.ignFlags); res != tc.eq {
  704. 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)
  705. }
  706. }
  707. }
  708. }
  709. }
  710. func TestSha256OfEmptyBlock(t *testing.T) {
  711. // every block size should have a correct entry in sha256OfEmptyBlock
  712. for blockSize := MinBlockSize; blockSize <= MaxBlockSize; blockSize *= 2 {
  713. expected := sha256.Sum256(make([]byte, blockSize))
  714. if sha256OfEmptyBlock[blockSize] != expected {
  715. t.Error("missing or wrong hash for block of size", blockSize)
  716. }
  717. }
  718. }
  719. // TestClusterConfigAfterClose checks that ClusterConfig does not deadlock when
  720. // ClusterConfig is called on a closed connection.
  721. func TestClusterConfigAfterClose(t *testing.T) {
  722. m := newTestModel()
  723. c := NewConnection(c0ID, &testutils.BlockingRW{}, &testutils.BlockingRW{}, m, "name", CompressAlways).(wireFormatConnection).Connection.(*rawConnection)
  724. c.Start()
  725. c.internalClose(errManual)
  726. done := make(chan struct{})
  727. go func() {
  728. c.ClusterConfig(ClusterConfig{})
  729. close(done)
  730. }()
  731. select {
  732. case <-done:
  733. case <-time.After(time.Second):
  734. t.Fatal("timed out before Cluster Config returned")
  735. }
  736. }
  737. func TestDispatcherToCloseDeadlock(t *testing.T) {
  738. // Verify that we don't deadlock when calling Close() from within one of
  739. // the model callbacks (ClusterConfig).
  740. m := newTestModel()
  741. c := NewConnection(c0ID, &testutils.BlockingRW{}, &testutils.NoopRW{}, m, "name", CompressAlways).(wireFormatConnection).Connection.(*rawConnection)
  742. m.ccFn = func(devID DeviceID, cc ClusterConfig) {
  743. c.Close(errManual)
  744. }
  745. c.Start()
  746. c.inbox <- &ClusterConfig{}
  747. select {
  748. case <-c.dispatcherLoopStopped:
  749. case <-time.After(time.Second):
  750. t.Fatal("timed out before dispatcher loop terminated")
  751. }
  752. }