forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathServerCertificateTest.cs
116 lines (103 loc) · 5.08 KB
/
ServerCertificateTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.ComponentModel;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
namespace System.Net.Http.WinHttpHandlerFunctional.Tests
{
public class ServerCertificateTest : BaseCertificateTest
{
public ServerCertificateTest(ITestOutputHelper output) : base(output)
{ }
public static bool DowngradeToHTTP1IfClientCertSet => PlatformDetection.WindowsVersion < 2004;
[OuterLoop]
[Fact]
public async Task NoCallback_ValidCertificate_CallbackNotCalled()
{
var handler = new WinHttpHandler();
using (var client = new HttpClient(handler))
using (HttpResponseMessage response = await client.GetAsync(Test.Common.Configuration.Http.SecureRemoteEchoServer))
{
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.False(_validationCallbackHistory.WasCalled);
}
}
[OuterLoop]
[Fact]
[ActiveIssue("https://github.com/dotnet/runtime/issues/110578")]
public async Task UseCallback_NotSecureConnection_CallbackNotCalled()
{
var handler = new WinHttpHandler();
handler.ServerCertificateValidationCallback = CustomServerCertificateValidationCallback;
using (var client = new HttpClient(handler))
using (HttpResponseMessage response = await client.GetAsync(Test.Common.Configuration.Http.RemoteEchoServer))
{
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.False(_validationCallbackHistory.WasCalled);
}
}
[OuterLoop]
[Fact]
public async Task UseCallback_ValidCertificate_ExpectedValuesDuringCallback()
{
var handler = new WinHttpHandler();
handler.ServerCertificateValidationCallback = CustomServerCertificateValidationCallback;
using (var client = new HttpClient(handler))
using (HttpResponseMessage response = await client.GetAsync(System.Net.Test.Common.Configuration.Http.SecureRemoteEchoServer))
{
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.True(_validationCallbackHistory.WasCalled);
ConfirmValidCertificate(Test.Common.Configuration.Http.Host);
}
}
[OuterLoop]
[Fact]
public async Task UseCallback_RedirectandValidCertificate_ExpectedValuesDuringCallback()
{
Uri uri = Test.Common.Configuration.Http.RemoteSecureHttp11Server.RedirectUriForDestinationUri(302, System.Net.Test.Common.Configuration.Http.SecureRemoteEchoServer, 1);
var handler = new WinHttpHandler();
handler.ServerCertificateValidationCallback = CustomServerCertificateValidationCallback;
using (var client = new HttpClient(handler))
using (HttpResponseMessage response = await client.GetAsync(uri))
{
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.True(_validationCallbackHistory.WasCalled);
ConfirmValidCertificate(Test.Common.Configuration.Http.Host);
}
}
[OuterLoop]
[Fact]
public async Task UseCallback_CallbackReturnsFailure_ThrowsInnerSecurityFailureException()
{
const int ERROR_WINHTTP_SECURE_FAILURE = 12175;
var handler = new WinHttpHandler();
handler.ServerCertificateValidationCallback = CustomServerCertificateValidationCallback;
using (var client = new HttpClient(handler))
{
var request = new HttpRequestMessage(HttpMethod.Get, Test.Common.Configuration.Http.SecureRemoteEchoServer);
_validationCallbackHistory.ReturnFailure = true;
HttpRequestException ex = await Assert.ThrowsAsync<HttpRequestException>(() =>
client.GetAsync(System.Net.Test.Common.Configuration.Http.SecureRemoteEchoServer));
var innerEx = (Win32Exception)ex.InnerException;
Assert.Equal(ERROR_WINHTTP_SECURE_FAILURE, innerEx.NativeErrorCode);
}
}
[OuterLoop]
[Fact]
public async Task UseCallback_CallbackThrowsSpecificException_SpecificExceptionPropagatesAsBaseException()
{
var handler = new WinHttpHandler();
handler.ServerCertificateValidationCallback = CustomServerCertificateValidationCallback;
using (var client = new HttpClient(handler))
{
_validationCallbackHistory.ThrowException = true;
HttpRequestException ex = await Assert.ThrowsAsync<HttpRequestException>(() =>
client.GetAsync(System.Net.Test.Common.Configuration.Http.SecureRemoteEchoServer));
Assert.True(ex.GetBaseException() is CustomException);
}
}
}
}