Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/Common/Commands.Common/MetricHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,13 @@ private static string HashMacAddress
{
if (_hashMacAddress == string.Empty)
{
var macAddress = NetworkInterface.GetAllNetworkInterfaces()
.FirstOrDefault(nic => nic.OperationalStatus == OperationalStatus.Up)?
var macAddress = NetworkInterface.GetAllNetworkInterfaces()?
.FirstOrDefault(nic => nic != null &&
nic.OperationalStatus == OperationalStatus.Up &&
nic.GetPhysicalAddress() != null &&
!string.IsNullOrWhiteSpace(nic.GetPhysicalAddress().ToString()))?
.GetPhysicalAddress()?.ToString();
_hashMacAddress = macAddress == null ? null : GenerateSha256HashString(macAddress).Replace("-", string.Empty).ToLowerInvariant();
_hashMacAddress = string.IsNullOrWhiteSpace(macAddress) ? null : GenerateSha256HashString(macAddress)?.Replace("-", string.Empty)?.ToLowerInvariant();
}

return _hashMacAddress;
Expand Down Expand Up @@ -246,7 +249,7 @@ public static string GenerateSha256HashString(string originInput)
{
if (string.IsNullOrWhiteSpace(originInput))
{
throw new ArgumentNullException("originInput");
return string.Empty;
}

using (var sha256 = new SHA256CryptoServiceProvider())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ protected override void InitializeQosEvent()
if (this.DefaultProfile != null &&
this.DefaultProfile.DefaultContext != null &&
this.DefaultProfile.DefaultContext.Account != null &&
this.DefaultProfile.DefaultContext.Account.Id != null)
!string.IsNullOrWhiteSpace(this.DefaultProfile.DefaultContext.Account.Id))
{
_qosEvent.Uid = MetricHelper.GenerateSha256HashString(
this.DefaultProfile.DefaultContext.Account.Id.ToString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

using Microsoft.WindowsAzure.Commands.Common;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using Moq;
using System;
using System.Net.NetworkInformation;
using Xunit;

namespace Microsoft.Azure.Commands.Profile.Test
Expand All @@ -23,9 +25,11 @@ public class TelemetryTests
{
[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void HashOfNullValueThrowsAppropriateException()
public void HashOfNullOrWhitespaceValueReturnsEmptyString()
{
Assert.Throws<ArgumentNullException>(() => MetricHelper.GenerateSha256HashString(null));
Assert.Equal(string.Empty, MetricHelper.GenerateSha256HashString(null));
Assert.Equal(string.Empty, MetricHelper.GenerateSha256HashString(string.Empty));
Assert.Equal(string.Empty, MetricHelper.GenerateSha256HashString(" "));
}

[Fact]
Expand All @@ -38,5 +42,15 @@ public void HashOfValidValueSucceeds()
Assert.True(hash.Length > 0);
Assert.NotEqual<string>(inputValue, hash, StringComparer.OrdinalIgnoreCase);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void NetworkInterfaceWithEmptyAddressReturnsEmptyString()
{
var address = new PhysicalAddress(new byte[] { } );
Assert.Equal(string.Empty, address.ToString());
var hashAddress = MetricHelper.GenerateSha256HashString(address.ToString());
Assert.Equal(string.Empty, hashAddress);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ protected override void InitializeQosEvent()

if (this.DefaultContext != null &&
this.DefaultContext.Account != null &&
this.DefaultContext.Account.Id != null)
!string.IsNullOrEmpty(this.DefaultContext.Account.Id))
{
_qosEvent.Uid = MetricHelper.GenerateSha256HashString(
this.DefaultContext.Account.Id.ToString());
Expand Down