예제 #1
0
        public void GetParentLocations()
        {
            MemoryStream baseStream = new MemoryStream();
            MemoryStream diffStream = new MemoryStream();
            using (DiskImageFile baseFile = DiskImageFile.InitializeDynamic(baseStream, Ownership.Dispose, 16 * 1024L * 1024 * 1024))
            {
                // Write some data - exposes bug if mis-calculating where to write data
                using (DiskImageFile diffFile = DiskImageFile.InitializeDifferencing(diffStream, Ownership.None, baseFile, @"C:\TEMP\Base.vhd", @".\Base.vhd", new DateTime(2007, 12, 31)))
                {
                    Disk disk = new Disk(new DiskImageFile[] { diffFile, baseFile }, Ownership.None);
                    disk.Content.Write(new byte[512], 0, 512);
                }
            }

            using (DiskImageFile diffFile = new DiskImageFile(diffStream))
            {
                // Testing the obsolete method - disable warning
            #pragma warning disable 618
                string[] locations = diffFile.GetParentLocations(@"E:\FOO\");
            #pragma warning restore 618
                Assert.AreEqual(2, locations.Length);
                Assert.AreEqual(@"C:\TEMP\Base.vhd", locations[0]);
                Assert.AreEqual(@"E:\FOO\Base.vhd", locations[1]);
            }

            using (DiskImageFile diffFile = new DiskImageFile(diffStream))
            {
                // Testing the new method - note relative path because diff file initialized without a path
                string[] locations = diffFile.GetParentLocations();
                Assert.AreEqual(2, locations.Length);
                Assert.AreEqual(@"C:\TEMP\Base.vhd", locations[0]);
                Assert.AreEqual(@".\Base.vhd", locations[1]);
            }
        }
예제 #2
0
        public void ConstructorFromFiles()
        {
            MemoryStream baseStream = new MemoryStream();
            DiskImageFile baseFile = DiskImageFile.InitializeDynamic(baseStream, Ownership.Dispose, 16 * 1024L * 1024 * 1024);

            MemoryStream childStream = new MemoryStream();
            DiskImageFile childFile = DiskImageFile.InitializeDifferencing(childStream, Ownership.Dispose, baseFile, @"C:\temp\foo.vhd", @".\foo.vhd", DateTime.Now);

            MemoryStream grandChildStream = new MemoryStream();
            DiskImageFile grandChildFile = DiskImageFile.InitializeDifferencing(grandChildStream, Ownership.Dispose, childFile, @"C:\temp\child1.vhd", @".\child1.vhd", DateTime.Now);

            using (Disk disk = new Disk(new DiskImageFile[]{grandChildFile, childFile, baseFile}, Ownership.Dispose))
            {
                Assert.IsNotNull(disk.Content);
            }
        }
예제 #3
0
 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);
     }
 }
예제 #4
0
        public void BuildFixed()
        {
            DiskBuilder builder = new DiskBuilder();
            builder.DiskType = FileType.Fixed;
            builder.Content = diskContent;

            DiskImageFileSpecification[] fileSpecs = builder.Build("foo");
            Assert.AreEqual(1, fileSpecs.Length);
            Assert.AreEqual("foo.vhd", fileSpecs[0].Name);

            using (Disk disk = new Disk(fileSpecs[0].OpenStream(), Ownership.Dispose))
            {
                for (int i = 0; i < 8; i += 1024 * 1024)
                {
                    disk.Content.Position = i;
                    Assert.AreEqual(i, disk.Content.ReadByte());
                }

                disk.Content.Position = 15 * 1024 * 1024;
                Assert.AreEqual(0xFF, disk.Content.ReadByte());
            }
        }
예제 #5
0
        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]);
                }
            }
        }
예제 #6
0
        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);
            }
        }
예제 #7
0
        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);
            }
        }