예제 #1
0
        public void VirtualDiskMirror_Synchronous()
        {
            string vhdxpath = Path.Combine(Environment.CurrentDirectory, "testvhdx.vhdx");

            if (File.Exists(vhdxpath))
            {
                File.Delete(vhdxpath);
            }

            string mirrorpath = Path.Combine(Environment.CurrentDirectory, "testmirror.vhdx");

            if (File.Exists(mirrorpath))
            {
                File.Delete(mirrorpath);
            }

            // Create an 8MB virtual disk with a non-standard block size
            VirtualDiskCreateParameters createparams = new VirtualDiskCreateParameters(vhdxpath, VirtualDiskType.VHDX, 8 * 1024 * 1024);

            createparams.BlockSize = 1024 * 1024;

            using (VirtualDisk vdisk = VirtualDisk.Create(createparams))
            {
                Assert.IsNotNull(vdisk);
                vdisk.Mirror(mirrorpath);
            }

            // Open the mirror and ensure that the virtual size and block size match the source
            using (VirtualDisk mirror = VirtualDisk.Open(mirrorpath))
            {
                Assert.IsNotNull(mirror);
                Assert.AreEqual((ulong)8 * 1024 * 1024, mirror.VirtualSize);
                Assert.AreEqual((ulong)1024 * 1024, mirror.BlockSize);
            }
        }
예제 #2
0
        public void VirtualDiskCreate_ISO()
        {
            // You can't create ISOs with the virtual disk API - should always fail
            string isopath = Path.Combine(Environment.CurrentDirectory, "testiso.iso");

            try { using (VirtualDisk vdisk = VirtualDisk.Create(isopath, VirtualDiskType.ISO, 4 * 1024 * 1024)) { }; Assert.Fail(); }
            catch (Exception) { }
        }
예제 #3
0
 public static void ClassInit(TestContext context)
 {
     s_vdiskpath = Path.Combine(Environment.CurrentDirectory, "testmetadata.vhdx");
     if (File.Exists(s_vdiskpath))
     {
         File.Delete(s_vdiskpath);
     }
     s_vdisk = VirtualDisk.Create(s_vdiskpath, VirtualDiskType.VHDX, 4 * 1024 * 1024);
     Assert.IsNotNull(s_vdisk);
 }
예제 #4
0
        public void VirtualDiskCreate_VHDX()
        {
            string vhdxpath = Path.Combine(Environment.CurrentDirectory, "testvhdx.vhdx");

            if (File.Exists(vhdxpath))
            {
                File.Delete(vhdxpath);
            }

            using (VirtualDisk vdisk = VirtualDisk.Create(vhdxpath, VirtualDiskType.VHDX, 4 * 1024 * 1024))
            {
                Assert.IsNotNull(vdisk);
            }
        }
예제 #5
0
        public void CreateDiffTest()
        {
            const int sz = 0x03010400;

            try
            {
                using (var vhdp = VirtualDisk.Create(tmpfn, sz))
                    using (var vhd = VirtualDisk.CreateDifferencing(tmpcfn, tmpfn))
                    {
                        Assert.That(System.IO.File.Exists(tmpcfn));
                        Assert.That(System.IO.File.Exists(tmpfn));
                        Assert.That(System.IO.File.Exists(tmpfn));
                        Assert.That(vhd.Attached, Is.False);
                        Assert.That(vhd.BlockSize, Is.EqualTo(0x200000));
                        Assert.That(vhd.DiskType, Is.EqualTo(VirtualDisk.DeviceType.Vhd));
                        Assert.That(vhd.Enabled, Is.False);
                        //Assert.That(vhd.FragmentationPercentage, Is.Zero); // must be non-differencing
                        Assert.That(vhd.Identifier, Is.Not.EqualTo(Guid.Empty));
                        Assert.That(vhd.Is4kAligned);
                        Assert.That(vhd.IsLoaded, Is.False);
                        Assert.That(vhd.IsRemote, Is.False);
                        Assert.That(vhd.LogicalSectorSize, Is.EqualTo(0x200));
                        //Assert.That(vhd.MostRecentId, Is.Null.Or.Empty);
                        Assert.That(vhd.NewerChanges, Is.False);
                        Assert.That(vhd.ParentBackingStore, Is.EqualTo(tmpfn));        // must be differencing
                        Assert.That(vhd.ParentIdentifier, Is.Not.EqualTo(Guid.Empty)); // must be differencing
                        Assert.That(vhd.ParentPaths, Is.Null);                         // must be differencing
                        Assert.That(vhd.ParentTimeStamp, Is.Zero);                     // must be differencing
                        //TestContext.WriteLine(vhd.PhysicalPath); // must be attached
                        Assert.That(vhd.PhysicalSectorSize, Is.EqualTo(0x200));
                        Assert.That(vhd.PhysicalSize, Is.LessThan(sz));
                        Assert.That(vhd.ProviderSubtype, Is.EqualTo(VirtualDisk.Subtype.Differencing));
                        Assert.That(vhd.SectorSize, Is.EqualTo(0x200));
                        //Debug.WriteLine(vhd.SmallestSafeVirtualSize); // must have file system
                        Assert.That(vhd.VendorId, Is.Not.EqualTo(Guid.Empty));
                        Assert.That(vhd.VhdPhysicalSectorSize, Is.EqualTo(0x200));
                        Assert.That(vhd.VirtualDiskId, Is.Not.EqualTo(Guid.Empty));
                        Assert.That(vhd.VirtualSize, Is.EqualTo(sz));
                    }
            }
            finally
            {
                System.IO.File.Delete(tmpcfn);
                System.IO.File.Delete(tmpfn);
            }
        }
예제 #6
0
        public void UnsafeResizeTest()
        {
            const int sz = 0x810400;

            try
            {
                using (var vhd = VirtualDisk.Create(tmpfn, sz * 2, true, null))
                {
                    Assert.That(System.IO.File.Exists(tmpfn));
                    Assert.That(vhd.VirtualSize, Is.EqualTo(sz * 2));
                    Assert.That(() => vhd.UnsafeResize(sz), Throws.Exception);
                }
            }
            finally
            {
                System.IO.File.Delete(tmpfn);
            }
        }
예제 #7
0
        public void VirtualDiskExpand_DynamicVHDX()
        {
            string vhdxpath = Path.Combine(Environment.CurrentDirectory, "testvhdx.vhdx");

            if (File.Exists(vhdxpath))
            {
                File.Delete(vhdxpath);
            }

            using (VirtualDisk vdisk = VirtualDisk.Create(vhdxpath, VirtualDiskType.VHDX, 4 * 1024 * 1024))
            {
                Assert.IsNotNull(vdisk);
                Assert.AreEqual((ulong)(4 * 1024 * 1024), vdisk.VirtualSize);

                vdisk.Expand(8 * 1024 * 1024);
                Assert.AreEqual((ulong)(8 * 1024 * 1024), vdisk.VirtualSize);
            }
        }
예제 #8
0
        public void VirtualDiskExpand_FixedVHD()
        {
            string vhdpath = Path.Combine(Environment.CurrentDirectory, "testvhd.vhd");

            if (File.Exists(vhdpath))
            {
                File.Delete(vhdpath);
            }

            using (VirtualDisk vdisk = VirtualDisk.Create(vhdpath, VirtualDiskType.VHDX, 4 * 1024 * 1024, VirtualDiskCreateFlags.FullPhysicalAllocation))
            {
                Assert.IsNotNull(vdisk);
                Assert.AreEqual((ulong)(4 * 1024 * 1024), vdisk.VirtualSize);

                vdisk.Expand(8 * 1024 * 1024);
                Assert.AreEqual((ulong)(8 * 1024 * 1024), vdisk.VirtualSize);
            }
        }
예제 #9
0
        public void CreateDynPropTest()
        {
            const int sz = 0x03010200;

            try
            {
                using (var vhd = VirtualDisk.Create(tmpfn, sz))
                //using (var pv = new PrivilegedCodeBlock(SystemPrivilege.ManageVolume))
                {
                    //vhd.Attach(true);
                    Assert.That(System.IO.File.Exists(tmpfn));
                    Assert.That(vhd.Attached, Is.False);
                    Assert.That(vhd.BlockSize, Is.EqualTo(0x200000));
                    Assert.That(vhd.DiskType, Is.EqualTo(VirtualDisk.DeviceType.Vhd));
                    Assert.That(vhd.Enabled, Is.False);
                    Assert.That(vhd.FragmentationPercentage, Is.Zero);
                    Assert.That(vhd.Identifier, Is.Not.EqualTo(Guid.Empty));
                    Assert.That(vhd.Is4kAligned);
                    Assert.That(vhd.IsLoaded, Is.False);
                    Assert.That(vhd.IsRemote, Is.False);
                    Assert.That(vhd.LogicalSectorSize, Is.EqualTo(0x200));
                    Assert.That(vhd.MostRecentId, Is.Null.Or.Empty);
                    Assert.That(vhd.NewerChanges, Is.False);
                    //Debug.WriteLine(vhd.ParentBackingStore); // must be differencing
                    //Debug.WriteLine(vhd.ParentIdentifier); // must be differencing
                    //Debug.WriteLine(vhd.ParentPaths); // must be differencing
                    //Debug.WriteLine(vhd.ParentTimeStamp); // must be differencing
                    //Debug.WriteLine(vhd.PhysicalPath); // must be attached
                    Assert.That(vhd.PhysicalSectorSize, Is.EqualTo(0x200));
                    Assert.That(vhd.PhysicalSize, Is.LessThan(sz));
                    Assert.That(vhd.ProviderSubtype, Is.EqualTo(VirtualDisk.Subtype.Dynamic));
                    Assert.That(vhd.SectorSize, Is.EqualTo(0x200));
                    //Debug.WriteLine(vhd.SmallestSafeVirtualSize); // must have file system
                    Assert.That(vhd.VendorId, Is.Not.EqualTo(Guid.Empty));
                    Assert.That(vhd.VhdPhysicalSectorSize, Is.EqualTo(0x200));
                    Assert.That(vhd.VirtualDiskId, Is.Not.EqualTo(Guid.Empty));
                    Assert.That(vhd.VirtualSize, Is.EqualTo(sz));
                }
            }
            finally
            {
                System.IO.File.Delete(tmpfn);
            }
        }
예제 #10
0
        public void ExpandTest()
        {
            const int sz = 0x810400;

            try
            {
                using (var vhd = VirtualDisk.Create(tmpfn, sz, true, null))
                {
                    Assert.That(System.IO.File.Exists(tmpfn));
                    Assert.That(vhd.VirtualSize, Is.EqualTo(sz));
                    vhd.Expand(sz * 2);
                    Assert.That(vhd.VirtualSize, Is.EqualTo(sz * 2));
                }
            }
            finally
            {
                System.IO.File.Delete(tmpfn);
            }
        }
예제 #11
0
        public void CreateFixedPropTest()
        {
            const int sz = 0x03010400;

            try
            {
                using (var vhd = VirtualDisk.Create(tmpfn, sz, false, null))
                {
                    Assert.That(System.IO.File.Exists(tmpfn));
                    Assert.That(vhd.PhysicalSize, Is.EqualTo(sz + 512));
                    Assert.That(vhd.ProviderSubtype, Is.EqualTo(VirtualDisk.Subtype.Fixed));
                    Assert.That(vhd.VirtualSize, Is.EqualTo(sz));
                }
            }
            finally
            {
                System.IO.File.Delete(tmpfn);
            }
        }
예제 #12
0
        public async Task VirtualDiskExpand_Async()
        {
            string vhdxpath = Path.Combine(Environment.CurrentDirectory, "testvhdx.vhdx");

            if (File.Exists(vhdxpath))
            {
                File.Delete(vhdxpath);
            }

            using (VirtualDisk vdisk = VirtualDisk.Create(vhdxpath, VirtualDiskType.VHDX, 4 * 1024 * 1024, VirtualDiskCreateFlags.FullPhysicalAllocation))
            {
                Assert.IsNotNull(vdisk);
                Assert.AreEqual((ulong)(4 * 1024 * 1024), vdisk.VirtualSize);

                await vdisk.ExpandAsync(new VirtualDiskExpandParameters(8 * 1024 * 1024, VirtualDiskExpandFlags.None), CancellationToken.None, null);

                Assert.AreEqual((ulong)(8 * 1024 * 1024), vdisk.VirtualSize);
            }
        }
예제 #13
0
        public void MergeTest()
        {
            const int sz = 0x03010400;

            try
            {
                using (var vhdp = VirtualDisk.Create(tmpfn, sz))
                    Assert.That(System.IO.File.Exists(tmpfn));
                using (var vhd = VirtualDisk.CreateDifferencing(tmpcfn, tmpfn))
                {
                    Assert.That(System.IO.File.Exists(tmpcfn));
                    vhd.Merge(1, 2);
                }
            }
            finally
            {
                System.IO.File.Delete(tmpcfn);
                System.IO.File.Delete(tmpfn);
            }
        }
예제 #14
0
        //[Test()]
        public void DetachTest()
        {
            const int sz = 0x03010400;

            try
            {
                using (var vhd = VirtualDisk.Create(tmpfn, sz))
                {
                    Assert.That(vhd.Attached, Is.False);
                    vhd.Attach(false, false);
                    Assert.That(vhd.Attached, Is.False);
                }
                Assert.That(VirtualDisk.GetAllAttachedVirtualDiskPaths(), Has.Some.EqualTo(tmpfn));
                Assert.That(() => VirtualDisk.Detach(tmpfn), Throws.Nothing);
                Assert.That(VirtualDisk.GetAllAttachedVirtualDiskPaths(), Is.Empty);
            }
            finally
            {
                System.IO.File.Delete(tmpfn);
            }
        }
예제 #15
0
        public void GetSetMetadataTest()
        {
            const int sz  = 0x03010200;
            var       lfn = tmpfn + "x";

            try
            {
                using (var vhd = VirtualDisk.Create(lfn, sz))
                {
                    var count = 0;
                    Assert.That(() => count = vhd.Metadata.Count, Throws.Nothing);

                    // Try get and set
                    var guid = Guid.NewGuid();
                    Assert.That(() => vhd.Metadata.Add(guid, new SafeCoTaskMemHandle("Testing")), Throws.Nothing);
                    Assert.That(vhd.Metadata.Count, Is.EqualTo(count + 1));
                    Assert.That(vhd.Metadata.ContainsKey(Guid.NewGuid()), Is.False);
                    Assert.That(vhd.Metadata.TryGetValue(guid, out var mem), Is.True);
                    Assert.That(mem.ToString(-1), Is.EqualTo("Testing"));

                    // Try enumerate and get
                    foreach (var mkv in vhd.Metadata)
                    {
                        Assert.That(mkv.Key, Is.Not.EqualTo(Guid.Empty));
                        Assert.That(mkv.Value.Size, Is.Not.Zero);
                        TestContext.WriteLine($"{mkv.Key}={mkv.Value.Size}b:{mkv.Value.ToString(-1)}");
                    }

                    // Try remove
                    Assert.That(vhd.Metadata.Remove(guid), Is.True);
                    Assert.That(vhd.Metadata.TryGetValue(guid, out mem), Is.False);
                    Assert.That(vhd.Metadata.Count, Is.EqualTo(count));
                }
            }
            finally
            {
                System.IO.File.Delete(lfn);
            }
        }