sharedpullerstate_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 http://mozilla.org/MPL/2.0/.
  6. package model
  7. import (
  8. "os"
  9. "testing"
  10. "github.com/syncthing/syncthing/lib/sync"
  11. )
  12. func TestSourceFileOK(t *testing.T) {
  13. s := sharedPullerState{
  14. realName: "testdata/foo",
  15. mut: sync.NewRWMutex(),
  16. }
  17. fd, err := s.sourceFile()
  18. if err != nil {
  19. t.Fatal(err)
  20. }
  21. if fd == nil {
  22. t.Fatal("Unexpected nil fd")
  23. }
  24. bs := make([]byte, 6)
  25. n, err := fd.Read(bs)
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. if n != len(bs) {
  30. t.Fatalf("Wrong read length %d != %d", n, len(bs))
  31. }
  32. if string(bs) != "foobar" {
  33. t.Fatalf("Wrong contents %s != foobar", string(bs))
  34. }
  35. if err := s.failed(); err != nil {
  36. t.Fatal(err)
  37. }
  38. }
  39. func TestSourceFileBad(t *testing.T) {
  40. s := sharedPullerState{
  41. realName: "nonexistent",
  42. mut: sync.NewRWMutex(),
  43. }
  44. fd, err := s.sourceFile()
  45. if err == nil {
  46. t.Fatal("Unexpected nil error")
  47. }
  48. if fd != nil {
  49. t.Fatal("Unexpected non-nil fd")
  50. }
  51. if err := s.failed(); err == nil {
  52. t.Fatal("Unexpected nil failed()")
  53. }
  54. }
  55. // Test creating temporary file inside read-only directory
  56. func TestReadOnlyDir(t *testing.T) {
  57. // Create a read only directory, clean it up afterwards.
  58. os.Mkdir("testdata/read_only_dir", 0555)
  59. defer func() {
  60. os.Chmod("testdata/read_only_dir", 0755)
  61. os.RemoveAll("testdata/read_only_dir")
  62. }()
  63. s := sharedPullerState{
  64. tempName: "testdata/read_only_dir/.temp_name",
  65. mut: sync.NewRWMutex(),
  66. }
  67. fd, err := s.tempFile()
  68. if err != nil {
  69. t.Fatal(err)
  70. }
  71. if fd == nil {
  72. t.Fatal("Unexpected nil fd")
  73. }
  74. s.fail("Test done", nil)
  75. s.finalClose()
  76. }