|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Security.Cryptography; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using ManagedCode.Storage.Azure; |
| 7 | +using ManagedCode.Storage.Azure.Options; |
| 8 | +using ManagedCode.Storage.Core.Primitives; |
| 9 | +using ManagedCode.Storage.Tests.Common; |
| 10 | +using Shouldly; |
| 11 | +using Testcontainers.Azurite; |
| 12 | +using Xunit; |
| 13 | + |
| 14 | +namespace ManagedCode.Storage.Tests.Storages.Azure; |
| 15 | + |
| 16 | +public sealed class AzureVerifiedUploadDigestTests : IAsyncLifetime |
| 17 | +{ |
| 18 | + private const int MultipartPayloadBytes = 4 * 1024 * 1024 + 9; |
| 19 | + |
| 20 | + private readonly AzuriteContainer _container = new AzuriteBuilder(ContainerImages.Azurite) |
| 21 | + .WithCommand("--skipApiVersionCheck").Build(); |
| 22 | + |
| 23 | + public ValueTask InitializeAsync() => new(_container.StartAsync()); |
| 24 | + public ValueTask DisposeAsync() => _container.DisposeAsync(); |
| 25 | + |
| 26 | + [Fact] |
| 27 | + public async Task StreamUpload_WithMatchingDigest_CommitsAndReusesRetry() |
| 28 | + { |
| 29 | + var cancellationToken = TestContext.Current.CancellationToken; |
| 30 | + using var storage = CreateStorage(); |
| 31 | + var objects = await CreateContainerAsync(storage); |
| 32 | + var bytes = RandomNumberGenerator.GetBytes(MultipartPayloadBytes); |
| 33 | + var digest = Convert.ToHexString(SHA256.HashData(bytes)); |
| 34 | + |
| 35 | + using var first = new MemoryStream(bytes, writable: false); |
| 36 | + var written = await objects.WriteIfAbsentOrSameAsync("archive.zip", first, bytes.LongLength, digest, |
| 37 | + cancellationToken: cancellationToken); |
| 38 | + using var retry = new MemoryStream(bytes, writable: false); |
| 39 | + var replay = await objects.WriteIfAbsentOrSameAsync("archive.zip", retry, bytes.LongLength, |
| 40 | + digest.ToLowerInvariant(), cancellationToken: cancellationToken); |
| 41 | + |
| 42 | + written.ReusedExisting.ShouldBeFalse(); |
| 43 | + written.Sha256.ShouldBe(digest.ToLowerInvariant()); |
| 44 | + written.Info.Length.ShouldBe(bytes.LongLength); |
| 45 | + replay.ReusedExisting.ShouldBeTrue(); |
| 46 | + replay.Info.ETag.ShouldBe(written.Info.ETag); |
| 47 | + } |
| 48 | + |
| 49 | + [Fact] |
| 50 | + public async Task StreamUpload_WithDifferentDigest_CommitsNothing() |
| 51 | + { |
| 52 | + var cancellationToken = TestContext.Current.CancellationToken; |
| 53 | + using var storage = CreateStorage(); |
| 54 | + var objects = await CreateContainerAsync(storage); |
| 55 | + var bytes = Encoding.UTF8.GetBytes("declared content"); |
| 56 | + var otherDigest = Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes("other content"))); |
| 57 | + |
| 58 | + using var source = new MemoryStream(bytes, writable: false); |
| 59 | + await Should.ThrowAsync<StorageUploadDigestException>(() => objects.WriteIfAbsentOrSameAsync( |
| 60 | + "mismatch.bin", source, bytes.LongLength, otherDigest, cancellationToken: cancellationToken)); |
| 61 | + |
| 62 | + (await objects.ObjectExistsAsync("mismatch.bin", cancellationToken)).ShouldBeFalse(); |
| 63 | + } |
| 64 | + |
| 65 | + [Fact] |
| 66 | + public async Task ByteUpload_WithExpectedDigest_WritesOnlyMatchingContent() |
| 67 | + { |
| 68 | + var cancellationToken = TestContext.Current.CancellationToken; |
| 69 | + using var storage = CreateStorage(); |
| 70 | + var objects = await CreateContainerAsync(storage); |
| 71 | + var bytes = Encoding.UTF8.GetBytes("{\"value\":1}"); |
| 72 | + var digest = Convert.ToHexStringLower(SHA256.HashData(bytes)); |
| 73 | + var options = new StorageWriteOptions { ContentType = "application/json" }; |
| 74 | + |
| 75 | + await Should.ThrowAsync<StorageUploadDigestException>(() => objects.WriteBytesIfAbsentOrSameAsync( |
| 76 | + "payload.json", Encoding.UTF8.GetBytes("{\"value\":2}"), digest, options, cancellationToken)); |
| 77 | + (await objects.ObjectExistsAsync("payload.json", cancellationToken)).ShouldBeFalse(); |
| 78 | + |
| 79 | + var written = await objects.WriteBytesIfAbsentOrSameAsync("payload.json", bytes, digest, options, |
| 80 | + cancellationToken); |
| 81 | + var replay = await objects.WriteBytesIfAbsentOrSameAsync("payload.json", bytes, digest, options, |
| 82 | + cancellationToken); |
| 83 | + |
| 84 | + written.ReusedExisting.ShouldBeFalse(); |
| 85 | + written.Sha256.ShouldBe(digest); |
| 86 | + replay.ReusedExisting.ShouldBeTrue(); |
| 87 | + replay.Info.ETag.ShouldBe(written.Info.ETag); |
| 88 | + } |
| 89 | + |
| 90 | + [Theory] |
| 91 | + [InlineData("abc")] |
| 92 | + [InlineData("zz00000000000000000000000000000000000000000000000000000000000000")] |
| 93 | + public async Task Upload_WithMalformedDigest_RejectsBeforeWriting(string expectedSha256) |
| 94 | + { |
| 95 | + var cancellationToken = TestContext.Current.CancellationToken; |
| 96 | + using var storage = CreateStorage(); |
| 97 | + var objects = await CreateContainerAsync(storage); |
| 98 | + var bytes = Encoding.UTF8.GetBytes("content"); |
| 99 | + |
| 100 | + using var source = new MemoryStream(bytes, writable: false); |
| 101 | + (await Should.ThrowAsync<ArgumentException>(() => objects.WriteIfAbsentOrSameAsync( |
| 102 | + "malformed.bin", source, bytes.LongLength, expectedSha256, cancellationToken: cancellationToken))) |
| 103 | + .ParamName.ShouldBe("expectedSha256"); |
| 104 | + (await Should.ThrowAsync<ArgumentException>(() => objects.WriteBytesIfAbsentOrSameAsync( |
| 105 | + "malformed.bin", bytes, expectedSha256, cancellationToken: cancellationToken))) |
| 106 | + .ParamName.ShouldBe("expectedSha256"); |
| 107 | + |
| 108 | + (await objects.ObjectExistsAsync("malformed.bin", cancellationToken)).ShouldBeFalse(); |
| 109 | + } |
| 110 | + |
| 111 | + private AzureStorage CreateStorage() => new(new AzureStorageOptions |
| 112 | + { |
| 113 | + ConnectionString = _container.GetConnectionString(), |
| 114 | + Container = $"verified-digest-{Guid.NewGuid():N}", |
| 115 | + CreateContainerIfNotExists = false |
| 116 | + }); |
| 117 | + |
| 118 | + private static async Task<IObjectStorage> CreateContainerAsync(AzureStorage storage) |
| 119 | + { |
| 120 | + var objects = storage.RequireObjectStorage(); |
| 121 | + await objects.CreatePrivateContainerAsync(cancellationToken: TestContext.Current.CancellationToken); |
| 122 | + return objects; |
| 123 | + } |
| 124 | +} |
0 commit comments