Skip to content

feat: Add ToString override to CommandApdu and ResponseApdu #270

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 1 commit into from
Jul 3, 2025

Conversation

DennisDyallo
Copy link
Collaborator

This pull request introduces ToString overrides in the CommandApdu and ResponseApdu classes to provide a human-readable string representation of their internal state. These changes improve debugging and logging capabilities.

Enhancements to debugging and logging:

@DennisDyallo DennisDyallo added the enhancement New feature or request label Jul 1, 2025
@DennisDyallo DennisDyallo changed the title feat: Add ToString method to CommandApdu and ResponseApdu feat: Add ToString override to CommandApdu and ResponseApdu Jul 1, 2025
@DennisDyallo DennisDyallo requested a review from Copilot July 1, 2025 10:58
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds human-readable ToString overrides to the APDU classes for improved debugging and logging.

  • CommandApdu: prints CLA, INS, P1, P2, Lc (Nc), Le (Ne), and data length.
  • ResponseApdu: prints SW1, SW2, and data length.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
Yubico.Core/src/Yubico/Core/Iso7816/ResponseApdu.cs Added ToString override to format SW1, SW2, and data length
Yubico.Core/src/Yubico/Core/Iso7816/CommandApdu.cs Added ToString override to format CLA, INS, P1, P2, Lc, Le, and data length
Comments suppressed due to low confidence (2)

Yubico.Core/src/Yubico/Core/Iso7816/CommandApdu.cs:331

  • [nitpick] Printing both Lc: {Nc} and Data: {Data.Length} may be redundant since Nc should equal Data.Length. Consider printing one or renaming Nc to Lc to reduce confusion.
        public override string ToString() => $"CLA: 0x{Cla:X2} INS: 0x{Ins:X2} P1: 0x{P1:X2} P2: 0x{P2:X2} Lc: {Nc} Le: {Ne} Data: {Data.Length} bytes";

Yubico.Core/src/Yubico/Core/Iso7816/ResponseApdu.cs:89

  • There are no existing tests verifying this new ToString output. Consider adding unit tests to validate the formatting for various APDU responses.
        public override string ToString() => $"SW1: 0x{SW1:X2} SW2: 0x{SW2:X2} Data: {Data.Span.Length} bytes";

@@ -85,5 +85,7 @@ public ResponseApdu(byte[] dataWithoutSW, short sw)
SW2 = (byte)(sw & 0xFF);
Data = dataWithoutSW.ToArray();
}

public override string ToString() => $"SW1: 0x{SW1:X2} SW2: 0x{SW2:X2} Data: {Data.Span.Length} bytes";
Copy link
Preview

Copilot AI Jul 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The use of Data.Span.Length here differs from Data.Length in CommandApdu. Consider unifying the approach (either always use Data.Length on the array or Data.Span.Length) for consistency across both classes.

Suggested change
public override string ToString() => $"SW1: 0x{SW1:X2} SW2: 0x{SW2:X2} Data: {Data.Span.Length} bytes";
public override string ToString() => $"SW1: 0x{SW1:X2} SW2: 0x{SW2:X2} Data: {Data.Length} bytes";

Copilot uses AI. Check for mistakes.

@@ -327,5 +327,7 @@ private byte[] GetLeField(ApduEncoding apduEncoding)

return leField;
}

public override string ToString() => $"CLA: 0x{Cla:X2} INS: 0x{Ins:X2} P1: 0x{P1:X2} P2: 0x{P2:X2} Lc: {Nc} Le: {Ne} Data: {Data.Length} bytes";
Copy link
Preview

Copilot AI Jul 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The current ToString uses spaces as separators, which can be hard to parse at a glance. Consider adding commas or another delimiter (e.g., CLA: ..., INS: ..., P1: ...) for better readability.

Suggested change
public override string ToString() => $"CLA: 0x{Cla:X2} INS: 0x{Ins:X2} P1: 0x{P1:X2} P2: 0x{P2:X2} Lc: {Nc} Le: {Ne} Data: {Data.Length} bytes";
public override string ToString() =>
$"CLA: 0x{Cla:X2}, INS: 0x{Ins:X2}, P1: 0x{P1:X2}, P2: 0x{P2:X2}, Lc: {Nc}, Le: {Ne}, Data: {Data.Length} bytes";

Copilot uses AI. Check for mistakes.

Copy link

github-actions bot commented Jul 1, 2025

Test Results: Windows

    2 files      2 suites   9s ⏱️
3 905 tests 3 905 ✅ 0 💤 0 ❌
3 907 runs  3 907 ✅ 0 💤 0 ❌

Results for commit d616a08.

Copy link

github-actions bot commented Jul 1, 2025

Test Results: Ubuntu

    2 files      2 suites   19s ⏱️
3 897 tests 3 897 ✅ 0 💤 0 ❌
3 899 runs  3 899 ✅ 0 💤 0 ❌

Results for commit d616a08.

Copy link

github-actions bot commented Jul 1, 2025

Test Results: MacOS

    2 files      2 suites   13s ⏱️
3 897 tests 3 897 ✅ 0 💤 0 ❌
3 899 runs  3 899 ✅ 0 💤 0 ❌

Results for commit d616a08.

Copy link

github-actions bot commented Jul 1, 2025

Code Coverage

Package Line Rate Branch Rate Complexity Health
Yubico.Core 40% 31% 4371
Yubico.YubiKey 51% 46% 20712
Summary 49% (35550 / 72561) 44% (8706 / 19851) 25083

Minimum allowed line rate is 40%

@DennisDyallo DennisDyallo requested a review from jdmoreira July 1, 2025 11:16
Copy link
Member

@jdmoreira jdmoreira left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏼

@equijano21
Copy link
Contributor

@DennisDyallo will this go out with the release tomorrow? If so, I'll add this to the release notes.

@DennisDyallo DennisDyallo merged commit 1beaf11 into develop Jul 3, 2025
12 checks passed
@DennisDyallo DennisDyallo deleted the dennisdyallo/apdu branch July 3, 2025 09:19
@DennisDyallo
Copy link
Collaborator Author

@equijano21 No, we'll save this for later :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

Successfully merging this pull request may close these issues.

3 participants