Skip to content

Commit 57a1aa0

Browse files
committed
chore: large file test
1 parent 5723c79 commit 57a1aa0

3 files changed

Lines changed: 78 additions & 11 deletions

File tree

BugSplatDotNetStandard.Test/Api/CrashPostClient.cs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
using System.Net.Http;
1616
using System.Net;
1717
using System.IO.Compression;
18+
using System.Security.Cryptography;
19+
using System.Text.RegularExpressions;
1820

1921
namespace Tests
2022
{
@@ -116,6 +118,25 @@ public async Task CrashPostClient_PostMinidump_ShouldReturn200()
116118
Assert.AreEqual(HttpStatusCode.OK, postResult.StatusCode);
117119
}
118120

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+
119140
[Test]
120141
public void CrashPostClient_PostException_ShouldNotThrowIfAttachmentLocked()
121142
{
@@ -192,7 +213,7 @@ public async Task CrashPostClient_PostCrashFile_PostCrashAndMetadata()
192213
stackTrace,
193214
ExceptionPostOptions.Create(bugsplat)
194215
);
195-
216+
196217
var postResponseContent = JObject.Parse(postResult.Content.ReadAsStringAsync().Result);
197218
var id = postResponseContent["crashId"].Value<int>();
198219
var crashDetails = await crashDetailsClient.GetCrashDetails(database, id);
@@ -277,5 +298,36 @@ private Mock<HttpMessageHandler> CreateMockHttpClientForExceptionPost(string cra
277298

278299
return handlerMock;
279300
}
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+
}
280332
}
281333
}

BugSplatDotNetStandard/Api/CrashPostClient.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ public async Task<HttpResponseMessage> PostCrashFile(
142142
var zipFile = ZipUtils.CreateZipFile(zipFileName, files);
143143
try
144144
{
145-
146145
using (
147146
var crashUploadResponse = await GetCrashUploadUrl(
148147
database,
@@ -287,19 +286,23 @@ private string GetETagFromResponseHeaders(HttpHeaders headers)
287286

288287
private async Task<Uri> GetPresignedUrlFromResponse(HttpResponseMessage response)
289288
{
290-
try
291-
{
292-
var json = await response.Content.ReadAsStringAsync();
289+
var json = await response.Content.ReadAsStringAsync();
293290

294-
var jsonObj = new JsonObject(json);
295-
var url = jsonObj.GetValue("url");
291+
var jsonObj = new JsonObject(json);
292+
var url = jsonObj.TryGetValue("url");
293+
var message = jsonObj.TryGetValue("message");
296294

297-
return new Uri(url);
295+
if (string.IsNullOrEmpty(url) && !string.IsNullOrEmpty(message))
296+
{
297+
throw new Exception($"Failed to parse symbol upload url: {message}");
298298
}
299-
catch (Exception ex)
299+
300+
if (string.IsNullOrEmpty(url))
300301
{
301-
throw new Exception("Failed to parse crash upload url", ex);
302+
throw new Exception("Failed to parse symbol upload url");
302303
}
304+
305+
return new Uri(url);
303306
}
304307
}
305308
}

BugSplatDotNetStandard/Http/JsonObject.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public JsonObject(string json)
2020
{
2121
this.json = json;
2222
}
23-
23+
2424
public string GetValue(params string[] path)
2525
{
2626
var jsonBytes = Encoding.UTF8.GetBytes(json);
@@ -30,6 +30,18 @@ public string GetValue(params string[] path)
3030
var key = string.Join("/", path);
3131
return root.XPathSelectElement($"//{key}").Value;
3232
}
33+
34+
public string TryGetValue(params string[] path)
35+
{
36+
try
37+
{
38+
return GetValue(path);
39+
}
40+
catch
41+
{
42+
return null;
43+
}
44+
}
3345
}
3446

3547
public static class JsonSerializer

0 commit comments

Comments
 (0)