Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ jobs:
- name: Build
run: dotnet build CrashReporter.NET.sln -c Release --no-restore -p:Version=${{ steps.version.outputs.version }} -p:ContinuousIntegrationBuild=true

- name: Test
run: dotnet test CrashReporter.NET.sln -c Release --no-build --logger "trx" --results-directory TestResults

- uses: actions/upload-artifact@v7
if: failure()
with:
name: test-results
path: TestResults

- name: Pack
run: dotnet pack CrashReporter.NET/CrashReporter.NET.csproj -c Release --no-build -p:Version=${{ steps.version.outputs.version }} -o artifacts

Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,6 @@ paket-files/
.idea/
*.sln.iml
*.snk
# Public strong-name key of this library (intentionally committed, see README).
!CrashReporter.NET/CrashReporter.NET.snk
/CrashReporter.NET/build/lib/net20
32 changes: 32 additions & 0 deletions CrashReporter.NET.Tests/CrashReporter.NET.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net10.0-windows;net48</TargetFrameworks>
<UseWindowsForms>true</UseWindowsForms>
<LangVersion>latest</LangVersion>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<RootNamespace>CrashReporterDotNET.Tests</RootNamespace>
<!-- Signed with the library key so it can access the library's internals (InternalsVisibleTo). -->
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\CrashReporter.NET\CrashReporter.NET.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.10.1" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net48'">
<Reference Include="System.Net.Http" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\CrashReporter.NET\CrashReporter.NET.csproj" />
</ItemGroup>

</Project>
181 changes: 181 additions & 0 deletions CrashReporter.NET.Tests/DrDumpSoapClientTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
using System;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using CrashReporterDotNET.com.drdump;
using CrashReporterDotNET.DrDump;
using Xunit;

namespace CrashReporterDotNET.Tests
{
public sealed class DrDumpSoapClientTests
{
private const string Namespace = "https://www.drdump.com/services";

private static string Envelope(string body) =>
"<s:Envelope xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\"><s:Body>" + body + "</s:Body></s:Envelope>";

private static string AnonymousResult(string type, string extra = "") => Envelope(
$"<SendAnonymousReportResponse xmlns=\"{Namespace}\"><SendAnonymousReportResult xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" i:type=\"{type}\">" +
$"<ClientID>cid</ClientID><Context>AQIDBA==</Context><UrlToProblem>https://drdump.com/p/1</UrlToProblem>{extra}</SendAnonymousReportResult></SendAnonymousReportResponse>");

private static readonly string Fault = Envelope(
"<s:Fault><s:Code><s:Value>s:Receiver</s:Value></s:Code><s:Reason><s:Text xml:lang=\"en-US\">Server is sad</s:Text></s:Reason></s:Fault>");

private static DrDumpSoapClient CreateClient(FakeHttpHandler handler) =>
new DrDumpSoapClient(new HttpClient(handler)) { Url = "https://drdump.test/Service/CrashReporterReportUploader.svc" };

private static Task<Response> SendAnonymous(DrDumpSoapClient client, CancellationToken cancellationToken = default) =>
client.SendAnonymousReportAsync(new ClientLib { V1 = 2, V2 = 1 },
new Application { AppName = "App", MainModule = "App", Email = "a@b.c" },
new ExceptionDescription { ClrVersion = "10.0", OS = "os", CrashDate = DateTime.UtcNow },
cancellationToken);

[Fact]
public async Task SuccessfulResponse_IsParsed()
{
var handler = new FakeHttpHandler(HttpStatusCode.OK, AnonymousResult("NeedReportResponse"));

var response = await SendAnonymous(CreateClient(handler));

Assert.IsType<NeedReportResponse>(response);
Assert.Equal(new byte[] { 1, 2, 3, 4 }, response.Context);
Assert.Equal("https://drdump.com/p/1", response.UrlToProblem);
Assert.StartsWith("application/soap+xml", handler.LastRequestContentType);
Assert.Contains("SendAnonymousReport\"", handler.LastRequestContentType);
}

[Fact]
public async Task ErrorResponse_IsReturnedAsTypedResult()
{
var handler = new FakeHttpHandler(HttpStatusCode.OK, AnonymousResult("ErrorResponse", "<Error>Valid e-mail is required</Error>"));

var response = await SendAnonymous(CreateClient(handler));

Assert.Equal("Valid e-mail is required", Assert.IsType<ErrorResponse>(response).Error);
}

[Theory]
[InlineData(HttpStatusCode.InternalServerError)]
[InlineData(HttpStatusCode.OK)]
public async Task SoapFault_IsReportedWithItsReason(HttpStatusCode statusCode)
{
var handler = new FakeHttpHandler(statusCode, Fault);

var error = await Assert.ThrowsAsync<InvalidOperationException>(() => SendAnonymous(CreateClient(handler)));

Assert.Equal("Server is sad", error.Message);
}

[Fact]
public async Task UnsuccessfulStatus_WithValidEnvelope_IsRejected()
{
var handler = new FakeHttpHandler(HttpStatusCode.InternalServerError, AnonymousResult("StopResponse"));

var error = await Assert.ThrowsAsync<HttpRequestException>(() => SendAnonymous(CreateClient(handler)));

Assert.Contains("HTTP 500", error.Message);
}

[Fact]
public async Task UnsuccessfulStatus_WithNonSoapBody_IsRejected()
{
var handler = new FakeHttpHandler(HttpStatusCode.BadGateway, "<html><body>Bad gateway</body></html>", "text/html");

var error = await Assert.ThrowsAsync<HttpRequestException>(() => SendAnonymous(CreateClient(handler)));

Assert.Contains("HTTP 502", error.Message);
}

[Fact]
public async Task NilResult_IsRejected()
{
var handler = new FakeHttpHandler(HttpStatusCode.OK, Envelope(
$"<SendAnonymousReportResponse xmlns=\"{Namespace}\"><SendAnonymousReportResult xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" i:nil=\"true\" /></SendAnonymousReportResponse>"));

var error = await Assert.ThrowsAsync<InvalidOperationException>(() => SendAnonymous(CreateClient(handler)));

Assert.Contains("no result", error.Message);
}

[Fact]
public async Task MissingResult_IsRejected()
{
var handler = new FakeHttpHandler(HttpStatusCode.OK, Envelope($"<SendAnonymousReportResponse xmlns=\"{Namespace}\" />"));

await Assert.ThrowsAsync<InvalidOperationException>(() => SendAnonymous(CreateClient(handler)));
}

[Fact]
public async Task MalformedSuccessfulResponse_IsRejected()
{
var handler = new FakeHttpHandler(HttpStatusCode.OK, "this is not xml");

var error = await Assert.ThrowsAsync<InvalidOperationException>(() => SendAnonymous(CreateClient(handler)));

Assert.Contains("invalid response", error.Message);
}

[Fact]
public async Task Cancellation_AbortsTheRequest()
{
var handler = new FakeHttpHandler(async (request, token) =>
{
await Task.Delay(Timeout.Infinite, token).ConfigureAwait(false);
return new HttpResponseMessage(HttpStatusCode.OK);
});
using (var cancellation = new CancellationTokenSource(TimeSpan.FromMilliseconds(100)))
{
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => SendAnonymous(CreateClient(handler), cancellation.Token));
}
}

[Fact]
public async Task InvalidXmlCharactersInReport_AreReplacedBeforeSending()
{
Exception exception;
try
{
throw new InvalidOperationException("bad \u0001 char \uD800 and ￿");
}
catch (Exception e)
{
exception = e;
}

var state = new SendRequestState
{
AnonymousData = new AnonymousData { Exception = exception, ToEmail = "a@b.c" },
PrivateData = new PrivateData { UserMessage = "user \u0002 message", DeveloperMessage = "dev", UserEmail = "u@x.y" }
};
var handler = new FakeHttpHandler(HttpStatusCode.OK, Envelope(
$"<SendAdditionalDataResponse xmlns=\"{Namespace}\"><SendAdditionalDataResult xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" i:type=\"StopResponse\"><UrlToProblem>u</UrlToProblem></SendAdditionalDataResult></SendAdditionalDataResponse>"));

var response = await CreateClient(handler).SendAdditionalDataAsync(new byte[] { 1 }, state.GetDetailedExceptionDescription());

Assert.IsType<StopResponse>(response);
Assert.Contains("bad � char � and �", handler.LastRequestBody);
Assert.Contains("user � message", handler.LastRequestBody);
}

[Fact]
public void HttpClients_AreSharedPerProxy()
{
var field = typeof(DrDumpSoapClient).GetField("_httpClient", BindingFlags.Instance | BindingFlags.NonPublic);
var proxy = new WebProxy("http://proxy.test:8080");

var defaultA = field.GetValue(new DrDumpSoapClient());
var defaultB = field.GetValue(new DrDumpSoapClient());
var proxyA = field.GetValue(new DrDumpSoapClient(proxy));
var proxyB = field.GetValue(new DrDumpSoapClient(proxy));
var otherProxy = field.GetValue(new DrDumpSoapClient(new WebProxy("http://other.test:8080")));

Assert.Same(defaultA, defaultB);
Assert.Same(proxyA, proxyB);
Assert.NotSame(defaultA, proxyA);
Assert.NotSame(proxyA, otherProxy);
}
}
}
Loading
Loading