sharedpullerstate_test.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package model
  7. import (
  8. "os"
  9. "testing"
  10. "github.com/syncthing/syncthing/lib/fs"
  11. "github.com/syncthing/syncthing/lib/sync"
  12. )
  13. func TestSourceFileOK(t *testing.T) {
  14. s := sharedPullerState{
  15. fs: fs.NewFilesystem(fs.FilesystemTypeBasic, "testdata"),
  16. realName: "foo",
  17. mut: sync.NewRWMutex(),
  18. }
  19. fd, err := s.sourceFile()
  20. if err != nil {
  21. t.Fatal(err)
  22. }
  23. if fd == nil {
  24. t.Fatal("Unexpected nil fd")
  25. }
  26. bs := make([]byte, 6)
  27. n, err := fd.Read(bs)
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. if n != len(bs) {
  32. t.Fatalf("Wrong read length %d != %d", n, len(bs))
  33. }
  34. if string(bs) != "foobar" {
  35. t.Fatalf("Wrong contents %s != foobar", string(bs))
  36. }
  37. if err := s.failed(); err != nil {
  38. t.Fatal(err)
  39. }
  40. }
  41. func TestSourceFileBad(t *testing.T) {
  42. s := sharedPullerState{
  43. fs: fs.NewFilesystem(fs.FilesystemTypeBasic, "testdata"),
  44. realName: "nonexistent",
  45. mut: sync.NewRWMutex(),
  46. }
  47. fd, err := s.sourceFile()
  48. if err == nil {
  49. t.Fatal("Unexpected nil error")
  50. }
  51. if fd != nil {
  52. t.Fatal("Unexpected non-nil fd")
  53. }
  54. if err := s.failed(); err == nil {
  55. t.Fatal("Unexpected nil failed()")
  56. }
  57. }
  58. // Test creating temporary file inside read-only directory
  59. func TestReadOnlyDir(t *testing.T) {
  60. // Create a read only directory, clean it up afterwards.
  61. os.Mkdir("testdata/read_only_dir", 0555)
  62. defer func() {
  63. os.Chmod("testdata/read_only_dir", 0755)
  64. os.RemoveAll("testdata/read_only_dir")
  65. }()
  66. s := sharedPullerState{
  67. fs: fs.NewFilesystem(fs.FilesystemTypeBasic, "testdata"),
  68. tempName: "read_only_dir/.temp_name",
  69. mut: sync.NewRWMutex(),
  70. }
  71. fd, err := s.tempFile()
  72. if err != nil {
  73. t.Fatal(err)
  74. }
  75. if fd == nil {
  76. t.Fatal("Unexpected nil fd")
  77. }
  78. s.fail("Test done", nil)
  79. s.finalClose()
  80. }