-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Base.cs
89 lines (75 loc) · 3.28 KB
/
Base.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.IO.Pipes;
using System.Threading;
using Microsoft.Win32.SafeHandles;
using Xunit;
namespace System.IO.Tests
{
public abstract class RandomAccess_Base<T> : FileSystemTest
{
protected abstract T MethodUnderTest(SafeFileHandle handle, byte[] bytes, long fileOffset);
protected virtual bool UsesOffsets => true;
public static IEnumerable<object[]> GetSyncAsyncOptions()
{
yield return new object[] { FileOptions.None };
if (PlatformDetection.IsAsyncFileIOSupported)
{
yield return new object[] { FileOptions.Asynchronous };
}
}
[Fact]
public void ThrowsArgumentNullExceptionForNullHandle()
{
AssertExtensions.Throws<ArgumentNullException>("handle", () => MethodUnderTest(null, Array.Empty<byte>(), 0));
}
[Fact]
public void ThrowsArgumentExceptionForInvalidHandle()
{
SafeFileHandle handle = new SafeFileHandle(new IntPtr(-1), ownsHandle: false);
AssertExtensions.Throws<ArgumentException>("handle", () => MethodUnderTest(handle, Array.Empty<byte>(), 0));
}
[Fact]
public void ThrowsObjectDisposedExceptionForDisposedHandle()
{
SafeFileHandle handle = File.OpenHandle(GetTestFilePath(), FileMode.Create, FileAccess.Write);
handle.Dispose();
Assert.Throws<ObjectDisposedException>(() => MethodUnderTest(handle, Array.Empty<byte>(), 0));
}
[Fact]
[SkipOnPlatform(TestPlatforms.Browser, "System.IO.Pipes aren't supported on browser")]
public void ThrowsNotSupportedExceptionForUnseekableFile()
{
using (var server = new AnonymousPipeServerStream(PipeDirection.Out))
using (SafeFileHandle handle = new SafeFileHandle(server.SafePipeHandle.DangerousGetHandle(), ownsHandle: false))
{
Assert.Throws<NotSupportedException>(() => MethodUnderTest(handle, Array.Empty<byte>(), 0));
}
}
[Theory]
[MemberData(nameof(GetSyncAsyncOptions))]
public void ThrowsArgumentOutOfRangeExceptionForNegativeFileOffset(FileOptions options)
{
if (UsesOffsets)
{
using (SafeFileHandle handle = File.OpenHandle(GetTestFilePath(), FileMode.CreateNew, FileAccess.Write, options: options))
{
AssertExtensions.Throws<ArgumentOutOfRangeException>("fileOffset", () => MethodUnderTest(handle, Array.Empty<byte>(), -1));
}
}
}
protected static CancellationTokenSource GetCancelledTokenSource()
{
CancellationTokenSource source = new CancellationTokenSource();
source.Cancel();
return source;
}
protected SafeFileHandle GetHandleToExistingFile(FileAccess access, FileOptions options)
{
string filePath = GetTestFilePath();
File.WriteAllBytes(filePath, new byte[1]);
return File.OpenHandle(filePath, FileMode.Open, access, FileShare.None, options);
}
}
}