manifest_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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/chunk"
  26. "github.com/ethereum/go-ethereum/swarm/storage"
  27. )
  28. func manifest(paths ...string) (manifestReader storage.LazySectionReader) {
  29. var entries []string
  30. for _, path := range paths {
  31. entry := fmt.Sprintf(`{"path":"%s"}`, path)
  32. entries = append(entries, entry)
  33. }
  34. manifest := fmt.Sprintf(`{"entries":[%s]}`, strings.Join(entries, ","))
  35. return &storage.LazyTestSectionReader{
  36. SectionReader: io.NewSectionReader(strings.NewReader(manifest), 0, int64(len(manifest))),
  37. }
  38. }
  39. func testGetEntry(t *testing.T, path, match string, multiple bool, paths ...string) *manifestTrie {
  40. quitC := make(chan bool)
  41. fileStore := storage.NewFileStore(nil, storage.NewFileStoreParams(), chunk.NewTags())
  42. ref := make([]byte, fileStore.HashSize())
  43. trie, err := readManifest(manifest(paths...), ref, fileStore, false, quitC, NOOPDecrypt)
  44. if err != nil {
  45. t.Errorf("unexpected error making manifest: %v", err)
  46. }
  47. checkEntry(t, path, match, multiple, trie)
  48. return trie
  49. }
  50. func checkEntry(t *testing.T, path, match string, multiple bool, trie *manifestTrie) {
  51. entry, fullpath := trie.getEntry(path)
  52. if match == "-" && entry != nil {
  53. t.Errorf("expected no match for '%s', got '%s'", path, fullpath)
  54. } else if entry == nil {
  55. if match != "-" {
  56. t.Errorf("expected entry '%s' to match '%s', got no match", match, path)
  57. }
  58. } else if fullpath != match {
  59. t.Errorf("incorrect entry retrieved for '%s'. expected path '%v', got '%s'", path, match, fullpath)
  60. }
  61. if multiple && entry.Status != http.StatusMultipleChoices {
  62. t.Errorf("Expected %d Multiple Choices Status for path %s, match %s, got %d", http.StatusMultipleChoices, path, match, entry.Status)
  63. } else if !multiple && entry != nil && entry.Status == http.StatusMultipleChoices {
  64. t.Errorf("Were not expecting %d Multiple Choices Status for path %s, match %s, but got it", http.StatusMultipleChoices, path, match)
  65. }
  66. }
  67. func TestGetEntry(t *testing.T) {
  68. // file system manifest always contains regularized paths
  69. testGetEntry(t, "a", "a", false, "a")
  70. testGetEntry(t, "b", "-", false, "a")
  71. testGetEntry(t, "/a//", "a", false, "a")
  72. // fallback
  73. testGetEntry(t, "/a", "", false, "")
  74. testGetEntry(t, "/a/b", "a/b", false, "a/b")
  75. // longest/deepest math
  76. testGetEntry(t, "read", "read", true, "readme.md", "readit.md")
  77. testGetEntry(t, "rf", "-", false, "readme.md", "readit.md")
  78. testGetEntry(t, "readme", "readme", false, "readme.md")
  79. testGetEntry(t, "readme", "-", false, "readit.md")
  80. testGetEntry(t, "readme.md", "readme.md", false, "readme.md")
  81. testGetEntry(t, "readme.md", "-", false, "readit.md")
  82. testGetEntry(t, "readmeAmd", "-", false, "readit.md")
  83. testGetEntry(t, "readme.mdffff", "-", false, "readme.md")
  84. testGetEntry(t, "ab", "ab", true, "ab/cefg", "ab/cedh", "ab/kkkkkk")
  85. testGetEntry(t, "ab/ce", "ab/ce", true, "ab/cefg", "ab/cedh", "ab/ceuuuuuuuuuu")
  86. testGetEntry(t, "abc", "abc", true, "abcd", "abczzzzef", "abc/def", "abc/e/g")
  87. testGetEntry(t, "a/b", "a/b", true, "a", "a/bc", "a/ba", "a/b/c")
  88. testGetEntry(t, "a/b", "a/b", false, "a", "a/b", "a/bb", "a/b/c")
  89. testGetEntry(t, "//a//b//", "a/b", false, "a", "a/b", "a/bb", "a/b/c")
  90. }
  91. func TestExactMatch(t *testing.T) {
  92. quitC := make(chan bool)
  93. mf := manifest("shouldBeExactMatch.css", "shouldBeExactMatch.css.map")
  94. fileStore := storage.NewFileStore(nil, storage.NewFileStoreParams(), chunk.NewTags())
  95. ref := make([]byte, fileStore.HashSize())
  96. trie, err := readManifest(mf, ref, fileStore, false, quitC, nil)
  97. if err != nil {
  98. t.Errorf("unexpected error making manifest: %v", err)
  99. }
  100. entry, _ := trie.getEntry("shouldBeExactMatch.css")
  101. if entry.Path != "" {
  102. t.Errorf("Expected entry to match %s, got: %s", "shouldBeExactMatch.css", entry.Path)
  103. }
  104. if entry.Status == http.StatusMultipleChoices {
  105. t.Errorf("Got status %d, which is unexepcted", http.StatusMultipleChoices)
  106. }
  107. }
  108. func TestDeleteEntry(t *testing.T) {
  109. }
  110. // TestAddFileWithManifestPath tests that adding an entry at a path which
  111. // already exists as a manifest just adds the entry to the manifest rather
  112. // than replacing the manifest with the entry
  113. func TestAddFileWithManifestPath(t *testing.T) {
  114. // create a manifest containing "ab" and "ac"
  115. manifest, _ := json.Marshal(&Manifest{
  116. Entries: []ManifestEntry{
  117. {Path: "ab", Hash: "ab"},
  118. {Path: "ac", Hash: "ac"},
  119. },
  120. })
  121. reader := &storage.LazyTestSectionReader{
  122. SectionReader: io.NewSectionReader(bytes.NewReader(manifest), 0, int64(len(manifest))),
  123. }
  124. fileStore := storage.NewFileStore(nil, storage.NewFileStoreParams(), chunk.NewTags())
  125. ref := make([]byte, fileStore.HashSize())
  126. trie, err := readManifest(reader, ref, fileStore, false, nil, NOOPDecrypt)
  127. if err != nil {
  128. t.Fatal(err)
  129. }
  130. checkEntry(t, "ab", "ab", false, trie)
  131. checkEntry(t, "ac", "ac", false, trie)
  132. // now add path "a" and check we can still get "ab" and "ac"
  133. entry := &manifestTrieEntry{}
  134. entry.Path = "a"
  135. entry.Hash = "a"
  136. trie.addEntry(entry, nil)
  137. checkEntry(t, "ab", "ab", false, trie)
  138. checkEntry(t, "ac", "ac", false, trie)
  139. checkEntry(t, "a", "a", false, trie)
  140. }
  141. // TestReadManifestOverSizeLimit creates a manifest reader with data longer then
  142. // manifestSizeLimit and checks if readManifest function will return the exact error
  143. // message.
  144. // The manifest data is not in json-encoded format, preventing possbile
  145. // successful parsing attempts if limit check fails.
  146. func TestReadManifestOverSizeLimit(t *testing.T) {
  147. manifest := make([]byte, manifestSizeLimit+1)
  148. reader := &storage.LazyTestSectionReader{
  149. SectionReader: io.NewSectionReader(bytes.NewReader(manifest), 0, int64(len(manifest))),
  150. }
  151. _, err := readManifest(reader, storage.Address{}, nil, false, nil, NOOPDecrypt)
  152. if err == nil {
  153. t.Fatal("got no error from readManifest")
  154. }
  155. // Error message is part of the http response body
  156. // which justifies exact string validation.
  157. got := err.Error()
  158. want := fmt.Sprintf("Manifest size of %v bytes exceeds the %v byte limit", len(manifest), manifestSizeLimit)
  159. if got != want {
  160. t.Fatalf("got error mesage %q, expected %q", got, want)
  161. }
  162. }