// See the LICENSE file in the project root for more information.
using System.Buffers;
+using System.Buffers.Text;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using (var jsonUtf8 = new Utf8JsonWriter(output, options))
{
+ Assert.Throws<ArgumentException>(() => jsonUtf8.WriteBase64StringValue(value.AsSpan(0, 125_000_001)));
+ }
+
+ using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+ {
Assert.Throws<ArgumentException>(() => jsonUtf8.WriteBase64StringValue(value));
}
}
}
+ // NOTE: WritingTooLargeProperty test is constrained to run on Windows and MacOSX because it causes
+ // problems on Linux due to the way deferred memory allocation works. On Linux, the allocation can
+ // succeed even if there is not enough memory but then the test may get killed by the OOM killer at the
+ // time the memory is accessed which triggers the full memory allocation.
+ [PlatformSpecific(TestPlatforms.Windows | TestPlatforms.OSX)]
+ [ConditionalTheory(nameof(IsX64))]
+ [OuterLoop]
+ [InlineData(true, true)]
+ [InlineData(true, false)]
+ [InlineData(false, true)]
+ [InlineData(false, false)]
+ public void WritingLargestPossibleBase64Bytes(bool formatted, bool skipValidation)
+ {
+ byte[] value;
+
+ try
+ {
+ value = new byte[125_000_000];
+ }
+ catch (OutOfMemoryException)
+ {
+ return;
+ }
+
+ value.AsSpan().Fill(168);
+
+ var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
+ var output = new ArrayBufferWriter<byte>(1024);
+
+ using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+ {
+ jsonUtf8.WriteBase64StringValue(value);
+ }
+
+ output.Clear();
+ using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+ {
+ jsonUtf8.WriteStartObject();
+ jsonUtf8.WriteBase64String("foo", value);
+ jsonUtf8.WriteEndObject();
+ }
+
+ output.Clear();
+ using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+ {
+ jsonUtf8.WriteStartObject();
+ jsonUtf8.WriteBase64String(Encoding.UTF8.GetBytes("foo"), value);
+ jsonUtf8.WriteEndObject();
+ }
+
+ output.Clear();
+ using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+ {
+ jsonUtf8.WriteStartObject();
+ jsonUtf8.WriteBase64String("foo".AsSpan(), value);
+ jsonUtf8.WriteEndObject();
+ }
+
+ output.Clear();
+ using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+ {
+ jsonUtf8.WriteStartObject();
+ jsonUtf8.WriteBase64String(JsonEncodedText.Encode("foo"), value);
+ jsonUtf8.WriteEndObject();
+ }
+ }
+
+ // https://github.com/dotnet/corefx/issues/40755
+ [Theory]
+ [InlineData(true, true)]
+ [InlineData(true, false)]
+ [InlineData(false, true)]
+ [InlineData(false, false)]
+ public void Writing3MBBase64Bytes(bool formatted, bool skipValidation)
+ {
+ byte[] value = new byte[3 * 1024 * 1024];
+
+ value.AsSpan().Fill(168);
+
+ byte[] base64StringUtf8 = new byte[Base64.GetMaxEncodedToUtf8Length(value.Length)];
+ Base64.EncodeToUtf8(value, base64StringUtf8, out _, out int bytesWritten);
+ string expectedValue = Encoding.UTF8.GetString(base64StringUtf8.AsSpan(0, bytesWritten).ToArray());
+
+ string expectedJson = formatted ? $"{{{Environment.NewLine} \"foo\": \"{expectedValue}\"{Environment.NewLine}}}" : $"{{\"foo\":\"{expectedValue}\"}}";
+
+ var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
+ var output = new ArrayBufferWriter<byte>(1024);
+
+ using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+ {
+ jsonUtf8.WriteBase64StringValue(value);
+ }
+ JsonTestHelper.AssertContents($"\"{expectedValue}\"", output);
+
+ output.Clear();
+ using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+ {
+ jsonUtf8.WriteStartObject();
+ jsonUtf8.WriteBase64String("foo", value);
+ jsonUtf8.WriteEndObject();
+ }
+ JsonTestHelper.AssertContents(expectedJson, output);
+
+ output.Clear();
+ using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+ {
+ jsonUtf8.WriteStartObject();
+ jsonUtf8.WriteBase64String(Encoding.UTF8.GetBytes("foo"), value);
+ jsonUtf8.WriteEndObject();
+ }
+ JsonTestHelper.AssertContents(expectedJson, output);
+
+ output.Clear();
+ using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+ {
+ jsonUtf8.WriteStartObject();
+ jsonUtf8.WriteBase64String("foo".AsSpan(), value);
+ jsonUtf8.WriteEndObject();
+ }
+ JsonTestHelper.AssertContents(expectedJson, output);
+
+ output.Clear();
+ using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+ {
+ jsonUtf8.WriteStartObject();
+ jsonUtf8.WriteBase64String(JsonEncodedText.Encode("foo"), value);
+ jsonUtf8.WriteEndObject();
+ }
+ JsonTestHelper.AssertContents(expectedJson, output);
+ }
+
[Theory]
[InlineData(true, true)]
[InlineData(true, false)]
JsonTestHelper.AssertContents("{" + ValidUtf8Expected + ":" + ValidUtf8Expected + "}", output);
}
- private static readonly string s_InvalidUtf16Input = new string (new char[2] { (char)0xD801, 'a' });
+ private static readonly string s_InvalidUtf16Input = new string(new char[2] { (char)0xD801, 'a' });
private const string InvalidUtf16Expected = "\"\\uFFFDa\"";
private static readonly string s_ValidUtf16Input = new string(new char[2] { (char)0xD801, (char)0xDC37 }); // 0x10437