Esempio n. 1
0
        public static void RunTest()
        {
            var testHandle = new TestSafeHandle(initialValue);

            Assert.True(SafeHandleNative.SafeHandleByValue(testHandle, initialValue));
            Assert.False(testHandle.IsClosed);

            Assert.True(SafeHandleNative.SafeHandleByRef(ref testHandle, initialValue, newValue));
            Assert.False(testHandle.IsClosed);

            testHandle = null;
            SafeHandleNative.SafeHandleOut(out testHandle, initialValue);
            Assert.False(testHandle.IsClosed);

            testHandle = SafeHandleNative.SafeHandleReturn(newValue);
            Assert.False(testHandle.IsClosed);

            testHandle = SafeHandleNative.SafeHandleReturn_Swapped(newValue);
            Assert.False(testHandle.IsClosed);

            var str = new SafeHandleNative.StructWithHandle
            {
                handle = new TestSafeHandle(initialValue)
            };

            SafeHandleNative.StructWithSafeHandleByValue(str, initialValue);
            Assert.False(str.handle.IsClosed);

            SafeHandleNative.StructWithSafeHandleByRef(ref str, initialValue, initialValue);
            Assert.False(str.handle.IsClosed);
        }
Esempio n. 2
0
        public static void RunTest()
        {
            // Test that our SafeHandle-derived object has its underlying handle set after a P/Invoke
            // even if there's an exception during the unmarshal phase.
            IntPtr         value = (IntPtr)123;
            TestSafeHandle h     = new TestSafeHandle();

            Assert.Throws <InvalidOperationException>(() => SafeHandleNative.GetHandleAndCookie(value, out h, out var cookie));

            Assert.AreEqual(value, h.DangerousGetHandle());
        }
Esempio n. 3
0
        public static void RunTest()
        {
            var testHandle = new TestSafeHandle(initialValue);

            Assert.True(SafeHandleNative.SafeHandleByValue(testHandle, initialValue));

            Assert.True(SafeHandleNative.SafeHandleByRef(ref testHandle, initialValue, newValue));
            Assert.Equal(newValue, testHandle.DangerousGetHandle());

            AbstractDerivedSafeHandle abstrHandle = new AbstractDerivedSafeHandleImplementation(initialValue);

            Assert.True(SafeHandleNative.SafeHandleInByRef(abstrHandle, initialValue));
            Assert.Throws <MarshalDirectiveException>(() => SafeHandleNative.SafeHandleByRef(ref abstrHandle, initialValue, newValue));

            NoDefaultConstructorSafeHandle noDefaultCtorHandle = new NoDefaultConstructorSafeHandle(initialValue);

            Assert.Throws <MissingMethodException>(() => SafeHandleNative.SafeHandleByRef(ref noDefaultCtorHandle, initialValue, newValue));

            testHandle = null;
            SafeHandleNative.SafeHandleOut(out testHandle, initialValue);
            Assert.Equal(initialValue, testHandle.DangerousGetHandle());

            testHandle = SafeHandleNative.SafeHandleReturn(newValue);
            Assert.Equal(newValue, testHandle.DangerousGetHandle());

            Assert.Throws <MarshalDirectiveException>(() => SafeHandleNative.SafeHandleReturn_AbstractDerived(initialValue));
            Assert.Throws <MissingMethodException>(() => SafeHandleNative.SafeHandleReturn_NoDefaultConstructor(initialValue));

            var abstractDerivedImplementationHandle = SafeHandleNative.SafeHandleReturn_AbstractDerivedImplementation(initialValue);

            Assert.Equal(initialValue, abstractDerivedImplementationHandle.DangerousGetHandle());

            testHandle = SafeHandleNative.SafeHandleReturn_Swapped(newValue);
            Assert.Equal(newValue, testHandle.DangerousGetHandle());

            Assert.Throws <MarshalDirectiveException>(() => SafeHandleNative.SafeHandleReturn_Swapped_AbstractDerived(initialValue));
            Assert.Throws <MissingMethodException>(() => SafeHandleNative.SafeHandleReturn_Swapped_NoDefaultConstructor(initialValue));

            var str = new SafeHandleNative.StructWithHandle
            {
                handle = new TestSafeHandle(initialValue)
            };

            Assert.True(SafeHandleNative.StructWithSafeHandleByValue(str, initialValue));

            Assert.True(SafeHandleNative.StructWithSafeHandleByRef(ref str, initialValue, initialValue));

            // Cannot change the value of a SafeHandle-derived field in a struct when marshalling byref.
            Assert.Throws <NotSupportedException>(() => SafeHandleNative.StructWithSafeHandleByRef(ref str, initialValue, newValue));

            // Cannot create a SafeHandle-derived field value.
            Assert.Throws <NotSupportedException>(() => SafeHandleNative.StructWithSafeHandleOut(out var defaultOutStruct, initialValue));
        }
Esempio n. 4
0
        public static void RunTest()
        {
            // Test that our SafeHandle-derived object has its underlying handle set after a P/Invoke
            // even if there's an exception during the unmarshal phase.
            IntPtr         value = (IntPtr)123;
            TestSafeHandle h     = new TestSafeHandle();

            Assert.Throws <InvalidOperationException>(() => SafeHandleNative.GetHandleAndCookie(out _, value, out h));

            Assert.AreEqual(value, h.DangerousGetHandle());

            // Try again, this time triggering unmarshal failure with an array.
            value = (IntPtr)456;
            h     = new TestSafeHandle();

            Assert.Throws <OverflowException>(() => SafeHandleNative.GetHandleAndArray(out _, out _, value, out h));

            Assert.AreEqual(value, h.DangerousGetHandle());
        }
        public static void RunTest()
        {
            if (TestLibrary.Utilities.IsWindows)
            {
                // The interface marshaller is only available when COM interop is
                // enabled. The interface marshaller is what initiates the COM
                // interop system which is what subsequently defines defined exception
                // type to throw - matches .NET Framework behavior. At present support
                // is limited to Windows so we branch on that.
                Assert.Throws <InvalidOperationException>(() => MarshalSafeHandleAsInterface());
            }
            else
            {
                // When the interface marshaller is not available we fallback to
                // the marshalling system which will throw a different exception.
                Assert.Throws <MarshalDirectiveException>(() => MarshalSafeHandleAsInterface());
            }

            Assert.Throws <MarshalDirectiveException>(() => SafeHandleNative.SafeHandle_Invalid(new TestSafeHandle[1]));
            Assert.Throws <TypeLoadException>(() => SafeHandleNative.SafeHandle_Invalid(new SafeHandleNative.StructWithSafeHandleArray()));
        }
 static void MarshalSafeHandleAsInterface()
 {
     SafeHandleNative.SafeHandle_Invalid(new TestSafeHandle());
 }
 public static void RunTest()
 {
     Assert.Throws <InvalidOperationException>(() => SafeHandleNative.SafeHandle_Invalid(new TestSafeHandle()));
     Assert.Throws <MarshalDirectiveException>(() => SafeHandleNative.SafeHandle_Invalid(new TestSafeHandle[1]));
     Assert.Throws <TypeLoadException>(() => SafeHandleNative.SafeHandle_Invalid(new SafeHandleNative.StructWithSafeHandleArray()));
 }