public async Task DeviceAuthenticationWithTokenRefresh_GetTokenAsync_NewTtl_Ok() { int ttl = 1; var refresher = new TestImplementation(TestDeviceId, ttl, 90); await refresher.GetTokenAsync(TestIoTHubName).ConfigureAwait(false); DateTime expectedExpiryTime = DateTime.UtcNow.AddSeconds(ttl); int timeDelta = (int)((refresher.ExpiresOn - expectedExpiryTime).TotalSeconds); Assert.IsTrue(Math.Abs(timeDelta) < 3, $"Expiration time delta is {timeDelta}"); // Wait for the token to expire; while (!refresher.IsExpiring) { await Task.Delay(100).ConfigureAwait(false); } // Configure the test token refresher to ignore the suggested TTL. ttl = 10; refresher.ActualTimeToLive = ttl; await refresher.GetTokenAsync(TestIoTHubName).ConfigureAwait(false); expectedExpiryTime = DateTime.UtcNow.AddSeconds(ttl); timeDelta = (int)((refresher.ExpiresOn - expectedExpiryTime).TotalSeconds); Assert.IsTrue(Math.Abs(timeDelta) < 3, $"Expiration time delta is {timeDelta}"); }
public async Task DeviceAuthenticationWithTokenRefresh_NonExpiredToken_GetTokenCached_Ok() { var refresher = new TestImplementation(TestDeviceId); string token1 = await refresher.GetTokenAsync(TestIoTHubName).ConfigureAwait(false); string token2 = await refresher.GetTokenAsync(TestIoTHubName).ConfigureAwait(false); Assert.AreEqual(1, refresher.SafeCreateNewTokenCallCount); // Cached. Assert.AreEqual(token1, token2); }
public async Task AuthenticationWithTokenRefresh_NonExpiredToken_GetTokenCached_Ok() { var refresher = new TestImplementation(); string expectedToken = CreateToken(DefaultTimeToLiveSeconds); string token1 = await refresher.GetTokenAsync(TestIoTHubName); string token2 = await refresher.GetTokenAsync(TestIoTHubName); Assert.AreEqual(1, refresher.SafeCreateNewTokenCallCount); // Cached. Assert.AreEqual(expectedToken, token1); Assert.AreEqual(token1, token2); }
public async Task DeviceAuthenticationWithTokenRefresh_InitializedToken_GetProperties_Ok() { int ttl = 5; int buffer = 20; // Token should refresh after 4 seconds. var refresher = new TestImplementation(TestDeviceId, ttl, buffer); await refresher.GetTokenAsync(TestIoTHubName).ConfigureAwait(false); DateTime currentTime = DateTime.UtcNow; DateTime expectedExpiryTime = currentTime.AddSeconds(ttl); DateTime expectedRefreshTime = expectedExpiryTime.AddSeconds(-((double)buffer / 100) * ttl); Assert.AreEqual(TestDeviceId, refresher.DeviceId); int timeDelta = (int)((refresher.ExpiresOn - expectedExpiryTime).TotalSeconds); Assert.IsTrue(Math.Abs(timeDelta) < 3, $"ExpiresOn time delta is {timeDelta}"); timeDelta = (int)((refresher.RefreshesOn - expectedRefreshTime).TotalSeconds); Assert.IsTrue(Math.Abs(timeDelta) < 3, $"RefreshesOn time delta is {timeDelta}"); TimeSpan delayTime = refresher.RefreshesOn - DateTime.UtcNow + TimeSpan.FromMilliseconds(500); // Wait for the expiration time given the time buffer. if (delayTime.TotalSeconds > 0) { await Task.Delay(delayTime).ConfigureAwait(false); } Debug.Assert(refresher.IsExpiring, $"Current time = {DateTime.UtcNow}"); Assert.AreEqual(true, refresher.IsExpiring); }
public async Task DeviceAuthenticationWithTokenRefresh_GetTokenAsync_ConcurrentUpdate_Ok() { var refresher = new TestImplementation(TestDeviceId); var tasks = new Task[5]; for (int i = 0; i < tasks.Length; i++) { tasks[i] = refresher.GetTokenAsync(TestIoTHubName); } await Task.WhenAll(tasks).ConfigureAwait(false); Assert.AreEqual(1, refresher.SafeCreateNewTokenCallCount); }
public async Task DeviceAuthenticationWithTokenRefresh_Populate_DefaultParameters_Ok() { var refresher = new TestImplementation(TestDeviceId); var csBuilder = IotHubConnectionStringBuilder.Create(TestIoTHubName, refresher); refresher.Populate(csBuilder); Assert.AreEqual(TestDeviceId, csBuilder.DeviceId); Assert.AreEqual(null, csBuilder.SharedAccessSignature); Assert.AreEqual(null, csBuilder.SharedAccessKey); Assert.AreEqual(null, csBuilder.SharedAccessKeyName); string token = await refresher.GetTokenAsync(TestIoTHubName).ConfigureAwait(false); refresher.Populate(csBuilder); Assert.AreEqual(TestDeviceId, csBuilder.DeviceId); Assert.AreEqual(token, csBuilder.SharedAccessSignature); Assert.AreEqual(null, csBuilder.SharedAccessKey); Assert.AreEqual(null, csBuilder.SharedAccessKeyName); }
public async Task AuthenticationWithTokenRefresh_Populate_DefaultParameters_Ok() { var refresher = new TestImplementation(); var csBuilder = IotHubConnectionStringBuilder.Create( TestIoTHubName, new ModuleAuthenticationWithRegistrySymmetricKey("deviceId", "moduleid", TestSharedAccessKey)); refresher.Populate(csBuilder); Assert.AreEqual(null, csBuilder.SharedAccessSignature); Assert.AreEqual(null, csBuilder.SharedAccessKey); Assert.AreEqual(null, csBuilder.SharedAccessKeyName); string token = await refresher.GetTokenAsync(TestIoTHubName); refresher.Populate(csBuilder); Assert.AreEqual(token, csBuilder.SharedAccessSignature); Assert.AreEqual(null, csBuilder.SharedAccessKey); Assert.AreEqual(null, csBuilder.SharedAccessKeyName); }