|
15 | 15 | using System.Net.Http; |
16 | 16 | using System.Net; |
17 | 17 | using System.IO.Compression; |
| 18 | +using System.Security.Cryptography; |
| 19 | +using System.Text.RegularExpressions; |
18 | 20 |
|
19 | 21 | namespace Tests |
20 | 22 | { |
@@ -116,6 +118,25 @@ public async Task CrashPostClient_PostMinidump_ShouldReturn200() |
116 | 118 | Assert.AreEqual(HttpStatusCode.OK, postResult.StatusCode); |
117 | 119 | } |
118 | 120 |
|
| 121 | + [Test] |
| 122 | + public void CrashPostClient_PostMinidump_Returns400IfFileIsTooBig() |
| 123 | + { |
| 124 | + var bugsplat = new BugSplat(database, application, version); |
| 125 | + var sut = new CrashPostClient(HttpClientFactory.Default, S3ClientFactory.Default); |
| 126 | + var largeFile = CreateLargeTempFile(1000 * 1024 * 1024); // 1 GB |
| 127 | + |
| 128 | + Assert.ThrowsAsync<Exception>(async () => |
| 129 | + { |
| 130 | + await sut.PostMinidump( |
| 131 | + database, |
| 132 | + application, |
| 133 | + version, |
| 134 | + largeFile, |
| 135 | + MinidumpPostOptions.Create(bugsplat) |
| 136 | + ); |
| 137 | + }, "Failed to parse symbol upload url: crash post size limit exceeded"); |
| 138 | + } |
| 139 | + |
119 | 140 | [Test] |
120 | 141 | public void CrashPostClient_PostException_ShouldNotThrowIfAttachmentLocked() |
121 | 142 | { |
@@ -192,7 +213,7 @@ public async Task CrashPostClient_PostCrashFile_PostCrashAndMetadata() |
192 | 213 | stackTrace, |
193 | 214 | ExceptionPostOptions.Create(bugsplat) |
194 | 215 | ); |
195 | | - |
| 216 | + |
196 | 217 | var postResponseContent = JObject.Parse(postResult.Content.ReadAsStringAsync().Result); |
197 | 218 | var id = postResponseContent["crashId"].Value<int>(); |
198 | 219 | var crashDetails = await crashDetailsClient.GetCrashDetails(database, id); |
@@ -277,5 +298,36 @@ private Mock<HttpMessageHandler> CreateMockHttpClientForExceptionPost(string cra |
277 | 298 |
|
278 | 299 | return handlerMock; |
279 | 300 | } |
| 301 | + |
| 302 | + private static FileInfo CreateLargeTempFile(long sizeInBytes, string fileNamePrefix = "LargeTestFile") |
| 303 | + { |
| 304 | + if (sizeInBytes < 0) |
| 305 | + { |
| 306 | + throw new ArgumentException("Size cannot be negative", nameof(sizeInBytes)); |
| 307 | + } |
| 308 | + |
| 309 | + string tempFilePath = Path.Combine(Path.GetTempPath(), $"{fileNamePrefix}_{Guid.NewGuid()}.tmp"); |
| 310 | + const int bufferSize = 81920; |
| 311 | + byte[] buffer = new byte[bufferSize]; |
| 312 | + |
| 313 | + using (var fileStream = new FileStream(tempFilePath, FileMode.Create, FileAccess.Write, FileShare.None, bufferSize)) |
| 314 | + { |
| 315 | + fileStream.SetLength(sizeInBytes); |
| 316 | + |
| 317 | + long bytesWritten = 0; |
| 318 | + using (var rng = RandomNumberGenerator.Create()) |
| 319 | + { |
| 320 | + while (bytesWritten < sizeInBytes) |
| 321 | + { |
| 322 | + int bytesToWrite = (int)Math.Min(bufferSize, sizeInBytes - bytesWritten); |
| 323 | + rng.GetBytes(buffer, 0, bytesToWrite); |
| 324 | + fileStream.Write(buffer, 0, bytesToWrite); |
| 325 | + bytesWritten += bytesToWrite; |
| 326 | + } |
| 327 | + } |
| 328 | + } |
| 329 | + |
| 330 | + return new FileInfo(tempFilePath); |
| 331 | + } |
280 | 332 | } |
281 | 333 | } |
0 commit comments