| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Buffers; |
| | | 6 | | using System.Buffers.Text; |
| | | 7 | | using System.Collections; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.IO; |
| | | 10 | | using System.IO.Pipelines; |
| | | 11 | | using System.Runtime.CompilerServices; |
| | | 12 | | using System.Runtime.InteropServices; |
| | | 13 | | using System.Text; |
| | | 14 | | using System.Text.Encodings.Web; |
| | | 15 | | using System.Text.Json; |
| | | 16 | | using System.Text.Json.Nodes; |
| | | 17 | | using System.Text.Json.Serialization; |
| | | 18 | | using System.Text.Unicode; |
| | | 19 | | using SharpFuzz; |
| | | 20 | | |
| | | 21 | | namespace DotnetFuzzing.Fuzzers; |
| | | 22 | | |
| | | 23 | | internal sealed class Utf8JsonReaderFuzzer : IFuzzer |
| | | 24 | | { |
| | 1 | 25 | | public string[] TargetAssemblies { get; } = ["System.Text.Json"]; |
| | | 26 | | |
| | 0 | 27 | | public string[] TargetCoreLibPrefixes => []; |
| | | 28 | | |
| | | 29 | | delegate T RefFunc<T>(ref Utf8JsonReader reader); |
| | | 30 | | |
| | 1 | 31 | | static byte[] s_controlBytes = []; |
| | | 32 | | static int s_controlIndex; |
| | | 33 | | |
| | | 34 | | static byte NextByte() |
| | 2493 | 35 | | { |
| | 2493 | 36 | | byte value = s_controlBytes[s_controlIndex]; |
| | 2493 | 37 | | s_controlIndex = (s_controlIndex + 1) % s_controlBytes.Length; |
| | 2493 | 38 | | return value; |
| | 2493 | 39 | | } |
| | | 40 | | |
| | | 41 | | public void FuzzTarget(ReadOnlySpan<byte> bytes) |
| | 106 | 42 | | { |
| | | 43 | | const int controlByteCount = 32; |
| | | 44 | | const int minLength = controlByteCount + 10; |
| | 106 | 45 | | if (bytes.Length < minLength) |
| | 0 | 46 | | { |
| | 0 | 47 | | return; |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | // Use the first bytes directly as control data instead of seeding a PRNG, |
| | | 51 | | // so the fuzzing engine can see the relationship between input and code coverage. |
| | 106 | 52 | | s_controlBytes = bytes.Slice(0, controlByteCount).ToArray(); |
| | 106 | 53 | | s_controlIndex = 0; |
| | 106 | 54 | | bytes = bytes.Slice(controlByteCount); |
| | | 55 | | |
| | 106 | 56 | | JsonSerializerOptions options = new JsonSerializerOptions |
| | 106 | 57 | | { |
| | 106 | 58 | | AllowTrailingCommas = NextByte() % 2 == 0, |
| | 106 | 59 | | Encoder = NextByte() % 2 == 0 ? JavaScriptEncoder.Default : JavaScriptEncoder.UnsafeRelaxedJsonEscaping, |
| | 106 | 60 | | // TODO: JsonExceptions differ between Span and Sequence paths |
| | 106 | 61 | | ReadCommentHandling = (JsonCommentHandling)(NextByte() % 2), |
| | 106 | 62 | | NumberHandling = (JsonNumberHandling)(NextByte() % 4), |
| | 106 | 63 | | }; |
| | | 64 | | |
| | | 65 | | // Fuzz using ReadOnlySpan<byte> |
| | 106 | 66 | | var readerSpan = new Utf8JsonReader(bytes, isFinalBlock: true, state: default); |
| | | 67 | | |
| | | 68 | | // Fuzz using ReadOnlySequence<byte> |
| | 106 | 69 | | var sequence = CreateVariableSegmentSequence(bytes); |
| | 106 | 70 | | var readerSequence = new Utf8JsonReader(sequence, isFinalBlock: true, state: default); |
| | | 71 | | |
| | 106 | 72 | | var byteArr = bytes.ToArray(); |
| | 106 | 73 | | TestDeserializeAsync<string>(byteArr, sequence, options); |
| | 106 | 74 | | TestDeserializeAsync<decimal>(byteArr, sequence, options); |
| | 106 | 75 | | TestDeserializeAsync<byte>(byteArr, sequence, options); |
| | 106 | 76 | | TestDeserializeAsync<Guid>(byteArr, sequence, options); |
| | 106 | 77 | | TestDeserializeAsync<DateTime>(byteArr, sequence, options); |
| | 106 | 78 | | TestDeserializeAsync<DateTimeOffset>(byteArr, sequence, options); |
| | 106 | 79 | | TestDeserializeAsync<IntEnum>(byteArr, sequence, options); |
| | 106 | 80 | | TestDeserializeAsync<StringEnum>(byteArr, sequence, options); |
| | 106 | 81 | | TestDeserializeAsync<bool>(byteArr, sequence, options); |
| | 106 | 82 | | TestDeserializeAsync<byte[]>(byteArr, sequence, options, (l, r) => l.AsSpan().SequenceEqual(r.AsSpan())); |
| | 106 | 83 | | TestDeserializeAsync<char>(byteArr, sequence, options); |
| | 106 | 84 | | TestDeserializeAsync<double>(byteArr, sequence, options); |
| | 106 | 85 | | TestDeserializeAsync<Int16>(byteArr, sequence, options); |
| | 106 | 86 | | TestDeserializeAsync<Int32>(byteArr, sequence, options); |
| | 106 | 87 | | TestDeserializeAsync<Int64>(byteArr, sequence, options); |
| | 106 | 88 | | TestDeserializeAsync<sbyte>(byteArr, sequence, options); |
| | 106 | 89 | | TestDeserializeAsync<Single>(byteArr, sequence, options); |
| | 106 | 90 | | TestDeserializeAsync<TimeSpan>(byteArr, sequence, options); |
| | 106 | 91 | | TestDeserializeAsync<UInt16>(byteArr, sequence, options); |
| | 106 | 92 | | TestDeserializeAsync<UInt32>(byteArr, sequence, options); |
| | 106 | 93 | | TestDeserializeAsync<UInt64>(byteArr, sequence, options); |
| | 106 | 94 | | TestDeserializeAsync<Uri>(byteArr, sequence, options); |
| | 106 | 95 | | TestDeserializeAsync<Version>(byteArr, sequence, options); |
| | 106 | 96 | | TestDeserializeAsync<object>(byteArr, sequence, options, (l, r) => JsonElement.DeepEquals((JsonElement)l!, (Json |
| | 106 | 97 | | TestDeserializeAsync<JsonElement>(byteArr, sequence, options, (l, r) => JsonElement.DeepEquals(l, r)); |
| | 106 | 98 | | TestDeserializeAsync<JsonDocument>(byteArr, sequence, options, (l, r) => JsonElement.DeepEquals(l!.RootElement, |
| | 106 | 99 | | TestDeserializeAsync<JsonArray>(byteArr, sequence, options, (l, r) => JsonNode.DeepEquals(l, r)); |
| | 106 | 100 | | TestDeserializeAsync<JsonValue>(byteArr, sequence, options, (l, r) => JsonNode.DeepEquals(l, r)); |
| | 106 | 101 | | TestDeserializeAsync<JsonNode>(byteArr, sequence, options, (l, r) => JsonNode.DeepEquals(l, r)); |
| | 106 | 102 | | TestDeserializeAsync<JsonObject>(byteArr, sequence, options, (l, r) => JsonNode.DeepEquals(l, r)); |
| | 106 | 103 | | TestDeserializeAsync<Memory<byte>>(byteArr, sequence, options, (l, r) => l.Span.SequenceEqual(r.Span)); |
| | 106 | 104 | | TestDeserializeAsync<ReadOnlyMemory<byte>>(byteArr, sequence, options, (l, r) => l.Span.SequenceEqual(r.Span)); |
| | 106 | 105 | | TestDeserializeAsync<DateOnly>(byteArr, sequence, options); |
| | 106 | 106 | | TestDeserializeAsync<TimeOnly>(byteArr, sequence, options); |
| | 106 | 107 | | TestDeserializeAsync<Half>(byteArr, sequence, options); |
| | 106 | 108 | | TestDeserializeAsync<Int128>(byteArr, sequence, options); |
| | 106 | 109 | | TestDeserializeAsync<UInt128>(byteArr, sequence, options); |
| | 106 | 110 | | TestDeserializeAsync<List<int>>(byteArr, sequence, options, (l, r) => |
| | 0 | 111 | | { |
| | 0 | 112 | | if (l!.Count != r!.Count) |
| | 0 | 113 | | return false; |
| | 106 | 114 | | |
| | 0 | 115 | | for (int i = 0; i < l.Count; i++) |
| | 0 | 116 | | { |
| | 0 | 117 | | if (l[i] != r[i]) |
| | 0 | 118 | | return false; |
| | 0 | 119 | | } |
| | 0 | 120 | | return true; |
| | 106 | 121 | | }); |
| | 106 | 122 | | TestDeserializeAsync<Dictionary<string, int>>(byteArr, sequence, options, (l, r) => |
| | 0 | 123 | | { |
| | 0 | 124 | | if (l!.Count != r!.Count) |
| | 0 | 125 | | return false; |
| | 106 | 126 | | |
| | 0 | 127 | | foreach (var kvp in l) |
| | 0 | 128 | | { |
| | 0 | 129 | | if (!r.TryGetValue(kvp.Key, out int value) || value != kvp.Value) |
| | 0 | 130 | | return false; |
| | 0 | 131 | | } |
| | 0 | 132 | | return true; |
| | 106 | 133 | | }); |
| | | 134 | | |
| | 106 | 135 | | TestRandomType(bytes, sequence, options, maxDepth: NextByte() % 3); |
| | 106 | 136 | | TestRandomType(bytes, sequence, options, maxDepth: NextByte() % 3); |
| | | 137 | | |
| | 106 | 138 | | while (true) |
| | 106 | 139 | | { |
| | 106 | 140 | | if (!Test(ref readerSpan, ref readerSequence, |
| | 212 | 141 | | static (ref readerSpan) => { return readerSpan.Read(); }, |
| | 212 | 142 | | static (ref readerSequence) => { return readerSequence.Read(); } |
| | 106 | 143 | | )) |
| | 106 | 144 | | { |
| | 106 | 145 | | break; |
| | | 146 | | } |
| | | 147 | | |
| | 0 | 148 | | if (readerSpan.TokenType != readerSequence.TokenType || |
| | 0 | 149 | | !(readerSequence.HasValueSequence ? readerSpan.ValueSpan.SequenceEqual(readerSequence.ValueSequence.ToAr |
| | 0 | 150 | | readerSpan.ValueSpan.SequenceEqual(readerSequence.ValueSpan))) |
| | 0 | 151 | | { |
| | 0 | 152 | | throw new InvalidOperationException("Span and Sequence readers diverged in token or value. " + |
| | 0 | 153 | | $"Span TokenType: {readerSpan.TokenType}, Value: {Convert.ToHexStrin |
| | 0 | 154 | | $"Seq TokenType: {readerSequence.TokenType}, Value: {Convert.ToHexSt |
| | | 155 | | } |
| | | 156 | | |
| | | 157 | | // Example: Test TryGetInt32 after each Read |
| | 0 | 158 | | if (readerSpan.TokenType == JsonTokenType.Number) |
| | 0 | 159 | | { |
| | 0 | 160 | | Test(ref readerSpan, ref readerSequence, |
| | 0 | 161 | | static (ref readerSpan) => { int value; return readerSpan.TryGetInt32(out value) ? value : (int?)nul |
| | 0 | 162 | | static (ref readerSequence) => { int value; return readerSequence.TryGetInt32(out value) ? value : ( |
| | 0 | 163 | | ); |
| | | 164 | | |
| | 0 | 165 | | Test(ref readerSpan, ref readerSequence, |
| | 0 | 166 | | static (ref readerSpan) => { byte value; return readerSpan.TryGetByte(out value) ? value : (byte?)nu |
| | 0 | 167 | | static (ref readerSequence) => { byte value; return readerSequence.TryGetByte(out value) ? value : ( |
| | 0 | 168 | | ); |
| | | 169 | | |
| | 0 | 170 | | Test(ref readerSpan, ref readerSequence, |
| | 0 | 171 | | static (ref readerSpan) => { short value; return readerSpan.TryGetInt16(out value) ? value : (short? |
| | 0 | 172 | | static (ref readerSequence) => { short value; return readerSequence.TryGetInt16(out value) ? value : |
| | 0 | 173 | | ); |
| | | 174 | | |
| | 0 | 175 | | Test(ref readerSpan, ref readerSequence, |
| | 0 | 176 | | static (ref readerSpan) => { long value; return readerSpan.TryGetInt64(out value) ? value : (long?)n |
| | 0 | 177 | | static (ref readerSequence) => { long value; return readerSequence.TryGetInt64(out value) ? value : |
| | 0 | 178 | | ); |
| | | 179 | | |
| | 0 | 180 | | Test(ref readerSpan, ref readerSequence, |
| | 0 | 181 | | static (ref readerSpan) => { float value; return readerSpan.TryGetSingle(out value) ? value : (float |
| | 0 | 182 | | static (ref readerSequence) => { float value; return readerSequence.TryGetSingle(out value) ? value |
| | 0 | 183 | | ); |
| | | 184 | | |
| | 0 | 185 | | Test(ref readerSpan, ref readerSequence, |
| | 0 | 186 | | static (ref readerSpan) => { double value; return readerSpan.TryGetDouble(out value) ? value : (doub |
| | 0 | 187 | | static (ref readerSequence) => { double value; return readerSequence.TryGetDouble(out value) ? value |
| | 0 | 188 | | ); |
| | | 189 | | |
| | 0 | 190 | | Test(ref readerSpan, ref readerSequence, |
| | 0 | 191 | | static (ref readerSpan) => { decimal value; return readerSpan.TryGetDecimal(out value) ? value : (de |
| | 0 | 192 | | static (ref readerSequence) => { decimal value; return readerSequence.TryGetDecimal(out value) ? val |
| | 0 | 193 | | ); |
| | | 194 | | |
| | 0 | 195 | | Test(ref readerSpan, ref readerSequence, |
| | 0 | 196 | | static (ref readerSpan) => { sbyte value; return readerSpan.TryGetSByte(out value) ? value : (sbyte? |
| | 0 | 197 | | static (ref readerSequence) => { sbyte value; return readerSequence.TryGetSByte(out value) ? value : |
| | 0 | 198 | | ); |
| | 0 | 199 | | } |
| | | 200 | | |
| | 0 | 201 | | if (readerSpan.TokenType == JsonTokenType.String) |
| | 0 | 202 | | { |
| | 0 | 203 | | Test(ref readerSpan, ref readerSequence, |
| | 0 | 204 | | static (ref readerSpan) => |
| | 0 | 205 | | { |
| | 0 | 206 | | DateTime value; |
| | 0 | 207 | | try |
| | 0 | 208 | | { |
| | 0 | 209 | | readerSpan.TryGetDateTime(out value); |
| | 0 | 210 | | } |
| | 0 | 211 | | catch (InvalidOperationException ex) when (ex.Message.Contains("surrogate")) |
| | 0 | 212 | | { |
| | 0 | 213 | | // If the string is not a valid DateTime, we expect an exception |
| | 0 | 214 | | return (DateTime?)null; |
| | 0 | 215 | | } |
| | 0 | 216 | | return value; |
| | 0 | 217 | | }, |
| | 0 | 218 | | static (ref readerSequence) => |
| | 0 | 219 | | { |
| | 0 | 220 | | DateTime value; |
| | 0 | 221 | | try |
| | 0 | 222 | | { |
| | 0 | 223 | | readerSequence.TryGetDateTime(out value); |
| | 0 | 224 | | } |
| | 0 | 225 | | catch (InvalidOperationException ex) when (ex.Message.Contains("surrogate")) |
| | 0 | 226 | | { |
| | 0 | 227 | | // If the string is not a valid DateTime, we expect an exception |
| | 0 | 228 | | return (DateTime?)null; |
| | 0 | 229 | | } |
| | 0 | 230 | | return value; |
| | 0 | 231 | | } |
| | 0 | 232 | | ); |
| | | 233 | | |
| | 0 | 234 | | Test(ref readerSpan, ref readerSequence, |
| | 0 | 235 | | static (ref readerSpan) => |
| | 0 | 236 | | { |
| | 0 | 237 | | string? value; |
| | 0 | 238 | | try |
| | 0 | 239 | | { |
| | 0 | 240 | | value = readerSpan.GetString(); |
| | 0 | 241 | | } |
| | 0 | 242 | | catch (InvalidOperationException ex) when (ex.Message.Contains("surrogate")) |
| | 0 | 243 | | { |
| | 0 | 244 | | // If the string contains invalid surrogate characters, we expect an exception |
| | 0 | 245 | | return null; |
| | 0 | 246 | | } |
| | 0 | 247 | | return value; |
| | 0 | 248 | | }, |
| | 0 | 249 | | static (ref readerSequence) => |
| | 0 | 250 | | { |
| | 0 | 251 | | string? value; |
| | 0 | 252 | | try |
| | 0 | 253 | | { |
| | 0 | 254 | | value = readerSequence.GetString(); |
| | 0 | 255 | | } |
| | 0 | 256 | | catch (InvalidOperationException ex) when (ex.Message.Contains("surrogate")) |
| | 0 | 257 | | { |
| | 0 | 258 | | // If the string contains invalid surrogate characters, we expect an exception |
| | 0 | 259 | | return null; |
| | 0 | 260 | | } |
| | 0 | 261 | | return value; |
| | 0 | 262 | | } |
| | 0 | 263 | | ); |
| | 0 | 264 | | } |
| | | 265 | | |
| | 0 | 266 | | if (readerSpan.TokenType == JsonTokenType.True || readerSpan.TokenType == JsonTokenType.False) |
| | 0 | 267 | | { |
| | 0 | 268 | | Test(ref readerSpan, ref readerSequence, |
| | 0 | 269 | | static (ref readerSpan) => { return readerSpan.GetBoolean(); }, |
| | 0 | 270 | | static (ref readerSequence) => { return readerSequence.GetBoolean(); } |
| | 0 | 271 | | ); |
| | 0 | 272 | | } |
| | 0 | 273 | | } |
| | 106 | 274 | | } |
| | | 275 | | |
| | | 276 | | private static T Test<T>( |
| | | 277 | | ref Utf8JsonReader spanReader, |
| | | 278 | | ref Utf8JsonReader seqReader, |
| | | 279 | | RefFunc<T> span, |
| | | 280 | | RefFunc<T> seq, |
| | | 281 | | Func<T, T, bool>? comparer = null) |
| | 4452 | 282 | | { |
| | 4452 | 283 | | T spanToken = default!; |
| | 4452 | 284 | | T seqToken = default!; |
| | 8904 | 285 | | Exception? spanEx = null, seqEx = null; |
| | | 286 | | |
| | | 287 | | try |
| | 4452 | 288 | | { |
| | 4452 | 289 | | spanToken = span(ref spanReader); |
| | 0 | 290 | | } |
| | 4452 | 291 | | catch (Exception ex) |
| | 4452 | 292 | | { |
| | 4452 | 293 | | spanEx = ex; |
| | 4452 | 294 | | } |
| | | 295 | | |
| | | 296 | | try |
| | 4452 | 297 | | { |
| | 4452 | 298 | | seqToken = seq(ref seqReader); |
| | 0 | 299 | | } |
| | 4452 | 300 | | catch (Exception ex) |
| | 4452 | 301 | | { |
| | 4452 | 302 | | seqEx = ex; |
| | 4452 | 303 | | } |
| | | 304 | | |
| | 4452 | 305 | | if (CompareExceptions(spanEx, seqEx)) |
| | 4452 | 306 | | { |
| | 4452 | 307 | | return default!; |
| | | 308 | | } |
| | | 309 | | |
| | 0 | 310 | | if (spanToken is null && seqToken is null) |
| | 0 | 311 | | { |
| | | 312 | | // Both returned null, consider it a match |
| | 0 | 313 | | return spanToken; |
| | | 314 | | } |
| | 0 | 315 | | else if (spanToken is null || seqToken is null) |
| | 0 | 316 | | { |
| | 0 | 317 | | throw new InvalidOperationException("Span and Sequence readers diverged in token availability. " + |
| | 0 | 318 | | $"Span has token: {spanToken}, Sequence has token: {seqToken}"); |
| | | 319 | | } |
| | | 320 | | |
| | 0 | 321 | | if (!comparer?.Invoke(spanToken, seqToken) ?? !spanToken.Equals(seqToken)) |
| | 0 | 322 | | { |
| | 0 | 323 | | throw new InvalidOperationException("Span and Sequence readers diverged in token availability. " + |
| | 0 | 324 | | $"Span has token: {spanToken}, Sequence has token: {seqToken}"); |
| | | 325 | | } |
| | | 326 | | |
| | 0 | 327 | | if (spanReader.BytesConsumed != seqReader.BytesConsumed || |
| | 0 | 328 | | spanReader.TokenStartIndex != seqReader.TokenStartIndex) |
| | 0 | 329 | | { |
| | 0 | 330 | | throw new InvalidOperationException("Span and Sequence readers diverged in BytesConsumed or TokenStartIndex. |
| | 0 | 331 | | $"Span BytesConsumed: {spanReader.BytesConsumed}, TokenStartIndex: {span |
| | 0 | 332 | | $"Seq BytesConsumed: {seqReader.BytesConsumed}, TokenStartIndex: {seqRea |
| | | 333 | | } |
| | | 334 | | |
| | 0 | 335 | | return spanToken; |
| | 4452 | 336 | | } |
| | | 337 | | |
| | | 338 | | internal static void TestDeserializeAsync<T>(byte[] bytes, ReadOnlySequence<byte> sequence, JsonSerializerOptions op |
| | 4346 | 339 | | { |
| | 4346 | 340 | | Utf8JsonReader readerSpan = new(); |
| | 4346 | 341 | | Test(ref readerSpan, ref readerSpan, |
| | 4346 | 342 | | (ref _) => (T?)JsonSerializer.DeserializeAsync(new MemoryStream(bytes), typeof(T), options).GetAwaiter().Get |
| | 4346 | 343 | | (ref _) => (T?)JsonSerializer.DeserializeAsync(PipeReader.Create(sequence), typeof(T), options).GetAwaiter() |
| | 4346 | 344 | | comparer); |
| | 4346 | 345 | | } |
| | | 346 | | |
| | 1 | 347 | | static readonly Type[] groundTypes = [ |
| | 1 | 348 | | typeof(int), |
| | 1 | 349 | | typeof(string), |
| | 1 | 350 | | typeof(decimal), |
| | 1 | 351 | | typeof(DateTimeOffset), |
| | 1 | 352 | | typeof(JsonDocument), |
| | 1 | 353 | | typeof(byte), |
| | 1 | 354 | | typeof(Guid), |
| | 1 | 355 | | typeof(DateTime), |
| | 1 | 356 | | typeof(IntEnum), |
| | 1 | 357 | | typeof(StringEnum), |
| | 1 | 358 | | typeof(bool), |
| | 1 | 359 | | typeof(double), |
| | 1 | 360 | | typeof(long), |
| | 1 | 361 | | typeof(Int128), |
| | 1 | 362 | | typeof(char), |
| | 1 | 363 | | ]; |
| | | 364 | | |
| | | 365 | | static Type GenerateRandomType(int maxDepth) |
| | 1382 | 366 | | { |
| | 1382 | 367 | | if (maxDepth <= 0) |
| | 1008 | 368 | | return groundTypes[NextByte() % groundTypes.Length]; |
| | | 369 | | |
| | 374 | 370 | | int kind = NextByte() % 5; |
| | 374 | 371 | | return kind switch |
| | 374 | 372 | | { |
| | 39 | 373 | | 0 => groundTypes[NextByte() % groundTypes.Length], |
| | 18 | 374 | | 1 => typeof(List<>).MakeGenericType(GenerateRandomType(maxDepth - 1)), |
| | 150 | 375 | | 2 => typeof(Dictionary<,>).MakeGenericType(typeof(string), GenerateRandomType(maxDepth - 1)), |
| | 162 | 376 | | 3 => typeof(PocoTemplate<,,,,,>).MakeGenericType( |
| | 162 | 377 | | GenerateRandomType(maxDepth - 1), GenerateRandomType(maxDepth - 1), |
| | 162 | 378 | | GenerateRandomType(maxDepth - 1), GenerateRandomType(maxDepth - 1), |
| | 162 | 379 | | GenerateRandomType(maxDepth - 1), GenerateRandomType(maxDepth - 1)), |
| | 5 | 380 | | 4 => typeof(PocoWithSetters<,,,,,>).MakeGenericType( |
| | 5 | 381 | | GenerateRandomType(maxDepth - 1), GenerateRandomType(maxDepth - 1), |
| | 5 | 382 | | GenerateRandomType(maxDepth - 1), GenerateRandomType(maxDepth - 1), |
| | 5 | 383 | | GenerateRandomType(maxDepth - 1), GenerateRandomType(maxDepth - 1)), |
| | 0 | 384 | | _ => groundTypes[0], |
| | 374 | 385 | | }; |
| | 1382 | 386 | | } |
| | | 387 | | |
| | | 388 | | static void TestRandomType(ReadOnlySpan<byte> bytes, ReadOnlySequence<byte> sequence, JsonSerializerOptions options, |
| | 212 | 389 | | { |
| | 212 | 390 | | Type type = GenerateRandomType(maxDepth); |
| | 212 | 391 | | Type testerType = typeof(Tester<>).MakeGenericType(type); |
| | 212 | 392 | | var testerInstance = (ITester)Activator.CreateInstance(testerType)!; |
| | 212 | 393 | | testerInstance.TestWrapper(bytes, sequence, options); |
| | 212 | 394 | | } |
| | | 395 | | |
| | | 396 | | private static ReadOnlySequence<byte> CreateVariableSegmentSequence(ReadOnlySpan<byte> bytes) |
| | 106 | 397 | | { |
| | | 398 | | // Split into 2-5 segments, but never less than 1 byte per segment |
| | 106 | 399 | | int maxSegments = Math.Min(6, bytes.Length + 1); |
| | 106 | 400 | | int segmentCount = maxSegments <= 2 ? 2 : 2 + NextByte() % (maxSegments - 2); |
| | | 401 | | |
| | 106 | 402 | | if (bytes.Length == 0 || bytes.Length < segmentCount) |
| | 0 | 403 | | { |
| | 0 | 404 | | return ReadOnlySequence<byte>.Empty; |
| | | 405 | | } |
| | | 406 | | |
| | 106 | 407 | | int[] segmentSizes = new int[segmentCount]; |
| | 106 | 408 | | int remaining = bytes.Length; |
| | | 409 | | |
| | | 410 | | // Assign at least 1 byte per segment |
| | 1084 | 411 | | for (int i = 0; i < segmentCount; i++) |
| | 436 | 412 | | { |
| | 436 | 413 | | int max = remaining - (segmentCount - i - 1); |
| | 436 | 414 | | segmentSizes[i] = (i == segmentCount - 1) ? max : (max <= 1 ? 1 : 1 + NextByte() % (max - 1)); |
| | 436 | 415 | | remaining -= segmentSizes[i]; |
| | 436 | 416 | | } |
| | | 417 | | |
| | | 418 | | // Build segments |
| | 106 | 419 | | var buffers = new List<ReadOnlyMemory<byte>>(segmentCount); |
| | 106 | 420 | | int offset = 0; |
| | 1084 | 421 | | for (int i = 0; i < segmentCount; i++) |
| | 436 | 422 | | { |
| | 436 | 423 | | buffers.Add(bytes.Slice(offset, segmentSizes[i]).ToArray()); |
| | 436 | 424 | | offset += segmentSizes[i]; |
| | 436 | 425 | | } |
| | | 426 | | |
| | | 427 | | // Link segments |
| | 106 | 428 | | ReadOnlySequence<byte> sequence = new ReadOnlySequence<byte>(buffers[0]); |
| | 106 | 429 | | if (buffers.Count == 1) |
| | 0 | 430 | | return sequence; |
| | | 431 | | |
| | 106 | 432 | | var segments = new List<BufferSegment>(); |
| | 1190 | 433 | | foreach (var buffer in buffers) |
| | 436 | 434 | | segments.Add(new BufferSegment(buffer)); |
| | | 435 | | |
| | 872 | 436 | | for (int i = 0; i < segments.Count - 1; i++) |
| | 330 | 437 | | segments[i].SetNext(segments[i + 1]); |
| | | 438 | | |
| | 106 | 439 | | return new ReadOnlySequence<byte>(segments[0], 0, segments[^1], segments[^1].Memory.Length); |
| | 106 | 440 | | } |
| | | 441 | | |
| | | 442 | | // Helper class for multi-segment ReadOnlySequence |
| | | 443 | | private sealed class BufferSegment : ReadOnlySequenceSegment<byte> |
| | | 444 | | { |
| | 436 | 445 | | public BufferSegment(ReadOnlyMemory<byte> memory) |
| | 436 | 446 | | { |
| | 436 | 447 | | Memory = memory; |
| | 436 | 448 | | } |
| | | 449 | | public void SetNext(BufferSegment next) |
| | 330 | 450 | | { |
| | 330 | 451 | | Next = next; |
| | 330 | 452 | | next.RunningIndex = RunningIndex + Memory.Length; |
| | 330 | 453 | | } |
| | | 454 | | } |
| | | 455 | | |
| | 0 | 456 | | public record PocoTemplate<T1, T2, T3, T4, T5, T6>( |
| | 0 | 457 | | T1 Property1, |
| | 0 | 458 | | T2 Property2, |
| | 0 | 459 | | T3 Property3, |
| | 0 | 460 | | T4 Property4, |
| | 0 | 461 | | T5 Property5, |
| | 0 | 462 | | T6 Property6 |
| | 0 | 463 | | ); |
| | | 464 | | |
| | | 465 | | public class PocoWithSetters<T1, T2, T3, T4, T5, T6> |
| | | 466 | | { |
| | 0 | 467 | | public T1 Property1 { get; set; } = default!; |
| | 0 | 468 | | public T2 Property2 { get; set; } = default!; |
| | 0 | 469 | | public T3 Property3 { get; set; } = default!; |
| | 0 | 470 | | public T4 Property4 { get; set; } = default!; |
| | 0 | 471 | | public T5 Property5 { get; set; } = default!; |
| | 0 | 472 | | public T6 Property6 { get; set; } = default!; |
| | | 473 | | |
| | | 474 | | public override bool Equals(object? obj) |
| | 0 | 475 | | { |
| | 0 | 476 | | if (ReferenceEquals(this, obj)) |
| | 0 | 477 | | return true; |
| | 0 | 478 | | if (obj is not PocoWithSetters<T1, T2, T3, T4, T5, T6> other) |
| | 0 | 479 | | return false; |
| | | 480 | | |
| | 0 | 481 | | return |
| | 0 | 482 | | EqualityComparer<T1>.Default.Equals(Property1, other.Property1) && |
| | 0 | 483 | | EqualityComparer<T2>.Default.Equals(Property2, other.Property2) && |
| | 0 | 484 | | EqualityComparer<T3>.Default.Equals(Property3, other.Property3) && |
| | 0 | 485 | | EqualityComparer<T4>.Default.Equals(Property4, other.Property4) && |
| | 0 | 486 | | EqualityComparer<T5>.Default.Equals(Property5, other.Property5) && |
| | 0 | 487 | | EqualityComparer<T6>.Default.Equals(Property6, other.Property6); |
| | 0 | 488 | | } |
| | | 489 | | |
| | | 490 | | public override int GetHashCode() |
| | 0 | 491 | | { |
| | 0 | 492 | | return HashCode.Combine( |
| | 0 | 493 | | Property1 is null ? 0 : EqualityComparer<T1>.Default.GetHashCode(Property1), |
| | 0 | 494 | | Property2 is null ? 0 : EqualityComparer<T2>.Default.GetHashCode(Property2), |
| | 0 | 495 | | Property3 is null ? 0 : EqualityComparer<T3>.Default.GetHashCode(Property3), |
| | 0 | 496 | | Property4 is null ? 0 : EqualityComparer<T4>.Default.GetHashCode(Property4), |
| | 0 | 497 | | Property5 is null ? 0 : EqualityComparer<T5>.Default.GetHashCode(Property5), |
| | 0 | 498 | | Property6 is null ? 0 : EqualityComparer<T6>.Default.GetHashCode(Property6) |
| | 0 | 499 | | ); |
| | 0 | 500 | | } |
| | | 501 | | } |
| | | 502 | | |
| | | 503 | | public interface ITester |
| | | 504 | | { |
| | | 505 | | void TestWrapper(ReadOnlySpan<byte> bytes, ReadOnlySequence<byte> sequence, JsonSerializerOptions options); |
| | | 506 | | } |
| | | 507 | | |
| | | 508 | | public class Tester<T> : ITester |
| | | 509 | | { |
| | | 510 | | public void TestWrapper(ReadOnlySpan<byte> bytes, ReadOnlySequence<byte> sequence, JsonSerializerOptions options |
| | 212 | 511 | | { |
| | 212 | 512 | | TestDeserializeAsync<T>(bytes.ToArray(), sequence, options, static (_, _) => true); |
| | 212 | 513 | | } |
| | | 514 | | } |
| | | 515 | | |
| | | 516 | | // true if had exception and processed it, false if no exception, throw if exceptions don't match or are unexpected |
| | | 517 | | public static bool CompareExceptions(Exception? spanEx, Exception? seqEx) |
| | 4452 | 518 | | { |
| | 4452 | 519 | | if (spanEx is null || seqEx is null) |
| | 0 | 520 | | { |
| | 0 | 521 | | if (spanEx is not null) |
| | 0 | 522 | | { |
| | 0 | 523 | | throw new InvalidOperationException( |
| | 0 | 524 | | $"Span and Sequence readers diverged in exception:{Environment.NewLine}" + |
| | 0 | 525 | | $"Span: {spanEx.GetType().Name} '{spanEx.Message}'{Environment.NewLine}" + |
| | 0 | 526 | | $"Seq: No Exception"); |
| | | 527 | | } |
| | 0 | 528 | | else if (seqEx is not null) |
| | 0 | 529 | | { |
| | 0 | 530 | | throw new InvalidOperationException( |
| | 0 | 531 | | $"Span and Sequence readers diverged in exception:{Environment.NewLine}" + |
| | 0 | 532 | | $"Span: No Exception{Environment.NewLine}" + |
| | 0 | 533 | | $"Seq: {seqEx.GetType().Name} '{seqEx.Message}"); |
| | | 534 | | } |
| | | 535 | | else |
| | 0 | 536 | | { |
| | | 537 | | // both are null exceptions |
| | 0 | 538 | | return false; |
| | | 539 | | } |
| | | 540 | | } |
| | | 541 | | |
| | 4452 | 542 | | if (spanEx.GetType() != seqEx.GetType()) |
| | 0 | 543 | | { |
| | 0 | 544 | | throw new InvalidOperationException( |
| | 0 | 545 | | $"Span and Sequence readers diverged in exception type:{Environment.NewLine}" + |
| | 0 | 546 | | $"Span: {spanEx.GetType().Name} '{spanEx.Message}'{Environment.NewLine}" + |
| | 0 | 547 | | $"Seq: {seqEx.GetType().Name} '{seqEx.Message}'"); |
| | | 548 | | } |
| | | 549 | | |
| | 4452 | 550 | | if (spanEx is JsonException spanJsonEx && seqEx is JsonException seqJsonEx) |
| | 4452 | 551 | | { |
| | 4452 | 552 | | if (//spanJsonEx.LineNumber != seqJsonEx.LineNumber || |
| | 4452 | 553 | | //spanJsonEx.BytePositionInLine != seqJsonEx.BytePositionInLine || |
| | 4452 | 554 | | spanJsonEx.Path != seqJsonEx.Path/* || |
| | 4452 | 555 | | spanJsonEx.Message != seqJsonEx.Message*/) |
| | 0 | 556 | | { |
| | 0 | 557 | | throw new InvalidOperationException( |
| | 0 | 558 | | $"Span and Sequence readers diverged in exception:{Environment.NewLine}" + |
| | 0 | 559 | | $"Span: {spanEx.GetType().Name} '{spanEx.Message}'{Environment.NewLine}" + |
| | 0 | 560 | | $"Seq: {seqEx.GetType().Name} '{seqEx.Message}'"); |
| | | 561 | | } |
| | | 562 | | // If both threw the same exception, consider the test passed for this input |
| | 4452 | 563 | | return true; |
| | | 564 | | } |
| | | 565 | | |
| | 0 | 566 | | if (spanEx is DecoderFallbackException spanDecoderEx && seqEx is DecoderFallbackException seqDecoderEx) |
| | 0 | 567 | | { |
| | 0 | 568 | | if (spanDecoderEx.BytesUnknown.SequenceEqual(seqDecoderEx.BytesUnknown) != true) |
| | 0 | 569 | | { |
| | 0 | 570 | | throw new InvalidOperationException( |
| | 0 | 571 | | $"Span and Sequence readers diverged in decoder exception:{Environment.NewLine}" + |
| | 0 | 572 | | $"Span: {spanDecoderEx.GetType().Name ?? "none"} '{Convert.ToHexString(spanDecoderEx?.BytesUnknown ? |
| | 0 | 573 | | $"Seq: {seqDecoderEx.GetType().Name ?? "none"} '{Convert.ToHexString(seqDecoderEx?.BytesUnknown ?? [ |
| | | 574 | | } |
| | | 575 | | // If both threw the same decoder exception, consider the test passed for this input |
| | 0 | 576 | | return true; |
| | | 577 | | } |
| | | 578 | | |
| | 0 | 579 | | if (spanEx is InvalidOperationException spanInvalidEx && seqEx is InvalidOperationException seqInvalidEx) |
| | 0 | 580 | | { |
| | 0 | 581 | | if (spanInvalidEx.Message.Contains("surrogate") || seqInvalidEx.Message.Contains("surrogate")) |
| | 0 | 582 | | { |
| | 0 | 583 | | if (spanInvalidEx?.Message.Equals(seqInvalidEx?.Message) is not true) |
| | 0 | 584 | | { |
| | 0 | 585 | | throw new InvalidOperationException( |
| | 0 | 586 | | $"Span and Sequence readers diverged in surrogate exception:{Environment.NewLine}" + |
| | 0 | 587 | | $"Span: '{spanInvalidEx?.Message ?? "null"}'{Environment.NewLine}" + |
| | 0 | 588 | | $"Seq: '{seqInvalidEx?.Message ?? "null"}'"); |
| | | 589 | | } |
| | | 590 | | |
| | | 591 | | // If both threw the same exception, consider the test passed for this input |
| | 0 | 592 | | return true; |
| | | 593 | | } |
| | 0 | 594 | | else if (spanInvalidEx.Message.Contains("invalid UTF-8") || seqInvalidEx.Message.Contains("invalid UTF-8")) |
| | 0 | 595 | | { |
| | 0 | 596 | | if (spanInvalidEx?.Message.Equals(seqInvalidEx?.Message) is not true) |
| | 0 | 597 | | { |
| | 0 | 598 | | throw new InvalidOperationException( |
| | 0 | 599 | | $"Span and Sequence readers diverged in invalid UTF-8 exception:{Environment.NewLine}" + |
| | 0 | 600 | | $"Span: '{spanInvalidEx?.Message ?? "null"}'{Environment.NewLine}" + |
| | 0 | 601 | | $"Seq: '{seqInvalidEx?.Message ?? "null"}'"); |
| | | 602 | | } |
| | | 603 | | |
| | | 604 | | // If both threw the same exception, consider the test passed for this input |
| | 0 | 605 | | return true; |
| | | 606 | | } |
| | 0 | 607 | | else if (spanInvalidEx.Message.Contains("element cannot be an object or array") || |
| | 0 | 608 | | seqInvalidEx.Message.Contains("element cannot be an object or array")) |
| | 0 | 609 | | { |
| | 0 | 610 | | if (spanInvalidEx?.Message.Equals(seqInvalidEx?.Message) is not true) |
| | 0 | 611 | | { |
| | 0 | 612 | | throw new InvalidOperationException( |
| | 0 | 613 | | $"Span and Sequence readers diverged in element object/array exception:{Environment.NewLine}" + |
| | 0 | 614 | | $"Span: '{spanInvalidEx?.Message ?? "null"}'{Environment.NewLine}" + |
| | 0 | 615 | | $"Seq: '{seqInvalidEx?.Message ?? "null"}'"); |
| | | 616 | | } |
| | | 617 | | |
| | | 618 | | // If both threw the same exception, consider the test passed for this input |
| | 0 | 619 | | return true; |
| | | 620 | | } |
| | 0 | 621 | | } |
| | | 622 | | |
| | 0 | 623 | | if (spanEx is ArgumentOutOfRangeException spanRangeEx && seqEx is ArgumentOutOfRangeException seqRangeEx) |
| | 0 | 624 | | { |
| | | 625 | | // e.g. 42.1e240071992547409914 |
| | 0 | 626 | | if (spanRangeEx.Message.Contains("exponent") || seqRangeEx.Message.Contains("exponent")) |
| | 0 | 627 | | { |
| | 0 | 628 | | if (spanRangeEx?.Message.Equals(seqRangeEx?.Message) is not true) |
| | 0 | 629 | | { |
| | 0 | 630 | | throw new InvalidOperationException( |
| | 0 | 631 | | $"Span and Sequence readers diverged in exponent exception:{Environment.NewLine}" + |
| | 0 | 632 | | $"Span: '{spanRangeEx?.Message ?? "null"}'{Environment.NewLine}" + |
| | 0 | 633 | | $"Seq: '{seqRangeEx?.Message ?? "null"}'"); |
| | | 634 | | } |
| | | 635 | | |
| | | 636 | | // If both threw the same exception, consider the test passed for this input |
| | 0 | 637 | | return true; |
| | | 638 | | } |
| | 0 | 639 | | } |
| | | 640 | | |
| | 0 | 641 | | if (spanEx is ArgumentException spanArgumentEx && seqEx is ArgumentException seqArgumentEx) |
| | 0 | 642 | | { |
| | | 643 | | // e.g. {"key":"value", "key":"value"} |
| | 0 | 644 | | if (spanArgumentEx.Message.Contains("same key") || seqArgumentEx.Message.Contains("same key")) |
| | 0 | 645 | | { |
| | 0 | 646 | | if (spanArgumentEx?.Message.Equals(seqArgumentEx?.Message) is not true) |
| | 0 | 647 | | { |
| | 0 | 648 | | throw new InvalidOperationException( |
| | 0 | 649 | | $"Span and Sequence readers diverged in same key exception:{Environment.NewLine}" + |
| | 0 | 650 | | $"Span: '{spanArgumentEx?.Message ?? "null"}'{Environment.NewLine}" + |
| | 0 | 651 | | $"Seq: '{seqArgumentEx?.Message ?? "null"}'"); |
| | | 652 | | } |
| | | 653 | | |
| | | 654 | | // If both threw the same exception, consider the test passed for this input |
| | 0 | 655 | | return true; |
| | | 656 | | } |
| | 0 | 657 | | } |
| | | 658 | | |
| | 0 | 659 | | throw new InvalidOperationException($"Unexpected error:{Environment.NewLine}" + |
| | 0 | 660 | | $"Span: '{spanEx?.GetType()}' '{spanEx?.Message ?? "null"}'{Environment.NewLine}" + |
| | 0 | 661 | | $"Seq: '{seqEx?.GetType()}' '{seqEx?.Message ?? "null"}'"); |
| | 4452 | 662 | | } |
| | | 663 | | } |
| | | 664 | | |
| | | 665 | | public enum IntEnum |
| | | 666 | | { |
| | | 667 | | } |
| | | 668 | | |
| | | 669 | | [JsonConverter(typeof(JsonStringEnumConverter))] |
| | | 670 | | public enum StringEnum |
| | | 671 | | { |
| | | 672 | | } |