sharedpullerstate_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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.NewMutex(),
  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 n != len(bs) {
  27. t.Fatalf("Wrong read length %d != %d", n, len(bs))
  28. }
  29. if string(bs) != "foobar" {
  30. t.Fatalf("Wrong contents %s != foobar", string(bs))
  31. }
  32. if err := s.failed(); err != nil {
  33. t.Fatal(err)
  34. }
  35. }
  36. func TestSourceFileBad(t *testing.T) {
  37. s := sharedPullerState{
  38. realName: "nonexistent",
  39. mut: sync.NewMutex(),
  40. }
  41. fd, err := s.sourceFile()
  42. if err == nil {
  43. t.Fatal("Unexpected nil error")
  44. }
  45. if fd != nil {
  46. t.Fatal("Unexpected non-nil fd")
  47. }
  48. if err := s.failed(); err == nil {
  49. t.Fatal("Unexpected nil failed()")
  50. }
  51. }
  52. // Test creating temporary file inside read-only directory
  53. func TestReadOnlyDir(t *testing.T) {
  54. // Create a read only directory, clean it up afterwards.
  55. os.Mkdir("testdata/read_only_dir", 0555)
  56. defer func() {
  57. os.Chmod("testdata/read_only_dir", 0755)
  58. os.RemoveAll("testdata/read_only_dir")
  59. }()
  60. s := sharedPullerState{
  61. tempName: "testdata/read_only_dir/.temp_name",
  62. mut: sync.NewMutex(),
  63. }
  64. fd, err := s.tempFile()
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. if fd == nil {
  69. t.Fatal("Unexpected nil fd")
  70. }
  71. s.fail("Test done", nil)
  72. s.finalClose()
  73. }