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