Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
apply review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
krwq committed Mar 20, 2018
1 parent b3b5f59 commit aab344a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 24 deletions.
10 changes: 8 additions & 2 deletions src/Common/tests/System/Net/VirtualNetwork/VirtualNetwork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public class VirtualNetworkConnectionBroken : Exception
public void ReadFrame(bool server, out byte[] buffer)
{
if (_connectionBroken)
{
throw new VirtualNetworkConnectionBroken();
}

SemaphoreSlim semaphore;
ConcurrentQueue<byte[]> packetQueue;
Expand All @@ -50,7 +52,9 @@ public void ReadFrame(bool server, out byte[] buffer)
}

if (_connectionBroken)
{
throw new VirtualNetworkConnectionBroken();
}

bool dequeueSucceeded = false;
int remainingTries = 3;
Expand All @@ -76,7 +80,9 @@ public void ReadFrame(bool server, out byte[] buffer)
public void WriteFrame(bool server, byte[] buffer)
{
if (_connectionBroken)
{
throw new VirtualNetworkConnectionBroken();
}

SemaphoreSlim semaphore;
ConcurrentQueue<byte[]> packetQueue;
Expand All @@ -102,8 +108,8 @@ public void WriteFrame(bool server, byte[] buffer)
public void BreakConnection()
{
_connectionBroken = true;
_serverDataAvailable.Release(1000000);
_clientDataAvailable.Release(1000000);
_serverDataAvailable.Release(1_000_000);
_clientDataAvailable.Release(1_000_000);
}
}
}
3 changes: 1 addition & 2 deletions src/System.Net.Security/ref/System.Net.Security.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public enum EncryptionPolicy
RequireEncryption = 0,
}
public delegate System.Security.Cryptography.X509Certificates.X509Certificate LocalCertificateSelectionCallback(object sender, string targetHost, System.Security.Cryptography.X509Certificates.X509CertificateCollection localCertificates, System.Security.Cryptography.X509Certificates.X509Certificate remoteCertificate, string[] acceptableIssuers);
public delegate X509Certificate ServerCertificateSelectionCallback(object sender, string hostName);
public delegate System.Security.Cryptography.X509Certificates.X509Certificate ServerCertificateSelectionCallback(object sender, string hostName);

public partial class NegotiateStream : AuthenticatedStream
{
Expand Down Expand Up @@ -182,7 +182,6 @@ public partial class SslStream : AuthenticatedStream
public virtual void AuthenticateAsServer(System.Security.Cryptography.X509Certificates.X509Certificate serverCertificate) { }
public virtual void AuthenticateAsServer(System.Security.Cryptography.X509Certificates.X509Certificate serverCertificate, bool clientCertificateRequired, System.Security.Authentication.SslProtocols enabledSslProtocols, bool checkCertificateRevocation) { }
public virtual void AuthenticateAsServer(System.Security.Cryptography.X509Certificates.X509Certificate serverCertificate, bool clientCertificateRequired, bool checkCertificateRevocation) { }
public void AuthenticateAsServer(SslServerAuthenticationOptions sslServerAuthenticationOptions) { }
public virtual System.Threading.Tasks.Task AuthenticateAsClientAsync(string targetHost) { throw null; }
public virtual System.Threading.Tasks.Task AuthenticateAsClientAsync(string targetHost, System.Security.Cryptography.X509Certificates.X509CertificateCollection clientCertificates, System.Security.Authentication.SslProtocols enabledSslProtocols, bool checkCertificateRevocation) { throw null; }
public virtual System.Threading.Tasks.Task AuthenticateAsClientAsync(string targetHost, System.Security.Cryptography.X509Certificates.X509CertificateCollection clientCertificates, bool checkCertificateRevocation) { throw null; }
Expand Down
4 changes: 1 addition & 3 deletions src/System.Net.Security/src/System/Net/Security/SslStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -351,16 +351,14 @@ public virtual void AuthenticateAsServer(X509Certificate serverCertificate, bool
AuthenticateAsServer(options);
}

public void AuthenticateAsServer(SslServerAuthenticationOptions sslServerAuthenticationOptions)
private void AuthenticateAsServer(SslServerAuthenticationOptions sslServerAuthenticationOptions)
{
SecurityProtocol.ThrowOnNotAllowed(sslServerAuthenticationOptions.EnabledSslProtocols);
SetAndVerifyValidationCallback(sslServerAuthenticationOptions.RemoteCertificateValidationCallback);

// Set the delegate on the options.
sslServerAuthenticationOptions._certValidationDelegate = _certValidationDelegate;

SetServerCertificateSelectionCallbackWrapper(sslServerAuthenticationOptions);

_sslState.ValidateCreateContext(sslServerAuthenticationOptions);
_sslState.ProcessAuthentication(null);
}
Expand Down
46 changes: 30 additions & 16 deletions src/System.Net.Security/tests/FunctionalTests/SslStreamSNITest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System;
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand All @@ -7,6 +11,7 @@
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

Expand All @@ -17,11 +22,16 @@ namespace System.Net.Security.Tests
[Trait("feature", "sni")]
public class SslStreamSNITest
{
private static IEnumerable<object[]> HostNameData()
{
yield return new object[] { "a" };
yield return new object[] { "test" };
yield return new object[] { new string('a', 100) };
}

[Theory]
[InlineData("a")]
[InlineData("test")]
[InlineData("aaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc")]
public void ClientSendsSNIServerReceivesIt(string hostName)
[MemberData(nameof(HostNameData))]
public void SslStream_ClientSendsSNIServerReceives_Ok(string hostName)
{
X509Certificate serverCert = Configuration.Certificates.GetSelfSignedServerCertificate();

Expand All @@ -33,17 +43,18 @@ public void ClientSendsSNIServerReceivesIt(string hostName)
SslServerAuthenticationOptions options = DefaultServerOptions();
bool callbackCalled = false;
int timesCallbackCalled = 0;
options.ServerCertificateSelectionCallback = (sender, actualHostName) =>
{
callbackCalled = true;
timesCallbackCalled++;
Assert.Equal(hostName, actualHostName);
return serverCert;
};
server.AuthenticateAsServer(options);
var cts = new CancellationTokenSource();
server.AuthenticateAsServerAsync(options, cts.Token).Wait();
Assert.True(callbackCalled);
Assert.Equal(1, timesCallbackCalled);
clientJob.Wait();
},
(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) =>
Expand All @@ -54,7 +65,7 @@ public void ClientSendsSNIServerReceivesIt(string hostName)
}

[Fact]
public void ServerDoesNotKnowTheHostName()
public void SslStream_UnknownHostName_Fails()
{
WithVirtualConnection((server, client) =>
{
Expand All @@ -63,20 +74,23 @@ public void ServerDoesNotKnowTheHostName()
=> client.AuthenticateAsClient("test"));
});
bool callbackCalled = false;
int timesCallbackCalled = 0;
SslServerAuthenticationOptions options = DefaultServerOptions();
options.ServerCertificateSelectionCallback = (sender, actualHostName) =>
{
callbackCalled = true;
timesCallbackCalled++;
return null;
};
Assert.Throws<NotSupportedException>(() => {
server.AuthenticateAsServer(options);
});
var cts = new CancellationTokenSource();
Assert.ThrowsAsync<NotSupportedException>(async () => {
await server.AuthenticateAsServerAsync(options, cts.Token);
}).Wait();
// to break connection so that client is not waiting
server.Dispose();
Assert.True(callbackCalled);
Assert.Equal(1, timesCallbackCalled);
clientJob.Wait();
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
<ItemGroup>
<Compile Include="NotifyReadVirtualNetworkStream.cs" />
<Compile Include="DummyTcpServer.cs" />
<Compile Include="SslStreamSNITest.cs" />
<Compile Include="TestConfiguration.cs" />
<!-- SslStream Tests -->
<Compile Include="CertificateChainValidation.cs" />
Expand All @@ -37,6 +36,7 @@
<Compile Include="ServerRequireEncryptionTest.cs" />
<Compile Include="SslStreamStreamToStreamTest.cs" />
<Compile Include="SslStreamNetworkStreamTest.cs" />
<Compile Include="SslStreamSNITest.cs" />
<Compile Include="TransportContextTest.cs" />
<!-- NegotiateStream Tests -->
<Compile Include="NegotiateStreamStreamToStreamTest.cs" />
Expand Down

0 comments on commit aab344a

Please sign in to comment.