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

Fixed cpu info for linux #84

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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
51 changes: 51 additions & 0 deletions src/DeviceId.Linux/Components/CpuInfoIdComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Security.Cryptography;
using System.Text;
using DeviceId.Internal.CommandExecutors;

namespace DeviceId.Linux.Components;

/// <summary>
/// An implementation of <see cref="IDeviceIdComponent"/>
/// </summary>
public class CpuInfoIdComponent : IDeviceIdComponent
{
/// <summary>
/// Command executor.
/// </summary>
private readonly ICommandExecutor _commandExecutor;

/// <summary>
/// Initializes a new instance of the <see cref="CpuInfoIdComponent"/> class.
/// </summary>
public CpuInfoIdComponent() : this(CommandExecutor.Bash) { }

/// <summary>
/// Initializes a new instance of the <see cref="CpuInfoIdComponent"/> class.
/// </summary>
/// <param name="commandExecutor">The command executor to use.</param>
internal CpuInfoIdComponent(ICommandExecutor commandExecutor)
{
_commandExecutor = commandExecutor;
}

/// <summary>
/// Gets the component value.
/// </summary>
/// <returns>The component value.</returns>
public string GetValue()
{
try
{
string content = _commandExecutor.Execute("cat /proc/cpuinfo | grep -v \"cpu MHz\"");
using var hasher = MD5.Create();
var hash = hasher.ComputeHash(Encoding.ASCII.GetBytes(content));
return BitConverter.ToString(hash).Replace("-", "").ToUpper();
}
catch
{
// Can fail if we have no permissions to access the file.
}
return null;
}
}
2 changes: 1 addition & 1 deletion src/DeviceId.Linux/LinuxDeviceIdBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static LinuxDeviceIdBuilder AddProductUuid(this LinuxDeviceIdBuilder buil
/// <returns>The <see cref="LinuxDeviceIdBuilder"/> instance.</returns>
public static LinuxDeviceIdBuilder AddCpuInfo(this LinuxDeviceIdBuilder builder)
{
return builder.AddComponent("CPUInfo", new FileContentsDeviceIdComponent("/proc/cpuinfo", true));
return builder.AddComponent("CPUInfo", new CpuInfoIdComponent());
}

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions src/DeviceId.Windows.Mmi/DeviceId.Windows.Mmi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<PackageId>DeviceId.Windows.Mmi</PackageId>
<Title>DeviceId (Windows / MMI)</Title>
<Description>Provides extra Windows-specific components (using MMI) for the DeviceId package.</Description>
<EnableWindowsTargeting>true</EnableWindowsTargeting>
<VersionPrefix>6.8.0</VersionPrefix>
</PropertyGroup>

Expand Down
1 change: 1 addition & 0 deletions src/DeviceId.Windows.Wmi/DeviceId.Windows.Wmi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<PackageId>DeviceId.Windows.Wmi</PackageId>
<Title>DeviceId (Windows / WMI)</Title>
<Description>Provides extra Windows-specific components (using WMI) for the DeviceId package.</Description>
<EnableWindowsTargeting>true</EnableWindowsTargeting>
<VersionPrefix>6.8.0</VersionPrefix>
</PropertyGroup>

Expand Down
1 change: 1 addition & 0 deletions src/DeviceId.Windows/DeviceId.Windows.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<PackageId>DeviceId.Windows</PackageId>
<Title>DeviceId (Windows)</Title>
<Description>Provides Windows-specific components for the DeviceId package.</Description>
<EnableWindowsTargeting>true</EnableWindowsTargeting>
<VersionPrefix>6.8.0</VersionPrefix>
</PropertyGroup>

Expand Down
21 changes: 21 additions & 0 deletions test/DeviceId.Tests/Components/CpuInfoIdComponentTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using DeviceId.Linux.Components;
using FluentAssertions;
using Xunit;

namespace DeviceId.Tests.Components;

public class CpuInfoIdComponentTests
{
[Fact]
public void GetValue_ShouldReturnSameValueForMultipleExecutions()
{
var component = new CpuInfoIdComponent();
var expectedComponentValue = component.GetValue();

for (int i = 0; i < 10; i++)
{
var currentValue = component.GetValue();
currentValue.Should().Be(expectedComponentValue);
}
}
}