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