github.com/cheng762/platon-go@v1.8.17-0.20190529111256-7deff2d7be26/swarm/api/client/client_test.go (about) 1 // Copyright 2017 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 client 18 19 import ( 20 "bytes" 21 "io/ioutil" 22 "os" 23 "path/filepath" 24 "reflect" 25 "sort" 26 "testing" 27 28 "github.com/PlatONnetwork/PlatON-Go/common" 29 "github.com/PlatONnetwork/PlatON-Go/crypto" 30 "github.com/PlatONnetwork/PlatON-Go/swarm/api" 31 swarmhttp "github.com/PlatONnetwork/PlatON-Go/swarm/api/http" 32 "github.com/PlatONnetwork/PlatON-Go/swarm/multihash" 33 "github.com/PlatONnetwork/PlatON-Go/swarm/storage/mru" 34 "github.com/PlatONnetwork/PlatON-Go/swarm/testutil" 35 ) 36 37 func serverFunc(api *api.API) testutil.TestServer { 38 return swarmhttp.NewServer(api, "") 39 } 40 41 // TestClientUploadDownloadRaw test uploading and downloading raw data to swarm 42 func TestClientUploadDownloadRaw(t *testing.T) { 43 testClientUploadDownloadRaw(false, t) 44 } 45 func TestClientUploadDownloadRawEncrypted(t *testing.T) { 46 testClientUploadDownloadRaw(true, t) 47 } 48 49 func testClientUploadDownloadRaw(toEncrypt bool, t *testing.T) { 50 srv := testutil.NewTestSwarmServer(t, serverFunc, nil) 51 defer srv.Close() 52 53 client := NewClient(srv.URL) 54 55 // upload some raw data 56 data := []byte("foo123") 57 hash, err := client.UploadRaw(bytes.NewReader(data), int64(len(data)), toEncrypt) 58 if err != nil { 59 t.Fatal(err) 60 } 61 62 // check we can download the same data 63 res, isEncrypted, err := client.DownloadRaw(hash) 64 if err != nil { 65 t.Fatal(err) 66 } 67 if isEncrypted != toEncrypt { 68 t.Fatalf("Expected encyption status %v got %v", toEncrypt, isEncrypted) 69 } 70 defer res.Close() 71 gotData, err := ioutil.ReadAll(res) 72 if err != nil { 73 t.Fatal(err) 74 } 75 if !bytes.Equal(gotData, data) { 76 t.Fatalf("expected downloaded data to be %q, got %q", data, gotData) 77 } 78 } 79 80 // TestClientUploadDownloadFiles test uploading and downloading files to swarm 81 // manifests 82 func TestClientUploadDownloadFiles(t *testing.T) { 83 testClientUploadDownloadFiles(false, t) 84 } 85 86 func TestClientUploadDownloadFilesEncrypted(t *testing.T) { 87 testClientUploadDownloadFiles(true, t) 88 } 89 90 func testClientUploadDownloadFiles(toEncrypt bool, t *testing.T) { 91 srv := testutil.NewTestSwarmServer(t, serverFunc, nil) 92 defer srv.Close() 93 94 client := NewClient(srv.URL) 95 upload := func(manifest, path string, data []byte) string { 96 file := &File{ 97 ReadCloser: ioutil.NopCloser(bytes.NewReader(data)), 98 ManifestEntry: api.ManifestEntry{ 99 Path: path, 100 ContentType: "text/plain", 101 Size: int64(len(data)), 102 }, 103 } 104 hash, err := client.Upload(file, manifest, toEncrypt) 105 if err != nil { 106 t.Fatal(err) 107 } 108 return hash 109 } 110 checkDownload := func(manifest, path string, expected []byte) { 111 file, err := client.Download(manifest, path) 112 if err != nil { 113 t.Fatal(err) 114 } 115 defer file.Close() 116 if file.Size != int64(len(expected)) { 117 t.Fatalf("expected downloaded file to be %d bytes, got %d", len(expected), file.Size) 118 } 119 if file.ContentType != "text/plain" { 120 t.Fatalf("expected downloaded file to have type %q, got %q", "text/plain", file.ContentType) 121 } 122 data, err := ioutil.ReadAll(file) 123 if err != nil { 124 t.Fatal(err) 125 } 126 if !bytes.Equal(data, expected) { 127 t.Fatalf("expected downloaded data to be %q, got %q", expected, data) 128 } 129 } 130 131 // upload a file to the root of a manifest 132 rootData := []byte("some-data") 133 rootHash := upload("", "", rootData) 134 135 // check we can download the root file 136 checkDownload(rootHash, "", rootData) 137 138 // upload another file to the same manifest 139 otherData := []byte("some-other-data") 140 newHash := upload(rootHash, "some/other/path", otherData) 141 142 // check we can download both files from the new manifest 143 checkDownload(newHash, "", rootData) 144 checkDownload(newHash, "some/other/path", otherData) 145 146 // replace the root file with different data 147 newHash = upload(newHash, "", otherData) 148 149 // check both files have the other data 150 checkDownload(newHash, "", otherData) 151 checkDownload(newHash, "some/other/path", otherData) 152 } 153 154 var testDirFiles = []string{ 155 "file1.txt", 156 "file2.txt", 157 "dir1/file3.txt", 158 "dir1/file4.txt", 159 "dir2/file5.txt", 160 "dir2/dir3/file6.txt", 161 "dir2/dir4/file7.txt", 162 "dir2/dir4/file8.txt", 163 } 164 165 func newTestDirectory(t *testing.T) string { 166 dir, err := ioutil.TempDir("", "swarm-client-test") 167 if err != nil { 168 t.Fatal(err) 169 } 170 171 for _, file := range testDirFiles { 172 path := filepath.Join(dir, file) 173 if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil { 174 os.RemoveAll(dir) 175 t.Fatalf("error creating dir for %s: %s", path, err) 176 } 177 if err := ioutil.WriteFile(path, []byte(file), 0644); err != nil { 178 os.RemoveAll(dir) 179 t.Fatalf("error writing file %s: %s", path, err) 180 } 181 } 182 183 return dir 184 } 185 186 // TestClientUploadDownloadDirectory tests uploading and downloading a 187 // directory of files to a swarm manifest 188 func TestClientUploadDownloadDirectory(t *testing.T) { 189 srv := testutil.NewTestSwarmServer(t, serverFunc, nil) 190 defer srv.Close() 191 192 dir := newTestDirectory(t) 193 defer os.RemoveAll(dir) 194 195 // upload the directory 196 client := NewClient(srv.URL) 197 defaultPath := testDirFiles[0] 198 hash, err := client.UploadDirectory(dir, defaultPath, "", false) 199 if err != nil { 200 t.Fatalf("error uploading directory: %s", err) 201 } 202 203 // check we can download the individual files 204 checkDownloadFile := func(path string, expected []byte) { 205 file, err := client.Download(hash, path) 206 if err != nil { 207 t.Fatal(err) 208 } 209 defer file.Close() 210 data, err := ioutil.ReadAll(file) 211 if err != nil { 212 t.Fatal(err) 213 } 214 if !bytes.Equal(data, expected) { 215 t.Fatalf("expected data to be %q, got %q", expected, data) 216 } 217 } 218 for _, file := range testDirFiles { 219 checkDownloadFile(file, []byte(file)) 220 } 221 222 // check we can download the default path 223 checkDownloadFile("", []byte(testDirFiles[0])) 224 225 // check we can download the directory 226 tmp, err := ioutil.TempDir("", "swarm-client-test") 227 if err != nil { 228 t.Fatal(err) 229 } 230 defer os.RemoveAll(tmp) 231 if err := client.DownloadDirectory(hash, "", tmp, ""); err != nil { 232 t.Fatal(err) 233 } 234 for _, file := range testDirFiles { 235 data, err := ioutil.ReadFile(filepath.Join(tmp, file)) 236 if err != nil { 237 t.Fatal(err) 238 } 239 if !bytes.Equal(data, []byte(file)) { 240 t.Fatalf("expected data to be %q, got %q", file, data) 241 } 242 } 243 } 244 245 // TestClientFileList tests listing files in a swarm manifest 246 func TestClientFileList(t *testing.T) { 247 testClientFileList(false, t) 248 } 249 250 func TestClientFileListEncrypted(t *testing.T) { 251 testClientFileList(true, t) 252 } 253 254 func testClientFileList(toEncrypt bool, t *testing.T) { 255 srv := testutil.NewTestSwarmServer(t, serverFunc, nil) 256 defer srv.Close() 257 258 dir := newTestDirectory(t) 259 defer os.RemoveAll(dir) 260 261 client := NewClient(srv.URL) 262 hash, err := client.UploadDirectory(dir, "", "", toEncrypt) 263 if err != nil { 264 t.Fatalf("error uploading directory: %s", err) 265 } 266 267 ls := func(prefix string) []string { 268 list, err := client.List(hash, prefix, "") 269 if err != nil { 270 t.Fatal(err) 271 } 272 paths := make([]string, 0, len(list.CommonPrefixes)+len(list.Entries)) 273 paths = append(paths, list.CommonPrefixes...) 274 for _, entry := range list.Entries { 275 paths = append(paths, entry.Path) 276 } 277 sort.Strings(paths) 278 return paths 279 } 280 281 tests := map[string][]string{ 282 "": {"dir1/", "dir2/", "file1.txt", "file2.txt"}, 283 "file": {"file1.txt", "file2.txt"}, 284 "file1": {"file1.txt"}, 285 "file2.txt": {"file2.txt"}, 286 "file12": {}, 287 "dir": {"dir1/", "dir2/"}, 288 "dir1": {"dir1/"}, 289 "dir1/": {"dir1/file3.txt", "dir1/file4.txt"}, 290 "dir1/file": {"dir1/file3.txt", "dir1/file4.txt"}, 291 "dir1/file3.txt": {"dir1/file3.txt"}, 292 "dir1/file34": {}, 293 "dir2/": {"dir2/dir3/", "dir2/dir4/", "dir2/file5.txt"}, 294 "dir2/file": {"dir2/file5.txt"}, 295 "dir2/dir": {"dir2/dir3/", "dir2/dir4/"}, 296 "dir2/dir3/": {"dir2/dir3/file6.txt"}, 297 "dir2/dir4/": {"dir2/dir4/file7.txt", "dir2/dir4/file8.txt"}, 298 "dir2/dir4/file": {"dir2/dir4/file7.txt", "dir2/dir4/file8.txt"}, 299 "dir2/dir4/file7.txt": {"dir2/dir4/file7.txt"}, 300 "dir2/dir4/file78": {}, 301 } 302 for prefix, expected := range tests { 303 actual := ls(prefix) 304 if !reflect.DeepEqual(actual, expected) { 305 t.Fatalf("expected prefix %q to return %v, got %v", prefix, expected, actual) 306 } 307 } 308 } 309 310 // TestClientMultipartUpload tests uploading files to swarm using a multipart 311 // upload 312 func TestClientMultipartUpload(t *testing.T) { 313 srv := testutil.NewTestSwarmServer(t, serverFunc, nil) 314 defer srv.Close() 315 316 // define an uploader which uploads testDirFiles with some data 317 data := []byte("some-data") 318 uploader := UploaderFunc(func(upload UploadFn) error { 319 for _, name := range testDirFiles { 320 file := &File{ 321 ReadCloser: ioutil.NopCloser(bytes.NewReader(data)), 322 ManifestEntry: api.ManifestEntry{ 323 Path: name, 324 ContentType: "text/plain", 325 Size: int64(len(data)), 326 }, 327 } 328 if err := upload(file); err != nil { 329 return err 330 } 331 } 332 return nil 333 }) 334 335 // upload the files as a multipart upload 336 client := NewClient(srv.URL) 337 hash, err := client.MultipartUpload("", uploader) 338 if err != nil { 339 t.Fatal(err) 340 } 341 342 // check we can download the individual files 343 checkDownloadFile := func(path string) { 344 file, err := client.Download(hash, path) 345 if err != nil { 346 t.Fatal(err) 347 } 348 defer file.Close() 349 gotData, err := ioutil.ReadAll(file) 350 if err != nil { 351 t.Fatal(err) 352 } 353 if !bytes.Equal(gotData, data) { 354 t.Fatalf("expected data to be %q, got %q", data, gotData) 355 } 356 } 357 for _, file := range testDirFiles { 358 checkDownloadFile(file) 359 } 360 } 361 362 func newTestSigner() (*mru.GenericSigner, error) { 363 privKey, err := crypto.HexToECDSA("deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef") 364 if err != nil { 365 return nil, err 366 } 367 return mru.NewGenericSigner(privKey), nil 368 } 369 370 // test the transparent resolving of multihash resource types with bzz:// scheme 371 // 372 // first upload data, and store the multihash to the resulting manifest in a resource update 373 // retrieving the update with the multihash should return the manifest pointing directly to the data 374 // and raw retrieve of that hash should return the data 375 func TestClientCreateResourceMultihash(t *testing.T) { 376 377 signer, _ := newTestSigner() 378 379 srv := testutil.NewTestSwarmServer(t, serverFunc, nil) 380 client := NewClient(srv.URL) 381 defer srv.Close() 382 383 // add the data our multihash aliased manifest will point to 384 databytes := []byte("bar") 385 386 swarmHash, err := client.UploadRaw(bytes.NewReader(databytes), int64(len(databytes)), false) 387 if err != nil { 388 t.Fatalf("Error uploading raw test data: %s", err) 389 } 390 391 s := common.FromHex(swarmHash) 392 mh := multihash.ToMultihash(s) 393 394 // our mutable resource "name" 395 resourceName := "foo.eth" 396 397 createRequest, err := mru.NewCreateUpdateRequest(&mru.ResourceMetadata{ 398 Name: resourceName, 399 Frequency: 13, 400 StartTime: srv.GetCurrentTime(), 401 Owner: signer.Address(), 402 }) 403 if err != nil { 404 t.Fatal(err) 405 } 406 createRequest.SetData(mh, true) 407 if err := createRequest.Sign(signer); err != nil { 408 t.Fatalf("Error signing update: %s", err) 409 } 410 411 resourceManifestHash, err := client.CreateResource(createRequest) 412 413 if err != nil { 414 t.Fatalf("Error creating resource: %s", err) 415 } 416 417 correctManifestAddrHex := "6d3bc4664c97d8b821cb74bcae43f592494fb46d2d9cd31e69f3c7c802bbbd8e" 418 if resourceManifestHash != correctManifestAddrHex { 419 t.Fatalf("Response resource key mismatch, expected '%s', got '%s'", correctManifestAddrHex, resourceManifestHash) 420 } 421 422 reader, err := client.GetResource(correctManifestAddrHex) 423 if err != nil { 424 t.Fatalf("Error retrieving resource: %s", err) 425 } 426 defer reader.Close() 427 gotData, err := ioutil.ReadAll(reader) 428 if err != nil { 429 t.Fatal(err) 430 } 431 if !bytes.Equal(mh, gotData) { 432 t.Fatalf("Expected: %v, got %v", mh, gotData) 433 } 434 435 } 436 437 // TestClientCreateUpdateResource will check that mutable resources can be created and updated via the HTTP client. 438 func TestClientCreateUpdateResource(t *testing.T) { 439 440 signer, _ := newTestSigner() 441 442 srv := testutil.NewTestSwarmServer(t, serverFunc, nil) 443 client := NewClient(srv.URL) 444 defer srv.Close() 445 446 // set raw data for the resource 447 databytes := []byte("En un lugar de La Mancha, de cuyo nombre no quiero acordarme...") 448 449 // our mutable resource name 450 resourceName := "El Quijote" 451 452 createRequest, err := mru.NewCreateUpdateRequest(&mru.ResourceMetadata{ 453 Name: resourceName, 454 Frequency: 13, 455 StartTime: srv.GetCurrentTime(), 456 Owner: signer.Address(), 457 }) 458 if err != nil { 459 t.Fatal(err) 460 } 461 createRequest.SetData(databytes, false) 462 if err := createRequest.Sign(signer); err != nil { 463 t.Fatalf("Error signing update: %s", err) 464 } 465 466 resourceManifestHash, err := client.CreateResource(createRequest) 467 468 correctManifestAddrHex := "cc7904c17b49f9679e2d8006fe25e87e3f5c2072c2b49cab50f15e544471b30a" 469 if resourceManifestHash != correctManifestAddrHex { 470 t.Fatalf("Response resource key mismatch, expected '%s', got '%s'", correctManifestAddrHex, resourceManifestHash) 471 } 472 473 reader, err := client.GetResource(correctManifestAddrHex) 474 if err != nil { 475 t.Fatalf("Error retrieving resource: %s", err) 476 } 477 defer reader.Close() 478 gotData, err := ioutil.ReadAll(reader) 479 if err != nil { 480 t.Fatal(err) 481 } 482 if !bytes.Equal(databytes, gotData) { 483 t.Fatalf("Expected: %v, got %v", databytes, gotData) 484 } 485 486 // define different data 487 databytes = []byte("... no ha mucho tiempo que vivĂa un hidalgo de los de lanza en astillero ...") 488 489 updateRequest, err := client.GetResourceMetadata(correctManifestAddrHex) 490 if err != nil { 491 t.Fatalf("Error retrieving update request template: %s", err) 492 } 493 494 updateRequest.SetData(databytes, false) 495 if err := updateRequest.Sign(signer); err != nil { 496 t.Fatalf("Error signing update: %s", err) 497 } 498 499 if err = client.UpdateResource(updateRequest); err != nil { 500 t.Fatalf("Error updating resource: %s", err) 501 } 502 503 reader, err = client.GetResource(correctManifestAddrHex) 504 if err != nil { 505 t.Fatalf("Error retrieving resource: %s", err) 506 } 507 defer reader.Close() 508 gotData, err = ioutil.ReadAll(reader) 509 if err != nil { 510 t.Fatal(err) 511 } 512 if !bytes.Equal(databytes, gotData) { 513 t.Fatalf("Expected: %v, got %v", databytes, gotData) 514 } 515 516 }