github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/cmd/swarm/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 //版权所有2018 Go Ethereum作者 10 //此文件是Go以太坊的一部分。 11 // 12 //Go以太坊是免费软件:您可以重新发布和/或修改它 13 //根据GNU通用公共许可证的条款 14 //自由软件基金会,或者许可证的第3版,或者 15 //(由您选择)任何更高版本。 16 // 17 //Go以太坊的分布希望它会有用, 18 //但没有任何保证;甚至没有 19 //适销性或特定用途的适用性。见 20 //GNU通用公共许可证了解更多详细信息。 21 // 22 //你应该已经收到一份GNU通用公共许可证的副本 23 //一起去以太坊吧。如果没有,请参见<http://www.gnu.org/licenses/>。 24 25 package main 26 27 import ( 28 "bytes" 29 "io/ioutil" 30 "os" 31 "path/filepath" 32 "testing" 33 34 "github.com/ethereum/go-ethereum/swarm/api" 35 swarm "github.com/ethereum/go-ethereum/swarm/api/client" 36 ) 37 38 //测试清单更改测试清单添加、更新和删除 39 //不加密的CLI命令。 40 func TestManifestChange(t *testing.T) { 41 testManifestChange(t, false) 42 } 43 44 //测试清单更改测试清单添加、更新和删除 45 // 46 func TestManifestChangeEncrypted(t *testing.T) { 47 testManifestChange(t, true) 48 } 49 50 // 51 //-清单添加 52 // 53 // 54 // 55 //根清单或嵌套清单中的路径上的命令。 56 //参数encrypt控制是否使用加密。 57 func testManifestChange(t *testing.T, encrypt bool) { 58 t.Parallel() 59 cluster := newTestCluster(t, 1) 60 defer cluster.Shutdown() 61 62 tmp, err := ioutil.TempDir("", "swarm-manifest-test") 63 if err != nil { 64 t.Fatal(err) 65 } 66 defer os.RemoveAll(tmp) 67 68 origDir := filepath.Join(tmp, "orig") 69 if err := os.Mkdir(origDir, 0777); err != nil { 70 t.Fatal(err) 71 } 72 73 indexDataFilename := filepath.Join(origDir, "index.html") 74 err = ioutil.WriteFile(indexDataFilename, []byte("<h1>Test</h1>"), 0666) 75 if err != nil { 76 t.Fatal(err) 77 } 78 // 79 // 80 // 81 err = ioutil.WriteFile(filepath.Join(origDir, "robots.txt"), []byte("Disallow: /"), 0666) 82 if err != nil { 83 t.Fatal(err) 84 } 85 err = ioutil.WriteFile(filepath.Join(origDir, "robots.html"), []byte("<strong>No Robots Allowed</strong>"), 0666) 86 if err != nil { 87 t.Fatal(err) 88 } 89 err = ioutil.WriteFile(filepath.Join(origDir, "mutants.txt"), []byte("Frank\nMarcus"), 0666) 90 if err != nil { 91 t.Fatal(err) 92 } 93 94 args := []string{ 95 "--bzzapi", 96 cluster.Nodes[0].URL, 97 "--recursive", 98 "--defaultpath", 99 indexDataFilename, 100 "up", 101 origDir, 102 } 103 if encrypt { 104 args = append(args, "--encrypt") 105 } 106 107 origManifestHash := runSwarmExpectHash(t, args...) 108 109 checkHashLength(t, origManifestHash, encrypt) 110 111 client := swarm.NewClient(cluster.Nodes[0].URL) 112 113 //上载新文件并使用其清单将其添加到原始清单。 114 t.Run("add", func(t *testing.T) { 115 humansData := []byte("Ann\nBob") 116 humansDataFilename := filepath.Join(tmp, "humans.txt") 117 err = ioutil.WriteFile(humansDataFilename, humansData, 0666) 118 if err != nil { 119 t.Fatal(err) 120 } 121 122 humansManifestHash := runSwarmExpectHash(t, 123 "--bzzapi", 124 cluster.Nodes[0].URL, 125 "up", 126 humansDataFilename, 127 ) 128 129 newManifestHash := runSwarmExpectHash(t, 130 "--bzzapi", 131 cluster.Nodes[0].URL, 132 "manifest", 133 "add", 134 origManifestHash, 135 "humans.txt", 136 humansManifestHash, 137 ) 138 139 checkHashLength(t, newManifestHash, encrypt) 140 141 newManifest := downloadManifest(t, client, newManifestHash, encrypt) 142 143 var found bool 144 for _, e := range newManifest.Entries { 145 if e.Path == "humans.txt" { 146 found = true 147 if e.Size != int64(len(humansData)) { 148 t.Errorf("expected humans.txt size %v, got %v", len(humansData), e.Size) 149 } 150 if e.ModTime.IsZero() { 151 t.Errorf("got zero mod time for humans.txt") 152 } 153 ct := "text/plain; charset=utf-8" 154 if e.ContentType != ct { 155 t.Errorf("expected content type %q, got %q", ct, e.ContentType) 156 } 157 break 158 } 159 } 160 if !found { 161 t.Fatal("no humans.txt in new manifest") 162 } 163 164 checkFile(t, client, newManifestHash, "humans.txt", humansData) 165 }) 166 167 //上载新文件并使用其清单添加原始清单, 168 // 169 t.Run("add nested", func(t *testing.T) { 170 robotsData := []byte(`{"disallow": "/"}`) 171 robotsDataFilename := filepath.Join(tmp, "robots.json") 172 err = ioutil.WriteFile(robotsDataFilename, robotsData, 0666) 173 if err != nil { 174 t.Fatal(err) 175 } 176 177 robotsManifestHash := runSwarmExpectHash(t, 178 "--bzzapi", 179 cluster.Nodes[0].URL, 180 "up", 181 robotsDataFilename, 182 ) 183 184 newManifestHash := runSwarmExpectHash(t, 185 "--bzzapi", 186 cluster.Nodes[0].URL, 187 "manifest", 188 "add", 189 origManifestHash, 190 "robots.json", 191 robotsManifestHash, 192 ) 193 194 checkHashLength(t, newManifestHash, encrypt) 195 196 newManifest := downloadManifest(t, client, newManifestHash, encrypt) 197 198 var found bool 199 loop: 200 for _, e := range newManifest.Entries { 201 if e.Path == "robots." { 202 nestedManifest := downloadManifest(t, client, e.Hash, encrypt) 203 for _, e := range nestedManifest.Entries { 204 if e.Path == "json" { 205 found = true 206 if e.Size != int64(len(robotsData)) { 207 t.Errorf("expected robots.json size %v, got %v", len(robotsData), e.Size) 208 } 209 if e.ModTime.IsZero() { 210 t.Errorf("got zero mod time for robots.json") 211 } 212 ct := "application/json" 213 if e.ContentType != ct { 214 t.Errorf("expected content type %q, got %q", ct, e.ContentType) 215 } 216 break loop 217 } 218 } 219 } 220 } 221 if !found { 222 t.Fatal("no robots.json in new manifest") 223 } 224 225 checkFile(t, client, newManifestHash, "robots.json", robotsData) 226 }) 227 228 // 229 t.Run("update", func(t *testing.T) { 230 indexData := []byte("<h1>Ethereum Swarm</h1>") 231 indexDataFilename := filepath.Join(tmp, "index.html") 232 err = ioutil.WriteFile(indexDataFilename, indexData, 0666) 233 if err != nil { 234 t.Fatal(err) 235 } 236 237 indexManifestHash := runSwarmExpectHash(t, 238 "--bzzapi", 239 cluster.Nodes[0].URL, 240 "up", 241 indexDataFilename, 242 ) 243 244 newManifestHash := runSwarmExpectHash(t, 245 "--bzzapi", 246 cluster.Nodes[0].URL, 247 "manifest", 248 "update", 249 origManifestHash, 250 "index.html", 251 indexManifestHash, 252 ) 253 254 checkHashLength(t, newManifestHash, encrypt) 255 256 newManifest := downloadManifest(t, client, newManifestHash, encrypt) 257 258 var found bool 259 for _, e := range newManifest.Entries { 260 if e.Path == "index.html" { 261 found = true 262 if e.Size != int64(len(indexData)) { 263 t.Errorf("expected index.html size %v, got %v", len(indexData), e.Size) 264 } 265 if e.ModTime.IsZero() { 266 t.Errorf("got zero mod time for index.html") 267 } 268 ct := "text/html; charset=utf-8" 269 if e.ContentType != ct { 270 t.Errorf("expected content type %q, got %q", ct, e.ContentType) 271 } 272 break 273 } 274 } 275 if !found { 276 t.Fatal("no index.html in new manifest") 277 } 278 279 checkFile(t, client, newManifestHash, "index.html", indexData) 280 281 // 282 checkFile(t, client, newManifestHash, "", indexData) 283 }) 284 285 // 286 // 287 t.Run("update nested", func(t *testing.T) { 288 robotsData := []byte(`<string>Only humans allowed!!!</strong>`) 289 robotsDataFilename := filepath.Join(tmp, "robots.html") 290 err = ioutil.WriteFile(robotsDataFilename, robotsData, 0666) 291 if err != nil { 292 t.Fatal(err) 293 } 294 295 humansManifestHash := runSwarmExpectHash(t, 296 "--bzzapi", 297 cluster.Nodes[0].URL, 298 "up", 299 robotsDataFilename, 300 ) 301 302 newManifestHash := runSwarmExpectHash(t, 303 "--bzzapi", 304 cluster.Nodes[0].URL, 305 "manifest", 306 "update", 307 origManifestHash, 308 "robots.html", 309 humansManifestHash, 310 ) 311 312 checkHashLength(t, newManifestHash, encrypt) 313 314 newManifest := downloadManifest(t, client, newManifestHash, encrypt) 315 316 var found bool 317 loop: 318 for _, e := range newManifest.Entries { 319 if e.Path == "robots." { 320 nestedManifest := downloadManifest(t, client, e.Hash, encrypt) 321 for _, e := range nestedManifest.Entries { 322 if e.Path == "html" { 323 found = true 324 if e.Size != int64(len(robotsData)) { 325 t.Errorf("expected robots.html size %v, got %v", len(robotsData), e.Size) 326 } 327 if e.ModTime.IsZero() { 328 t.Errorf("got zero mod time for robots.html") 329 } 330 ct := "text/html; charset=utf-8" 331 if e.ContentType != ct { 332 t.Errorf("expected content type %q, got %q", ct, e.ContentType) 333 } 334 break loop 335 } 336 } 337 } 338 } 339 if !found { 340 t.Fatal("no robots.html in new manifest") 341 } 342 343 checkFile(t, client, newManifestHash, "robots.html", robotsData) 344 }) 345 346 // 347 t.Run("remove", func(t *testing.T) { 348 newManifestHash := runSwarmExpectHash(t, 349 "--bzzapi", 350 cluster.Nodes[0].URL, 351 "manifest", 352 "remove", 353 origManifestHash, 354 "mutants.txt", 355 ) 356 357 checkHashLength(t, newManifestHash, encrypt) 358 359 newManifest := downloadManifest(t, client, newManifestHash, encrypt) 360 361 var found bool 362 for _, e := range newManifest.Entries { 363 if e.Path == "mutants.txt" { 364 found = true 365 break 366 } 367 } 368 if found { 369 t.Fatal("mutants.txt is not removed") 370 } 371 }) 372 373 // 374 //原始清单的嵌套清单。 375 t.Run("remove nested", func(t *testing.T) { 376 newManifestHash := runSwarmExpectHash(t, 377 "--bzzapi", 378 cluster.Nodes[0].URL, 379 "manifest", 380 "remove", 381 origManifestHash, 382 "robots.html", 383 ) 384 385 checkHashLength(t, newManifestHash, encrypt) 386 387 newManifest := downloadManifest(t, client, newManifestHash, encrypt) 388 389 var found bool 390 loop: 391 for _, e := range newManifest.Entries { 392 if e.Path == "robots." { 393 nestedManifest := downloadManifest(t, client, e.Hash, encrypt) 394 for _, e := range nestedManifest.Entries { 395 if e.Path == "html" { 396 found = true 397 break loop 398 } 399 } 400 } 401 } 402 if found { 403 t.Fatal("robots.html in not removed") 404 } 405 }) 406 } 407 408 // 409 // 410 func TestNestedDefaultEntryUpdate(t *testing.T) { 411 testNestedDefaultEntryUpdate(t, false) 412 } 413 414 // 415 //如果嵌套清单中的文件 416 // 417 func TestNestedDefaultEntryUpdateEncrypted(t *testing.T) { 418 testNestedDefaultEntryUpdate(t, true) 419 } 420 421 func testNestedDefaultEntryUpdate(t *testing.T, encrypt bool) { 422 t.Parallel() 423 cluster := newTestCluster(t, 1) 424 defer cluster.Shutdown() 425 426 tmp, err := ioutil.TempDir("", "swarm-manifest-test") 427 if err != nil { 428 t.Fatal(err) 429 } 430 defer os.RemoveAll(tmp) 431 432 origDir := filepath.Join(tmp, "orig") 433 if err := os.Mkdir(origDir, 0777); err != nil { 434 t.Fatal(err) 435 } 436 437 indexData := []byte("<h1>Test</h1>") 438 indexDataFilename := filepath.Join(origDir, "index.html") 439 err = ioutil.WriteFile(indexDataFilename, indexData, 0666) 440 if err != nil { 441 t.Fatal(err) 442 } 443 // 444 // 445 err = ioutil.WriteFile(filepath.Join(origDir, "index.txt"), []byte("Test"), 0666) 446 if err != nil { 447 t.Fatal(err) 448 } 449 450 args := []string{ 451 "--bzzapi", 452 cluster.Nodes[0].URL, 453 "--recursive", 454 "--defaultpath", 455 indexDataFilename, 456 "up", 457 origDir, 458 } 459 if encrypt { 460 args = append(args, "--encrypt") 461 } 462 463 origManifestHash := runSwarmExpectHash(t, args...) 464 465 checkHashLength(t, origManifestHash, encrypt) 466 467 client := swarm.NewClient(cluster.Nodes[0].URL) 468 469 newIndexData := []byte("<h1>Ethereum Swarm</h1>") 470 newIndexDataFilename := filepath.Join(tmp, "index.html") 471 err = ioutil.WriteFile(newIndexDataFilename, newIndexData, 0666) 472 if err != nil { 473 t.Fatal(err) 474 } 475 476 newIndexManifestHash := runSwarmExpectHash(t, 477 "--bzzapi", 478 cluster.Nodes[0].URL, 479 "up", 480 newIndexDataFilename, 481 ) 482 483 newManifestHash := runSwarmExpectHash(t, 484 "--bzzapi", 485 cluster.Nodes[0].URL, 486 "manifest", 487 "update", 488 origManifestHash, 489 "index.html", 490 newIndexManifestHash, 491 ) 492 493 checkHashLength(t, newManifestHash, encrypt) 494 495 newManifest := downloadManifest(t, client, newManifestHash, encrypt) 496 497 var found bool 498 for _, e := range newManifest.Entries { 499 if e.Path == "index." { 500 found = true 501 newManifest = downloadManifest(t, client, e.Hash, encrypt) 502 break 503 } 504 } 505 if !found { 506 t.Fatal("no index. path in new manifest") 507 } 508 509 found = false 510 for _, e := range newManifest.Entries { 511 if e.Path == "html" { 512 found = true 513 if e.Size != int64(len(newIndexData)) { 514 t.Errorf("expected index.html size %v, got %v", len(newIndexData), e.Size) 515 } 516 if e.ModTime.IsZero() { 517 t.Errorf("got zero mod time for index.html") 518 } 519 ct := "text/html; charset=utf-8" 520 if e.ContentType != ct { 521 t.Errorf("expected content type %q, got %q", ct, e.ContentType) 522 } 523 break 524 } 525 } 526 if !found { 527 t.Fatal("no html in new manifest") 528 } 529 530 checkFile(t, client, newManifestHash, "index.html", newIndexData) 531 532 // 533 checkFile(t, client, newManifestHash, "", newIndexData) 534 } 535 536 func runSwarmExpectHash(t *testing.T, args ...string) (hash string) { 537 t.Helper() 538 hashRegexp := `[a-f\d]{64,128}` 539 up := runSwarm(t, args...) 540 _, matches := up.ExpectRegexp(hashRegexp) 541 up.ExpectExit() 542 543 if len(matches) < 1 { 544 t.Fatal("no matches found") 545 } 546 return matches[0] 547 } 548 549 func checkHashLength(t *testing.T, hash string, encrypted bool) { 550 t.Helper() 551 l := len(hash) 552 if encrypted && l != 128 { 553 t.Errorf("expected hash length 128, got %v", l) 554 } 555 if !encrypted && l != 64 { 556 t.Errorf("expected hash length 64, got %v", l) 557 } 558 } 559 560 func downloadManifest(t *testing.T, client *swarm.Client, hash string, encrypted bool) (manifest *api.Manifest) { 561 t.Helper() 562 m, isEncrypted, err := client.DownloadManifest(hash) 563 if err != nil { 564 t.Fatal(err) 565 } 566 567 if encrypted != isEncrypted { 568 t.Error("new manifest encryption flag is not correct") 569 } 570 return m 571 } 572 573 func checkFile(t *testing.T, client *swarm.Client, hash, path string, expected []byte) { 574 t.Helper() 575 f, err := client.Download(hash, path) 576 if err != nil { 577 t.Fatal(err) 578 } 579 580 got, err := ioutil.ReadAll(f) 581 if err != nil { 582 t.Fatal(err) 583 } 584 if !bytes.Equal(got, expected) { 585 t.Errorf("expected file content %q, got %q", expected, got) 586 } 587 }