-
Notifications
You must be signed in to change notification settings - Fork 35
Generate not-empty machine identifier #162
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| using Backtrace.Unity.Extensions; | ||
| using System; | ||
| using System.Linq; | ||
| using System.Net.NetworkInformation; | ||
| using UnityEngine; | ||
|
|
||
| [assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Backtrace.Unity.Tests.Runtime")] | ||
| namespace Backtrace.Unity.Model | ||
| { | ||
| /// <summary> | ||
| /// Backtrace Machine Id storage | ||
| /// </summary> | ||
| internal class MachineIdStorage | ||
| { | ||
| /// <summary> | ||
| /// Player prefs machine identifier key | ||
| /// </summary> | ||
| internal const string MachineIdentifierKey = "backtrace-machine-id"; | ||
|
|
||
| /// <summary> | ||
| /// Generate unique machine id. | ||
| /// </summary> | ||
| /// <returns>Unique machine id Guid in a string format</returns> | ||
| internal string GenerateMachineId() | ||
| { | ||
| var storageMachineId = FetchMachineIdFromStorage(); | ||
| if (!string.IsNullOrEmpty(storageMachineId)) | ||
| { | ||
| return storageMachineId; | ||
| } | ||
|
|
||
| #if !UNITY_WEBGL && !UNITY_SWITCH | ||
| var unityIdentifier = UseUnityIdentifier(); | ||
| if (!GuidHelper.IsNullOrEmpty(unityIdentifier)) | ||
| { | ||
| StoreMachineId(unityIdentifier); | ||
vlussenburg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return unityIdentifier; | ||
| } | ||
| var networkIdentifier = UseNetworkingIdentifier(); | ||
| if (!GuidHelper.IsNullOrEmpty(networkIdentifier)) | ||
| { | ||
| StoreMachineId(networkIdentifier); | ||
| return networkIdentifier; | ||
| } | ||
| #endif | ||
| var backtraceRandomIdentifier = Guid.NewGuid().ToString(); | ||
| StoreMachineId(backtraceRandomIdentifier); | ||
| return backtraceRandomIdentifier; | ||
| } | ||
|
|
||
|
|
||
| /// <summary> | ||
| /// Fetch a machine id in the internal storage | ||
| /// </summary> | ||
| /// <returns>machine identifier in the GUID string format</returns> | ||
| private string FetchMachineIdFromStorage() | ||
| { | ||
| return PlayerPrefs.GetString(MachineIdentifierKey); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Set a machine id in the internal storage | ||
| /// </summary> | ||
| /// <param name="machineId">machine identifier</param> | ||
| private void StoreMachineId(string machineId) | ||
| { | ||
| PlayerPrefs.SetString(MachineIdentifierKey, machineId); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Use Unity device identifier to generate machine identifier | ||
| /// </summary> | ||
| /// <returns>Unity machine identifier if the device identifier is supported. Otherwise null</returns> | ||
| protected virtual string UseUnityIdentifier() | ||
| { | ||
| if (SystemInfo.deviceUniqueIdentifier == SystemInfo.unsupportedIdentifier) | ||
| { | ||
| return null; | ||
| } | ||
| return SystemInfo.deviceUniqueIdentifier; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Use Networking interface to generate machine identifier - MAC number from the networking interface. | ||
| /// </summary> | ||
| /// <returns>Machine id - MAC in a GUID format. If the networking interface is not available then it returns null.</returns> | ||
| protected virtual string UseNetworkingIdentifier() | ||
| { | ||
| var interfaces = NetworkInterface.GetAllNetworkInterfaces() | ||
| .Where(n => n.OperationalStatus == OperationalStatus.Up); | ||
|
|
||
| foreach (var @interface in interfaces) | ||
| { | ||
| var physicalAddress = @interface.GetPhysicalAddress(); | ||
| if (physicalAddress == null) | ||
| { | ||
| continue; | ||
| } | ||
| var macAddress = physicalAddress.ToString(); | ||
| if (string.IsNullOrEmpty(macAddress)) | ||
| { | ||
| continue; | ||
| } | ||
| string hex = macAddress.Replace(":", string.Empty); | ||
| var value = Convert.ToInt64(hex, 16); | ||
| return GuidHelper.FromLong(value).ToString(); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| using Backtrace.Unity.Extensions; | ||
| using Backtrace.Unity.Model; | ||
| using Backtrace.Unity.Tests.Runtime.Client.Mocks; | ||
| using NUnit.Framework; | ||
| using UnityEngine; | ||
|
|
||
| namespace Backtrace.Unity.Tests.Runtime.Client | ||
| { | ||
| class BacktraceAttributeMachineIdTests | ||
konraddysput marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| [SetUp] | ||
| public void Cleanup() | ||
| { | ||
| PlayerPrefs.DeleteKey(MachineIdStorage.MachineIdentifierKey); | ||
| } | ||
|
|
||
| [Test] | ||
| public void TestMachineAttributes_ShouldUseUnityIdentifier_ShouldReturnUnityIdentitfier() | ||
| { | ||
| var machineIdStorage = new MachineIdStorageMock(); | ||
|
|
||
| var machineId = machineIdStorage.GenerateMachineId(); | ||
|
|
||
| Assert.IsFalse(GuidHelper.IsNullOrEmpty(machineId)); | ||
| } | ||
|
|
||
| [Test] | ||
| public void TestMachineAttributes_ShouldUseMac_ShouldReturnNetowrkingIdentifier() | ||
| { | ||
| var machineIdStorage = new MachineIdStorageMock(false); | ||
|
|
||
| var machineId = machineIdStorage.GenerateMachineId(); | ||
|
|
||
| Assert.IsFalse(GuidHelper.IsNullOrEmpty(machineId)); | ||
| } | ||
|
|
||
| [Test] | ||
| public void TestMachineAttributes_ShouldUseRandomMachineId_ShouldReturnRandomMachineId() | ||
| { | ||
| var machineIdStorage = new MachineIdStorageMock(false, false); | ||
|
|
||
| var machineId = machineIdStorage.GenerateMachineId(); | ||
|
|
||
| Assert.IsFalse(GuidHelper.IsNullOrEmpty(machineId)); | ||
| } | ||
|
|
||
| [Test] | ||
| public void TestMachineAttributes_ShouldAlwaysReturnTheSameValueUnityId_IdentifierAreTheSame() | ||
| { | ||
| var firstMachineIdStorage = new MachineIdStorageMock().GenerateMachineId(); | ||
| var secGenerationOfMachineIdStorage = new MachineIdStorageMock().GenerateMachineId(); | ||
|
|
||
| Assert.IsTrue(firstMachineIdStorage == secGenerationOfMachineIdStorage); | ||
| } | ||
|
|
||
| [Test] | ||
| public void TestMachineAttributes_ShouldAlwaysReturnTheSameValueMacId_IdentifierAreTheSame() | ||
| { | ||
| var firstMachineIdStorage = new MachineIdStorageMock(false).GenerateMachineId(); | ||
| var secGenerationOfMachineIdStorage = new MachineIdStorageMock(false).GenerateMachineId(); | ||
|
|
||
| Assert.IsTrue(firstMachineIdStorage == secGenerationOfMachineIdStorage); | ||
| } | ||
|
|
||
| [Test] | ||
| public void TestMachineAttributes_ShouldAlwaysReturnTheSameValueRandomId_IdentifierAreTheSame() | ||
| { | ||
| var firstMachineIdStorage = new MachineIdStorageMock(false, false).GenerateMachineId(); | ||
| var secGenerationOfMachineIdStorage = new MachineIdStorageMock(false, false).GenerateMachineId(); | ||
|
|
||
| Assert.IsTrue(firstMachineIdStorage == secGenerationOfMachineIdStorage); | ||
| } | ||
|
|
||
| [Test] | ||
| public void TestMachineAttributes_ShouldAlwaysGenerateTheSameUntiyAttribute_ShouldReturnTheSameUnityIdentitfier() | ||
| { | ||
| var machineIdStorage = new MachineIdStorageMock(); | ||
|
|
||
| var machineId = machineIdStorage.GenerateMachineId(); | ||
| PlayerPrefs.DeleteKey(MachineIdStorage.MachineIdentifierKey); | ||
| var machineIdAfterCleanup = machineIdStorage.GenerateMachineId(); | ||
|
|
||
| Assert.AreEqual(machineId, machineIdAfterCleanup); | ||
| } | ||
|
|
||
| [Test] | ||
| public void TestMachineAttributes_ShouldAlwaysGenerateTheSameMacAttribute_ShouldReturnTheSameMacIdentitfier() | ||
| { | ||
| var machineIdStorage = new MachineIdStorageMock(false); | ||
|
|
||
| var machineId = machineIdStorage.GenerateMachineId(); | ||
| PlayerPrefs.DeleteKey(MachineIdStorage.MachineIdentifierKey); | ||
| var machineIdAfterCleanup = machineIdStorage.GenerateMachineId(); | ||
|
|
||
| Assert.AreEqual(machineId, machineIdAfterCleanup); | ||
| } | ||
| } | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| using Backtrace.Unity.Model; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Backtrace.Unity.Tests.Runtime.Client.Mocks | ||
| { | ||
| internal class MachineIdStorageMock : MachineIdStorage | ||
| { | ||
| private readonly bool _allowUnityIdentifier; | ||
| private readonly bool _allowNetworking; | ||
| public MachineIdStorageMock(bool allowUnityIdentifier = true, bool allowNetworking = true) : base() | ||
| { | ||
| _allowUnityIdentifier = allowUnityIdentifier; | ||
| _allowNetworking = allowNetworking; | ||
| } | ||
|
|
||
|
|
||
| protected override string UseNetworkingIdentifier() | ||
| { | ||
| if (!_allowNetworking) | ||
| { | ||
| return null; | ||
| } | ||
| return base.UseNetworkingIdentifier(); | ||
| } | ||
|
|
||
| protected override string UseUnityIdentifier() | ||
| { | ||
| if (!_allowUnityIdentifier) | ||
| { | ||
| return null; | ||
| } | ||
| return base.UseUnityIdentifier(); | ||
| } | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.