client_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package client
  17. import (
  18. "io/ioutil"
  19. "os"
  20. "path/filepath"
  21. "reflect"
  22. "sort"
  23. "testing"
  24. "github.com/ethereum/go-ethereum/swarm/testutil"
  25. )
  26. func TestClientManifestFileList(t *testing.T) {
  27. srv := testutil.NewTestSwarmServer(t)
  28. defer srv.Close()
  29. dir, err := ioutil.TempDir("", "swarm-client-test")
  30. if err != nil {
  31. t.Fatal(err)
  32. }
  33. files := []string{
  34. "file1.txt",
  35. "file2.txt",
  36. "dir1/file3.txt",
  37. "dir1/file4.txt",
  38. "dir2/file5.txt",
  39. "dir2/dir3/file6.txt",
  40. "dir2/dir4/file7.txt",
  41. "dir2/dir4/file8.txt",
  42. }
  43. for _, file := range files {
  44. path := filepath.Join(dir, file)
  45. if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
  46. t.Fatalf("error creating dir for %s: %s", path, err)
  47. }
  48. if err := ioutil.WriteFile(path, []byte("data"), 0644); err != nil {
  49. t.Fatalf("error writing file %s: %s", path, err)
  50. }
  51. }
  52. client := NewClient(srv.URL)
  53. hash, err := client.UploadDirectory(dir, "")
  54. if err != nil {
  55. t.Fatalf("error uploading directory: %s", err)
  56. }
  57. ls := func(prefix string) []string {
  58. entries, err := client.ManifestFileList(hash, prefix)
  59. if err != nil {
  60. t.Fatal(err)
  61. }
  62. paths := make([]string, len(entries))
  63. for i, entry := range entries {
  64. paths[i] = entry.Path
  65. }
  66. sort.Strings(paths)
  67. return paths
  68. }
  69. tests := map[string][]string{
  70. "": []string{"dir1/", "dir2/", "file1.txt", "file2.txt"},
  71. "file": []string{"file1.txt", "file2.txt"},
  72. "file1": []string{"file1.txt"},
  73. "file2.txt": []string{"file2.txt"},
  74. "file12": []string{},
  75. "dir": []string{"dir1/", "dir2/"},
  76. "dir1": []string{"dir1/"},
  77. "dir1/": []string{"dir1/file3.txt", "dir1/file4.txt"},
  78. "dir1/file": []string{"dir1/file3.txt", "dir1/file4.txt"},
  79. "dir1/file3.txt": []string{"dir1/file3.txt"},
  80. "dir1/file34": []string{},
  81. "dir2/": []string{"dir2/dir3/", "dir2/dir4/", "dir2/file5.txt"},
  82. "dir2/file": []string{"dir2/file5.txt"},
  83. "dir2/dir": []string{"dir2/dir3/", "dir2/dir4/"},
  84. "dir2/dir3/": []string{"dir2/dir3/file6.txt"},
  85. "dir2/dir4/": []string{"dir2/dir4/file7.txt", "dir2/dir4/file8.txt"},
  86. "dir2/dir4/file": []string{"dir2/dir4/file7.txt", "dir2/dir4/file8.txt"},
  87. "dir2/dir4/file7.txt": []string{"dir2/dir4/file7.txt"},
  88. "dir2/dir4/file78": []string{},
  89. }
  90. for prefix, expected := range tests {
  91. actual := ls(prefix)
  92. if !reflect.DeepEqual(actual, expected) {
  93. t.Fatalf("expected prefix %q to return paths %v, got %v", prefix, expected, actual)
  94. }
  95. }
  96. }