From ffa8f3f09249af3aaa2045331aa0492281a0a4db Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Sun, 14 Feb 2016 08:11:17 +0000 Subject: [PATCH] Helper function for test socket creation --- .../RequestTests.cs | 3 +- .../TestConnection.cs | 35 ++++++++ .../EngineTests.cs | 38 +------- .../MultipleLoopTests.cs | 22 +---- .../NetworkingTests.cs | 89 +------------------ .../TestConnection.cs | 41 +++++---- 6 files changed, 68 insertions(+), 160 deletions(-) create mode 100644 test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/TestConnection.cs diff --git a/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/RequestTests.cs b/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/RequestTests.cs index 3f9b0870dd..3f9cf20a75 100644 --- a/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/RequestTests.cs +++ b/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/RequestTests.cs @@ -158,9 +158,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests { host.Start(); - using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) + using (var socket = TestConnection.CreateConnectedLoopbackSocket(port)) { - socket.Connect(new IPEndPoint(IPAddress.Loopback, port)); socket.Send(Encoding.ASCII.GetBytes("GET /%41%CC%8A/A/../B/%41%CC%8A HTTP/1.1\r\n\r\n")); socket.Shutdown(SocketShutdown.Send); diff --git a/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/TestConnection.cs b/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/TestConnection.cs new file mode 100644 index 0000000000..db602658f0 --- /dev/null +++ b/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/TestConnection.cs @@ -0,0 +1,35 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Net; +using System.Net.Sockets; +using Microsoft.AspNetCore.Server.Kestrel.Networking; + +namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests +{ + public class TestConnection + { + public static Socket CreateConnectedLoopbackSocket(int port) + { + var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + if (PlatformApis.IsWindows) + { + const int SIO_LOOPBACK_FAST_PATH = -1744830448; + var optionInValue = BitConverter.GetBytes(1); + try + { + socket.IOControl(SIO_LOOPBACK_FAST_PATH, optionInValue, null); + } + catch + { + // If the operating system version on this machine did + // not support SIO_LOOPBACK_FAST_PATH (i.e. version + // prior to Windows 8 / Windows Server 2012), handle the exception + } + } + socket.Connect(new IPEndPoint(IPAddress.Loopback, port)); + return socket; + } + } +} diff --git a/test/Microsoft.AspNetCore.Server.KestrelTests/EngineTests.cs b/test/Microsoft.AspNetCore.Server.KestrelTests/EngineTests.cs index d75ec82902..ea1cfe638c 100644 --- a/test/Microsoft.AspNetCore.Server.KestrelTests/EngineTests.cs +++ b/test/Microsoft.AspNetCore.Server.KestrelTests/EngineTests.cs @@ -118,24 +118,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests var started = engine.CreateServer(address); Console.WriteLine("Started"); - var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - if (PlatformApis.IsWindows) - { - const int SIO_LOOPBACK_FAST_PATH = (-1744830448); - var optionInValue = BitConverter.GetBytes(1); - try - { - socket.IOControl(SIO_LOOPBACK_FAST_PATH, optionInValue, null); - } - catch - { - // If the operating system version on this machine did - // not support SIO_LOOPBACK_FAST_PATH (i.e. version - // prior to Windows 8 / Windows Server 2012), handle the exception - } - } - socket.NoDelay = true; - socket.Connect(new IPEndPoint(IPAddress.Loopback, port)); + var socket = TestConnection.CreateConnectedLoopbackSocket(port); socket.Send(Encoding.ASCII.GetBytes("POST / HTTP/1.0\r\n\r\nHello World")); socket.Shutdown(SocketShutdown.Send); var buffer = new byte[8192]; @@ -491,24 +474,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests { using (var server = new TestServer(App, testContext)) { - var socket = new Socket(SocketType.Stream, ProtocolType.Tcp); - if (PlatformApis.IsWindows) - { - const int SIO_LOOPBACK_FAST_PATH = (-1744830448); - var optionInValue = BitConverter.GetBytes(1); - try - { - socket.IOControl(SIO_LOOPBACK_FAST_PATH, optionInValue, null); - } - catch - { - // If the operating system version on this machine did - // not support SIO_LOOPBACK_FAST_PATH (i.e. version - // prior to Windows 8 / Windows Server 2012), handle the exception - } - } - socket.NoDelay = true; - socket.Connect(IPAddress.Loopback, server.Port); + var socket = TestConnection.CreateConnectedLoopbackSocket(server.Port); await Task.Delay(200); socket.Dispose(); diff --git a/test/Microsoft.AspNetCore.Server.KestrelTests/MultipleLoopTests.cs b/test/Microsoft.AspNetCore.Server.KestrelTests/MultipleLoopTests.cs index 16ebbd3421..fedc72e6b0 100644 --- a/test/Microsoft.AspNetCore.Server.KestrelTests/MultipleLoopTests.cs +++ b/test/Microsoft.AspNetCore.Server.KestrelTests/MultipleLoopTests.cs @@ -124,6 +124,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests { var pipeName = @"\\.\pipe\ServerPipeDispatchConnections" + Guid.NewGuid().ToString("n"); + var port = TestServer.GetNextPort(); var loop = new UvLoopHandle(_logger); loop.Init(_uv); @@ -153,7 +154,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests var serverListenTcp = new UvTcpHandle(_logger); serverListenTcp.Init(loop); - var address = ServerAddress.FromUrl("http://localhost:54321/"); + var address = ServerAddress.FromUrl($"http://localhost:{port}/"); serverListenTcp.Bind(address); serverListenTcp.Listen(128, (_1, status, error, _2) => { @@ -234,24 +235,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests { serverConnectionPipeAcceptedEvent.WaitOne(); - var socket = new Socket(SocketType.Stream, ProtocolType.IP); - if (PlatformApis.IsWindows) - { - const int SIO_LOOPBACK_FAST_PATH = (-1744830448); - var optionInValue = BitConverter.GetBytes(1); - try - { - socket.IOControl(SIO_LOOPBACK_FAST_PATH, optionInValue, null); - } - catch - { - // If the operating system version on this machine did - // not support SIO_LOOPBACK_FAST_PATH (i.e. version - // prior to Windows 8 / Windows Server 2012), handle the exception - } - } - socket.NoDelay = true; - socket.Connect(IPAddress.Loopback, 54321); + var socket = TestConnection.CreateConnectedLoopbackSocket(port); socket.Send(new byte[] { 6, 7, 8, 9 }); socket.Shutdown(SocketShutdown.Send); var cb = socket.Receive(new byte[64]); diff --git a/test/Microsoft.AspNetCore.Server.KestrelTests/NetworkingTests.cs b/test/Microsoft.AspNetCore.Server.KestrelTests/NetworkingTests.cs index c8824c70c7..35faf85266 100644 --- a/test/Microsoft.AspNetCore.Server.KestrelTests/NetworkingTests.cs +++ b/test/Microsoft.AspNetCore.Server.KestrelTests/NetworkingTests.cs @@ -87,38 +87,9 @@ namespace Microsoft.AspNetCore.Server.KestrelTests tcp2.Dispose(); stream.Dispose(); }, null); - var t = Task.Run(async () => + var t = Task.Run(() => { - var socket = new Socket( - AddressFamily.InterNetwork, - SocketType.Stream, - ProtocolType.Tcp); - if (PlatformApis.IsWindows) - { - const int SIO_LOOPBACK_FAST_PATH = (-1744830448); - var optionInValue = BitConverter.GetBytes(1); - try - { - socket.IOControl(SIO_LOOPBACK_FAST_PATH, optionInValue, null); - } - catch - { - // If the operating system version on this machine did - // not support SIO_LOOPBACK_FAST_PATH (i.e. version - // prior to Windows 8 / Windows Server 2012), handle the exception - } - } - socket.NoDelay = true; -#if DNX451 - await Task.Factory.FromAsync( - socket.BeginConnect, - socket.EndConnect, - new IPEndPoint(IPAddress.Loopback, port), - null, - TaskCreationOptions.None); -#else - await socket.ConnectAsync(new IPEndPoint(IPAddress.Loopback, port)); -#endif + var socket = TestConnection.CreateConnectedLoopbackSocket(port); socket.Dispose(); }); loop.Run(); @@ -159,33 +130,8 @@ namespace Microsoft.AspNetCore.Server.KestrelTests Console.WriteLine("Task.Run"); var t = Task.Run(async () => { - var socket = new Socket( - AddressFamily.InterNetwork, - SocketType.Stream, - ProtocolType.Tcp); - if (PlatformApis.IsWindows) - { - const int SIO_LOOPBACK_FAST_PATH = (-1744830448); - var optionInValue = BitConverter.GetBytes(1); - try - { - socket.IOControl(SIO_LOOPBACK_FAST_PATH, optionInValue, null); - } - catch - { - // If the operating system version on this machine did - // not support SIO_LOOPBACK_FAST_PATH (i.e. version - // prior to Windows 8 / Windows Server 2012), handle the exception - } - } - socket.NoDelay = true; + var socket = TestConnection.CreateConnectedLoopbackSocket(port); #if DNX451 - await Task.Factory.FromAsync( - socket.BeginConnect, - socket.EndConnect, - new IPEndPoint(IPAddress.Loopback, port), - null, - TaskCreationOptions.None); await Task.Factory.FromAsync( socket.BeginSend, socket.EndSend, @@ -194,7 +140,6 @@ namespace Microsoft.AspNetCore.Server.KestrelTests null, TaskCreationOptions.None); #else - await socket.ConnectAsync(new IPEndPoint(IPAddress.Loopback, port)); await socket.SendAsync(new[] { new ArraySegment(new byte[] { 1, 2, 3, 4, 5 }) }, SocketFlags.None); #endif @@ -262,33 +207,8 @@ namespace Microsoft.AspNetCore.Server.KestrelTests Console.WriteLine("Task.Run"); var t = Task.Run(async () => { - var socket = new Socket( - AddressFamily.InterNetwork, - SocketType.Stream, - ProtocolType.Tcp); - if (PlatformApis.IsWindows) - { - const int SIO_LOOPBACK_FAST_PATH = (-1744830448); - var optionInValue = BitConverter.GetBytes(1); - try - { - socket.IOControl(SIO_LOOPBACK_FAST_PATH, optionInValue, null); - } - catch - { - // If the operating system version on this machine did - // not support SIO_LOOPBACK_FAST_PATH (i.e. version - // prior to Windows 8 / Windows Server 2012), handle the exception - } - } - socket.NoDelay = true; + var socket = TestConnection.CreateConnectedLoopbackSocket(port); #if DNX451 - await Task.Factory.FromAsync( - socket.BeginConnect, - socket.EndConnect, - new IPEndPoint(IPAddress.Loopback, port), - null, - TaskCreationOptions.None); await Task.Factory.FromAsync( socket.BeginSend, socket.EndSend, @@ -297,7 +217,6 @@ namespace Microsoft.AspNetCore.Server.KestrelTests null, TaskCreationOptions.None); #else - await socket.ConnectAsync(new IPEndPoint(IPAddress.Loopback, port)); await socket.SendAsync(new[] { new ArraySegment(new byte[] { 1, 2, 3, 4, 5 }) }, SocketFlags.None); #endif diff --git a/test/Microsoft.AspNetCore.Server.KestrelTests/TestConnection.cs b/test/Microsoft.AspNetCore.Server.KestrelTests/TestConnection.cs index 8e2ac64f69..f7f3e36b4e 100644 --- a/test/Microsoft.AspNetCore.Server.KestrelTests/TestConnection.cs +++ b/test/Microsoft.AspNetCore.Server.KestrelTests/TestConnection.cs @@ -29,24 +29,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests public void Create(int port) { - _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - if (PlatformApis.IsWindows) - { - const int SIO_LOOPBACK_FAST_PATH = (-1744830448); - var optionInValue = BitConverter.GetBytes(1); - try - { - _socket.IOControl(SIO_LOOPBACK_FAST_PATH, optionInValue, null); - } - catch - { - // If the operating system version on this machine did - // not support SIO_LOOPBACK_FAST_PATH (i.e. version - // prior to Windows 8 / Windows Server 2012), handle the exception - } - } - _socket.NoDelay = true; - _socket.Connect(new IPEndPoint(IPAddress.Loopback, port)); + _socket = CreateConnectedLoopbackSocket(port); _stream = new NetworkStream(_socket, false); _reader = new StreamReader(_stream, Encoding.ASCII); @@ -142,5 +125,27 @@ namespace Microsoft.AspNetCore.Server.KestrelTests var text = new string(ch, 0, count); Assert.Equal("", text); } + + public static Socket CreateConnectedLoopbackSocket(int port) + { + var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + if (PlatformApis.IsWindows) + { + const int SIO_LOOPBACK_FAST_PATH = -1744830448; + var optionInValue = BitConverter.GetBytes(1); + try + { + socket.IOControl(SIO_LOOPBACK_FAST_PATH, optionInValue, null); + } + catch + { + // If the operating system version on this machine did + // not support SIO_LOOPBACK_FAST_PATH (i.e. version + // prior to Windows 8 / Windows Server 2012), handle the exception + } + } + socket.Connect(new IPEndPoint(IPAddress.Loopback, port)); + return socket; + } } }