|
1 | | -using System.Text; |
| 1 | +using System.Buffers; |
| 2 | +using System.Text; |
2 | 3 | using Xunit; |
3 | 4 |
|
4 | 5 | namespace System.Tests |
@@ -34,93 +35,124 @@ public static void CompleteValueRange() |
34 | 35 |
|
35 | 36 | private static void TestSequence(byte[] expected, string actual) |
36 | 37 | { |
37 | | - Assert.Equal(expected, Convert.FromHexString(actual)); |
| 38 | + byte[] fromResult = Convert.FromHexString(actual); |
| 39 | + Assert.Equal(expected, fromResult); |
| 40 | + |
| 41 | + Span<byte> tryResult = new byte[actual.Length / 2]; |
| 42 | + Assert.Equal(OperationStatus.Done, Convert.FromHexString(actual, tryResult, out int consumed, out int written)); |
| 43 | + Assert.Equal(fromResult.Length, written); |
| 44 | + Assert.Equal(actual.Length, consumed); |
| 45 | + AssertExtensions.SequenceEqual(expected, tryResult); |
38 | 46 | } |
39 | 47 |
|
40 | 48 | [Fact] |
41 | 49 | public static void InvalidInputString_Null() |
42 | 50 | { |
43 | 51 | AssertExtensions.Throws<ArgumentNullException>("s", () => Convert.FromHexString(null)); |
| 52 | + AssertExtensions.Throws<ArgumentNullException>("source", () => Convert.FromHexString(null, default, out _, out _)); |
44 | 53 | } |
45 | 54 |
|
46 | | - [Fact] |
47 | | - public static void InvalidInputString_HalfByte() |
| 55 | + [Theory] |
| 56 | + [InlineData("01-02-FD-FE-FF")] |
| 57 | + [InlineData("00 01 02FD FE FF")] |
| 58 | + [InlineData("000102FDFEFF ")] |
| 59 | + [InlineData(" 000102FDFEFF")] |
| 60 | + [InlineData("\u200B 000102FDFEFF")] |
| 61 | + [InlineData("0\u0308")] |
| 62 | + [InlineData("0x")] |
| 63 | + [InlineData("x0")] |
| 64 | + public static void InvalidInputString_FormatException_Or_FalseResult(string invalidInput) |
48 | 65 | { |
49 | | - Assert.Throws<FormatException>(() => Convert.FromHexString("ABC")); |
50 | | - } |
| 66 | + Assert.Throws<FormatException>(() => Convert.FromHexString(invalidInput)); |
51 | 67 |
|
52 | | - [Fact] |
53 | | - public static void InvalidInputString_BadFirstCharacter() |
54 | | - { |
55 | | - Assert.Throws<FormatException>(() => Convert.FromHexString("x0")); |
| 68 | + Span<byte> buffer = stackalloc byte[invalidInput.Length / 2]; |
| 69 | + Assert.Equal(OperationStatus.InvalidData, Convert.FromHexString(invalidInput.AsSpan(), buffer, out _, out _)); |
56 | 70 | } |
57 | 71 |
|
58 | 72 | [Fact] |
59 | | - public static void InvalidInputString_BadSecondCharacter() |
| 73 | + public static void ZeroLength() |
60 | 74 | { |
61 | | - Assert.Throws<FormatException>(() => Convert.FromHexString("0x")); |
62 | | - } |
| 75 | + Assert.Same(Array.Empty<byte>(), Convert.FromHexString(string.Empty)); |
63 | 76 |
|
64 | | - [Fact] |
65 | | - public static void InvalidInputString_NonAsciiCharacter() |
66 | | - { |
67 | | - Assert.Throws<FormatException>(() => Convert.FromHexString("0\u0308")); |
68 | | - } |
| 77 | + OperationStatus convertResult = Convert.FromHexString(string.Empty, Span<byte>.Empty, out int consumed, out int written); |
69 | 78 |
|
70 | | - [Fact] |
71 | | - public static void InvalidInputString_ZeroWidthSpace() |
72 | | - { |
73 | | - Assert.Throws<FormatException>(() => Convert.FromHexString("\u200B 000102FDFEFF")); |
| 79 | + Assert.Equal(OperationStatus.Done, convertResult); |
| 80 | + Assert.Equal(0, written); |
| 81 | + Assert.Equal(0, consumed); |
74 | 82 | } |
75 | 83 |
|
76 | 84 | [Fact] |
77 | | - public static void InvalidInputString_LeadingWhiteSpace() |
| 85 | + public static void ToHexFromHexRoundtrip() |
78 | 86 | { |
79 | | - Assert.Throws<FormatException>(() => Convert.FromHexString(" 000102FDFEFF")); |
80 | | - } |
| 87 | + const int loopCount = 50; |
| 88 | + Span<char> buffer = stackalloc char[loopCount * 2]; |
| 89 | + for (int i = 1; i < loopCount; i++) |
| 90 | + { |
| 91 | + byte[] data = Security.Cryptography.RandomNumberGenerator.GetBytes(i); |
| 92 | + string hex = Convert.ToHexString(data); |
81 | 93 |
|
82 | | - [Fact] |
83 | | - public static void InvalidInputString_TrailingWhiteSpace() |
84 | | - { |
85 | | - Assert.Throws<FormatException>(() => Convert.FromHexString("000102FDFEFF ")); |
86 | | - } |
| 94 | + Span<char> currentBuffer = buffer.Slice(0, i * 2); |
| 95 | + bool tryHex = Convert.TryToHexString(data, currentBuffer, out int written); |
| 96 | + Assert.True(tryHex); |
| 97 | + AssertExtensions.SequenceEqual(hex.AsSpan(), currentBuffer); |
| 98 | + Assert.Equal(hex.Length, written); |
87 | 99 |
|
88 | | - [Fact] |
89 | | - public static void InvalidInputString_WhiteSpace() |
90 | | - { |
91 | | - Assert.Throws<FormatException>(() => Convert.FromHexString("00 01 02FD FE FF")); |
92 | | - } |
| 100 | + TestSequence(data, hex); |
| 101 | + TestSequence(data, hex.ToLowerInvariant()); |
| 102 | + TestSequence(data, hex.ToUpperInvariant()); |
93 | 103 |
|
94 | | - [Fact] |
95 | | - public static void InvalidInputString_Dash() |
96 | | - { |
97 | | - Assert.Throws<FormatException>(() => Convert.FromHexString("01-02-FD-FE-FF")); |
| 104 | + string mixedCase1 = hex.Substring(0, hex.Length / 2).ToUpperInvariant() + |
| 105 | + hex.Substring(hex.Length / 2).ToLowerInvariant(); |
| 106 | + string mixedCase2 = hex.Substring(0, hex.Length / 2).ToLowerInvariant() + |
| 107 | + hex.Substring(hex.Length / 2).ToUpperInvariant(); |
| 108 | + |
| 109 | + TestSequence(data, mixedCase1); |
| 110 | + TestSequence(data, mixedCase2); |
| 111 | + |
| 112 | + Assert.Throws<FormatException>(() => Convert.FromHexString(hex + " ")); |
| 113 | + Assert.Throws<FormatException>(() => Convert.FromHexString("\uAAAA" + hex)); |
| 114 | + } |
98 | 115 | } |
99 | 116 |
|
100 | 117 | [Fact] |
101 | | - public static void ZeroLength() |
| 118 | + public static void TooShortDestination() |
102 | 119 | { |
103 | | - Assert.Same(Array.Empty<byte>(), Convert.FromHexString(string.Empty)); |
| 120 | + const int destinationSize = 10; |
| 121 | + Span<byte> destination = stackalloc byte[destinationSize]; |
| 122 | + byte[] data = Security.Cryptography.RandomNumberGenerator.GetBytes(destinationSize * 2 + 1); |
| 123 | + string hex = Convert.ToHexString(data); |
| 124 | + |
| 125 | + OperationStatus result = Convert.FromHexString(hex, destination, out int charsConsumed, out int bytesWritten); |
| 126 | + |
| 127 | + Assert.Equal(OperationStatus.DestinationTooSmall, result); |
| 128 | + Assert.Equal(destinationSize * 2, charsConsumed); |
| 129 | + Assert.Equal(destinationSize, bytesWritten); |
104 | 130 | } |
105 | 131 |
|
106 | 132 | [Fact] |
107 | | - public static void ToHexFromHexRoundtrip() |
| 133 | + public static void NeedMoreData_OrFormatException() |
108 | 134 | { |
109 | | - for (int i = 1; i < 50; i++) |
110 | | - { |
111 | | - byte[] data = System.Security.Cryptography.RandomNumberGenerator.GetBytes(i); |
112 | | - string hex = Convert.ToHexString(data); |
113 | | - Assert.Equal(data, Convert.FromHexString(hex.ToLowerInvariant())); |
114 | | - Assert.Equal(data, Convert.FromHexString(hex.ToUpperInvariant())); |
115 | | - string mixedCase1 = hex.Substring(0, hex.Length / 2).ToUpperInvariant() + |
116 | | - hex.Substring(hex.Length / 2).ToLowerInvariant(); |
117 | | - string mixedCase2 = hex.Substring(0, hex.Length / 2).ToLowerInvariant() + |
118 | | - hex.Substring(hex.Length / 2).ToUpperInvariant(); |
119 | | - Assert.Equal(data, Convert.FromHexString(mixedCase1)); |
120 | | - Assert.Equal(data, Convert.FromHexString(mixedCase2)); |
121 | | - Assert.Throws<FormatException>(() => Convert.FromHexString(hex + " ")); |
122 | | - Assert.Throws<FormatException>(() => Convert.FromHexString("\uAAAA" + hex)); |
123 | | - } |
| 135 | + const int destinationSize = 10; |
| 136 | + byte[] data = Security.Cryptography.RandomNumberGenerator.GetBytes(destinationSize); |
| 137 | + Span<byte> destination = stackalloc byte[destinationSize]; |
| 138 | + var hex = Convert.ToHexString(data); |
| 139 | + |
| 140 | + var spanHex = hex.AsSpan(0, 1); |
| 141 | + var singeResult = Convert.FromHexString(spanHex, destination, out int consumed, out int written); |
| 142 | + |
| 143 | + Assert.Throws<FormatException>(() => Convert.FromHexString(hex.Substring(0, 1))); |
| 144 | + Assert.Equal(OperationStatus.NeedMoreData, singeResult); |
| 145 | + Assert.Equal(0, consumed); |
| 146 | + Assert.Equal(0, written); |
| 147 | + |
| 148 | + spanHex = hex.AsSpan(0, hex.Length - 1); |
| 149 | + |
| 150 | + var oneOffResult = Convert.FromHexString(spanHex, destination, out consumed, out written); |
| 151 | + |
| 152 | + Assert.Throws<FormatException>(() => Convert.FromHexString(hex.Substring(0, hex.Length - 1))); |
| 153 | + Assert.Equal(OperationStatus.NeedMoreData, oneOffResult); |
| 154 | + Assert.Equal(spanHex.Length - 1, consumed); |
| 155 | + Assert.Equal((spanHex.Length - 1) / 2, written); |
124 | 156 | } |
125 | 157 | } |
126 | 158 | } |
0 commit comments