Skip to content

Commit 0c4ee9e

Browse files
authored
Use u8 in more places (#69995)
* Use u8 in more places Primarily replacing Encoding.UTF8/ASCII.GetBytes("literal") with "literal"u8.ToArray() * Address PR feedback (and fix some vars)
1 parent 9f654ce commit 0c4ee9e

File tree

138 files changed

+522
-578
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

138 files changed

+522
-578
lines changed

src/libraries/Common/tests/StreamConformanceTests/System/IO/StreamConformanceTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,7 +1855,7 @@ public virtual async Task Read_Eof_Returns0(ReadWriteMode mode, bool dataAvailab
18551855
{
18561856
write = Task.Run(async () =>
18571857
{
1858-
await writeable.WriteAsync(Encoding.UTF8.GetBytes("hello"));
1858+
await writeable.WriteAsync("hello"u8.ToArray());
18591859
await writeable.DisposeAsync();
18601860
});
18611861
}
@@ -2206,7 +2206,7 @@ public virtual async Task ZeroByteRead_BlocksUntilDataAvailableOrNops(ReadWriteM
22062206

22072207
Task write = Task.Run(async () =>
22082208
{
2209-
await writeable.WriteAsync(Encoding.UTF8.GetBytes("hello"));
2209+
await writeable.WriteAsync("hello"u8.ToArray());
22102210
if (FlushRequiredToWriteData)
22112211
{
22122212
if (FlushGuaranteesAllDataWritten)
@@ -2261,7 +2261,7 @@ public virtual async Task ZeroByteRead_BlocksUntilDataAvailableOrNops(ReadWriteM
22612261
[SkipOnPlatform(TestPlatforms.LinuxBionic, "SElinux blocks UNIX sockets")]
22622262
public virtual async Task ZeroByteWrite_OtherDataReceivedSuccessfully(ReadWriteMode mode)
22632263
{
2264-
byte[][] buffers = new[] { Array.Empty<byte>(), Encoding.UTF8.GetBytes("hello"), Array.Empty<byte>(), Encoding.UTF8.GetBytes("world") };
2264+
byte[][] buffers = new[] { Array.Empty<byte>(), "hello"u8.ToArray(), Array.Empty<byte>(), "world"u8.ToArray() };
22652265

22662266
using StreamPair streams = await CreateConnectedStreamsAsync();
22672267
foreach ((Stream writeable, Stream readable) in GetReadWritePairs(streams))
@@ -2915,7 +2915,7 @@ public virtual async Task ZeroByteRead_PerformsZeroByteReadOnUnderlyingStreamWhe
29152915
// (a) produce at least two readable bytes, so we can unblock the reader and read a single byte without clearing its buffer; and
29162916
// (b) produce no more than 1K of readable bytes, so we can clear the reader buffer below.
29172917
// If this isn't the case for some Stream(s), we can modify the data or parameterize it per Stream.
2918-
byte[] data = Encoding.UTF8.GetBytes("hello world");
2918+
byte[] data = "hello world"u8.ToArray();
29192919

29202920
using StreamPair innerStreams = ConnectedStreams.CreateBidirectional();
29212921
(Stream innerWriteable, Stream innerReadable) = GetReadWritePair(innerStreams);

src/libraries/Common/tests/System/Net/Http/Http2LoopbackConnection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ private async Task ReadPrefixAsync()
100100
// Since those tests are not set up to handle multiple retries, we instead just send back an invalid response here
101101
// so that SocketsHttpHandler will not induce retry.
102102
// The contents of what we send don't really matter, as long as it is interpreted by SocketsHttpHandler as an invalid response.
103-
await _connectionStream.WriteAsync(Encoding.ASCII.GetBytes("HTTP/2.0 400 Bad Request\r\n\r\n"));
103+
await _connectionStream.WriteAsync("HTTP/2.0 400 Bad Request\r\n\r\n"u8.ToArray());
104104
_connectionSocket.Shutdown(SocketShutdown.Send);
105105
// If WinHTTP doesn't support streaming a request without a length then it will fallback
106106
// to HTTP/1.1. Throwing an exception to detect this case in WinHttpHandler tests.

src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/DSA/DSAKeyFileTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public static void ReadWriteDsa1024EncryptedPkcs8_PasswordBytes()
182182
qE3Db1UI4anCCnyEj/jDA8R6hZTFDjxu6bG0Z66g7I2GBDEYaaB+8x0vtiyu5LXo
183183
6UZ53SX6S+jfIqJoF5YME9zVMoO2kwS/EGvc64+epCGcee1Nx4SGgUcr5HJYz1P4
184184
CU+l4wPQR0rRmYHIJJIvFh5OXk84pV0crsOrekw7tHeNU6DMzw==",
185-
Encoding.UTF8.GetBytes("Password > cipher"),
185+
"Password > cipher"u8.ToArray(),
186186
new PbeParameters(
187187
PbeEncryptionAlgorithm.Aes192Cbc,
188188
HashAlgorithmName.SHA256,

src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/DSA/DSAKeyPemTests.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ public static void ImportFromEncryptedPem_Pkcs8_Encrypted_Byte_Simple()
287287
xTL6VO9mx52x6h5WDAQAisMVeMkBoxQUWLANXiw1zSfVbsmB7mDknsRcvD3tcgMs
288288
7YLD7LQMiPAIjDlOP8XP/w==
289289
-----END ENCRYPTED PRIVATE KEY-----";
290-
byte[] passwordBytes = Encoding.UTF8.GetBytes("test");
290+
byte[] passwordBytes = "test"u8.ToArray();
291291
dsa.ImportFromEncryptedPem(pem, passwordBytes);
292292
DSAParameters dsaParameters = dsa.ExportParameters(true);
293293

@@ -319,9 +319,8 @@ public static void ImportFromEncryptedPem_Pkcs8_Encrypted_AmbiguousPem()
319319
dVYwfovccu8ktEAwk5XAOo0r+5CCw2lDDw/hbDeO87BToC5Cc5nu3F5LxAUj8Flc
320320
v8pi3w==
321321
-----END ENCRYPTED PRIVATE KEY-----";
322-
byte[] passwordBytes = Encoding.UTF8.GetBytes("test");
323322
ArgumentException ae = AssertExtensions.Throws<ArgumentException>("input", () =>
324-
dsa.ImportFromEncryptedPem(pem, passwordBytes));
323+
dsa.ImportFromEncryptedPem(pem, "test"u8));
325324
Assert.Contains(AmbiguousExceptionMarker, ae.Message);
326325
}
327326
}
@@ -332,9 +331,8 @@ public static void ImportFromEncryptedPem_Pkcs8_Byte_NoPem()
332331
using (DSA dsa = DSAFactory.Create())
333332
{
334333
string pem = "";
335-
byte[] passwordBytes = Encoding.UTF8.GetBytes("test");
336334
ArgumentException ae = AssertExtensions.Throws<ArgumentException>("input", () =>
337-
dsa.ImportFromEncryptedPem(pem, passwordBytes));
335+
dsa.ImportFromEncryptedPem(pem, "test"u8));
338336
Assert.Contains(NoPemExceptionMarker, ae.Message);
339337
}
340338
}

src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/DSA/DSATestData.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ internal static void GetDSA1024_186_2(out DSAParameters parameters, out byte[] s
171171
"41e2345f1f56df2458f426d155b4ba2db6dcd8c8"
172172
).HexToByteArray();
173173

174-
data = Encoding.ASCII.GetBytes("abc");
174+
data = "abc"u8.ToArray();
175175
}
176176
}
177177
}

src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/EC/ECKeyFileTests.LimitedPrivate.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public void ReadNistP521EncryptedPkcs8_Pbes2_Aes128_LimitedPrivateKey_PasswordBy
8383

8484
ReadWriteBase64EncryptedPkcs8(
8585
base64,
86-
Encoding.UTF8.GetBytes("qwerty"),
86+
"qwerty"u8.ToArray(),
8787
new PbeParameters(
8888
PbeEncryptionAlgorithm.Aes256Cbc,
8989
HashAlgorithmName.SHA1,

src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/EC/ECKeyFileTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public void ReadNistP521EncryptedPkcs8_Pbes2_Aes128_Sha384_PasswordBytes()
195195

196196
ReadWriteBase64EncryptedPkcs8(
197197
base64,
198-
Encoding.UTF8.GetBytes("qwerty"),
198+
"qwerty"u8.ToArray(),
199199
new PbeParameters(
200200
PbeEncryptionAlgorithm.Aes256Cbc,
201201
HashAlgorithmName.SHA1,

src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/EC/ECKeyPemTests.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,7 @@ public void ImportFromEncryptedPem_Pkcs8_Byte_Simple()
342342
/OMt/tWvtMSj17+dJvShsu/NYJXF5fsfpSJbd3e50Y3AisW0Ob7mmF54KBfg6Y+4
343343
aATwwQdUIKVzUZsQctsHPjbriQKKn7GKSyUOikBUNQ+TozojX8/g7JAsl+T9jGM=
344344
-----END ENCRYPTED PRIVATE KEY-----";
345-
byte[] passwordBytes = Encoding.UTF8.GetBytes("test");
346-
key.ImportFromEncryptedPem(pem, passwordBytes);
345+
key.ImportFromEncryptedPem(pem, "test"u8);
347346
ECParameters ecParameters = key.ExportParameters(true);
348347
ECParameters expected = EccTestData.GetNistP256ReferenceKey();
349348
EccTestBase.AssertEqual(expected, ecParameters);
@@ -370,10 +369,9 @@ public void ImportFromEncryptedPem_AmbiguousPem_Byte()
370369
opSAJ/pvHONL5kyAJLeNyG9c/mR2qyrP2L9gL0Z5fB9NyPejKTLi0PXMGQWdDTH8
371370
Qh0fqdrNovgFLubbJFMQN/MwwIAfIuf0Mn0WFYYeQiBJ3kg=
372371
-----END ENCRYPTED PRIVATE KEY-----";
373-
byte[] passwordBytes = Encoding.UTF8.GetBytes("test");
374372

375373
ArgumentException ae = AssertExtensions.Throws<ArgumentException>("input", () =>
376-
key.ImportFromEncryptedPem(pem, passwordBytes));
374+
key.ImportFromEncryptedPem(pem, "test"u8));
377375

378376
Assert.Contains(AmbiguousExceptionMarker, ae.Message);
379377
}

src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/ECDsa/ECDsaTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,6 @@ public static IEnumerable<object[]> InteroperableSignatureConfigurations()
274274
[MemberData(nameof(InteroperableSignatureConfigurations))]
275275
public void SignVerify_InteroperableSameKeys_RoundTripsUnlessTampered(ECDsa ecdsa, HashAlgorithmName hashAlgorithm)
276276
{
277-
byte[] data = Encoding.UTF8.GetBytes("something to repeat and sign");
278-
279277
// large enough to make hashing work though multiple iterations and not a multiple of 4KB it uses.
280278
byte[] dataArray = new byte[33333];
281279

src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSAKeyFileTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ public static void ReadPbes2Rc2EncryptedDiminishedDP_PasswordBytes()
781781

782782
ReadBase64EncryptedPkcs8(
783783
base64,
784-
Encoding.UTF8.GetBytes("rc2"),
784+
"rc2"u8.ToArray(),
785785
new PbeParameters(
786786
PbeEncryptionAlgorithm.Aes192Cbc,
787787
HashAlgorithmName.SHA256,

0 commit comments

Comments
 (0)