Helper function for test socket creation
This commit is contained in:
parent
020e0b9dc5
commit
ffa8f3f092
|
|
@ -158,9 +158,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
|
||||||
{
|
{
|
||||||
host.Start();
|
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.Send(Encoding.ASCII.GetBytes("GET /%41%CC%8A/A/../B/%41%CC%8A HTTP/1.1\r\n\r\n"));
|
||||||
socket.Shutdown(SocketShutdown.Send);
|
socket.Shutdown(SocketShutdown.Send);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -118,24 +118,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
|
||||||
var started = engine.CreateServer(address);
|
var started = engine.CreateServer(address);
|
||||||
|
|
||||||
Console.WriteLine("Started");
|
Console.WriteLine("Started");
|
||||||
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
var socket = TestConnection.CreateConnectedLoopbackSocket(port);
|
||||||
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.Send(Encoding.ASCII.GetBytes("POST / HTTP/1.0\r\n\r\nHello World"));
|
socket.Send(Encoding.ASCII.GetBytes("POST / HTTP/1.0\r\n\r\nHello World"));
|
||||||
socket.Shutdown(SocketShutdown.Send);
|
socket.Shutdown(SocketShutdown.Send);
|
||||||
var buffer = new byte[8192];
|
var buffer = new byte[8192];
|
||||||
|
|
@ -491,24 +474,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
|
||||||
{
|
{
|
||||||
using (var server = new TestServer(App, testContext))
|
using (var server = new TestServer(App, testContext))
|
||||||
{
|
{
|
||||||
var socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
|
var socket = TestConnection.CreateConnectedLoopbackSocket(server.Port);
|
||||||
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);
|
|
||||||
await Task.Delay(200);
|
await Task.Delay(200);
|
||||||
socket.Dispose();
|
socket.Dispose();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -124,6 +124,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
|
||||||
{
|
{
|
||||||
var pipeName = @"\\.\pipe\ServerPipeDispatchConnections" + Guid.NewGuid().ToString("n");
|
var pipeName = @"\\.\pipe\ServerPipeDispatchConnections" + Guid.NewGuid().ToString("n");
|
||||||
|
|
||||||
|
var port = TestServer.GetNextPort();
|
||||||
var loop = new UvLoopHandle(_logger);
|
var loop = new UvLoopHandle(_logger);
|
||||||
loop.Init(_uv);
|
loop.Init(_uv);
|
||||||
|
|
||||||
|
|
@ -153,7 +154,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
|
||||||
|
|
||||||
var serverListenTcp = new UvTcpHandle(_logger);
|
var serverListenTcp = new UvTcpHandle(_logger);
|
||||||
serverListenTcp.Init(loop);
|
serverListenTcp.Init(loop);
|
||||||
var address = ServerAddress.FromUrl("http://localhost:54321/");
|
var address = ServerAddress.FromUrl($"http://localhost:{port}/");
|
||||||
serverListenTcp.Bind(address);
|
serverListenTcp.Bind(address);
|
||||||
serverListenTcp.Listen(128, (_1, status, error, _2) =>
|
serverListenTcp.Listen(128, (_1, status, error, _2) =>
|
||||||
{
|
{
|
||||||
|
|
@ -234,24 +235,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
|
||||||
{
|
{
|
||||||
serverConnectionPipeAcceptedEvent.WaitOne();
|
serverConnectionPipeAcceptedEvent.WaitOne();
|
||||||
|
|
||||||
var socket = new Socket(SocketType.Stream, ProtocolType.IP);
|
var socket = TestConnection.CreateConnectedLoopbackSocket(port);
|
||||||
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);
|
|
||||||
socket.Send(new byte[] { 6, 7, 8, 9 });
|
socket.Send(new byte[] { 6, 7, 8, 9 });
|
||||||
socket.Shutdown(SocketShutdown.Send);
|
socket.Shutdown(SocketShutdown.Send);
|
||||||
var cb = socket.Receive(new byte[64]);
|
var cb = socket.Receive(new byte[64]);
|
||||||
|
|
|
||||||
|
|
@ -87,38 +87,9 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
|
||||||
tcp2.Dispose();
|
tcp2.Dispose();
|
||||||
stream.Dispose();
|
stream.Dispose();
|
||||||
}, null);
|
}, null);
|
||||||
var t = Task.Run(async () =>
|
var t = Task.Run(() =>
|
||||||
{
|
{
|
||||||
var socket = new Socket(
|
var socket = TestConnection.CreateConnectedLoopbackSocket(port);
|
||||||
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
|
|
||||||
socket.Dispose();
|
socket.Dispose();
|
||||||
});
|
});
|
||||||
loop.Run();
|
loop.Run();
|
||||||
|
|
@ -159,33 +130,8 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
|
||||||
Console.WriteLine("Task.Run");
|
Console.WriteLine("Task.Run");
|
||||||
var t = Task.Run(async () =>
|
var t = Task.Run(async () =>
|
||||||
{
|
{
|
||||||
var socket = new Socket(
|
var socket = TestConnection.CreateConnectedLoopbackSocket(port);
|
||||||
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
|
#if DNX451
|
||||||
await Task.Factory.FromAsync(
|
|
||||||
socket.BeginConnect,
|
|
||||||
socket.EndConnect,
|
|
||||||
new IPEndPoint(IPAddress.Loopback, port),
|
|
||||||
null,
|
|
||||||
TaskCreationOptions.None);
|
|
||||||
await Task.Factory.FromAsync(
|
await Task.Factory.FromAsync(
|
||||||
socket.BeginSend,
|
socket.BeginSend,
|
||||||
socket.EndSend,
|
socket.EndSend,
|
||||||
|
|
@ -194,7 +140,6 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
|
||||||
null,
|
null,
|
||||||
TaskCreationOptions.None);
|
TaskCreationOptions.None);
|
||||||
#else
|
#else
|
||||||
await socket.ConnectAsync(new IPEndPoint(IPAddress.Loopback, port));
|
|
||||||
await socket.SendAsync(new[] { new ArraySegment<byte>(new byte[] { 1, 2, 3, 4, 5 }) },
|
await socket.SendAsync(new[] { new ArraySegment<byte>(new byte[] { 1, 2, 3, 4, 5 }) },
|
||||||
SocketFlags.None);
|
SocketFlags.None);
|
||||||
#endif
|
#endif
|
||||||
|
|
@ -262,33 +207,8 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
|
||||||
Console.WriteLine("Task.Run");
|
Console.WriteLine("Task.Run");
|
||||||
var t = Task.Run(async () =>
|
var t = Task.Run(async () =>
|
||||||
{
|
{
|
||||||
var socket = new Socket(
|
var socket = TestConnection.CreateConnectedLoopbackSocket(port);
|
||||||
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
|
#if DNX451
|
||||||
await Task.Factory.FromAsync(
|
|
||||||
socket.BeginConnect,
|
|
||||||
socket.EndConnect,
|
|
||||||
new IPEndPoint(IPAddress.Loopback, port),
|
|
||||||
null,
|
|
||||||
TaskCreationOptions.None);
|
|
||||||
await Task.Factory.FromAsync(
|
await Task.Factory.FromAsync(
|
||||||
socket.BeginSend,
|
socket.BeginSend,
|
||||||
socket.EndSend,
|
socket.EndSend,
|
||||||
|
|
@ -297,7 +217,6 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
|
||||||
null,
|
null,
|
||||||
TaskCreationOptions.None);
|
TaskCreationOptions.None);
|
||||||
#else
|
#else
|
||||||
await socket.ConnectAsync(new IPEndPoint(IPAddress.Loopback, port));
|
|
||||||
await socket.SendAsync(new[] { new ArraySegment<byte>(new byte[] { 1, 2, 3, 4, 5 }) },
|
await socket.SendAsync(new[] { new ArraySegment<byte>(new byte[] { 1, 2, 3, 4, 5 }) },
|
||||||
SocketFlags.None);
|
SocketFlags.None);
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -29,24 +29,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
|
||||||
|
|
||||||
public void Create(int port)
|
public void Create(int port)
|
||||||
{
|
{
|
||||||
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
_socket = CreateConnectedLoopbackSocket(port);
|
||||||
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));
|
|
||||||
|
|
||||||
_stream = new NetworkStream(_socket, false);
|
_stream = new NetworkStream(_socket, false);
|
||||||
_reader = new StreamReader(_stream, Encoding.ASCII);
|
_reader = new StreamReader(_stream, Encoding.ASCII);
|
||||||
|
|
@ -142,5 +125,27 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
|
||||||
var text = new string(ch, 0, count);
|
var text = new string(ch, 0, count);
|
||||||
Assert.Equal("", text);
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue