forked from dotnet/SqlClient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestRoutingTdsServer.cs
64 lines (52 loc) · 3.07 KB
/
TestRoutingTdsServer.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
// 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.Net;
using System.Runtime.CompilerServices;
using Microsoft.SqlServer.TDS.EndPoint;
using Microsoft.SqlServer.TDS.Servers;
namespace Microsoft.Data.SqlClient.Tests
{
internal class TestRoutingTdsServer : RoutingTDSServer, IDisposable
{
private const int DefaultConnectionTimeout = 5;
private TDSServerEndPoint _endpoint = null;
private SqlConnectionStringBuilder _connectionStringBuilder;
public TestRoutingTdsServer(RoutingTDSServerArguments args) : base(args) { }
public static TestRoutingTdsServer StartTestServer(IPEndPoint destinationEndpoint, bool enableFedAuth = false, bool enableLog = false, int connectionTimeout = DefaultConnectionTimeout, bool excludeEncryption = false, [CallerMemberName] string methodName = "")
{
RoutingTDSServerArguments args = new RoutingTDSServerArguments()
{
Log = enableLog ? Console.Out : null,
RoutingTCPHost = destinationEndpoint.Address.ToString() == IPAddress.Any.ToString() ? IPAddress.Loopback.ToString() : destinationEndpoint.Address.ToString(),
RoutingTCPPort = (ushort)destinationEndpoint.Port,
};
if (enableFedAuth)
{
args.FedAuthRequiredPreLoginOption = SqlServer.TDS.PreLogin.TdsPreLoginFedAuthRequiredOption.FedAuthRequired;
}
if (excludeEncryption)
{
args.Encryption = SqlServer.TDS.PreLogin.TDSPreLoginTokenEncryptionType.None;
}
TestRoutingTdsServer server = new TestRoutingTdsServer(args);
server._endpoint = new TDSServerEndPoint(server) { ServerEndPoint = new IPEndPoint(IPAddress.Any, 0) };
server._endpoint.EndpointName = methodName;
// The server EventLog should be enabled as it logs the exceptions.
server._endpoint.EventLog = enableLog ? Console.Out : null;
server._endpoint.Start();
int port = server._endpoint.ServerEndPoint.Port;
server._connectionStringBuilder = excludeEncryption
// Allow encryption to be set when encryption is to be excluded from pre-login response.
? new SqlConnectionStringBuilder() { DataSource = "localhost," + port, ConnectTimeout = connectionTimeout, Encrypt = SqlConnectionEncryptOption.Mandatory }
: new SqlConnectionStringBuilder() { DataSource = "localhost," + port, ConnectTimeout = connectionTimeout, Encrypt = SqlConnectionEncryptOption.Optional };
server.ConnectionString = server._connectionStringBuilder.ConnectionString;
server.Endpoint = server._endpoint.ServerEndPoint;
return server;
}
public void Dispose() => _endpoint?.Stop();
public string ConnectionString { get; private set; }
public IPEndPoint Endpoint { get; private set; }
}
}