manifest_test.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 api
  17. import (
  18. // "encoding/json"
  19. "fmt"
  20. "io"
  21. "strings"
  22. "testing"
  23. "github.com/ethereum/go-ethereum/swarm/storage"
  24. )
  25. func manifest(paths ...string) (manifestReader storage.LazySectionReader) {
  26. var entries []string
  27. for _, path := range paths {
  28. entry := fmt.Sprintf(`{"path":"%s"}`, path)
  29. entries = append(entries, entry)
  30. }
  31. manifest := fmt.Sprintf(`{"entries":[%s]}`, strings.Join(entries, ","))
  32. return &storage.LazyTestSectionReader{
  33. SectionReader: io.NewSectionReader(strings.NewReader(manifest), 0, int64(len(manifest))),
  34. }
  35. }
  36. func testGetEntry(t *testing.T, path, match string, paths ...string) *manifestTrie {
  37. quitC := make(chan bool)
  38. trie, err := readManifest(manifest(paths...), nil, nil, quitC)
  39. if err != nil {
  40. t.Errorf("unexpected error making manifest: %v", err)
  41. }
  42. checkEntry(t, path, match, trie)
  43. return trie
  44. }
  45. func checkEntry(t *testing.T, path, match string, trie *manifestTrie) {
  46. entry, fullpath := trie.getEntry(path)
  47. if match == "-" && entry != nil {
  48. t.Errorf("expected no match for '%s', got '%s'", path, fullpath)
  49. } else if entry == nil {
  50. if match != "-" {
  51. t.Errorf("expected entry '%s' to match '%s', got no match", match, path)
  52. }
  53. } else if fullpath != match {
  54. t.Errorf("incorrect entry retrieved for '%s'. expected path '%v', got '%s'", path, match, fullpath)
  55. }
  56. }
  57. func TestGetEntry(t *testing.T) {
  58. // file system manifest always contains regularized paths
  59. testGetEntry(t, "a", "a", "a")
  60. testGetEntry(t, "b", "-", "a")
  61. testGetEntry(t, "/a//", "a", "a")
  62. // fallback
  63. testGetEntry(t, "/a", "", "")
  64. testGetEntry(t, "/a/b", "a/b", "a/b")
  65. // longest/deepest math
  66. testGetEntry(t, "a/b", "-", "a", "a/ba", "a/b/c")
  67. testGetEntry(t, "a/b", "a/b", "a", "a/b", "a/bb", "a/b/c")
  68. testGetEntry(t, "//a//b//", "a/b", "a", "a/b", "a/bb", "a/b/c")
  69. }
  70. func TestDeleteEntry(t *testing.T) {
  71. }