Skip to content

Commit

Permalink
Fix test references by making a local copy for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
David R. Williamson committed Oct 4, 2021
1 parent e1d3f7c commit 5445416
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 3 deletions.
62 changes: 62 additions & 0 deletions e2e/test/helpers/CryptoKeyGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Security.Cryptography;

#if !NET451

using System.Linq;

#endif

namespace Microsoft.Azure.Devices.E2ETests
{
/// <summary>
/// Utility methods for generating cryptographically secure keys and passwords.
/// </summary>
internal static class CryptoKeyGenerator
{
#if NET451
private const int DefaultPasswordLength = 16;
private const int GuidLength = 16;
#endif

/// <summary>
/// Size of the SHA 512 key.
/// </summary>
internal const int Sha512KeySize = 64;

/// <summary>
/// Generate a key with a specified key size.
/// </summary>
/// <param name="keySize">The size of the key.</param>
/// <returns>Byte array representing the key.</returns>
internal static byte[] GenerateKeyBytes(int keySize)
{
#if NET451
byte[] keyBytes = new byte[keySize];
using var cyptoProvider = new RNGCryptoServiceProvider();
cyptoProvider.GetNonZeroBytes(keyBytes);
#else
byte[] keyBytes = new byte[keySize];
using var cyptoProvider = RandomNumberGenerator.Create();
while (keyBytes.Contains(byte.MinValue))
{
cyptoProvider.GetBytes(keyBytes);
}
#endif
return keyBytes;
}

/// <summary>
/// Generates a key of the specified size.
/// </summary>
/// <param name="keySize">Desired key size.</param>
/// <returns>A generated key.</returns>
internal static string GenerateKey(int keySize)
{
return Convert.ToBase64String(GenerateKeyBytes(keySize));
}
}
}
2 changes: 0 additions & 2 deletions e2e/test/provisioning/ProvisioningServiceClientE2ETests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.Tracing;
using System.Net;
using System.Threading.Tasks;
using Microsoft.Azure.Devices.Common;
using Microsoft.Azure.Devices.Provisioning.Security.Samples;
using Microsoft.Azure.Devices.Provisioning.Service;
using Microsoft.Azure.Devices.Shared;
Expand Down
62 changes: 62 additions & 0 deletions iothub/service/tests/CryptoKeyGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Security.Cryptography;

#if !NET451

using System.Linq;

#endif

namespace Microsoft.Azure.Devices.Tests
{
/// <summary>
/// Utility methods for generating cryptographically secure keys and passwords.
/// </summary>
internal static class CryptoKeyGenerator
{
#if NET451
private const int DefaultPasswordLength = 16;
private const int GuidLength = 16;
#endif

/// <summary>
/// Size of the SHA 512 key.
/// </summary>
internal const int Sha512KeySize = 64;

/// <summary>
/// Generate a key with a specified key size.
/// </summary>
/// <param name="keySize">The size of the key.</param>
/// <returns>Byte array representing the key.</returns>
internal static byte[] GenerateKeyBytes(int keySize)
{
#if NET451
byte[] keyBytes = new byte[keySize];
using var cyptoProvider = new RNGCryptoServiceProvider();
cyptoProvider.GetNonZeroBytes(keyBytes);
#else
byte[] keyBytes = new byte[keySize];
using var cyptoProvider = RandomNumberGenerator.Create();
while (keyBytes.Contains(byte.MinValue))
{
cyptoProvider.GetBytes(keyBytes);
}
#endif
return keyBytes;
}

/// <summary>
/// Generates a key of the specified size.
/// </summary>
/// <param name="keySize">Desired key size.</param>
/// <returns>A generated key.</returns>
internal static string GenerateKey(int keySize)
{
return Convert.ToBase64String(GenerateKeyBytes(keySize));
}
}
}
2 changes: 1 addition & 1 deletion iothub/service/tests/DeviceAuthenticationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Devices.Common;
using Microsoft.Azure.Devices.Tests;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

Expand Down

0 comments on commit 5445416

Please sign in to comment.