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

Correctly handle different architectures for devdeviceID #43471

Merged
merged 3 commits into from
Sep 18, 2024
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
16 changes: 11 additions & 5 deletions src/Cli/dotnet/Telemetry/DevDeviceIDGetter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ private static string GetCachedDeviceId()

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// Get device Id from Windows registry
using (var key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\DeveloperTools"))
// Get device Id from Windows registry matching the OS architecture
using (var key = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64).OpenSubKey(@"SOFTWARE\Microsoft\DeveloperTools"))
{
deviceId = key?.GetValue("deviceid") as string;
}
Expand Down Expand Up @@ -80,10 +80,16 @@ private static void CacheDeviceId(string deviceId)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// Cache device Id in Windows registry
using (var key = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Microsoft\DeveloperTools"))
// Cache device Id in Windows registry matching the OS architecture
using (RegistryKey baseKey = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64))
{
key.SetValue("deviceid", deviceId);
using(var key = baseKey.CreateSubKey(@"SOFTWARE\Microsoft\DeveloperTools"))
{
if (key != null)
{
key.SetValue("deviceid", deviceId);
}
}
}
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
Expand Down
6 changes: 5 additions & 1 deletion test/dotnet.Tests/TelemetryCommonPropertiesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,16 @@ public void TelemetryCommonPropertiesShouldReturnNewGuidWhenCannotGetMacAddress(
}

[Fact]
public void TelemetryCommonPropertiesShouldReturnNewGuidWhenCannotDevDeviceId()
public void TelemetryCommonPropertiesShouldEnsureDevDeviceIDIsCached()
{
var unitUnderTest = new TelemetryCommonProperties(userLevelCacheWriter: new NothingCache());
var assignedMachineId = unitUnderTest.GetTelemetryCommonProperties()["devdeviceid"];

Guid.TryParse(assignedMachineId, out var _).Should().BeTrue("it should be a guid");
var secondAssignedMachineId = unitUnderTest.GetTelemetryCommonProperties()["devdeviceid"];

Guid.TryParse(secondAssignedMachineId, out var _).Should().BeTrue("it should be a guid");
secondAssignedMachineId.Should().Be(assignedMachineId, "it should match the previously assigned guid");
}

[Fact]
Expand Down