sharedpullerstate_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  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 "testing"
  17. func TestSourceFileOK(t *testing.T) {
  18. s := sharedPullerState{
  19. realName: "testdata/foo",
  20. }
  21. fd, err := s.sourceFile()
  22. if err != nil {
  23. t.Fatal(err)
  24. }
  25. if fd == nil {
  26. t.Fatal("Unexpected nil fd")
  27. }
  28. bs := make([]byte, 6)
  29. n, err := fd.Read(bs)
  30. if n != len(bs) {
  31. t.Fatal("Wrong read length %d != %d", n, len(bs))
  32. }
  33. if string(bs) != "foobar" {
  34. t.Fatal("Wrong contents %s != foobar", bs)
  35. }
  36. if err := s.failed(); err != nil {
  37. t.Fatal(err)
  38. }
  39. }
  40. func TestSourceFileBad(t *testing.T) {
  41. s := sharedPullerState{
  42. realName: "nonexistent",
  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. }