Пример #1
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);
            }
        }
Пример #2
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);
            }
        }
Пример #3
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);
            }
        }