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