Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup IDE warnings. From 442 down to 31. #2254

Merged
merged 9 commits into from
Dec 8, 2021
1 change: 0 additions & 1 deletion common/src/Logging.Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,6 @@ public static void DumpBuffer(object thisOrContextObject, byte[] buffer, int off
/// <param name="count">The number of bytes to log.</param>
/// <param name="memberName">The calling member.</param>
[NonEvent]
[SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "Parameters 'thisOrContextObject' and 'memberName' used in NET451; remove when no longer supported.")]
azabbasi marked this conversation as resolved.
Show resolved Hide resolved
public static unsafe void DumpBuffer(object thisOrContextObject, IntPtr bufferPtr, int count, [CallerMemberName] string memberName = null)
{
Debug.Assert(bufferPtr != IntPtr.Zero);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public static string BuildSasSignature(string keyName, string key, string target

public static string BuildExpiresOn(TimeSpan timeToLive)
{
DateTime epochTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
var epochTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
DateTime expiresOn = DateTime.UtcNow.Add(timeToLive);
TimeSpan secondsFromBaseTime = expiresOn.Subtract(epochTime);
long seconds = Convert.ToInt64(secondsFromBaseTime.TotalSeconds, CultureInfo.InvariantCulture);
Expand Down
4 changes: 2 additions & 2 deletions common/src/service/HttpClientHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -882,10 +882,10 @@ internal static HttpMessageHandler CreateDefaultHttpMessageHandler(IWebProxy web
#pragma warning disable CA2000 // Dispose objects before losing scope (object is returned by this method, so the caller is responsible for disposing it)
#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_0 && !NETCOREAPP1_1
// SocketsHttpHandler is only available in netcoreapp2.1 and onwards
SocketsHttpHandler httpMessageHandler = new SocketsHttpHandler();
var httpMessageHandler = new SocketsHttpHandler();
httpMessageHandler.SslOptions.EnabledSslProtocols = TlsVersions.Instance.Preferred;
#else
HttpClientHandler httpMessageHandler = new HttpClientHandler();
var httpMessageHandler = new HttpClientHandler();
#if !NET451
httpMessageHandler.SslProtocols = TlsVersions.Instance.Preferred;
httpMessageHandler.CheckCertificateRevocationList = TlsVersions.Instance.CertificateRevocationCheck;
Expand Down
4 changes: 2 additions & 2 deletions common/src/service/StringValidationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ public static bool IsBase64String(string value)
return false;
}

var lengthNoPadding = value.Length;
int lengthNoPadding = value.Length;
value = value.TrimEnd(Base64Padding);
var lengthPadding = value.Length;
int lengthPadding = value.Length;

if ((lengthNoPadding - lengthPadding) > 2)
{
Expand Down
17 changes: 17 additions & 0 deletions e2e/test/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// This file is used by Code Analysis to maintain SuppressMessage
// attributes that are applied to this project.
// Project-level suppressions either have no target or are given
// a specific target and scoped to a namespace, type, member, etc.

using System.Diagnostics.CodeAnalysis;

[assembly: SuppressMessage(
"Style",
"IDE1006:Naming Styles",
Justification = "Missing Async suffix on test method names. Test method names may be misleading when they have the Async suffix. Additionally, not changing test names help to maintain ADO history.",
Scope = "module")]
[assembly: SuppressMessage(
"CodeQuality",
"IDE0079:Remove unnecessary suppression",
Justification = "Each frameworks consider certain suppressions required by other frameworks unnecessary.",
Scope = "module")]
12 changes: 6 additions & 6 deletions e2e/test/config/TestConfiguration.IoTHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,39 +59,39 @@ public static string GetIotHubSharedAccessSignature(TimeSpan timeToLive)
public static X509Certificate2 GetCertificateWithPrivateKey()
{
const string hubPfxCert = "IOTHUB_X509_PFX_CERTIFICATE";
var cert = GetBase64EncodedCertificate(hubPfxCert, defaultValue: string.Empty);
X509Certificate2 cert = GetBase64EncodedCertificate(hubPfxCert, defaultValue: string.Empty);
Assert.IsTrue(cert.NotAfter > DateTime.UtcNow, $"The X509 cert from {hubPfxCert} has expired.");
return cert;
}

public static X509Certificate2 GetChainDeviceCertificateWithPrivateKey()
{
const string hubPfxCert = "HUB_CHAIN_DEVICE_PFX_CERTIFICATE";
var cert = GetBase64EncodedCertificate(hubPfxCert, defaultValue: string.Empty);
X509Certificate2 cert = GetBase64EncodedCertificate(hubPfxCert, defaultValue: string.Empty);
Assert.IsTrue(cert.NotAfter > DateTime.UtcNow, $"The X509 cert from {hubPfxCert} has expired.");
return cert;
}

public static X509Certificate2 GetRootCACertificate()
{
const string hubCert = "HUB_CHAIN_ROOT_CA_CERTIFICATE";
var cert = GetBase64EncodedCertificate(hubCert);
X509Certificate2 cert = GetBase64EncodedCertificate(hubCert);
Assert.IsTrue(cert.NotAfter > DateTime.UtcNow, $"The X509 cert from {hubCert} has expired.");
return cert;
}

public static X509Certificate2 GetIntermediate1Certificate()
{
const string hubCert = "HUB_CHAIN_INTERMEDIATE1_CERTIFICATE";
var cert = GetBase64EncodedCertificate(hubCert);
X509Certificate2 cert = GetBase64EncodedCertificate(hubCert);
Assert.IsTrue(cert.NotAfter > DateTime.UtcNow, $"The X509 cert from {hubCert} has expired.");
return cert;
}

public static X509Certificate2 GetIntermediate2Certificate()
{
const string hubCert = "HUB_CHAIN_INTERMEDIATE2_CERTIFICATE";
var cert = GetBase64EncodedCertificate(hubCert);
X509Certificate2 cert = GetBase64EncodedCertificate(hubCert);
Assert.IsTrue(cert.NotAfter > DateTime.UtcNow, $"The X509 cert from {hubCert} has expired.");
return cert;
}
Expand All @@ -111,7 +111,7 @@ public static X509Certificate2 GetIntermediate2Certificate()

private static string GenerateSasToken(string resourceUri, string sharedAccessKey, TimeSpan timeToLive, string policyName = default)
{
DateTime epochTime = new DateTime(1970, 1, 1);
var epochTime = new DateTime(1970, 1, 1);
DateTime expiresOn = DateTime.UtcNow.Add(timeToLive);
TimeSpan secondsFromEpochTime = expiresOn.Subtract(epochTime);
long seconds = Convert.ToInt64(secondsFromEpochTime.TotalSeconds, CultureInfo.InvariantCulture);
Expand Down
2 changes: 1 addition & 1 deletion e2e/test/helpers/StorageContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ protected virtual void Dispose(bool disposing)

private async Task InitializeAsync()
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(TestConfiguration.Storage.ConnectionString);
var storageAccount = CloudStorageAccount.Parse(TestConfiguration.Storage.ConnectionString);
CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer = cloudBlobClient.GetContainerReference(ContainerName);
await CloudBlobContainer.CreateIfNotExistsAsync().ConfigureAwait(false);
Expand Down
2 changes: 1 addition & 1 deletion e2e/test/helpers/TestDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private static async Task<TestDevice> CreateDeviceAsync(TestDeviceType type, str
string deviceName = "E2E_" + prefix + Guid.NewGuid();

// Delete existing devices named this way and create a new one.
using RegistryManager rm = RegistryManager.CreateFromConnectionString(TestConfiguration.IoTHub.ConnectionString);
using var rm = RegistryManager.CreateFromConnectionString(TestConfiguration.IoTHub.ConnectionString);
s_logger.Trace($"{nameof(GetTestDeviceAsync)}: Creating device {deviceName} with type {type}.");

Client.IAuthenticationMethod auth = null;
Expand Down
20 changes: 13 additions & 7 deletions e2e/test/iothub/AuthenticationWithTokenRefreshDisposalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private async Task AuthenticationMethodDisposesTokenRefresher(Client.TransportTy
var authenticationMethod = new DeviceAuthenticationSasToken(testDevice.ConnectionString, disposeWithClient: true);

// Create an instance of the device client, send a test message and then close and dispose it.
DeviceClient deviceClient = DeviceClient.Create(testDevice.IoTHubHostName, authenticationMethod, transport);
var deviceClient = DeviceClient.Create(testDevice.IoTHubHostName, authenticationMethod, transport);
using var message1 = new Client.Message();
await deviceClient.SendEventAsync(message1).ConfigureAwait(false);
await deviceClient.CloseAsync();
Expand All @@ -115,7 +115,7 @@ private async Task AuthenticationMethodDisposesTokenRefresher(Client.TransportTy
// Perform the same steps again, reusing the previously created authentication method instance.
// Since the default behavior is to dispose AuthenticationWithTokenRefresh authentication method on DeviceClient disposal,
// this should now throw an ObjectDisposedException.
DeviceClient deviceClient2 = DeviceClient.Create(testDevice.IoTHubHostName, authenticationMethod, transport);
var deviceClient2 = DeviceClient.Create(testDevice.IoTHubHostName, authenticationMethod, transport);
using var message2 = new Client.Message();

Func<Task> act = async () => await deviceClient2.SendEventAsync(message2).ConfigureAwait(false);
Expand All @@ -131,16 +131,16 @@ private async Task ReuseAuthenticationMethod_SingleDevice(Client.TransportType t
var authenticationMethod = new DeviceAuthenticationSasToken(testDevice.ConnectionString, disposeWithClient: false);

// Create an instance of the device client, send a test message and then close and dispose it.
DeviceClient deviceClient = DeviceClient.Create(testDevice.IoTHubHostName, authenticationMethod, transport);
var deviceClient = DeviceClient.Create(testDevice.IoTHubHostName, authenticationMethod, transport);
using var message1 = new Client.Message();
await deviceClient.SendEventAsync(message1).ConfigureAwait(false);
await deviceClient.CloseAsync();
deviceClient.Dispose();
Logger.Trace("Test with instance 1 completed");

// Perform the same steps again, reusing the previously created authentication method instance to ensure
// that the sdk did not dispose the user supplied authentication method instance.
DeviceClient deviceClient2 = DeviceClient.Create(testDevice.IoTHubHostName, authenticationMethod, transport);
// that the SDK did not dispose the user supplied authentication method instance.
var deviceClient2 = DeviceClient.Create(testDevice.IoTHubHostName, authenticationMethod, transport);
using var message2 = new Client.Message();
await deviceClient2.SendEventAsync(message2).ConfigureAwait(false);
await deviceClient2.CloseAsync();
Expand Down Expand Up @@ -182,7 +182,7 @@ private async Task ReuseAuthenticationMethod_MuxedDevices(Client.TransportType t
for (int i = 0; i < devicesCount; i++)
{
#pragma warning disable CA2000 // Dispose objects before losing scope - the client instance is disposed during the course of the test.
DeviceClient deviceClient = DeviceClient.Create(testDevices[i].IoTHubHostName, authenticationMethods[i], new ITransportSettings[] { amqpTransportSettings });
var deviceClient = DeviceClient.Create(testDevices[i].IoTHubHostName, authenticationMethods[i], new ITransportSettings[] { amqpTransportSettings });
#pragma warning restore CA2000 // Dispose objects before losing scope

var amqpConnectionStatusChange = new AmqpConnectionStatusChange(testDevices[i].Id, Logger);
Expand Down Expand Up @@ -246,7 +246,13 @@ private async Task ReuseAuthenticationMethod_MuxedDevices(Client.TransportType t
for (int i = 0; i < devicesCount; i++)
{
#pragma warning disable CA2000 // Dispose objects before losing scope - the client instance is disposed at the end of the test.
DeviceClient deviceClient = DeviceClient.Create(testDevices[i].IoTHubHostName, authenticationMethods[i], new ITransportSettings[] { amqpTransportSettings });
var deviceClient = DeviceClient.Create(
testDevices[i].IoTHubHostName,
authenticationMethods[i],
new ITransportSettings[]
{
amqpTransportSettings
});
#pragma warning restore CA2000 // Dispose objects before losing scope

var amqpConnectionStatusChange = new AmqpConnectionStatusChange(testDevices[i].Id, Logger);
Expand Down
16 changes: 8 additions & 8 deletions e2e/test/iothub/ConnectionStatusChangeHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private async Task DeviceClient_Gives_ConnectionStatus_DeviceDisabled_Base(
ConnectionStatusChangeReason? statusChangeReason = null;
int deviceDisabledReceivedCount = 0;

using (DeviceClient deviceClient = DeviceClient.CreateFromConnectionString(deviceConnectionString, protocol))
using (var deviceClient = DeviceClient.CreateFromConnectionString(deviceConnectionString, protocol))
{
ConnectionStatusChangesHandler statusChangeHandler = (s, r) =>
{
Expand All @@ -135,11 +135,11 @@ private async Task DeviceClient_Gives_ConnectionStatus_DeviceDisabled_Base(

// Receiving the module twin should succeed right now.
Logger.Trace($"{nameof(DeviceClient_Gives_ConnectionStatus_DeviceDisabled_Base)}: DeviceClient GetTwinAsync.");
var twin = await deviceClient.GetTwinAsync().ConfigureAwait(false);
Shared.Twin twin = await deviceClient.GetTwinAsync().ConfigureAwait(false);
Assert.IsNotNull(twin);

// Delete/disable the device in IoT Hub. This should trigger the ConnectionStatusChangesHandler.
using (RegistryManager registryManager = RegistryManager.CreateFromConnectionString(TestConfiguration.IoTHub.ConnectionString))
using (var registryManager = RegistryManager.CreateFromConnectionString(TestConfiguration.IoTHub.ConnectionString))
{
await registryManagerOperation(registryManager, deviceId).ConfigureAwait(false);
}
Expand Down Expand Up @@ -168,8 +168,8 @@ private async Task DeviceClient_Gives_ConnectionStatus_DeviceDisabled_Base(
private async Task ModuleClient_Gives_ConnectionStatus_DeviceDisabled_Base(
Client.TransportType protocol, Func<RegistryManager, string, Task> registryManagerOperation)
{
AmqpTransportSettings amqpTransportSettings = new AmqpTransportSettings(protocol);
ITransportSettings[] transportSettings = new ITransportSettings[] { amqpTransportSettings };
var amqpTransportSettings = new AmqpTransportSettings(protocol);
var transportSettings = new ITransportSettings[] { amqpTransportSettings };

TestModule testModule = await TestModule.GetTestModuleAsync(DevicePrefix + $"_{Guid.NewGuid()}", ModulePrefix, Logger).ConfigureAwait(false);
ConnectionStatus? status = null;
Expand All @@ -185,7 +185,7 @@ private async Task ModuleClient_Gives_ConnectionStatus_DeviceDisabled_Base(
}
};

using (ModuleClient moduleClient = ModuleClient.CreateFromConnectionString(testModule.ConnectionString, transportSettings))
using (var moduleClient = ModuleClient.CreateFromConnectionString(testModule.ConnectionString, transportSettings))
{
moduleClient.SetConnectionStatusChangesHandler(statusChangeHandler);
Logger.Trace($"{nameof(ModuleClient_Gives_ConnectionStatus_DeviceDisabled_Base)}: Created {nameof(ModuleClient)} with moduleId={testModule.Id}");
Expand All @@ -194,11 +194,11 @@ private async Task ModuleClient_Gives_ConnectionStatus_DeviceDisabled_Base(

// Receiving the module twin should succeed right now.
Logger.Trace($"{nameof(ModuleClient_Gives_ConnectionStatus_DeviceDisabled_Base)}: ModuleClient GetTwinAsync.");
var twin = await moduleClient.GetTwinAsync().ConfigureAwait(false);
Shared.Twin twin = await moduleClient.GetTwinAsync().ConfigureAwait(false);
Assert.IsNotNull(twin);

// Delete/disable the device in IoT Hub.
using (RegistryManager registryManager = RegistryManager.CreateFromConnectionString(TestConfiguration.IoTHub.ConnectionString))
using (var registryManager = RegistryManager.CreateFromConnectionString(TestConfiguration.IoTHub.ConnectionString))
{
await registryManagerOperation(registryManager, testModule.DeviceId).ConfigureAwait(false);
}
Expand Down
8 changes: 4 additions & 4 deletions e2e/test/iothub/DeviceClientX509AuthenticationE2ETests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public async Task X509_Cert_Chain_Install_Test_MQTT_TCP()
TestConfiguration.IoTHub.X509ChainDeviceName,
s_chainCertificateWithPrivateKey,
chainCerts);
using DeviceClient deviceClient = DeviceClient.Create(
using var deviceClient = DeviceClient.Create(
_hostName,
auth,
DeviceTransportType.Mqtt_Tcp_Only);
Expand All @@ -186,7 +186,7 @@ public async Task X509_Cert_Chain_Install_Test_AMQP_TCP()
TestConfiguration.IoTHub.X509ChainDeviceName,
s_chainCertificateWithPrivateKey,
chainCerts);
using DeviceClient deviceClient = DeviceClient.Create(
using var deviceClient = DeviceClient.Create(
_hostName,
auth,
DeviceTransportType.Amqp_Tcp_Only);
Expand Down Expand Up @@ -253,7 +253,7 @@ private async Task X509InvalidDeviceIdOpenAsyncTest(Client.TransportType transpo
{
string deviceName = $"DEVICE_NOT_EXIST_{Guid.NewGuid()}";
using var auth = new DeviceAuthenticationWithX509Certificate(deviceName, s_selfSignedCertificateWithPrivateKey);
using DeviceClient deviceClient = DeviceClient.Create(_hostName, auth, transportType);
using var deviceClient = DeviceClient.Create(_hostName, auth, transportType);

try
{
Expand All @@ -274,7 +274,7 @@ private async Task X509InvalidDeviceIdOpenAsyncTwiceTest(Client.TransportType tr
{
string deviceName = $"DEVICE_NOT_EXIST_{Guid.NewGuid()}";
using var auth = new DeviceAuthenticationWithX509Certificate(deviceName, s_selfSignedCertificateWithPrivateKey);
using DeviceClient deviceClient = DeviceClient.Create(_hostName, auth, transportType);
using var deviceClient = DeviceClient.Create(_hostName, auth, transportType);

for (int i = 0; i < 2; i++)
{
Expand Down
Loading