manifest_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. "bytes"
  19. "encoding/json"
  20. "fmt"
  21. "io"
  22. "net/http"
  23. "strings"
  24. "testing"
  25. "github.com/ethereum/go-ethereum/swarm/storage"
  26. )
  27. func manifest(paths ...string) (manifestReader storage.LazySectionReader) {
  28. var entries []string
  29. for _, path := range paths {
  30. entry := fmt.Sprintf(`{"path":"%s"}`, path)
  31. entries = append(entries, entry)
  32. }
  33. manifest := fmt.Sprintf(`{"entries":[%s]}`, strings.Join(entries, ","))
  34. return &storage.LazyTestSectionReader{
  35. SectionReader: io.NewSectionReader(strings.NewReader(manifest), 0, int64(len(manifest))),
  36. }
  37. }
  38. func testGetEntry(t *testing.T, path, match string, multiple bool, paths ...string) *manifestTrie {
  39. quitC := make(chan bool)
  40. trie, err := readManifest(manifest(paths...), nil, nil, quitC)
  41. if err != nil {
  42. t.Errorf("unexpected error making manifest: %v", err)
  43. }
  44. checkEntry(t, path, match, multiple, trie)
  45. return trie
  46. }
  47. func checkEntry(t *testing.T, path, match string, multiple bool, trie *manifestTrie) {
  48. entry, fullpath := trie.getEntry(path)
  49. if match == "-" && entry != nil {
  50. t.Errorf("expected no match for '%s', got '%s'", path, fullpath)
  51. } else if entry == nil {
  52. if match != "-" {
  53. t.Errorf("expected entry '%s' to match '%s', got no match", match, path)
  54. }
  55. } else if fullpath != match {
  56. t.Errorf("incorrect entry retrieved for '%s'. expected path '%v', got '%s'", path, match, fullpath)
  57. }
  58. if multiple && entry.Status != http.StatusMultipleChoices {
  59. t.Errorf("Expected %d Multiple Choices Status for path %s, match %s, got %d", http.StatusMultipleChoices, path, match, entry.Status)
  60. } else if !multiple && entry != nil && entry.Status == http.StatusMultipleChoices {
  61. t.Errorf("Were not expecting %d Multiple Choices Status for path %s, match %s, but got it", http.StatusMultipleChoices, path, match)
  62. }
  63. }
  64. func TestGetEntry(t *testing.T) {
  65. // file system manifest always contains regularized paths
  66. testGetEntry(t, "a", "a", false, "a")
  67. testGetEntry(t, "b", "-", false, "a")
  68. testGetEntry(t, "/a//", "a", false, "a")
  69. // fallback
  70. testGetEntry(t, "/a", "", false, "")
  71. testGetEntry(t, "/a/b", "a/b", false, "a/b")
  72. // longest/deepest math
  73. testGetEntry(t, "read", "read", true, "readme.md", "readit.md")
  74. testGetEntry(t, "rf", "-", false, "readme.md", "readit.md")
  75. testGetEntry(t, "readme", "readme", false, "readme.md")
  76. testGetEntry(t, "readme", "-", false, "readit.md")
  77. testGetEntry(t, "readme.md", "readme.md", false, "readme.md")
  78. testGetEntry(t, "readme.md", "-", false, "readit.md")
  79. testGetEntry(t, "readmeAmd", "-", false, "readit.md")
  80. testGetEntry(t, "readme.mdffff", "-", false, "readme.md")
  81. testGetEntry(t, "ab", "ab", true, "ab/cefg", "ab/cedh", "ab/kkkkkk")
  82. testGetEntry(t, "ab/ce", "ab/ce", true, "ab/cefg", "ab/cedh", "ab/ceuuuuuuuuuu")
  83. testGetEntry(t, "abc", "abc", true, "abcd", "abczzzzef", "abc/def", "abc/e/g")
  84. testGetEntry(t, "a/b", "a/b", true, "a", "a/bc", "a/ba", "a/b/c")
  85. testGetEntry(t, "a/b", "a/b", false, "a", "a/b", "a/bb", "a/b/c")
  86. testGetEntry(t, "//a//b//", "a/b", false, "a", "a/b", "a/bb", "a/b/c")
  87. }
  88. func TestExactMatch(t *testing.T) {
  89. quitC := make(chan bool)
  90. mf := manifest("shouldBeExactMatch.css", "shouldBeExactMatch.css.map")
  91. trie, err := readManifest(mf, nil, nil, quitC)
  92. if err != nil {
  93. t.Errorf("unexpected error making manifest: %v", err)
  94. }
  95. entry, _ := trie.getEntry("shouldBeExactMatch.css")
  96. if entry.Path != "" {
  97. t.Errorf("Expected entry to match %s, got: %s", "shouldBeExactMatch.css", entry.Path)
  98. }
  99. if entry.Status == http.StatusMultipleChoices {
  100. t.Errorf("Got status %d, which is unexepcted", http.StatusMultipleChoices)
  101. }
  102. }
  103. func TestDeleteEntry(t *testing.T) {
  104. }
  105. // TestAddFileWithManifestPath tests that adding an entry at a path which
  106. // already exists as a manifest just adds the entry to the manifest rather
  107. // than replacing the manifest with the entry
  108. func TestAddFileWithManifestPath(t *testing.T) {
  109. // create a manifest containing "ab" and "ac"
  110. manifest, _ := json.Marshal(&Manifest{
  111. Entries: []ManifestEntry{
  112. {Path: "ab", Hash: "ab"},
  113. {Path: "ac", Hash: "ac"},
  114. },
  115. })
  116. reader := &storage.LazyTestSectionReader{
  117. SectionReader: io.NewSectionReader(bytes.NewReader(manifest), 0, int64(len(manifest))),
  118. }
  119. trie, err := readManifest(reader, nil, nil, nil)
  120. if err != nil {
  121. t.Fatal(err)
  122. }
  123. checkEntry(t, "ab", "ab", false, trie)
  124. checkEntry(t, "ac", "ac", false, trie)
  125. // now add path "a" and check we can still get "ab" and "ac"
  126. entry := &manifestTrieEntry{}
  127. entry.Path = "a"
  128. entry.Hash = "a"
  129. trie.addEntry(entry, nil)
  130. checkEntry(t, "ab", "ab", false, trie)
  131. checkEntry(t, "ac", "ac", false, trie)
  132. checkEntry(t, "a", "a", false, trie)
  133. }