Skip to content

Commit

Permalink
More use of u8
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub committed Apr 18, 2022
1 parent 7681a69 commit 9fc19b8
Show file tree
Hide file tree
Showing 16 changed files with 105 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,14 @@ public class HPackDecoderTests
private const string _userAgentString = "user-agent";

private const string _headerNameString = "new-header";

private static readonly byte[] _headerNameBytes = Encoding.ASCII.GetBytes(_headerNameString);
private static readonly byte[] _headerNameBytes = "new-header"u8;

// n e w - h e a d e r *
// 10101000 10111110 00010110 10011100 10100011 10010000 10110110 01111111
private static readonly byte[] _headerNameHuffmanBytes = new byte[] { 0xa8, 0xbe, 0x16, 0x9c, 0xa3, 0x90, 0xb6, 0x7f };

private const string _headerValueString = "value";

private static readonly byte[] _headerValueBytes = Encoding.ASCII.GetBytes(_headerValueString);
private static readonly byte[] _headerValueBytes = "value"u8;

// v a l u e *
// 11101110 00111010 00101101 00101111
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public class QPackDecoderTests
private static readonly byte[] _headerNameHuffmanBytes = new byte[] { 0xa8, 0xbe, 0x16, 0x9c, 0xa3, 0x90, 0xb6, 0x7f };

private const string _headerNameString = "new-header";
private const string _headerValueString = "value";

private static readonly byte[] _headerValueBytes = Encoding.ASCII.GetBytes(_headerValueString);
private const string _headerValueString = "value";
private static readonly byte[] _headerValueBytes = "value"u8;

// v a l u e *
// 11101110 00111010 00101101 00101111
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,13 +410,13 @@ protected override Stream CreateStream()

protected override Stream GetSmallStream()
{
byte[] testData = new byte[] { 72, 69, 76, 76, 79 };
byte[] testData = "HELLO"u8;
return new BufferedStream(new MemoryStream(testData));
}

protected override Stream GetLargeStream()
{
byte[] testData = new byte[] { 72, 69, 76, 76, 79 };
byte[] testData = "HELLO"u8;
List<byte> data = new List<byte>();
for (int i = 0; i < 1000; i++)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static void InputStreamClosed()
public static void CreationFromMemoryStreamWithEncodingFalse()
{
var ms2 = new MemoryStream();
ms2.Write(new byte[] { 65, 66, 67, 68 }, 0, 4);
ms2.Write("ABCD"u8, 0, 4);
ms2.Position = 0;
var sr2 = new StreamReader(ms2, false);

Expand All @@ -43,7 +43,7 @@ public static void CreationFromMemoryStreamWithEncodingFalse()
public static void CreationFromMemoryStreamWithEncodingTrue()
{
var ms2 = new MemoryStream();
ms2.Write(new byte[] { 65, 66, 67, 68 }, 0, 4);
ms2.Write("ABCD"u8, 0, 4);
ms2.Position = 0;
var sr2 = new StreamReader(ms2, true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static void ReadToEnd_detectEncodingFromByteOrderMarks(bool detectEncodin
string testfile = Path.GetTempFileName();
try
{
File.WriteAllBytes(testfile, new byte[] { 65, 66, 67, 68 });
File.WriteAllBytes(testfile, "ABCD"u8);
using (var sr2 = new StreamReader(testfile, detectEncodingFromByteOrderMarks))
{
Assert.Equal("ABCD", sr2.ReadToEnd());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ protected virtual Stream CreateStream()

protected virtual Stream GetSmallStream()
{
byte[] testData = new byte[] { 72, 69, 76, 76, 79 };
byte[] testData = "HELLO"u8;
return new MemoryStream(testData);
}

protected virtual Stream GetLargeStream()
{
byte[] testData = new byte[] { 72, 69, 76, 76, 79 };
// System.Collections.Generic.
byte[] testData = "HELLO"u8;

List<byte> data = new List<byte>();
for (int i = 0; i < 1000; i++)
Expand Down
28 changes: 14 additions & 14 deletions src/libraries/System.Memory/tests/Base64/Base64DecoderUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ public void DecodingInvalidBytes(bool isFinalBlock)

for (int j = 0; j < 8; j++)
{
Span<byte> source = new byte[8] { 50, 50, 50, 50, 80, 80, 80, 80 }; // valid input - "2222PPPP"
Span<byte> source = "2222PPPP"u8; // valid input
Span<byte> decodedBytes = new byte[Base64.GetMaxDecodedFromUtf8Length(source.Length)];

for (int i = 0; i < invalidBytes.Length; i++)
Expand Down Expand Up @@ -368,7 +368,7 @@ public void DecodingInvalidBytes(bool isFinalBlock)
// Input that is not a multiple of 4 is considered invalid, if isFinalBlock = true
if (isFinalBlock)
{
Span<byte> source = new byte[7] { 50, 50, 50, 50, 80, 80, 80 }; // incomplete input - "2222PPP"
Span<byte> source = "2222PPP"u8; // incomplete input
Span<byte> decodedBytes = new byte[Base64.GetMaxDecodedFromUtf8Length(source.Length)];
Assert.Equal(OperationStatus.InvalidData, Base64.DecodeFromUtf8(source, decodedBytes, out int consumed, out int decodedByteCount));
Assert.Equal(4, consumed);
Expand All @@ -385,7 +385,7 @@ public void DecodingInvalidBytesPadding(bool isFinalBlock)
// Only last 2 bytes can be padding, all other occurrence of padding is invalid
for (int j = 0; j < 7; j++)
{
Span<byte> source = new byte[] { 50, 50, 50, 50, 80, 80, 80, 80 }; // valid input - "2222PPPP"
Span<byte> source = "2222PPPP"u8; // valid input
Span<byte> decodedBytes = new byte[Base64.GetMaxDecodedFromUtf8Length(source.Length)];
source[j] = Base64TestHelper.EncodingPad;
Assert.Equal(OperationStatus.InvalidData, Base64.DecodeFromUtf8(source, decodedBytes, out int consumed, out int decodedByteCount, isFinalBlock));
Expand All @@ -405,7 +405,7 @@ public void DecodingInvalidBytesPadding(bool isFinalBlock)

// Invalid input with valid padding
{
Span<byte> source = new byte[] { 50, 50, 50, 50, 80, 42, 42, 42 };
Span<byte> source = "2222P***"u8;
Span<byte> decodedBytes = new byte[Base64.GetMaxDecodedFromUtf8Length(source.Length)];
source[6] = Base64TestHelper.EncodingPad;
source[7] = Base64TestHelper.EncodingPad; // invalid input - "2222P*=="
Expand All @@ -415,7 +415,7 @@ public void DecodingInvalidBytesPadding(bool isFinalBlock)
Assert.Equal(3, decodedByteCount);
Assert.True(Base64TestHelper.VerifyDecodingCorrectness(4, 3, source, decodedBytes));

source = new byte[] { 50, 50, 50, 50, 80, 42, 42, 42 };
source = "2222P***"u8;
decodedBytes = new byte[Base64.GetMaxDecodedFromUtf8Length(source.Length)];
source[7] = Base64TestHelper.EncodingPad; // invalid input - "2222PP**="
Assert.Equal(OperationStatus.InvalidData, Base64.DecodeFromUtf8(source, decodedBytes, out consumed, out decodedByteCount, isFinalBlock));
Expand All @@ -427,7 +427,7 @@ public void DecodingInvalidBytesPadding(bool isFinalBlock)

// The last byte or the last 2 bytes being the padding character is valid, if isFinalBlock = true
{
Span<byte> source = new byte[] { 50, 50, 50, 50, 80, 80, 80, 80 };
Span<byte> source = "2222PPPP"u8;
Span<byte> decodedBytes = new byte[Base64.GetMaxDecodedFromUtf8Length(source.Length)];
source[6] = Base64TestHelper.EncodingPad;
source[7] = Base64TestHelper.EncodingPad; // valid input - "2222PP=="
Expand All @@ -441,7 +441,7 @@ public void DecodingInvalidBytesPadding(bool isFinalBlock)
Assert.Equal(expectedWritten, decodedByteCount);
Assert.True(Base64TestHelper.VerifyDecodingCorrectness(expectedConsumed, expectedWritten, source, decodedBytes));

source = new byte[] { 50, 50, 50, 50, 80, 80, 80, 80 };
source = "2222PPPP"u8;
decodedBytes = new byte[Base64.GetMaxDecodedFromUtf8Length(source.Length)];
source[7] = Base64TestHelper.EncodingPad; // valid input - "2222PPP="

Expand Down Expand Up @@ -535,7 +535,7 @@ public void DecodeInPlaceInvalidBytes()
{
for (int i = 0; i < invalidBytes.Length; i++)
{
Span<byte> buffer = new byte[8] { 50, 50, 50, 50, 80, 80, 80, 80 }; // valid input - "2222PPPP"
Span<byte> buffer = "2222PPPP"u8; // valid input

// Don't test padding (byte 61 i.e. '='), which is tested in DecodeInPlaceInvalidBytesPadding
if (invalidBytes[i] == Base64TestHelper.EncodingPad)
Expand All @@ -562,7 +562,7 @@ public void DecodeInPlaceInvalidBytes()

// Input that is not a multiple of 4 is considered invalid
{
Span<byte> buffer = new byte[7] { 50, 50, 50, 50, 80, 80, 80 }; // incomplete input - "2222PPP"
Span<byte> buffer = "2222PPP"u8; // incomplete input
Assert.Equal(OperationStatus.InvalidData, Base64.DecodeFromUtf8InPlace(buffer, out int bytesWritten));
Assert.Equal(0, bytesWritten);
}
Expand All @@ -574,7 +574,7 @@ public void DecodeInPlaceInvalidBytesPadding()
// Only last 2 bytes can be padding, all other occurrence of padding is invalid
for (int j = 0; j < 7; j++)
{
Span<byte> buffer = new byte[] { 50, 50, 50, 50, 80, 80, 80, 80 }; // valid input - "2222PPPP"
Span<byte> buffer = "2222PPPP"u8; // valid input
buffer[j] = Base64TestHelper.EncodingPad;
string sourceString = Encoding.ASCII.GetString(buffer.Slice(0, 4).ToArray());

Expand All @@ -594,7 +594,7 @@ public void DecodeInPlaceInvalidBytesPadding()

// Invalid input with valid padding
{
Span<byte> buffer = new byte[] { 50, 50, 50, 50, 80, 42, 42, 42 };
Span<byte> buffer = "2222P***"u8;
buffer[6] = Base64TestHelper.EncodingPad;
buffer[7] = Base64TestHelper.EncodingPad; // invalid input - "2222P*=="
string sourceString = Encoding.ASCII.GetString(buffer.Slice(0, 4).ToArray());
Expand All @@ -605,7 +605,7 @@ public void DecodeInPlaceInvalidBytesPadding()
}

{
Span<byte> buffer = new byte[] { 50, 50, 50, 50, 80, 42, 42, 42 };
Span<byte> buffer = "2222P***"u8;
buffer[7] = Base64TestHelper.EncodingPad; // invalid input - "2222P**="
string sourceString = Encoding.ASCII.GetString(buffer.Slice(0, 4).ToArray());
Assert.Equal(OperationStatus.InvalidData, Base64.DecodeFromUtf8InPlace(buffer, out int bytesWritten));
Expand All @@ -616,7 +616,7 @@ public void DecodeInPlaceInvalidBytesPadding()

// The last byte or the last 2 bytes being the padding character is valid
{
Span<byte> buffer = new byte[] { 50, 50, 50, 50, 80, 80, 80, 80 };
Span<byte> buffer = "2222PPPP"u8;
buffer[6] = Base64TestHelper.EncodingPad;
buffer[7] = Base64TestHelper.EncodingPad; // valid input - "2222PP=="
string sourceString = Encoding.ASCII.GetString(buffer.ToArray());
Expand All @@ -627,7 +627,7 @@ public void DecodeInPlaceInvalidBytesPadding()
}

{
Span<byte> buffer = new byte[] { 50, 50, 50, 50, 80, 80, 80, 80 };
Span<byte> buffer = "2222PPPP"u8;
buffer[7] = Base64TestHelper.EncodingPad; // valid input - "2222PPP="
string sourceString = Encoding.ASCII.GetString(buffer.ToArray());
Assert.Equal(OperationStatus.Done, Base64.DecodeFromUtf8InPlace(buffer, out int bytesWritten));
Expand Down
12 changes: 6 additions & 6 deletions src/libraries/System.Memory/tests/SequenceReader/ReadTo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,18 +173,18 @@ public void TryReadToSpan_Sequence(bool advancePastDelimiter)
}

bytes = SequenceFactory.Create(new byte[][] {
new byte[] { 47, 42, 66, 32, 42, 32, 66, 42, 47 } // /*b * b*/
"/*B * B*/"u8
});

baseReader = new SequenceReader<byte>(bytes);
SequenceReader<byte> copyReader = baseReader;

Assert.True(copyReader.TryReadTo(out ReadOnlySpan<byte> span, new byte[] { 42, 47 }, advancePastDelimiter)); // */
Assert.True(span.SequenceEqual(new byte[] { 47, 42, 66, 32, 42, 32, 66 }));
Assert.True(copyReader.TryReadTo(out ReadOnlySpan<byte> span, "*/"u8, advancePastDelimiter)); // */
Assert.True(span.SequenceEqual("/*B * B"u8));

copyReader = baseReader;
Assert.True(copyReader.TryReadTo(out ReadOnlySequence<byte> sequence, new byte[] { 42, 47 }, advancePastDelimiter)); // */
Assert.True(sequence.ToArray().AsSpan().SequenceEqual(new byte[] { 47, 42, 66, 32, 42, 32, 66 }));
Assert.True(copyReader.TryReadTo(out ReadOnlySequence<byte> sequence, "*/"u8, advancePastDelimiter)); // */
Assert.True(sequence.ToArray().AsSpan().SequenceEqual("/*B * B"u8));
}

[Theory,
Expand Down Expand Up @@ -258,7 +258,7 @@ public void TryReadTo_SingleDelimiter()
[Fact]
public void TryReadTo_Span_At_Segments_Boundary()
{
Span<byte> delimiter = new byte[] { 13, 10 }; // \r\n
Span<byte> delimiter = "\r\n"u8;
BufferSegment<byte> segment = new BufferSegment<byte>("Hello\r"u8);
segment.Append("\nWorld"u8); // add next segment
ReadOnlySequence<byte> inputSeq = new ReadOnlySequence<byte>(segment, 0, segment, 6); // span only the first segment!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ namespace System.Net.Http
{
internal sealed class WinHttpRequestStream : Stream
{
private static readonly byte[] s_crLfTerminator = new byte[] { 0x0d, 0x0a }; // "\r\n"
private static readonly byte[] s_endChunk = new byte[] { 0x30, 0x0d, 0x0a, 0x0d, 0x0a }; // "0\r\n\r\n"
private static readonly byte[] s_crLfTerminator = "\r\n"u8;
private static readonly byte[] s_endChunk = "0\r\n\r\n"u8;

private volatile bool _disposed;
private readonly WinHttpRequestState _state;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ public static IEnumerable<object[]> HeaderEncodingTestData()
yield return new object[] { ":path", "/", new byte[] { 0x84 } };

// Indexed name, literal value.
yield return new object[] { "content-type", "text/plain; charset=utf-8", new byte[] { 0x0F, 0x10, 0x19, 0x74, 0x65, 0x78, 0x74, 0x2F, 0x70, 0x6C, 0x61, 0x69, 0x6E, 0x3B, 0x20, 0x63, 0x68, 0x61, 0x72, 0x73, 0x65, 0x74, 0x3D, 0x75, 0x74, 0x66, 0x2D, 0x38 } };
yield return new object[] { "content-type", "text/plain; charset=utf-8", "\u000f\u0010\u0019text/plain; charset=utf-8"u8 };

// Literal name, literal value.
yield return new object[] { LiteralHeaderName, LiteralHeaderValue, new byte[] { 0x00, 0x10, 0x78, 0x2D, 0x6C, 0x69, 0x74, 0x65, 0x72, 0x61, 0x6C, 0x2D, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x0B, 0x74, 0x65, 0x73, 0x74, 0x69, 0x6E, 0x67, 0x20, 0x34, 0x35, 0x36 } };
yield return new object[] { LiteralHeaderName, LiteralHeaderValue, "\0\u0010x-literal-header\vtesting 456"u8 };
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ internal async Task WriteWebSocketHandshakeHeadersAsync()
}
}

private static byte[] s_crlf = new byte[] { 13, 10 };
private static byte[] s_crlf = "\r\n"u8;
private static byte[] GetChunkSizeBytes(int size, bool final) =>
Encoding.ASCII.GetBytes($"{size:x}\r\n{(final ? "\r\n" : "")}");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ namespace System.Net.Security
public readonly struct SslApplicationProtocol : IEquatable<SslApplicationProtocol>
{
private static readonly Encoding s_utf8 = Encoding.GetEncoding(Encoding.UTF8.CodePage, EncoderFallback.ExceptionFallback, DecoderFallback.ExceptionFallback);
private static readonly byte[] s_http3Utf8 = new byte[] { 0x68, 0x33 }; // "h3"
private static readonly byte[] s_http2Utf8 = new byte[] { 0x68, 0x32 }; // "h2"
private static readonly byte[] s_http11Utf8 = new byte[] { 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31 }; // "http/1.1"
private static readonly byte[] s_http3Utf8 = "h3"u8;
private static readonly byte[] s_http2Utf8 = "h2"u8;
private static readonly byte[] s_http11Utf8 = "http/1.1"u8;

// Refer to IANA on ApplicationProtocols: https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml#alpn-protocol-ids
/// <summary>Defines a <see cref="SslApplicationProtocol"/> instance for HTTP 3.0.</summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,16 @@ public class SslApplicationProtocolTests
[Fact]
public void Constants_Values_AreCorrect()
{
Assert.Equal(new SslApplicationProtocol(new byte[] { 0x68, 0x33 }), SslApplicationProtocol.Http3);
Assert.Equal(new SslApplicationProtocol(new byte[] { 0x68, 0x32 }), SslApplicationProtocol.Http2);
Assert.Equal(new SslApplicationProtocol(new byte[] { 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31 }), SslApplicationProtocol.Http11);
Assert.Equal(new SslApplicationProtocol("h3"u8), SslApplicationProtocol.Http3);
Assert.Equal(new SslApplicationProtocol("h2"u8), SslApplicationProtocol.Http2);
Assert.Equal(new SslApplicationProtocol("http/1.1"u8), SslApplicationProtocol.Http11);
}

[Fact]
public void Constructor_Overloads_Succeeds()
{
const string hello = "hello";
byte[] expected = Encoding.UTF8.GetBytes(hello);
SslApplicationProtocol byteProtocol = new SslApplicationProtocol(expected);
SslApplicationProtocol stringProtocol = new SslApplicationProtocol(hello);
SslApplicationProtocol byteProtocol = new SslApplicationProtocol("hello"u8);
SslApplicationProtocol stringProtocol = new SslApplicationProtocol("hello");
Assert.Equal(byteProtocol, stringProtocol);

SslApplicationProtocol defaultProtocol = default;
Expand Down Expand Up @@ -95,7 +93,7 @@ public static IEnumerable<object[]> Protocol_Equality_TestData()
public static IEnumerable<object[]> Protocol_InEquality_TestData()
{
yield return new object[] { new SslApplicationProtocol("hello"), new SslApplicationProtocol("world") };
yield return new object[] { new SslApplicationProtocol(new byte[] { 0x42 }), new SslApplicationProtocol(new byte[] { 0x52, 0x62 }) };
yield return new object[] { new SslApplicationProtocol(new byte[] { 0x42 }), new SslApplicationProtocol("Rb"u8) };
yield return new object[] { null, new SslApplicationProtocol(new byte[] { 0x42 }) };
yield return new object[] { new SslApplicationProtocol(new byte[] { 0x42 }), null };
}
Expand Down
Loading

0 comments on commit 9fc19b8

Please sign in to comment.