1 // Licensed to the .NET Foundation under one or more agreements. 2 // The .NET Foundation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using System.Collections.Generic; 6 using System.Data.Common; 7 using System.Diagnostics; 8 using System.Reflection; 9 using System.Threading; 10 using Xunit; 11 12 namespace System.Data.SqlClient.Tests 13 { 14 public class SqlConnectionBasicTests 15 { 16 [Fact] 17 [ActiveIssue("dotnet/corefx #23435", TestPlatforms.Any)] ConnectionTest()18 public void ConnectionTest() 19 { 20 using (TestTdsServer server = TestTdsServer.StartTestServer()) 21 { 22 using (SqlConnection connection = new SqlConnection(server.ConnectionString)) 23 { 24 connection.Open(); 25 } 26 } 27 } 28 29 [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoServer), nameof(PlatformDetection.IsNotArmProcess))] 30 [ActiveIssue("dotnet/corefx #23435", TestPlatforms.Any)] 31 [PlatformSpecific(TestPlatforms.Windows)] IntegratedAuthConnectionTest()32 public void IntegratedAuthConnectionTest() 33 { 34 using (TestTdsServer server = TestTdsServer.StartTestServer()) 35 { 36 SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(server.ConnectionString); 37 builder.IntegratedSecurity = true; 38 using (SqlConnection connection = new SqlConnection(builder.ConnectionString)) 39 { 40 connection.Open(); 41 } 42 } 43 } 44 45 [Fact] SqlConnectionDbProviderFactoryTest()46 public void SqlConnectionDbProviderFactoryTest() 47 { 48 SqlConnection con = new SqlConnection(); 49 PropertyInfo dbProviderFactoryProperty = con.GetType().GetProperty("DbProviderFactory", BindingFlags.NonPublic | BindingFlags.Instance); 50 Assert.NotNull(dbProviderFactoryProperty); 51 DbProviderFactory factory = dbProviderFactoryProperty.GetValue(con) as DbProviderFactory; 52 Assert.NotNull(factory); 53 Assert.Same(typeof(SqlClientFactory), factory.GetType()); 54 Assert.Same(SqlClientFactory.Instance, factory); 55 } 56 57 [Fact] SqlConnectionValidParameters()58 public void SqlConnectionValidParameters() 59 { 60 var con = new SqlConnection("Timeout=1234;packet Size=5678 ;;; ;"); 61 Assert.Equal(1234, con.ConnectionTimeout); 62 Assert.Equal(5678, con.PacketSize); 63 } 64 65 [Fact] SqlConnectionEmptyParameters()66 public void SqlConnectionEmptyParameters() 67 { 68 var con = new SqlConnection("Timeout=;packet Size= ;Integrated Security=;"); 69 //default values are defined in internal class DbConnectionStringDefaults 70 Assert.Equal(15, con.ConnectionTimeout); 71 Assert.Equal(8000, con.PacketSize); 72 Assert.False(new SqlConnectionStringBuilder(con.ConnectionString).IntegratedSecurity); 73 } 74 75 [Fact] SqlConnectionInvalidParameters()76 public void SqlConnectionInvalidParameters() 77 { 78 Assert.Throws<ArgumentException>(() => new SqlConnection("Timeout=null;")); 79 Assert.Throws<ArgumentException>(() => new SqlConnection("Timeout= null;")); 80 Assert.Throws<ArgumentException>(() => new SqlConnection("Timeout=1 1;")); 81 Assert.Throws<ArgumentException>(() => new SqlConnection("Timeout=1a;")); 82 Assert.Throws<ArgumentException>(() => new SqlConnection("Integrated Security=truee")); 83 } 84 85 [Fact] ConnectionTimeoutTestWithThread()86 public void ConnectionTimeoutTestWithThread() 87 { 88 int timeoutSec = 5; 89 string connStrNotAvailable = $"Server=tcp:fakeServer,1433;uid=fakeuser;pwd=fakepwd;Connection Timeout={timeoutSec}"; 90 91 List<ConnectionWorker> list = new List<ConnectionWorker>(); 92 for (int i = 0; i < 10; ++i) 93 { 94 list.Add(new ConnectionWorker(connStrNotAvailable)); 95 } 96 97 ConnectionWorker.Start(); 98 ConnectionWorker.Stop(); 99 100 double theMax = 0; 101 foreach (ConnectionWorker w in list) 102 { 103 if (theMax < w.MaxTimeElapsed) 104 { 105 theMax = w.MaxTimeElapsed; 106 } 107 } 108 109 int threshold = (timeoutSec + 1) * 1000; 110 111 Console.WriteLine($"ConnectionTimeoutTestWithThread: Elapsed Time {theMax} and threshold {threshold}"); 112 } 113 114 public class ConnectionWorker 115 { 116 private static ManualResetEventSlim startEvent = new ManualResetEventSlim(false); 117 private static List<ConnectionWorker> workerList = new List<ConnectionWorker>(); 118 private ManualResetEventSlim doneEvent = new ManualResetEventSlim(false); 119 private double maxTimeElapsed; 120 private Thread thread; 121 private string connectionString; 122 ConnectionWorker(string connectionString)123 public ConnectionWorker(string connectionString) 124 { 125 workerList.Add(this); 126 this.connectionString = connectionString; 127 thread = new Thread(new ThreadStart(SqlConnectionOpen)); 128 thread.Start(); 129 } 130 131 public double MaxTimeElapsed 132 { 133 get 134 { 135 return maxTimeElapsed; 136 } 137 } 138 Start()139 public static void Start() 140 { 141 startEvent.Set(); 142 } 143 Stop()144 public static void Stop() 145 { 146 foreach (ConnectionWorker w in workerList) 147 { 148 w.doneEvent.Wait(); 149 } 150 } 151 SqlConnectionOpen()152 public void SqlConnectionOpen() 153 { 154 startEvent.Wait(); 155 156 Stopwatch sw = new Stopwatch(); 157 using (SqlConnection con = new SqlConnection(connectionString)) 158 { 159 sw.Start(); 160 try 161 { 162 con.Open(); 163 } 164 catch { } 165 sw.Stop(); 166 } 167 168 double elapsed = sw.Elapsed.TotalMilliseconds; 169 if (maxTimeElapsed < elapsed) 170 { 171 maxTimeElapsed = elapsed; 172 } 173 174 doneEvent.Set(); 175 } 176 } 177 } 178 } 179