sharedpullerstate_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package model
  16. import (
  17. "os"
  18. "testing"
  19. )
  20. func TestSourceFileOK(t *testing.T) {
  21. s := sharedPullerState{
  22. realName: "testdata/foo",
  23. }
  24. fd, err := s.sourceFile()
  25. if err != nil {
  26. t.Fatal(err)
  27. }
  28. if fd == nil {
  29. t.Fatal("Unexpected nil fd")
  30. }
  31. bs := make([]byte, 6)
  32. n, err := fd.Read(bs)
  33. if n != len(bs) {
  34. t.Fatalf("Wrong read length %d != %d", n, len(bs))
  35. }
  36. if string(bs) != "foobar" {
  37. t.Fatalf("Wrong contents %s != foobar", string(bs))
  38. }
  39. if err := s.failed(); err != nil {
  40. t.Fatal(err)
  41. }
  42. }
  43. func TestSourceFileBad(t *testing.T) {
  44. s := sharedPullerState{
  45. realName: "nonexistent",
  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. tempName: "testdata/read_only_dir/.temp_name",
  68. }
  69. fd, err := s.tempFile()
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. if fd == nil {
  74. t.Fatal("Unexpected nil fd")
  75. }
  76. s.fail("Test done", nil)
  77. }