From d975d583ee41bfa549c8591ab3a47741e21899a6 Mon Sep 17 00:00:00 2001 From: Lennart Dohmann Date: Thu, 8 Feb 2024 08:40:58 +0100 Subject: [PATCH] Let dotnet handle contentLength --- dotnet/Vaas/src/Vaas/Vaas.cs | 11 +++++------ dotnet/Vaas/test/Vaas.Test/IntegrationTests.cs | 14 ++++---------- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/dotnet/Vaas/src/Vaas/Vaas.cs b/dotnet/Vaas/src/Vaas/Vaas.cs index dc6f197d..b20054c8 100644 --- a/dotnet/Vaas/src/Vaas/Vaas.cs +++ b/dotnet/Vaas/src/Vaas/Vaas.cs @@ -185,13 +185,12 @@ public async Task ForUrlAsync(Uri uri, CancellationToken cancellati public async Task ForStreamAsync( Stream stream, - long contentLength, CancellationToken cancellationToken, Dictionary? verdictRequestAttributes = null ) { - if (stream == null || contentLength <= 0) - throw new VaasClientException("Stream or content length was null."); + if (stream == null) + throw new VaasClientException("Stream was null."); var verdictResponse = await ForStreamRequestAsync( new VerdictRequestForStream(SessionId ?? throw new InvalidOperationException()) @@ -216,14 +215,14 @@ public async Task ForStreamAsync( } var response = WaitForResponseAsync(verdictResponse.Guid); - await UploadStream(stream, contentLength, verdictResponse.Url, verdictResponse.UploadToken, cancellationToken); + await UploadStream(stream, verdictResponse.Url, verdictResponse.UploadToken, cancellationToken); return new VaasVerdict(await response); } - private async Task UploadStream(Stream stream, long contentLength, string url, string token, CancellationToken cancellationToken) + private async Task UploadStream(Stream stream, string url, string token, CancellationToken cancellationToken) { - using var requestContent = new StreamContent(stream, (int)contentLength); + using var requestContent = new StreamContent(stream); using var requestMessage = new HttpRequestMessage(HttpMethod.Put, url); requestMessage.Content = requestContent; requestMessage.Headers.Authorization = new AuthenticationHeaderValue(token); diff --git a/dotnet/Vaas/test/Vaas.Test/IntegrationTests.cs b/dotnet/Vaas/test/Vaas.Test/IntegrationTests.cs index b8c3fe0f..83c26811 100644 --- a/dotnet/Vaas/test/Vaas.Test/IntegrationTests.cs +++ b/dotnet/Vaas/test/Vaas.Test/IntegrationTests.cs @@ -190,10 +190,9 @@ public async Task ForStream_WithEicarString_ReturnsMalicious() var eicarBytes = System.Text.Encoding.UTF8.GetBytes("X5O!P%@AP[4\\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*"); targetStream.Write(eicarBytes, 0, eicarBytes.Length); targetStream.Position = 0; - var contentLength = targetStream.Length; // Act - var verdict = await vaas.ForStreamAsync(targetStream, contentLength, CancellationToken.None); + var verdict = await vaas.ForStreamAsync(targetStream, CancellationToken.None); // Assert Assert.Equal(Verdict.Malicious, verdict.Verdict); @@ -208,10 +207,9 @@ public async Task ForStream_WithCleanString_ReturnsClean() var cleanBytes = System.Text.Encoding.UTF8.GetBytes("This is a clean file"); targetStream.Write(cleanBytes, 0, cleanBytes.Length); targetStream.Position = 0; - var contentLength = targetStream.Length; // Act - var verdict = await vaas.ForStreamAsync(targetStream, contentLength, CancellationToken.None); + var verdict = await vaas.ForStreamAsync(targetStream, CancellationToken.None); // Assert Assert.Equal(Verdict.Clean, verdict.Verdict); @@ -225,11 +223,9 @@ public async Task ForStream_WithCleanUrl_ReturnsClean() var url = new Uri("https://raw.githubusercontent.com/GDATASoftwareAG/vaas/main/Readme.md"); var response = await _httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Get, url), CancellationToken.None); var targetStream = await response.Content.ReadAsStreamAsync(); - var contentLength = response.Content.Headers.ContentLength; - Assert.NotNull(contentLength); // Act - var verdict = await vaas.ForStreamAsync(targetStream, (long)contentLength, CancellationToken.None); + var verdict = await vaas.ForStreamAsync(targetStream, CancellationToken.None); // Assert Assert.Equal(Verdict.Clean, verdict.Verdict); @@ -243,11 +239,9 @@ public async Task ForStream_WithEicarUrl_ReturnsEicar() var url = new Uri("https://secure.eicar.org/eicar.com.txt"); var response = await _httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Get, url), CancellationToken.None); var targetStream = await response.Content.ReadAsStreamAsync(); - var contentLength = response.Content.Headers.ContentLength; - Assert.NotNull(contentLength); // Act - var verdict = await vaas.ForStreamAsync(targetStream, (long)contentLength, CancellationToken.None); + var verdict = await vaas.ForStreamAsync(targetStream, CancellationToken.None); // Assert Assert.Equal(Verdict.Malicious, verdict.Verdict);