-
Notifications
You must be signed in to change notification settings - Fork 55
feat: Implement reading of VersionQualifier into YubikeyDeviceInfo #240
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
50e1ee4
feat: Implement reading of VersionQualifier into YubikeyDeviceInfo
DennisDyallo 9789d88
feat: Implement reading of VersionQualifier into YubikeyDeviceInfo
DennisDyallo 0e46a66
Update Yubico.YubiKey/src/Yubico/YubiKey/YubiKeyDeviceInfo.cs
DennisDyallo f82920c
Update Yubico.YubiKey/tests/unit/Yubico/YubiKey/YubikeyDeviceInfoTest…
DennisDyallo 587bd17
Update Yubico.YubiKey/tests/unit/Yubico/YubiKey/YubikeyDeviceInfoTest…
DennisDyallo 2d03c3f
Update Yubico.YubiKey/tests/unit/Yubico/YubiKey/YubikeyDeviceInfoTest…
DennisDyallo 1d38503
Update Yubico.YubiKey/tests/unit/Yubico/YubiKey/VersionQualifierTests.cs
DennisDyallo f15f0e4
chore: use long to capture uint32 value
DennisDyallo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright 2025 Yubico AB | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). | ||
// You may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using System; | ||
|
||
namespace Yubico.YubiKey; | ||
|
||
/// <summary> | ||
/// Represents the type of version qualifier for a firmware version. | ||
/// The version qualifier type indicates whether the version is an Alpha, Beta, or Final release. | ||
/// </summary> | ||
internal enum VersionQualifierType : byte | ||
{ | ||
Alpha = 0x00, | ||
Beta = 0x01, | ||
Final = 0x02 | ||
} | ||
|
||
/// <summary> | ||
/// Represents a version qualifier for a firmware version. | ||
/// A version qualifier typically includes the firmware version, a type (such as Alpha, Beta, or Final), | ||
/// and an iteration number. | ||
/// </summary> | ||
internal class VersionQualifier | ||
{ | ||
/// <summary> | ||
/// Represents the firmware version associated with this qualifier. | ||
/// </summary> | ||
public FirmwareVersion FirmwareVersion { get; } | ||
/// <summary> | ||
/// Represents the type of version qualifier, such as Alpha, Beta, or Final. | ||
/// </summary> | ||
public VersionQualifierType Type { get; } | ||
|
||
/// <summary> | ||
/// Represents the iteration number of the version qualifier. | ||
/// </summary> | ||
public long Iteration { get; } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="VersionQualifier"/> class. | ||
/// This constructor allows you to specify the firmware version, type, and iteration. | ||
/// The iteration must be a non-negative value and less than or equal to int.MaxValue. | ||
/// If the firmware version is null, an <see cref="ArgumentNullException"/> will be thrown. | ||
/// If the iteration is negative or greater than int.MaxValue, an <see cref="ArgumentOutOfRangeException"/> will be thrown. | ||
/// </summary> | ||
/// <param name="firmwareVersion">The firmware version associated with this qualifier.</param> | ||
/// <param name="type">The type of version qualifier (Alpha, Beta, Final).</param> | ||
/// <param name="iteration">The iteration number of the version qualifier, must be a non-negative value and less than or equal to int.MaxValue.</param> | ||
/// <exception cref="ArgumentOutOfRangeException"></exception> | ||
/// <exception cref="ArgumentNullException"></exception> | ||
public VersionQualifier(FirmwareVersion firmwareVersion, VersionQualifierType type, long iteration) | ||
{ | ||
if (iteration < 0 || iteration > uint.MaxValue) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(iteration), | ||
$"Iteration must be between 0 and {uint.MaxValue}."); | ||
} | ||
|
||
FirmwareVersion = firmwareVersion ?? throw new ArgumentNullException(nameof(firmwareVersion)); | ||
Type = type; | ||
Iteration = iteration; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="VersionQualifier"/> class with default values. | ||
/// The default firmware version is set to a new instance of <see cref="FirmwareVersion"/>, | ||
/// the type is set to <see cref="VersionQualifierType.Final"/>, and the iteration is set to 0. | ||
/// </summary> | ||
public VersionQualifier() | ||
{ | ||
FirmwareVersion = new FirmwareVersion(); | ||
Type = VersionQualifierType.Final; | ||
Iteration = 0; | ||
} | ||
|
||
public override string ToString() => $"{FirmwareVersion}.{Type.ToString().ToLowerInvariant()}.{Iteration}"; | ||
public override bool Equals(object obj) => obj is VersionQualifier other && | ||
FirmwareVersion.Equals(other.FirmwareVersion) && | ||
Type == other.Type && | ||
Iteration == other.Iteration; | ||
public override int GetHashCode() => HashCode.Combine(FirmwareVersion, Type, Iteration); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
Yubico.YubiKey/tests/unit/Yubico/YubiKey/VersionQualifierTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Copyright 2024 Yubico AB | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). | ||
// You may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using Xunit; | ||
|
||
namespace Yubico.YubiKey | ||
{ | ||
public class VersionQualifierTests | ||
{ | ||
[Fact] | ||
public void TestVersion() | ||
{ | ||
var version = new FirmwareVersion(5, 7, 2); | ||
Assert.Equal( | ||
version, new VersionQualifier(version, VersionQualifierType.Alpha, 1).FirmwareVersion); | ||
} | ||
|
||
[Fact] | ||
public void TestType() | ||
{ | ||
Assert.Equal( | ||
VersionQualifierType.Alpha, | ||
new VersionQualifier(new FirmwareVersion(5, 7, 2), VersionQualifierType.Alpha, 1).Type); | ||
Assert.Equal( | ||
VersionQualifierType.Beta, | ||
new VersionQualifier(new FirmwareVersion(5, 7, 2), VersionQualifierType.Beta, 1).Type); | ||
Assert.Equal( | ||
VersionQualifierType.Final, | ||
new VersionQualifier(new FirmwareVersion(5, 7, 2), VersionQualifierType.Final, 1).Type); | ||
} | ||
|
||
[Fact] | ||
public void TestIteration() | ||
{ | ||
var version = new FirmwareVersion(5, 7, 2); | ||
var type = VersionQualifierType.Alpha; | ||
Assert.Equal(0, new VersionQualifier(version, type, 0).Iteration); | ||
Assert.Equal(128, new VersionQualifier(version, type, 128).Iteration); | ||
Assert.Equal(255, new VersionQualifier(version, type, 255).Iteration); | ||
} | ||
|
||
[Fact] | ||
public void TestToString() | ||
{ | ||
Assert.Equal( | ||
"5.7.2.alpha.0", | ||
new VersionQualifier(new FirmwareVersion(5, 7, 2), VersionQualifierType.Alpha, 0).ToString()); | ||
Assert.Equal( | ||
"5.6.6.beta.16384", | ||
new VersionQualifier(new FirmwareVersion(5, 6, 6), VersionQualifierType.Beta, 16384).ToString()); | ||
Assert.Equal( | ||
"3.4.0.final.2147483648", | ||
new VersionQualifier(new FirmwareVersion(3, 4, 0), VersionQualifierType.Final, 0x80000000).ToString()); | ||
Assert.Equal( | ||
"3.4.0.final.2147483647", | ||
new VersionQualifier(new FirmwareVersion(3, 4, 0), VersionQualifierType.Final, 0x7fffffff).ToString()); | ||
} | ||
|
||
[Fact] | ||
public void TestEqualsAndHashCode() | ||
{ | ||
var version1 = new FirmwareVersion(1, 0, 0); | ||
var version2 = new FirmwareVersion(1, 0, 0); | ||
var qualifier1 = new VersionQualifier(version1, VersionQualifierType.Alpha, 1); | ||
var qualifier2 = new VersionQualifier(version2, VersionQualifierType.Alpha, 1); | ||
var qualifier3 = new VersionQualifier(version1, VersionQualifierType.Beta, 2); | ||
|
||
Assert.Equal(qualifier1, qualifier2); | ||
Assert.Equal(qualifier1.GetHashCode(), qualifier2.GetHashCode()); | ||
Assert.NotEqual(qualifier1, qualifier3); | ||
// Hash codes are not guaranteed to be different for non-equal objects, | ||
// but for these specific inputs, they likely will be. | ||
// If this assertion fails, it might not indicate a bug in GetHashCode itself, | ||
// as long as the Equals contract is maintained. | ||
// Assert.NotEqual(qualifier1.GetHashCode(), qualifier3.GetHashCode()); | ||
} | ||
|
||
[Fact] | ||
public void TestTypeFromValue() | ||
{ | ||
Assert.Equal(VersionQualifierType.Alpha, (VersionQualifierType)0); | ||
Assert.Equal(VersionQualifierType.Beta, (VersionQualifierType)1); | ||
Assert.Equal(VersionQualifierType.Final, (VersionQualifierType)2); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.