|
| 1 | +using Xunit; |
| 2 | +using Yubico.YubiKey.Otp; |
| 3 | + |
| 4 | +namespace Yubico.YubiKey.Pipelines; |
| 5 | + |
| 6 | +public class OtpErrorTransformTests |
| 7 | +{ |
| 8 | + [Theory] |
| 9 | + [InlineData(0, 1)] // First increment |
| 10 | + [InlineData(5, 6)] // Normal increment |
| 11 | + [InlineData(254, 255)] // Near byte boundary |
| 12 | + [InlineData(257, 258)] // Near byte boundary |
| 13 | + |
| 14 | + public void IsValidSequenceProgression_NormalIncrement_ReturnsTrue(int before, int after) |
| 15 | + { |
| 16 | + var status = CreateOtpStatus(touchLevel: 0xFF); // TouchLevel irrelevant |
| 17 | + |
| 18 | + bool isValid = OtpErrorTransform.IsValidSequenceProgression(before, after, status); |
| 19 | + |
| 20 | + Assert.True(isValid); |
| 21 | + } |
| 22 | + |
| 23 | + [Theory] |
| 24 | + [InlineData(0, 2)] // Skip increment |
| 25 | + [InlineData(5, 7)] // Jump by 2 |
| 26 | + [InlineData(10, 8)] // Backwards |
| 27 | + public void IsValidSequenceProgression_InvalidIncrement_ReturnsFalse(int before, int after) |
| 28 | + { |
| 29 | + var status = CreateOtpStatus(touchLevel: 0x00); // TouchLevel irrelevant |
| 30 | + |
| 31 | + bool isValid = OtpErrorTransform.IsValidSequenceProgression(before, after, status); |
| 32 | + |
| 33 | + Assert.False(isValid); |
| 34 | + } |
| 35 | + |
| 36 | + [Theory] |
| 37 | + [InlineData(1, 0x00)] // Config bits clear |
| 38 | + [InlineData(5, 0x20)] // Upper bits set, config clear |
| 39 | + [InlineData(255, 0xE0)] // Multiple upper bits, config clear |
| 40 | + public void IsValidSequenceProgression_ValidReset_ReturnsTrue(int before, byte touchLevel) |
| 41 | + { |
| 42 | + var status = CreateOtpStatus(touchLevel); |
| 43 | + |
| 44 | + bool isValid = OtpErrorTransform.IsValidSequenceProgression(before, 0, status); |
| 45 | + |
| 46 | + Assert.True(isValid); |
| 47 | + } |
| 48 | + |
| 49 | + [Theory] |
| 50 | + [InlineData(0, 0x00)] // Already zero (invalid before state) |
| 51 | + [InlineData(1, 0x01)] // Config bit set |
| 52 | + [InlineData(5, 0x1F)] // All config bits set |
| 53 | + [InlineData(5, 0x21)] // Config + upper bits set |
| 54 | + public void IsValidSequenceProgression_InvalidReset_ReturnsFalse(int before, byte touchLevel) |
| 55 | + { |
| 56 | + var status = CreateOtpStatus(touchLevel); |
| 57 | + |
| 58 | + bool isValid = OtpErrorTransform.IsValidSequenceProgression(before, 0, status); |
| 59 | + |
| 60 | + Assert.False(isValid); |
| 61 | + } |
| 62 | + |
| 63 | + private static OtpStatus CreateOtpStatus(byte touchLevel) => new() |
| 64 | + { |
| 65 | + FirmwareVersion = new FirmwareVersion(), |
| 66 | + SequenceNumber = 0, |
| 67 | + TouchLevel = touchLevel, |
| 68 | + ShortPressConfigured = false, |
| 69 | + LongPressConfigured = false, |
| 70 | + ShortPressRequiresTouch = false, |
| 71 | + LongPressRequiresTouch = false, |
| 72 | + LedBehaviorInverted = false |
| 73 | + }; |
| 74 | +} |
0 commit comments