sharedpullerstate_test.go 1.6 KB

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