github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/swarm/api/manifest_test.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 19:16:43</date> 10 //</624450112331976704> 11 12 13 package api 14 15 import ( 16 "bytes" 17 "encoding/json" 18 "fmt" 19 "io" 20 "net/http" 21 "strings" 22 "testing" 23 24 "github.com/ethereum/go-ethereum/swarm/storage" 25 ) 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 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()) 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 51 func checkEntry(t *testing.T, path, match string, multiple bool, trie *manifestTrie) { 52 entry, fullpath := trie.getEntry(path) 53 if match == "-" && entry != nil { 54 t.Errorf("expected no match for '%s', got '%s'", path, fullpath) 55 } else if entry == nil { 56 if match != "-" { 57 t.Errorf("expected entry '%s' to match '%s', got no match", match, path) 58 } 59 } else if fullpath != match { 60 t.Errorf("incorrect entry retrieved for '%s'. expected path '%v', got '%s'", path, match, fullpath) 61 } 62 63 if multiple && entry.Status != http.StatusMultipleChoices { 64 t.Errorf("Expected %d Multiple Choices Status for path %s, match %s, got %d", http.StatusMultipleChoices, path, match, entry.Status) 65 } else if !multiple && entry != nil && entry.Status == http.StatusMultipleChoices { 66 t.Errorf("Were not expecting %d Multiple Choices Status for path %s, match %s, but got it", http.StatusMultipleChoices, path, match) 67 } 68 } 69 70 func TestGetEntry(t *testing.T) { 71 //文件系统清单始终包含规范化路径 72 testGetEntry(t, "a", "a", false, "a") 73 testGetEntry(t, "b", "-", false, "a") 74 testGetEntry(t, "/a//“,”A“,假,”A“) 75 //退路 76 testGetEntry(t, "/a", "", false, "") 77 testGetEntry(t, "/a/b", "a/b", false, "a/b") 78 //最长/最深数学 79 testGetEntry(t, "read", "read", true, "readme.md", "readit.md") 80 testGetEntry(t, "rf", "-", false, "readme.md", "readit.md") 81 testGetEntry(t, "readme", "readme", false, "readme.md") 82 testGetEntry(t, "readme", "-", false, "readit.md") 83 testGetEntry(t, "readme.md", "readme.md", false, "readme.md") 84 testGetEntry(t, "readme.md", "-", false, "readit.md") 85 testGetEntry(t, "readmeAmd", "-", false, "readit.md") 86 testGetEntry(t, "readme.mdffff", "-", false, "readme.md") 87 testGetEntry(t, "ab", "ab", true, "ab/cefg", "ab/cedh", "ab/kkkkkk") 88 testGetEntry(t, "ab/ce", "ab/ce", true, "ab/cefg", "ab/cedh", "ab/ceuuuuuuuuuu") 89 testGetEntry(t, "abc", "abc", true, "abcd", "abczzzzef", "abc/def", "abc/e/g") 90 testGetEntry(t, "a/b", "a/b", true, "a", "a/bc", "a/ba", "a/b/c") 91 testGetEntry(t, "a/b", "a/b", false, "a", "a/b", "a/bb", "a/b/c") 92 testGetEntry(t, "//A//B/“,”A/B“,假,”A“,”A/B“,”A/BB“,”A/B/C“) 93 } 94 95 func TestExactMatch(t *testing.T) { 96 quitC := make(chan bool) 97 mf := manifest("shouldBeExactMatch.css", "shouldBeExactMatch.css.map") 98 fileStore := storage.NewFileStore(nil, storage.NewFileStoreParams()) 99 ref := make([]byte, fileStore.HashSize()) 100 trie, err := readManifest(mf, ref, fileStore, false, quitC, nil) 101 if err != nil { 102 t.Errorf("unexpected error making manifest: %v", err) 103 } 104 entry, _ := trie.getEntry("shouldBeExactMatch.css") 105 if entry.Path != "" { 106 t.Errorf("Expected entry to match %s, got: %s", "shouldBeExactMatch.css", entry.Path) 107 } 108 if entry.Status == http.StatusMultipleChoices { 109 t.Errorf("Got status %d, which is unexepcted", http.StatusMultipleChoices) 110 } 111 } 112 113 func TestDeleteEntry(t *testing.T) { 114 115 } 116 117 //testaddfilewithmanifestPath测试在路径中添加项 118 //已经作为清单存在,只是将条目添加到清单 119 //而不是用条目替换清单 120 func TestAddFileWithManifestPath(t *testing.T) { 121 //创建包含“ab”和“ac”的清单 122 manifest, _ := json.Marshal(&Manifest{ 123 Entries: []ManifestEntry{ 124 {Path: "ab", Hash: "ab"}, 125 {Path: "ac", Hash: "ac"}, 126 }, 127 }) 128 reader := &storage.LazyTestSectionReader{ 129 SectionReader: io.NewSectionReader(bytes.NewReader(manifest), 0, int64(len(manifest))), 130 } 131 fileStore := storage.NewFileStore(nil, storage.NewFileStoreParams()) 132 ref := make([]byte, fileStore.HashSize()) 133 trie, err := readManifest(reader, ref, fileStore, false, nil, NOOPDecrypt) 134 if err != nil { 135 t.Fatal(err) 136 } 137 checkEntry(t, "ab", "ab", false, trie) 138 checkEntry(t, "ac", "ac", false, trie) 139 140 //现在添加路径“a”并检查我们仍然可以得到“ab”和“ac” 141 entry := &manifestTrieEntry{} 142 entry.Path = "a" 143 entry.Hash = "a" 144 trie.addEntry(entry, nil) 145 checkEntry(t, "ab", "ab", false, trie) 146 checkEntry(t, "ac", "ac", false, trie) 147 checkEntry(t, "a", "a", false, trie) 148 } 149 150 //testreadmanifestsuperlimit创建一个清单阅读器,其中数据的长度超过 151 //manifestSizeLimit并检查readManifest函数是否返回准确的错误 152 //消息。 153 //清单数据不是JSON编码格式,因此无法 154 //如果限制检查失败,则成功分析尝试。 155 func TestReadManifestOverSizeLimit(t *testing.T) { 156 manifest := make([]byte, manifestSizeLimit+1) 157 reader := &storage.LazyTestSectionReader{ 158 SectionReader: io.NewSectionReader(bytes.NewReader(manifest), 0, int64(len(manifest))), 159 } 160 _, err := readManifest(reader, storage.Address{}, nil, false, nil, NOOPDecrypt) 161 if err == nil { 162 t.Fatal("got no error from readManifest") 163 } 164 //错误消息是HTTP响应正文的一部分 165 //这证明了精确的字符串验证是正确的。 166 got := err.Error() 167 want := fmt.Sprintf("Manifest size of %v bytes exceeds the %v byte limit", len(manifest), manifestSizeLimit) 168 if got != want { 169 t.Fatalf("got error mesage %q, expected %q", got, want) 170 } 171 } 172