|
12 | 12 | // See the License for the specific language governing permissions and
|
13 | 13 | // limitations under the License.
|
14 | 14 |
|
| 15 | +using Google.Apis.Upload; |
15 | 16 | using System;
|
| 17 | +using System.Collections.Generic; |
16 | 18 | using System.IO;
|
17 | 19 | using System.Linq;
|
18 | 20 | using System.Net;
|
@@ -476,5 +478,138 @@ private void PutWithCustomHeadersTest_InitDelayTest()
|
476 | 478 | Assert.Null(obj);
|
477 | 479 | });
|
478 | 480 | }
|
| 481 | + |
| 482 | + [Fact] |
| 483 | + public async Task ResumableUploadTest() => await _fixture.FinishDelayTest(GetTestName()); |
| 484 | + |
| 485 | + private void ResumableUploadTest_InitDelayTest() |
| 486 | + { |
| 487 | + var bucket = _fixture.SingleVersionBucket; |
| 488 | + var name = GenerateName(); |
| 489 | + var data = _fixture.SmallContent; |
| 490 | + string url = null; |
| 491 | + |
| 492 | + _fixture.RegisterDelayTest(_duration, |
| 493 | + beforeDelay: async duration => |
| 494 | + { |
| 495 | + url = _fixture.UrlSigner.Sign(bucket, name, duration, UrlSigner.ResumableHttpMethod); |
| 496 | + |
| 497 | + // Verify that the URL works initially. |
| 498 | + var uploader = SignedUrlResumableUpload.Create(url, new MemoryStream(data)); |
| 499 | + var progress = await uploader.UploadAsync(); |
| 500 | + Assert.Equal(UploadStatus.Completed, progress.Status); |
| 501 | + |
| 502 | + var result = new MemoryStream(); |
| 503 | + await _fixture.Client.DownloadObjectAsync(bucket, name, result); |
| 504 | + Assert.Equal(result.ToArray(), data); |
| 505 | + |
| 506 | + // Reset the state. |
| 507 | + await _fixture.Client.DeleteObjectAsync(bucket, name); |
| 508 | + }, |
| 509 | + afterDelay: async () => |
| 510 | + { |
| 511 | + var uploader = SignedUrlResumableUpload.Create(url, new MemoryStream(data)); |
| 512 | + |
| 513 | + // Verify that the URL no longer works. |
| 514 | + var progress = await uploader.UploadAsync(); |
| 515 | + Assert.Equal(UploadStatus.Failed, progress.Status); |
| 516 | + Assert.IsType(typeof(GoogleApiException), progress.Exception); |
| 517 | + |
| 518 | + var obj = await _fixture.Client.ListObjectsAsync(bucket, name).FirstOrDefault(o => o.Name == name); |
| 519 | + Assert.Null(obj); |
| 520 | + }); |
| 521 | + } |
| 522 | + |
| 523 | + [Fact] |
| 524 | + public async Task ResumableUploadResumeTest() => await _fixture.FinishDelayTest(GetTestName()); |
| 525 | + |
| 526 | + private void ResumableUploadResumeTest_InitDelayTest() |
| 527 | + { |
| 528 | + var bucket = _fixture.SingleVersionBucket; |
| 529 | + var name = GenerateName(); |
| 530 | + var data = _fixture.SmallContent; |
| 531 | + string url = null; |
| 532 | + |
| 533 | + _fixture.RegisterDelayTest(_duration, |
| 534 | + beforeDelay: async duration => |
| 535 | + { |
| 536 | + url = _fixture.UrlSigner.Sign(bucket, name, duration, UrlSigner.ResumableHttpMethod); |
| 537 | + var sessionUri = await SignedUrlResumableUpload.InitiateSessionAsync(url); |
| 538 | + |
| 539 | + // Verify that the URL works initially. |
| 540 | + var uploader = ResumableUpload.CreateFromUploadUri(sessionUri, new MemoryStream(data)); |
| 541 | + await uploader.ResumeAsync(sessionUri); |
| 542 | + var result = new MemoryStream(); |
| 543 | + await _fixture.Client.DownloadObjectAsync(bucket, name, result); |
| 544 | + Assert.Equal(result.ToArray(), data); |
| 545 | + |
| 546 | + // Reset the state. |
| 547 | + await _fixture.Client.DeleteObjectAsync(bucket, name); |
| 548 | + }, |
| 549 | + afterDelay: async () => |
| 550 | + { |
| 551 | + // Verify that the URL no longer works. |
| 552 | + await Assert.ThrowsAsync<GoogleApiException>(() => SignedUrlResumableUpload.InitiateSessionAsync(url)); |
| 553 | + |
| 554 | + var obj = await _fixture.Client.ListObjectsAsync(bucket, name).FirstOrDefault(o => o.Name == name); |
| 555 | + Assert.Null(obj); |
| 556 | + }); |
| 557 | + } |
| 558 | + |
| 559 | + [Fact] |
| 560 | + public async Task ResumableUploadWithCustomerSuppliedEncryptionKeysTest() => await _fixture.FinishDelayTest(GetTestName()); |
| 561 | + |
| 562 | + private void ResumableUploadWithCustomerSuppliedEncryptionKeysTest_InitDelayTest() |
| 563 | + { |
| 564 | + var bucket = _fixture.SingleVersionBucket; |
| 565 | + var name = GenerateName(); |
| 566 | + var data = _fixture.SmallContent; |
| 567 | + string url = null; |
| 568 | + |
| 569 | + EncryptionKey key = EncryptionKey.Generate(); |
| 570 | + |
| 571 | + _fixture.RegisterDelayTest(_duration, |
| 572 | + beforeDelay: async duration => |
| 573 | + { |
| 574 | + url = _fixture.UrlSigner.Sign( |
| 575 | + bucket, |
| 576 | + name, |
| 577 | + duration, |
| 578 | + UrlSigner.ResumableHttpMethod, |
| 579 | + requestHeaders: new Dictionary<string, IEnumerable<string>> { |
| 580 | + { "x-goog-encryption-algorithm", new [] { "AES256" } }, |
| 581 | + { "x-goog-encryption-key", new [] { key.Base64Key } }, |
| 582 | + { "x-goog-encryption-key-sha256", new []{ key.Base64Hash } } |
| 583 | + }); |
| 584 | + |
| 585 | + // Verify that the URL works initially. |
| 586 | + var uploader = SignedUrlResumableUpload.Create( |
| 587 | + url, |
| 588 | + new MemoryStream(data), |
| 589 | + new ResumableUploadOptions { ModifySessionInitiationRequest = key.ModifyRequest }); |
| 590 | + var progress = await uploader.UploadAsync(); |
| 591 | + Assert.Equal(UploadStatus.Completed, progress.Status); |
| 592 | + |
| 593 | + // Make sure the encryption succeeded. |
| 594 | + var downloadedData = new MemoryStream(); |
| 595 | + await Assert.ThrowsAsync<GoogleApiException>( |
| 596 | + () => _fixture.Client.DownloadObjectAsync(bucket, name, downloadedData)); |
| 597 | + |
| 598 | + await _fixture.Client.DownloadObjectAsync(bucket, name, downloadedData, new DownloadObjectOptions { EncryptionKey = key }); |
| 599 | + Assert.Equal(data, downloadedData.ToArray()); |
| 600 | + }, |
| 601 | + afterDelay: async () => |
| 602 | + { |
| 603 | + var uploader = SignedUrlResumableUpload.Create( |
| 604 | + url, |
| 605 | + new MemoryStream(data), |
| 606 | + new ResumableUploadOptions { ModifySessionInitiationRequest = key.ModifyRequest }); |
| 607 | + |
| 608 | + // Verify that the URL no longer works. |
| 609 | + var progress = await uploader.UploadAsync(); |
| 610 | + Assert.Equal(UploadStatus.Failed, progress.Status); |
| 611 | + Assert.IsType(typeof(GoogleApiException), progress.Exception); |
| 612 | + }); |
| 613 | + } |
479 | 614 | }
|
480 | 615 | }
|
0 commit comments