github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/swarm/api/manifest_test.go (about) 1 2 //此源码被清华学神尹成大魔王专业翻译分析并修改 3 //尹成QQ77025077 4 //尹成微信18510341407 5 //尹成所在QQ群721929980 6 //尹成邮箱 yinc13@mails.tsinghua.edu.cn 7 //尹成毕业于清华大学,微软区块链领域全球最有价值专家 8 //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620 9 // 10 // 11 // 12 // 13 // 14 // 15 // 16 // 17 // 18 // 19 // 20 // 21 // 22 // 23 // 24 25 package api 26 27 import ( 28 "bytes" 29 "encoding/json" 30 "fmt" 31 "io" 32 "net/http" 33 "strings" 34 "testing" 35 36 "github.com/ethereum/go-ethereum/swarm/storage" 37 ) 38 39 func manifest(paths ...string) (manifestReader storage.LazySectionReader) { 40 var entries []string 41 for _, path := range paths { 42 entry := fmt.Sprintf(`{"path":"%s"}`, path) 43 entries = append(entries, entry) 44 } 45 manifest := fmt.Sprintf(`{"entries":[%s]}`, strings.Join(entries, ",")) 46 return &storage.LazyTestSectionReader{ 47 SectionReader: io.NewSectionReader(strings.NewReader(manifest), 0, int64(len(manifest))), 48 } 49 } 50 51 func testGetEntry(t *testing.T, path, match string, multiple bool, paths ...string) *manifestTrie { 52 quitC := make(chan bool) 53 fileStore := storage.NewFileStore(nil, storage.NewFileStoreParams()) 54 ref := make([]byte, fileStore.HashSize()) 55 trie, err := readManifest(manifest(paths...), ref, fileStore, false, quitC, NOOPDecrypt) 56 if err != nil { 57 t.Errorf("unexpected error making manifest: %v", err) 58 } 59 checkEntry(t, path, match, multiple, trie) 60 return trie 61 } 62 63 func checkEntry(t *testing.T, path, match string, multiple bool, trie *manifestTrie) { 64 entry, fullpath := trie.getEntry(path) 65 if match == "-" && entry != nil { 66 t.Errorf("expected no match for '%s', got '%s'", path, fullpath) 67 } else if entry == nil { 68 if match != "-" { 69 t.Errorf("expected entry '%s' to match '%s', got no match", match, path) 70 } 71 } else if fullpath != match { 72 t.Errorf("incorrect entry retrieved for '%s'. expected path '%v', got '%s'", path, match, fullpath) 73 } 74 75 if multiple && entry.Status != http.StatusMultipleChoices { 76 t.Errorf("Expected %d Multiple Choices Status for path %s, match %s, got %d", http.StatusMultipleChoices, path, match, entry.Status) 77 } else if !multiple && entry != nil && entry.Status == http.StatusMultipleChoices { 78 t.Errorf("Were not expecting %d Multiple Choices Status for path %s, match %s, but got it", http.StatusMultipleChoices, path, match) 79 } 80 } 81 82 func TestGetEntry(t *testing.T) { 83 // 84 testGetEntry(t, "a", "a", false, "a") 85 testGetEntry(t, "b", "-", false, "a") 86 testGetEntry(t, "/a// 87 // 88 testGetEntry(t, "/a", "", false, "") 89 testGetEntry(t, "/a/b", "a/b", false, "a/b") 90 // 91 testGetEntry(t, "read", "read", true, "readme.md", "readit.md") 92 testGetEntry(t, "rf", "-", false, "readme.md", "readit.md") 93 testGetEntry(t, "readme", "readme", false, "readme.md") 94 testGetEntry(t, "readme", "-", false, "readit.md") 95 testGetEntry(t, "readme.md", "readme.md", false, "readme.md") 96 testGetEntry(t, "readme.md", "-", false, "readit.md") 97 testGetEntry(t, "readmeAmd", "-", false, "readit.md") 98 testGetEntry(t, "readme.mdffff", "-", false, "readme.md") 99 testGetEntry(t, "ab", "ab", true, "ab/cefg", "ab/cedh", "ab/kkkkkk") 100 testGetEntry(t, "ab/ce", "ab/ce", true, "ab/cefg", "ab/cedh", "ab/ceuuuuuuuuuu") 101 testGetEntry(t, "abc", "abc", true, "abcd", "abczzzzef", "abc/def", "abc/e/g") 102 testGetEntry(t, "a/b", "a/b", true, "a", "a/bc", "a/ba", "a/b/c") 103 testGetEntry(t, "a/b", "a/b", false, "a", "a/b", "a/bb", "a/b/c") 104 testGetEntry(t, "// 105 } 106 107 func TestExactMatch(t *testing.T) { 108 quitC := make(chan bool) 109 mf := manifest("shouldBeExactMatch.css", "shouldBeExactMatch.css.map") 110 fileStore := storage.NewFileStore(nil, storage.NewFileStoreParams()) 111 ref := make([]byte, fileStore.HashSize()) 112 trie, err := readManifest(mf, ref, fileStore, false, quitC, nil) 113 if err != nil { 114 t.Errorf("unexpected error making manifest: %v", err) 115 } 116 entry, _ := trie.getEntry("shouldBeExactMatch.css") 117 if entry.Path != "" { 118 t.Errorf("Expected entry to match %s, got: %s", "shouldBeExactMatch.css", entry.Path) 119 } 120 if entry.Status == http.StatusMultipleChoices { 121 t.Errorf("Got status %d, which is unexepcted", http.StatusMultipleChoices) 122 } 123 } 124 125 func TestDeleteEntry(t *testing.T) { 126 127 } 128 129 // 130 // 131 // 132 func TestAddFileWithManifestPath(t *testing.T) { 133 // 134 manifest, _ := json.Marshal(&Manifest{ 135 Entries: []ManifestEntry{ 136 {Path: "ab", Hash: "ab"}, 137 {Path: "ac", Hash: "ac"}, 138 }, 139 }) 140 reader := &storage.LazyTestSectionReader{ 141 SectionReader: io.NewSectionReader(bytes.NewReader(manifest), 0, int64(len(manifest))), 142 } 143 fileStore := storage.NewFileStore(nil, storage.NewFileStoreParams()) 144 ref := make([]byte, fileStore.HashSize()) 145 trie, err := readManifest(reader, ref, fileStore, false, nil, NOOPDecrypt) 146 if err != nil { 147 t.Fatal(err) 148 } 149 checkEntry(t, "ab", "ab", false, trie) 150 checkEntry(t, "ac", "ac", false, trie) 151 152 // 153 entry := &manifestTrieEntry{} 154 entry.Path = "a" 155 entry.Hash = "a" 156 trie.addEntry(entry, nil) 157 checkEntry(t, "ab", "ab", false, trie) 158 checkEntry(t, "ac", "ac", false, trie) 159 checkEntry(t, "a", "a", false, trie) 160 } 161 162 // 163 // 164 // 165 // 166 // 167 func TestReadManifestOverSizeLimit(t *testing.T) { 168 manifest := make([]byte, manifestSizeLimit+1) 169 reader := &storage.LazyTestSectionReader{ 170 SectionReader: io.NewSectionReader(bytes.NewReader(manifest), 0, int64(len(manifest))), 171 } 172 _, err := readManifest(reader, storage.Address{}, nil, false, nil, NOOPDecrypt) 173 if err == nil { 174 t.Fatal("got no error from readManifest") 175 } 176 // 177 // 178 got := err.Error() 179 want := fmt.Sprintf("Manifest size of %v bytes exceeds the %v byte limit", len(manifest), manifestSizeLimit) 180 if got != want { 181 t.Fatalf("got error mesage %q, expected %q", got, want) 182 } 183 }