This repository was archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Add tests for calling Span APIs via reflection to verify graceful failures. #28674
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5679d46
Add tests for calling Span APIs via reflection to verify graceful fai…
ahsonkhan 5d48d5e
Merge branch 'master' of https://github.com/dotnet/corefx into AddRef…
ahsonkhan 20682f1
Add Span reflection tests to verify graceful failures
ahsonkhan f189b6f
Address PR feedback (test cleanup and rename)
ahsonkhan 367b945
Fix up some test names
ahsonkhan ec3e46f
Minor tweak: Reorder the tests
ahsonkhan 8ebe8e0
Exclude running the reflection tests for portable span (netfx)
ahsonkhan ffb5a90
Remove unused constant from csproj
ahsonkhan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using Xunit; | ||
| using System.Buffers; | ||
| using System.Buffers.Binary; | ||
| using System.Reflection; | ||
| using System.Runtime.InteropServices; | ||
| using System.MemoryTests; | ||
|
|
||
| namespace System.SpanTests | ||
| { | ||
| public static partial class SpanTests | ||
| { | ||
| // Calling Span APIs via Reflection is not supported yet. | ||
| // These tests check that using reflection results in graceful failures. See https://github.com/dotnet/coreclr/issues/17296 | ||
| // These tests are only relevant for fast span. | ||
|
|
||
| [Fact] | ||
| public static void MemoryExtensions_StaticReturningReadOnlySpan() | ||
| { | ||
| Type type = typeof(MemoryExtensions); | ||
|
|
||
| MethodInfo method = type.GetMethod(nameof(MemoryExtensions.AsSpan), new Type[] { typeof(string) }); | ||
| Assert.Throws<NotSupportedException>(() => method.Invoke(null, new object[] { "Hello" })); | ||
|
|
||
| method = type.GetMethod(nameof(MemoryExtensions.AsSpan), new Type[] { typeof(string), typeof(int), typeof(int) }); | ||
| Assert.Throws<NotSupportedException>(() => method.Invoke(null, new object[] { "Hello", 1, 1 })); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void MemoryExtensions_StaticWithSpanArguments() | ||
| { | ||
| Type type = typeof(MemoryExtensions); | ||
|
|
||
| MethodInfo method = type.GetMethod(nameof(MemoryExtensions.CompareTo)); | ||
|
|
||
| int result = (int)method.Invoke(null, new object[] { default, default, StringComparison.Ordinal }); | ||
| Assert.Equal(0, result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void BinaryPrimitives_StaticWithSpanArgument() | ||
| { | ||
| Type type = typeof(BinaryPrimitives); | ||
|
|
||
| MethodInfo method = type.GetMethod(nameof(BinaryPrimitives.ReadInt16LittleEndian)); | ||
| Assert.Throws<TargetInvocationException>(() => method.Invoke(null, new object[] { default })); | ||
|
|
||
| method = type.GetMethod(nameof(BinaryPrimitives.TryReadInt16LittleEndian)); | ||
| bool result = (bool)method.Invoke(null, new object[] { default, null }); | ||
| Assert.False(result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void MemoryMarshal_GenericStaticReturningSpan() | ||
| { | ||
| Type type = typeof(MemoryMarshal); | ||
|
|
||
| int value = 0; | ||
| ref int refInt = ref value; | ||
|
|
||
| MethodInfo method = type.GetMethod(nameof(MemoryMarshal.CreateSpan)).MakeGenericMethod((refInt.GetType())); | ||
| Assert.Throws<NotSupportedException>(() => method.Invoke(null, new object[] { null, 0 })); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void Span_Constructor() | ||
| { | ||
| Type type = typeof(Span<int>); | ||
|
|
||
| ConstructorInfo ctor = type.GetConstructor(new Type[] { typeof(int[]) }); | ||
| Assert.Throws<TargetException>(() => ctor.Invoke(new object[] { new int[10] })); | ||
|
|
||
| ctor = type.GetConstructor(new Type[] { typeof(int[]), typeof(int), typeof(int) }); | ||
| Assert.Throws<TargetException>(() => ctor.Invoke(new object[] { new int[10], 1, 1 })); | ||
|
|
||
| ctor = type.GetConstructor(new Type[] { typeof(void*), typeof(int) }); | ||
| Assert.Throws<TargetException>(() => ctor.Invoke(new object[] { null, 1 })); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void Span_Property() | ||
| { | ||
| Type type = typeof(Span<int>); | ||
|
|
||
| PropertyInfo property = type.GetProperty(nameof(Span<int>.Empty)); | ||
| Assert.Throws<NotSupportedException>(() => property.GetValue(default)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void Span_StaticOperator() | ||
| { | ||
| Type type = typeof(Span<int>); | ||
|
|
||
| MethodInfo method = type.GetMethod("op_Equality"); | ||
| Assert.Throws<NotSupportedException>(() => method.Invoke(null, new object[] { default, default })); | ||
|
|
||
| method = type.GetMethod("op_Inequality"); | ||
| Assert.Throws<NotSupportedException>(() => method.Invoke(null, new object[] { default, default })); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void Span_InstanceMethod() | ||
| { | ||
| Type type = typeof(Span<int>); | ||
|
|
||
| MethodInfo method = type.GetMethod(nameof(Span<int>.CopyTo), new Type[] { typeof(Span<int>) }); | ||
| Assert.Throws<NotSupportedException>(() => method.Invoke(default, new object[] { default })); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void ReadOnlySpan_Constructor() | ||
| { | ||
| Type type = typeof(ReadOnlySpan<int>); | ||
|
|
||
| ConstructorInfo ctor = type.GetConstructor(new Type[] { typeof(int[]) }); | ||
| Assert.Throws<TargetException>(() => ctor.Invoke(new object[] { new int[10] })); | ||
|
|
||
| ctor = type.GetConstructor(new Type[] { typeof(int[]), typeof(int), typeof(int) }); | ||
| Assert.Throws<TargetException>(() => ctor.Invoke(new object[] { new int[10], 1, 1 })); | ||
|
|
||
| ctor = type.GetConstructor(new Type[] { typeof(void*), typeof(int) }); | ||
| Assert.Throws<TargetException>(() => ctor.Invoke(new object[] { null, 1 })); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void ReadOnlySpan_Property() | ||
| { | ||
| Type type = typeof(ReadOnlySpan<int>); | ||
|
|
||
| PropertyInfo property = type.GetProperty(nameof(ReadOnlySpan<int>.Empty)); | ||
| Assert.Throws<NotSupportedException>(() => property.GetValue(default)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void ReadOnlySpan_Operator() | ||
| { | ||
| Type type = typeof(ReadOnlySpan<int>); | ||
|
|
||
| MethodInfo method = type.GetMethod("op_Equality"); | ||
| Assert.Throws<NotSupportedException>(() => method.Invoke(null, new object[] { default, default })); | ||
|
|
||
| method = type.GetMethod("op_Inequality"); | ||
| Assert.Throws<NotSupportedException>(() => method.Invoke(null, new object[] { default, default })); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void ReadOnlySpan_InstanceMethod() | ||
| { | ||
| Type type = typeof(ReadOnlySpan<int>); | ||
|
|
||
| MethodInfo method = type.GetMethod(nameof(ReadOnlySpan<int>.CopyTo), new Type[] { typeof(Span<int>) }); | ||
| Assert.Throws<NotSupportedException>(() => method.Invoke(default, new object[] { default })); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void Memory_PropertyReturningSpan() | ||
| { | ||
| Type type = typeof(Memory<int>); | ||
|
|
||
| PropertyInfo property = type.GetProperty(nameof(Memory<int>.Span)); | ||
| Assert.Throws<NotSupportedException>(() => property.GetValue(null)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void ReadOnlyMemory_PropertyReturningReadOnlySpan() | ||
| { | ||
| Type type = typeof(ReadOnlyMemory<int>); | ||
|
|
||
| PropertyInfo property = type.GetProperty(nameof(ReadOnlyMemory<int>.Span)); | ||
| Assert.Throws<NotSupportedException>(() => property.GetValue(null)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void MemoryManager_MethodReturningSpan() | ||
| { | ||
| Type type = typeof(MemoryManager<int>); | ||
|
|
||
| MemoryManager<int> manager = new CustomMemoryForTest<int>(new int[10]); | ||
| MethodInfo method = type.GetMethod(nameof(MemoryManager<int>.GetSpan), BindingFlags.Public | BindingFlags.Instance); | ||
| Assert.Throws<NotSupportedException>(() => method.Invoke(manager, null)); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Anything to validate about the TargetException's inner exception?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What should we validate about the inner exception? That it is null?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the inner exception is null, then nevermind. I thought TargetException is often thrown from reflection when it's wrapping another exception, but maybe that's only true of TargetInvocationException?