public void Attributes()
        {
            MemoryStream stream = new MemoryStream();

            using (Disk disk = Disk.InitializeDynamic(stream, Ownership.Dispose, 16 * 1024L * 1024 * 1024))
            {
                Stream s = disk.Content;
                Assert.IsTrue(s.CanRead);
                Assert.IsTrue(s.CanWrite);
                Assert.IsTrue(s.CanSeek);
            }
        }
示例#2
0
        public override VirtualDisk CreateDisk(FileLocator locator, string variant, string path, VirtualDiskParameters diskParameters)
        {
            switch (variant)
            {
            case "fixed":
                return(Disk.InitializeFixed(locator, path, diskParameters.Capacity, diskParameters.Geometry));

            case "dynamic":
                return(Disk.InitializeDynamic(locator, path, diskParameters.Capacity, diskParameters.Geometry, DynamicHeader.DefaultBlockSize));

            default:
                throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Unknown VHD disk variant '{0}'", variant), "variant");
            }
        }
        public void ReadWriteSmall()
        {
            MemoryStream stream = new MemoryStream();

            using (Disk disk = Disk.InitializeDynamic(stream, Ownership.None, 16 * 1024L * 1024 * 1024))
            {
                byte[] content = new byte[100];
                for (int i = 0; i < content.Length; ++i)
                {
                    content[i] = (byte)i;
                }

                Stream s = disk.Content;
                s.Write(content, 10, 40);
                Assert.AreEqual(40, s.Position);
                s.Write(content, 50, 50);
                Assert.AreEqual(90, s.Position);
                s.Position = 0;

                byte[] buffer = new byte[100];
                s.Read(buffer, 10, 60);
                Assert.AreEqual(60, s.Position);
                for (int i = 0; i < 10; ++i)
                {
                    Assert.AreEqual(0, buffer[i]);
                }
                for (int i = 10; i < 60; ++i)
                {
                    Assert.AreEqual(i, buffer[i]);
                }
            }

            // Check the data persisted
            using (Disk disk = new Disk(stream, Ownership.Dispose))
            {
                Stream s = disk.Content;

                byte[] buffer = new byte[100];
                s.Read(buffer, 10, 20);
                Assert.AreEqual(20, s.Position);
                for (int i = 0; i < 10; ++i)
                {
                    Assert.AreEqual(0, buffer[i]);
                }
                for (int i = 10; i < 20; ++i)
                {
                    Assert.AreEqual(i, buffer[i]);
                }
            }
        }
        public void UndisposedChangedDynamic()
        {
            byte[] firstSector = new byte[512];
            byte[] lastSector  = new byte[512];

            MemoryStream ms = new MemoryStream();

            using (Disk newDisk = Disk.InitializeDynamic(ms, Ownership.None, 16 * 1024L * 1024 * 1024))
            {
            }

            using (Disk disk = new Disk(ms, Ownership.None))
            {
                disk.Content.Write(new byte[1024], 0, 1024);

                ms.Position = 0;
                ms.Read(firstSector, 0, 512);
                ms.Seek(-512, SeekOrigin.End);
                ms.Read(lastSector, 0, 512);
                Assert.AreEqual(firstSector, lastSector);
            }

            // Check disabling AutoCommit really doesn't do the commit
            using (Disk disk = new Disk(ms, Ownership.None))
            {
                disk.AutoCommitFooter = false;
                disk.Content.Position = 10 * 1024 * 1024;
                disk.Content.Write(new byte[1024], 0, 1024);

                ms.Position = 0;
                ms.Read(firstSector, 0, 512);
                ms.Seek(-512, SeekOrigin.End);
                ms.Read(lastSector, 0, 512);
                Assert.AreNotEqual(firstSector, lastSector);
            }

            // Also check that after disposing, the commit happens
            ms.Position = 0;
            ms.Read(firstSector, 0, 512);
            ms.Seek(-512, SeekOrigin.End);
            ms.Read(lastSector, 0, 512);
            Assert.AreEqual(firstSector, lastSector);


            // Finally, check default value for AutoCommit lines up with behaviour
            using (Disk disk = new Disk(ms, Ownership.None))
            {
                Assert.True(disk.AutoCommitFooter);
            }
        }
        public void Setup()
        {
            MemoryStream fileStream = new MemoryStream();
            Disk         baseFile   = Disk.InitializeDynamic(fileStream, Ownership.Dispose, 16 * 1024L * 1024);

            for (int i = 0; i < 8; i += 1024 * 1024)
            {
                baseFile.Content.Position = i;
                baseFile.Content.WriteByte((byte)i);
            }

            baseFile.Content.Position = 15 * 1024 * 1024;
            baseFile.Content.WriteByte(0xFF);

            diskContent = baseFile.Content;
        }
        public void InitializeDynamic()
        {
            MemoryStream ms = new MemoryStream();

            using (Disk disk = Disk.InitializeDynamic(ms, Ownership.None, 16 * 1024L * 1024 * 1024))
            {
                Assert.IsNotNull(disk);
                Assert.That(disk.Geometry.Capacity > 15.8 * 1024L * 1024 * 1024 && disk.Geometry.Capacity <= 16 * 1024L * 1024 * 1024);
            }

            Assert.Greater(1 * 1024 * 1024, ms.Length);

            using (Disk disk = new Disk(ms, Ownership.Dispose))
            {
                Assert.That(disk.Geometry.Capacity > 15.8 * 1024L * 1024 * 1024 && disk.Geometry.Capacity <= 16 * 1024L * 1024 * 1024);
            }
        }
        public void DisposeTest()
        {
            Stream contentStream;

            MemoryStream stream = new MemoryStream();

            using (Disk disk = Disk.InitializeDynamic(stream, Ownership.None, 16 * 1024L * 1024 * 1024))
            {
                contentStream = disk.Content;
            }

            try
            {
                contentStream.Position = 0;
                Assert.Fail();
            }
            catch (ObjectDisposedException) { }
        }
        public void ReadNotPresent()
        {
            MemoryStream stream = new MemoryStream();

            using (Disk disk = Disk.InitializeDynamic(stream, Ownership.Dispose, 16 * 1024L * 1024 * 1024))
            {
                byte[] buffer = new byte[100];
                disk.Content.Seek(2 * 1024 * 1024, SeekOrigin.Current);
                disk.Content.Read(buffer, 0, buffer.Length);

                for (int i = 0; i < 100; ++i)
                {
                    if (buffer[i] != 0)
                    {
                        Assert.Fail();
                    }
                }
            }
        }
        public void ConstructorDynamic()
        {
            Geometry     geometry;
            MemoryStream ms = new MemoryStream();

            using (Disk disk = Disk.InitializeDynamic(ms, Ownership.None, 16 * 1024L * 1024 * 1024))
            {
                geometry = disk.Geometry;
            }
            using (Disk disk = new Disk(ms, Ownership.None))
            {
                Assert.AreEqual(geometry, disk.Geometry);
                Assert.IsNotNull(disk.Content);
            }
            using (Disk disk = new Disk(ms, Ownership.Dispose))
            {
                Assert.AreEqual(geometry, disk.Geometry);
                Assert.IsNotNull(disk.Content);
            }
        }