-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
BinaryWriter.EncodingTests_Serial.cs
55 lines (48 loc) · 2.33 KB
/
BinaryWriter.EncodingTests_Serial.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Numerics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.DotNet.XUnitExtensions;
using Xunit;
namespace System.IO.Tests
{
// WriteChars_VeryLargeArray_DoesNotOverflow allocates a lot of memory and can cause OOM,
// it should not be executed in parallel with other tests
[Collection(nameof(DisableParallelization))]
public class BinaryWriter_EncodingTests_Serial
{
[OuterLoop("Allocates a lot of memory")]
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.Is64BitProcess))]
[SkipOnPlatform(TestPlatforms.Android, "OOM on Android could be uncatchable & kill the test runner")]
public unsafe void WriteChars_VeryLargeArray_DoesNotOverflow()
{
const nuint INT32_OVERFLOW_SIZE = (nuint)int.MaxValue + 3;
SafeBuffer unmanagedBuffer = null;
try
{
try
{
unmanagedBuffer = SafeBufferUtil.CreateSafeBuffer(INT32_OVERFLOW_SIZE * sizeof(byte));
}
catch (OutOfMemoryException)
{
throw new SkipTestException($"Unable to execute {nameof(WriteChars_VeryLargeArray_DoesNotOverflow)} due to OOM"); // skip test in low-mem conditions
}
Assert.True((long)unmanagedBuffer.ByteLength > int.MaxValue);
// reuse same memory for input and output to avoid allocating more memory and OOMs
Span<char> span = new Span<char>((char*)unmanagedBuffer.DangerousGetHandle(), (int)(INT32_OVERFLOW_SIZE / sizeof(char)));
span.Fill('\u0224'); // LATIN CAPITAL LETTER Z WITH HOOK
Stream outStream = new UnmanagedMemoryStream(unmanagedBuffer, 0, (long)unmanagedBuffer.ByteLength, FileAccess.ReadWrite);
BinaryWriter writer = new BinaryWriter(outStream);
writer.Write(span); // will write slightly more than int.MaxValue bytes to the output
Assert.Equal((long)INT32_OVERFLOW_SIZE, outStream.Position);
}
finally
{
unmanagedBuffer?.Dispose();
}
}
}
}