public async Task Returns_400_Bad_Request_If_Upload_Checksum_Header_Is_Unparsable(string methodToUse) { var store = Substitute.For <ITusStore, ITusCreationStore, ITusChecksumStore>(); ITusChecksumStore cstore = null; using (var server = TestServerFactory.Create(app => { // ReSharper disable once SuspiciousTypeConversion.Global cstore = (ITusChecksumStore)store; cstore.GetSupportedAlgorithmsAsync(CancellationToken.None).ReturnsForAnyArgs(new[] { "md5" }); store.FileExistAsync("checksum", CancellationToken.None).ReturnsForAnyArgs(true); store.GetUploadOffsetAsync("checksum", Arg.Any <CancellationToken>()).Returns(5); store.GetUploadLengthAsync("checksum", Arg.Any <CancellationToken>()).Returns(10); store.AppendDataAsync("checksum", Arg.Any <Stream>(), Arg.Any <CancellationToken>()).Returns(5); app.UseTus(request => new DefaultTusConfiguration { Store = store, UrlPath = "/files" }); })) { // ReSharper disable once LoopCanBePartlyConvertedToQuery - Only applies to netstandard foreach (var unparsables in new[] { "Kq5sNclPz7QV2+lfQIuc6R7oRu0=", "sha1 ", "", "sha1 Kq5sNclPz7QV2+lfQIuc6R7oRu0" }) { #if netstandard // ASP.NET Core ignores empty headers so there is no way of knowing if the header was sent empty // or if the header is simply absent if (unparsables?.Length == 0) { continue; } #endif var response = await server .CreateRequest("/files/checksum") .And(m => m.AddBody()) .AddTusResumableHeader() .AddHeader("Upload-Offset", "5") .AddHeader("Upload-Checksum", unparsables) .OverrideHttpMethodIfNeeded("PATCH", methodToUse) .SendAsync(methodToUse); await response.ShouldBeErrorResponse(HttpStatusCode.BadRequest, "Could not parse Upload-Checksum header"); response.ShouldContainTusResumableHeader(); } #pragma warning disable 4014 store.DidNotReceive().FileExistAsync("checksum", Arg.Any <CancellationToken>()); store.DidNotReceive().GetUploadOffsetAsync("checksum", Arg.Any <CancellationToken>()); //store.DidNotReceive().GetUploadLengthAsync("checksum", Arg.Any<CancellationToken>()); store.DidNotReceive().AppendDataAsync("checksum", Arg.Any <Stream>(), Arg.Any <CancellationToken>()); cstore.DidNotReceive().GetSupportedAlgorithmsAsync(Arg.Any <CancellationToken>()); #pragma warning restore 4014 } }
public override async Task Validate(ContextAdapter context) { ITusChecksumStore checksumStore = context.Configuration.Store as ITusChecksumStore; var providedChecksum = GetProvidedChecksum(context); if (checksumStore != null && providedChecksum != null) { if (!providedChecksum.IsValid) { await BadRequest($"Could not parse {HeaderConstants.UploadChecksum} header"); return; } var checksumAlgorithms = (await checksumStore.GetSupportedAlgorithmsAsync(context.CancellationToken)).ToList(); if (!checksumAlgorithms.Contains(providedChecksum.Algorithm)) { await BadRequest( $"Unsupported checksum algorithm. Supported algorithms are: {string.Join(",", checksumAlgorithms)}"); } } }