Beispiel #1
0
 public void Ctor_NullPointerIsZero()
 {
     //Act
     using (var target = new SafeComMemoryHandle(IntPtr.Zero))
     {
         //Assert
         target.Pointer.Should().BeZero();
         target.IsInvalid.Should().BeTrue();
     };
 }
Beispiel #2
0
 public void StringToUnicode_NullStringWorks()
 {
     //Act
     using (var target = SafeComMemoryHandle.StringToUnicode(null))
     {
         //Assert
         target.IsInvalid.Should().BeTrue();
         target.Pointer.Should().BeZero();
     };
 }
Beispiel #3
0
        public void SecureStringToUnicode_ZeroesMemoryWhenClosed()
        {
            var str    = CreateSecureString("Hello");
            var target = SafeComMemoryHandle.SecureStringToUnicode(str);

            //Act
            target.Close();

            //Assert - All we can do is verify it doesn't blow up
            target.IsClosed.Should().BeTrue();
        }
Beispiel #4
0
        public void Dispose_NullPointerWorks()
        {
            //Act
            var target = new SafeComMemoryHandle();

            target.Dispose();

            //Assert - No real way to confirm the memory was released
            target.Pointer.Should().BeZero();
            target.IsInvalid.Should().BeTrue();
        }
Beispiel #5
0
        public void Ctor_ValidPointerIsSet()
        {
            var expected = AllocateMemory(200);

            //Act
            using (var target = new SafeComMemoryHandle(expected))
            {
                //Assert
                target.Pointer.Should().Be(expected);
                target.IsInvalid.Should().BeFalse();
            };
        }
Beispiel #6
0
        public void StringToUnicode_ValidStringWorks()
        {
            string str = "Hello";

            //Act
            using (var target = SafeComMemoryHandle.StringToUnicode(str))
            {
                //Assert
                target.IsInvalid.Should().BeFalse();
                AssertMemory(target.Pointer, Encoding.Unicode.GetBytes("Hello"));
            };
        }
Beispiel #7
0
        public void SecureStringToAnsi_ValidStringWorks()
        {
            var str = CreateSecureString("Hello");

            //Act
            using (var target = SafeComMemoryHandle.SecureStringToAnsi(str))
            {
                //Assert
                target.IsInvalid.Should().BeFalse();
                AssertMemory(target.Pointer, Encoding.ASCII.GetBytes("Hello"));
            };
        }
Beispiel #8
0
        public void Attach_NullPointerWorks()
        {
            //Act
            using (var target = new SafeComMemoryHandle())
            {
                target.Attach(IntPtr.Zero);

                //Assert
                target.Pointer.Should().BeZero();
                target.IsInvalid.Should().BeTrue();
            };
        }
Beispiel #9
0
        public void Detach_NullPointerWorks()
        {
            //Act
            using (var target = new SafeComMemoryHandle())
            {
                var actual = target.Detach();

                //Assert
                actual.Should().BeZero();
                target.Pointer.Should().BeZero();
                target.IsInvalid.Should().BeTrue();
            };
        }
Beispiel #10
0
        public void Allocate_ValidSizeWorks()
        {
            //Act
            using (var target = SafeComMemoryHandle.Allocate(256))
            {
                var actual = target.Pointer;

                var written = WriteMemory(target.Pointer, 256);

                //Assert
                target.IsInvalid.Should().BeFalse();
                AssertMemory(target.Pointer, written);
            };
        }
Beispiel #11
0
        public void Dispose_AttachedPointerWorks()
        {
            var ptr = AllocateMemory(10);

            //Act
            var target = new SafeComMemoryHandle();

            target.Attach(ptr);
            target.Dispose();

            //Assert - doesn't really confirm the memory was released
            target.Pointer.Should().BeZero();
            target.IsInvalid.Should().BeTrue();
        }
Beispiel #12
0
        public void Attach_ValidPointerWorks()
        {
            var expected = AllocateMemory(100);

            //Act
            using (var target = new SafeComMemoryHandle())
            {
                target.Attach(expected);

                //Assert
                target.Pointer.Should().Be(expected);
                target.IsInvalid.Should().BeFalse();
            };
        }
Beispiel #13
0
        public void Detach_ValidPointerWorks()
        {
            var ptr = AllocateMemory(50);

            try
            {
                //Act
                using (var target = new SafeComMemoryHandle(ptr))
                {
                    var actual = target.Detach();

                    //Assert
                    actual.Should().Be(ptr);
                    target.Pointer.Should().BeZero();
                    target.IsInvalid.Should().BeTrue();
                };
            } finally
            {
                FreeMemory(ptr);
            };
        }
Beispiel #14
0
        public void Allocate_InvalidSizeFails()
        {
            Action action = () => SafeComMemoryHandle.Allocate(-5);

            action.ShouldThrowArgumentOutOfRangeException();
        }