예제 #1
0
        public unsafe void CreateFromUnmanagedMemoryStream_LargeIntSize()
        {
            var assembly = TestResources.Basic.Members;

            fixed(byte *assemblyPtr = assembly)
            {
                // ensure that having an extremely large stream is not a problem (e.g. that we don't wrap the int around
                // to be a negative size).
                var disposed = false;
                var seeked   = false;
                var stream   = new MockUnmanagedMemoryStream(assemblyPtr, (long)int.MaxValue + 1)
                {
                    OnDispose = _ => disposed = true,
                    OnSeek    = (_, _) => seeked = true,
                };

                var metadata = ModuleMetadata.CreateFromStream(stream, leaveOpen: false);

                Assert.Equal(new AssemblyIdentity("Members"), metadata.Module.ReadAssemblyIdentityOrThrow());

                // Disposing the metadata should dispose the stream.
                metadata.Dispose();
                Assert.True(disposed);

                // We should have not seeked.  This stream will still be read as a direct memory block.
                Assert.False(seeked);
            }
        }
예제 #2
0
        public unsafe void CreateFromMetadata_Assembly_Stream_DisposeOwnerFalse()
        {
            var       assembly = TestResources.Basic.Members;
            PEHeaders h        = new PEHeaders(new MemoryStream(assembly));

            fixed(byte *ptr = &assembly[h.MetadataStartOffset])
            {
                var disposed = false;
                var seeked   = false;
                var stream   = new MockUnmanagedMemoryStream(ptr, h.MetadataSize)
                {
                    OnDispose = _ => disposed = true,
                    OnSeek    = (_, _) => seeked = true,
                };

                var metadata = ModuleMetadata.CreateFromMetadata((IntPtr)stream.PositionPointer, (int)stream.Length, stream, disposeOwner: false);

                Assert.Equal(new AssemblyIdentity("Members"), metadata.Module.ReadAssemblyIdentityOrThrow());

                // Disposing the metadata should not dispose the stream.
                metadata.Dispose();
                Assert.False(disposed);

                stream.Dispose();
                Assert.True(disposed);

                // We should have never seeked.  The pointer should have been used directly.
                Assert.False(seeked);
            }
        }
예제 #3
0
        public unsafe void CreateFromUnmanagedMemoryStream_Prefetcha_LeaveOpenTrue(PEStreamOptions options)
        {
            var assembly = TestResources.Basic.Members;

            fixed(byte *assemblyPtr = assembly)
            {
                var disposed = false;
                var seeked   = false;
                var stream   = new MockUnmanagedMemoryStream(assemblyPtr, assembly.LongLength)
                {
                    OnDispose = _ => disposed = true,
                    OnSeek    = (_, _) => seeked = true,
                };

                var metadata = ModuleMetadata.CreateFromStream(stream, options | PEStreamOptions.LeaveOpen);

                Assert.Equal(new AssemblyIdentity("Members"), metadata.Module.ReadAssemblyIdentityOrThrow());

                // Disposing the metadata should not dispose the stream.
                metadata.Dispose();
                Assert.False(disposed);

                stream.Dispose();
                Assert.True(disposed);

                // We should have seeked.  This stream will be viewed as a normal stream since we're prefetching
                // everything.
                Assert.True(seeked);
            }
        }
예제 #4
0
        public unsafe void CreateFromUnmanagedMemoryStream_LeaveOpenTrue()
        {
            var assembly = TestResources.Basic.Members;

            fixed(byte *assemblyPtr = assembly)
            {
                var disposed = false;
                var seeked   = false;
                var stream   = new MockUnmanagedMemoryStream(assemblyPtr, assembly.LongLength)
                {
                    OnDispose = _ => disposed = true,
                    OnSeek    = (_, _) => seeked = true,
                };

                var metadata = ModuleMetadata.CreateFromStream(stream, leaveOpen: true);

                Assert.Equal(new AssemblyIdentity("Members"), metadata.Module.ReadAssemblyIdentityOrThrow());

                // Disposing the metadata should not dispose the stream.
                metadata.Dispose();
                Assert.False(disposed);

                stream.Dispose();
                Assert.True(disposed);

                // We should have never seeked.  The pointer should have been used directly.
                Assert.False(seeked);
            }
        }