|
| 1 | +// Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0 |
| 2 | + |
| 3 | +using static Valkey.Glide.ConnectionConfiguration; |
| 4 | +using static Valkey.Glide.Errors; |
| 5 | + |
| 6 | +namespace Valkey.Glide.IntegrationTests; |
| 7 | + |
| 8 | +public class UpdateConnectionPasswordTests(TestConfiguration config) |
| 9 | +{ |
| 10 | + public TestConfiguration Config { get; } = config; |
| 11 | + |
| 12 | + private static readonly string Password = "PASSWORD"; |
| 13 | + private static readonly string InvalidPassword = "INVALID"; |
| 14 | + private static readonly GlideString[] KillClientCommandArgs = ["CLIENT", "KILL", "TYPE", "NORMAL"]; |
| 15 | + |
| 16 | + [Fact] |
| 17 | + public async Task UpdateConnectionPassword_Standalone_DelayAuth() |
| 18 | + { |
| 19 | + string serverName = $"test_{Guid.NewGuid():N}"; |
| 20 | + |
| 21 | + try |
| 22 | + { |
| 23 | + // Start server and build clients. |
| 24 | + var addresses = ServerManager.StartStandaloneServer(serverName); |
| 25 | + var config = new StandaloneClientConfigurationBuilder() |
| 26 | + .WithAddress(addresses[0].host, addresses[0].port).Build(); |
| 27 | + |
| 28 | + using var client = await GlideClient.CreateClient(config); |
| 29 | + using var adminClient = await GlideClient.CreateClient(config); |
| 30 | + |
| 31 | + VerifyConnection(client); |
| 32 | + VerifyConnection(adminClient); |
| 33 | + |
| 34 | + // Update client connection password. |
| 35 | + await client.UpdateConnectionPasswordAsync(Password, immediateAuth: false); |
| 36 | + |
| 37 | + VerifyConnection(client); // No reconnect |
| 38 | + |
| 39 | + // Update server password and kill all clients. |
| 40 | + await adminClient.ConfigSetAsync("requirepass", Password); |
| 41 | + await adminClient.CustomCommand(KillClientCommandArgs); |
| 42 | + Task.Delay(1000).Wait(); |
| 43 | + |
| 44 | + VerifyConnection(client); // Reconnect |
| 45 | + |
| 46 | + // Clear client connection password. |
| 47 | + await client.ClearConnectionPasswordAsync(immediateAuth: false); |
| 48 | + |
| 49 | + VerifyConnection(client); // No reconnect |
| 50 | + |
| 51 | + // Clear server password and kill all clients. |
| 52 | + await adminClient.ConfigSetAsync("requirepass", ""); |
| 53 | + await adminClient.CustomCommand(KillClientCommandArgs); |
| 54 | + Task.Delay(1000).Wait(); |
| 55 | + |
| 56 | + VerifyConnection(client); // Reconnect |
| 57 | + } |
| 58 | + finally |
| 59 | + { |
| 60 | + ServerManager.StopServer(serverName); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + [Fact] |
| 65 | + public async Task UpdateConnectionPassword_Standalone_ImmediateAuth() |
| 66 | + { |
| 67 | + string serverName = $"test_{Guid.NewGuid():N}"; |
| 68 | + |
| 69 | + try |
| 70 | + { |
| 71 | + // Start server and build client. |
| 72 | + var addresses = ServerManager.StartStandaloneServer(serverName); |
| 73 | + var config = new StandaloneClientConfigurationBuilder() |
| 74 | + .WithAddress(addresses[0].host, addresses[0].port).Build(); |
| 75 | + |
| 76 | + using var client = await GlideClient.CreateClient(config); |
| 77 | + |
| 78 | + VerifyConnection(client); |
| 79 | + |
| 80 | + // Update server password. |
| 81 | + await client.ConfigSetAsync("requirepass", Password); |
| 82 | + Task.Delay(1000).Wait(); |
| 83 | + |
| 84 | + // Update client connection password. |
| 85 | + await client.UpdateConnectionPasswordAsync(Password, immediateAuth: true); |
| 86 | + |
| 87 | + VerifyConnection(client); |
| 88 | + |
| 89 | + // Clear server password. |
| 90 | + await client.ConfigSetAsync("requirepass", ""); |
| 91 | + Task.Delay(1000).Wait(); |
| 92 | + |
| 93 | + // Clear client connection password. |
| 94 | + await client.ClearConnectionPasswordAsync(immediateAuth: false); |
| 95 | + |
| 96 | + VerifyConnection(client); |
| 97 | + } |
| 98 | + finally |
| 99 | + { |
| 100 | + ServerManager.StopServer(serverName); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + [Fact] |
| 105 | + public async Task UpdateConnectionPassword_Standalone_InvalidPassword() |
| 106 | + { |
| 107 | + using var client = TestConfiguration.DefaultStandaloneClient(); |
| 108 | + await Assert.ThrowsAsync<ArgumentException>(() => client.UpdateConnectionPasswordAsync(null!, immediateAuth: true)); |
| 109 | + await Assert.ThrowsAsync<ArgumentException>(() => client.UpdateConnectionPasswordAsync("", immediateAuth: true)); |
| 110 | + await Assert.ThrowsAsync<RequestException>(() => client.UpdateConnectionPasswordAsync(InvalidPassword, immediateAuth: true)); |
| 111 | + } |
| 112 | + |
| 113 | + [Fact] |
| 114 | + public async Task UpdateConnectionPassword_Cluster_DelayAuth() |
| 115 | + { |
| 116 | + string serverName = $"test_{Guid.NewGuid():N}"; |
| 117 | + |
| 118 | + try |
| 119 | + { |
| 120 | + // Start cluster and build clients. |
| 121 | + var addresses = ServerManager.StartClusterServer(serverName); |
| 122 | + var config = new ClusterClientConfigurationBuilder() |
| 123 | + .WithAddress(addresses[0].host, addresses[0].port).Build(); |
| 124 | + |
| 125 | + using var client = await GlideClusterClient.CreateClient(config); |
| 126 | + using var adminClient = await GlideClusterClient.CreateClient(config); |
| 127 | + |
| 128 | + VerifyConnection(client); |
| 129 | + VerifyConnection(adminClient); |
| 130 | + |
| 131 | + // Update client connection password. |
| 132 | + await client.UpdateConnectionPasswordAsync(Password, immediateAuth: false); |
| 133 | + |
| 134 | + VerifyConnection(client); // No reconnect |
| 135 | + |
| 136 | + // Update server password and kill all clients. |
| 137 | + await adminClient.ConfigSetAsync("requirepass", Password); |
| 138 | + await adminClient.CustomCommand(KillClientCommandArgs); |
| 139 | + Task.Delay(1000).Wait(); |
| 140 | + |
| 141 | + VerifyConnection(client); // Reconnect |
| 142 | + |
| 143 | + // Clear client connection password. |
| 144 | + await client.ClearConnectionPasswordAsync(immediateAuth: false); |
| 145 | + |
| 146 | + VerifyConnection(client); // No reconnect |
| 147 | + |
| 148 | + // Clear server password and kill all clients. |
| 149 | + await adminClient.ConfigSetAsync("requirepass", ""); |
| 150 | + await adminClient.CustomCommand(KillClientCommandArgs); |
| 151 | + Task.Delay(1000).Wait(); |
| 152 | + |
| 153 | + VerifyConnection(client); // Reconnect |
| 154 | + } |
| 155 | + finally |
| 156 | + { |
| 157 | + ServerManager.StopServer(serverName); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + [Fact] |
| 162 | + public async Task UpdateConnectionPassword_Cluster_ImmediateAuth() |
| 163 | + { |
| 164 | + string serverName = $"test_{Guid.NewGuid():N}"; |
| 165 | + |
| 166 | + try |
| 167 | + { |
| 168 | + // Start cluster and build client. |
| 169 | + var addresses = ServerManager.StartClusterServer(serverName); |
| 170 | + var config = new ClusterClientConfigurationBuilder() |
| 171 | + .WithAddress(addresses[0].host, addresses[0].port).Build(); |
| 172 | + |
| 173 | + using var client = await GlideClusterClient.CreateClient(config); |
| 174 | + |
| 175 | + VerifyConnection(client); |
| 176 | + |
| 177 | + // Update server password. |
| 178 | + await client.ConfigSetAsync("requirepass", Password, Route.AllNodes); |
| 179 | + Task.Delay(1000).Wait(); |
| 180 | + |
| 181 | + // Update client connection password. |
| 182 | + await client.UpdateConnectionPasswordAsync(Password, immediateAuth: true); |
| 183 | + |
| 184 | + VerifyConnection(client); |
| 185 | + |
| 186 | + // Clear server password. |
| 187 | + await client.ConfigSetAsync("requirepass", "", Route.AllNodes); |
| 188 | + Task.Delay(1000).Wait(); |
| 189 | + |
| 190 | + // Clear client connection password. |
| 191 | + await client.ClearConnectionPasswordAsync(immediateAuth: false); |
| 192 | + |
| 193 | + VerifyConnection(client); |
| 194 | + } |
| 195 | + finally |
| 196 | + { |
| 197 | + ServerManager.StopServer(serverName); |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + [Fact] |
| 202 | + public async Task UpdateConnectionPassword_Cluster_InvalidPassword() |
| 203 | + { |
| 204 | + using var client = TestConfiguration.DefaultClusterClient(); |
| 205 | + await Assert.ThrowsAsync<ArgumentException>(() => client.UpdateConnectionPasswordAsync(null!, immediateAuth: true)); |
| 206 | + await Assert.ThrowsAsync<ArgumentException>(() => client.UpdateConnectionPasswordAsync("", immediateAuth: true)); |
| 207 | + await Assert.ThrowsAsync<RequestException>(() => client.UpdateConnectionPasswordAsync(InvalidPassword, immediateAuth: true)); |
| 208 | + } |
| 209 | + |
| 210 | + private static async void VerifyConnection(GlideClient client) |
| 211 | + { |
| 212 | + Assert.True(await client.PingAsync() > TimeSpan.Zero); |
| 213 | + } |
| 214 | + |
| 215 | + private static async void VerifyConnection(GlideClusterClient client) |
| 216 | + { |
| 217 | + Assert.True(await client.PingAsync() > TimeSpan.Zero); |
| 218 | + } |
| 219 | +} |
0 commit comments