|
| 1 | +using Microsoft.Data.SqlClient; |
| 2 | +using static Microsoft.VisualStudio.TestTools.UnitTesting.Assert; |
| 3 | + |
| 4 | +namespace SqlServerSimulator; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Credential enforcement at the wire endpoint: an empty |
| 8 | +/// <c>CREATE LOGIN</c> registry accepts any credentials (the |
| 9 | +/// zero-configuration default every other test in this project relies on); |
| 10 | +/// once a login exists, the LOGIN7 username/password must match or the |
| 11 | +/// connection fails with the probe-confirmed Msg 18456 severity 14 state 1 |
| 12 | +/// shape and closes. |
| 13 | +/// </summary> |
| 14 | +[TestClass] |
| 15 | +public sealed class AuthenticationTests |
| 16 | +{ |
| 17 | + public TestContext TestContext { get; set; } = null!; |
| 18 | + |
| 19 | + private static string CredentialConnectionString(SimulatedNetworkListener listener, string user, string password) => |
| 20 | + $"Server=127.0.0.1,{listener.Port};User ID={user};Password='{password.Replace("'", "''", StringComparison.Ordinal)}';TrustServerCertificate=True;Pooling=False;Connect Timeout=15"; |
| 21 | + |
| 22 | + private static async Task<SqlException> AssertLoginFails(SimulatedNetworkListener listener, string user, string password, CancellationToken cancellationToken) |
| 23 | + { |
| 24 | + var ex = await Assert.ThrowsAsync<SqlException>(async () => |
| 25 | + { |
| 26 | + await using var connection = new SqlConnection(CredentialConnectionString(listener, user, password)); |
| 27 | + await connection.OpenAsync(cancellationToken); |
| 28 | + }); |
| 29 | + |
| 30 | + AreEqual(18456, ex.Number); |
| 31 | + AreEqual(14, ex.Class); |
| 32 | + AreEqual(1, ex.State); |
| 33 | + AreEqual($"Login failed for user '{user}'.", ex.Message); |
| 34 | + return ex; |
| 35 | + } |
| 36 | + |
| 37 | + private static async Task AssertLoginSucceeds(SimulatedNetworkListener listener, string user, string password, CancellationToken cancellationToken) |
| 38 | + { |
| 39 | + await using var connection = new SqlConnection(CredentialConnectionString(listener, user, password)); |
| 40 | + await connection.OpenAsync(cancellationToken); |
| 41 | + await using var command = new SqlCommand("select 1", connection); |
| 42 | + AreEqual(1, await command.ExecuteScalarAsync(cancellationToken)); |
| 43 | + } |
| 44 | + |
| 45 | + [TestMethod] |
| 46 | + public async Task EmptyRegistry_AcceptsAnyCredentials() |
| 47 | + { |
| 48 | + var simulation = new Simulation(); |
| 49 | + await using var listener = await simulation.ListenAsync(0, TestContext.CancellationToken); |
| 50 | + await AssertLoginSucceeds(listener, "whoever", "whatever", TestContext.CancellationToken); |
| 51 | + } |
| 52 | + |
| 53 | + [TestMethod] |
| 54 | + public async Task CorrectPassword_Connects() |
| 55 | + { |
| 56 | + var simulation = new Simulation(); |
| 57 | + Wire.ExecInProc(simulation, "CREATE LOGIN app WITH PASSWORD = 'S3cret!Pass'"); |
| 58 | + await using var listener = await simulation.ListenAsync(0, TestContext.CancellationToken); |
| 59 | + await AssertLoginSucceeds(listener, "app", "S3cret!Pass", TestContext.CancellationToken); |
| 60 | + } |
| 61 | + |
| 62 | + [TestMethod] |
| 63 | + public async Task WrongPassword_Fails18456() |
| 64 | + { |
| 65 | + var simulation = new Simulation(); |
| 66 | + Wire.ExecInProc(simulation, "CREATE LOGIN app WITH PASSWORD = 'S3cret!Pass'"); |
| 67 | + await using var listener = await simulation.ListenAsync(0, TestContext.CancellationToken); |
| 68 | + _ = await AssertLoginFails(listener, "app", "wrong", TestContext.CancellationToken); |
| 69 | + } |
| 70 | + |
| 71 | + [TestMethod] |
| 72 | + public async Task UnknownUser_Fails18456() |
| 73 | + { |
| 74 | + var simulation = new Simulation(); |
| 75 | + Wire.ExecInProc(simulation, "CREATE LOGIN app WITH PASSWORD = 'S3cret!Pass'"); |
| 76 | + await using var listener = await simulation.ListenAsync(0, TestContext.CancellationToken); |
| 77 | + _ = await AssertLoginFails(listener, "nosuchuser", "S3cret!Pass", TestContext.CancellationToken); |
| 78 | + } |
| 79 | + |
| 80 | + [TestMethod] |
| 81 | + public async Task EmptyPassword_Fails18456() |
| 82 | + { |
| 83 | + var simulation = new Simulation(); |
| 84 | + Wire.ExecInProc(simulation, "CREATE LOGIN app WITH PASSWORD = 'S3cret!Pass'"); |
| 85 | + await using var listener = await simulation.ListenAsync(0, TestContext.CancellationToken); |
| 86 | + _ = await AssertLoginFails(listener, "app", "", TestContext.CancellationToken); |
| 87 | + } |
| 88 | + |
| 89 | + [TestMethod] |
| 90 | + public async Task NonAsciiPassword_RoundTripsObfuscation() |
| 91 | + { |
| 92 | + // Non-ASCII UTF-16 units (including a surrogate pair) exercise the |
| 93 | + // LOGIN7 nibble-swap/XOR de-obfuscation and the char-counted length. |
| 94 | + var simulation = new Simulation(); |
| 95 | + Wire.ExecInProc(simulation, "CREATE LOGIN app WITH PASSWORD = N'p䣀🙂ß'"); |
| 96 | + await using var listener = await simulation.ListenAsync(0, TestContext.CancellationToken); |
| 97 | + await AssertLoginSucceeds(listener, "app", "p䣀🙂ß", TestContext.CancellationToken); |
| 98 | + _ = await AssertLoginFails(listener, "app", "p䣀ß", TestContext.CancellationToken); |
| 99 | + } |
| 100 | + |
| 101 | + [TestMethod] |
| 102 | + public async Task UserNameLookup_IsCaseInsensitive() |
| 103 | + { |
| 104 | + var simulation = new Simulation(); |
| 105 | + Wire.ExecInProc(simulation, "CREATE LOGIN app WITH PASSWORD = 'S3cret!Pass'"); |
| 106 | + await using var listener = await simulation.ListenAsync(0, TestContext.CancellationToken); |
| 107 | + await AssertLoginSucceeds(listener, "APP", "S3cret!Pass", TestContext.CancellationToken); |
| 108 | + } |
| 109 | + |
| 110 | + [TestMethod] |
| 111 | + public async Task AlterLogin_ChangesEnforcedPassword() |
| 112 | + { |
| 113 | + var simulation = new Simulation(); |
| 114 | + Wire.ExecInProc(simulation, "CREATE LOGIN app WITH PASSWORD = 'OldPass1!'"); |
| 115 | + await using var listener = await simulation.ListenAsync(0, TestContext.CancellationToken); |
| 116 | + await AssertLoginSucceeds(listener, "app", "OldPass1!", TestContext.CancellationToken); |
| 117 | + |
| 118 | + Wire.ExecInProc(simulation, "ALTER LOGIN app WITH PASSWORD = 'NewPass2!'"); |
| 119 | + _ = await AssertLoginFails(listener, "app", "OldPass1!", TestContext.CancellationToken); |
| 120 | + await AssertLoginSucceeds(listener, "app", "NewPass2!", TestContext.CancellationToken); |
| 121 | + } |
| 122 | + |
| 123 | + [TestMethod] |
| 124 | + public async Task DropLastLogin_RevertsToAcceptAnything() |
| 125 | + { |
| 126 | + var simulation = new Simulation(); |
| 127 | + Wire.ExecInProc(simulation, "CREATE LOGIN app WITH PASSWORD = 'S3cret!Pass'"); |
| 128 | + await using var listener = await simulation.ListenAsync(0, TestContext.CancellationToken); |
| 129 | + _ = await AssertLoginFails(listener, "other", "whatever", TestContext.CancellationToken); |
| 130 | + |
| 131 | + Wire.ExecInProc(simulation, "DROP LOGIN app"); |
| 132 | + await AssertLoginSucceeds(listener, "other", "whatever", TestContext.CancellationToken); |
| 133 | + } |
| 134 | + |
| 135 | + [TestMethod] |
| 136 | + public async Task SecondLogin_BothEnforced() |
| 137 | + { |
| 138 | + var simulation = new Simulation(); |
| 139 | + Wire.ExecInProc(simulation, "CREATE LOGIN app1 WITH PASSWORD = 'Pass!One1'"); |
| 140 | + Wire.ExecInProc(simulation, "CREATE LOGIN app2 WITH PASSWORD = 'Pass!Two2'"); |
| 141 | + await using var listener = await simulation.ListenAsync(0, TestContext.CancellationToken); |
| 142 | + await AssertLoginSucceeds(listener, "app1", "Pass!One1", TestContext.CancellationToken); |
| 143 | + await AssertLoginSucceeds(listener, "app2", "Pass!Two2", TestContext.CancellationToken); |
| 144 | + _ = await AssertLoginFails(listener, "app1", "Pass!Two2", TestContext.CancellationToken); |
| 145 | + } |
| 146 | +} |
0 commit comments