Skip to content

Commit 6b82726

Browse files
authored
Merge pull request Azure#4458 from cormacpayne/telemetry-test
Add tests and null checks around MAC address functionality
2 parents 286e30c + 6a1ba6a commit 6b82726

File tree

4 files changed

+25
-8
lines changed

4 files changed

+25
-8
lines changed

src/Common/Commands.Common/MetricHelper.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,13 @@ private static string HashMacAddress
6262
{
6363
if (_hashMacAddress == string.Empty)
6464
{
65-
var macAddress = NetworkInterface.GetAllNetworkInterfaces()
66-
.FirstOrDefault(nic => nic.OperationalStatus == OperationalStatus.Up)?
65+
var macAddress = NetworkInterface.GetAllNetworkInterfaces()?
66+
.FirstOrDefault(nic => nic != null &&
67+
nic.OperationalStatus == OperationalStatus.Up &&
68+
nic.GetPhysicalAddress() != null &&
69+
!string.IsNullOrWhiteSpace(nic.GetPhysicalAddress().ToString()))?
6770
.GetPhysicalAddress()?.ToString();
68-
_hashMacAddress = macAddress == null ? null : GenerateSha256HashString(macAddress).Replace("-", string.Empty).ToLowerInvariant();
71+
_hashMacAddress = string.IsNullOrWhiteSpace(macAddress) ? null : GenerateSha256HashString(macAddress)?.Replace("-", string.Empty)?.ToLowerInvariant();
6972
}
7073

7174
return _hashMacAddress;
@@ -246,7 +249,7 @@ public static string GenerateSha256HashString(string originInput)
246249
{
247250
if (string.IsNullOrWhiteSpace(originInput))
248251
{
249-
throw new ArgumentNullException("originInput");
252+
return string.Empty;
250253
}
251254

252255
using (var sha256 = new SHA256CryptoServiceProvider())

src/ResourceManager/Common/Commands.ResourceManager.Common/AzureRMCmdlet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ protected override void InitializeQosEvent()
225225
if (this.DefaultProfile != null &&
226226
this.DefaultProfile.DefaultContext != null &&
227227
this.DefaultProfile.DefaultContext.Account != null &&
228-
this.DefaultProfile.DefaultContext.Account.Id != null)
228+
!string.IsNullOrWhiteSpace(this.DefaultProfile.DefaultContext.Account.Id))
229229
{
230230
_qosEvent.Uid = MetricHelper.GenerateSha256HashString(
231231
this.DefaultProfile.DefaultContext.Account.Id.ToString());

src/ResourceManager/Profile/Commands.Profile.Test/TelemetryTests.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414

1515
using Microsoft.WindowsAzure.Commands.Common;
1616
using Microsoft.WindowsAzure.Commands.ScenarioTest;
17+
using Moq;
1718
using System;
19+
using System.Net.NetworkInformation;
1820
using Xunit;
1921

2022
namespace Microsoft.Azure.Commands.Profile.Test
@@ -23,9 +25,11 @@ public class TelemetryTests
2325
{
2426
[Fact]
2527
[Trait(Category.AcceptanceType, Category.CheckIn)]
26-
public void HashOfNullValueThrowsAppropriateException()
28+
public void HashOfNullOrWhitespaceValueReturnsEmptyString()
2729
{
28-
Assert.Throws<ArgumentNullException>(() => MetricHelper.GenerateSha256HashString(null));
30+
Assert.Equal(string.Empty, MetricHelper.GenerateSha256HashString(null));
31+
Assert.Equal(string.Empty, MetricHelper.GenerateSha256HashString(string.Empty));
32+
Assert.Equal(string.Empty, MetricHelper.GenerateSha256HashString(" "));
2933
}
3034

3135
[Fact]
@@ -38,5 +42,15 @@ public void HashOfValidValueSucceeds()
3842
Assert.True(hash.Length > 0);
3943
Assert.NotEqual<string>(inputValue, hash, StringComparer.OrdinalIgnoreCase);
4044
}
45+
46+
[Fact]
47+
[Trait(Category.AcceptanceType, Category.CheckIn)]
48+
public void NetworkInterfaceWithEmptyAddressReturnsEmptyString()
49+
{
50+
var address = new PhysicalAddress(new byte[] { } );
51+
Assert.Equal(string.Empty, address.ToString());
52+
var hashAddress = MetricHelper.GenerateSha256HashString(address.ToString());
53+
Assert.Equal(string.Empty, hashAddress);
54+
}
4155
}
4256
}

src/ServiceManagement/Common/Commands.ServiceManagement.Common/AzureSMCmdlet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ protected override void InitializeQosEvent()
111111

112112
if (this.DefaultContext != null &&
113113
this.DefaultContext.Account != null &&
114-
this.DefaultContext.Account.Id != null)
114+
!string.IsNullOrEmpty(this.DefaultContext.Account.Id))
115115
{
116116
_qosEvent.Uid = MetricHelper.GenerateSha256HashString(
117117
this.DefaultContext.Account.Id.ToString());

0 commit comments

Comments
 (0)