public static void Test(string path, long size)
        {
            int bytesPerCluster = 512;

            while (bytesPerCluster <= 65536)
            {
                string          volumeLabel = "FormatTest_" + bytesPerCluster.ToString();
                VirtualHardDisk disk        = VirtualHardDisk.CreateFixedDisk(path, size);
                disk.ExclusiveLock();
                Partition partition = CreatePrimaryPartition(disk);
                NTFSVolumeCreator.Format(partition, bytesPerCluster, volumeLabel);
                disk.ReleaseLock();
                VHDMountHelper.MountVHD(path);
                string driveName = MountHelper.WaitForDriveToMount(volumeLabel);
                if (driveName == null)
                {
                    throw new Exception("Timeout waiting for volume to mount");
                }
                bool isErrorFree = ChkdskHelper.Chkdsk(driveName);
                if (!isErrorFree)
                {
                    throw new InvalidDataException("CHKDSK reported errors");
                }
                VHDMountHelper.UnmountVHD(path);
                File.Delete(path);

                bytesPerCluster = bytesPerCluster * 2;
            }
        }
Example #2
0
        public static void TestRedo(string path, long size)
        {
            int bytesPerCluster = 512;

            while (bytesPerCluster <= 65536)
            {
                string volumeLabel = "RedoTest_" + bytesPerCluster.ToString();
                string fileName    = "Test.txt";
                byte[] fileData    = System.Text.Encoding.ASCII.GetBytes("Redone");
                CreateVolumeWithPendingFileCreation(path, size, bytesPerCluster, volumeLabel, fileName, fileData);

                VHDMountHelper.MountVHD(path);
                string driveName = MountHelper.WaitForDriveToMount(volumeLabel);
                if (driveName == null)
                {
                    throw new Exception("Timeout waiting for volume to mount");
                }
                bool isErrorFree = ChkdskHelper.Chkdsk(driveName);
                if (!isErrorFree)
                {
                    throw new InvalidDataException("CHKDSK reported errors");
                }
                byte[] bytesRead = File.ReadAllBytes(driveName + fileName);
                if (!ByteUtils.AreByteArraysEqual(fileData, bytesRead))
                {
                    throw new InvalidDataException("Test failed");
                }
                VHDMountHelper.UnmountVHD(path);
                File.Delete(path);

                bytesPerCluster = bytesPerCluster * 2;
            }
        }
        private static void TestCreateAndDeleteFiles(string path, long size, int count)
        {
            int bytesPerCluster = 512;

            while (bytesPerCluster <= 65536)
            {
                string          volumeLabel = "CreateFilesTest_" + bytesPerCluster.ToString();
                VirtualHardDisk disk        = VirtualHardDisk.CreateFixedDisk(path, size);
                disk.ExclusiveLock();
                Partition  partition     = NTFSFormatTests.CreatePrimaryPartition(disk);
                NTFSVolume volume        = NTFSVolumeCreator.Format(partition, bytesPerCluster, volumeLabel);
                string     directoryName = "Directory";
                CreateFiles(volume, directoryName, count);
                disk.ReleaseLock();

                VHDMountHelper.MountVHD(path);
                string driveName = MountHelper.WaitForDriveToMount(volumeLabel);
                if (driveName == null)
                {
                    throw new Exception("Timeout waiting for volume to mount");
                }
                bool isErrorFree = ChkdskHelper.Chkdsk(driveName);
                if (!isErrorFree)
                {
                    throw new InvalidDataException("CHKDSK reported errors");
                }

                if (count != Directory.GetFiles(driveName + directoryName).Length)
                {
                    throw new InvalidDataException("Test failed");
                }
                VHDMountHelper.UnmountVHD(path);
                disk.ExclusiveLock();
                volume = new NTFSVolume(partition);
                DeleteFiles(volume, directoryName, count);
                disk.ReleaseLock();
                VHDMountHelper.MountVHD(path);
                isErrorFree = ChkdskHelper.Chkdsk(driveName);
                if (!isErrorFree)
                {
                    throw new InvalidDataException("CHKDSK reported errors");
                }

                if (Directory.GetFiles(driveName + directoryName).Length > 0)
                {
                    throw new InvalidDataException("Test failed");
                }
                VHDMountHelper.UnmountVHD(path);
                File.Delete(path);

                bytesPerCluster = bytesPerCluster * 2;
            }
        }
        private static void TestMove(string path, long size)
        {
            int bytesPerCluster = 512;

            while (bytesPerCluster <= 65536)
            {
                string          volumeLabel = "MoveTest_" + bytesPerCluster.ToString();
                VirtualHardDisk disk        = VirtualHardDisk.CreateFixedDisk(path, size);
                disk.ExclusiveLock();
                Partition  partition = NTFSFormatTests.CreatePrimaryPartition(disk);
                NTFSVolume volume    = NTFSVolumeCreator.Format(partition, bytesPerCluster, volumeLabel);

                string     directory1Name   = "Directory1";
                string     directory2Name   = "Directory2";
                FileRecord directory1Record = volume.CreateFile(NTFSVolume.RootDirSegmentReference, directory1Name, true);
                FileRecord directory2Record = volume.CreateFile(NTFSVolume.RootDirSegmentReference, directory2Name, true);
                string     fileNameBefore   = "Test1.txt";
                string     fileNameAfter    = "Test2.txt";
                FileRecord fileRecord       = volume.CreateFile(directory1Record.BaseSegmentReference, fileNameBefore, false);
                NTFSFile   file             = new NTFSFile(volume, fileRecord);
                byte[]     fileData         = System.Text.Encoding.ASCII.GetBytes("Test");
                file.Data.WriteBytes(0, fileData);
                volume.UpdateFileRecord(fileRecord);
                volume.MoveFile(fileRecord, directory2Record.BaseSegmentReference, fileNameAfter);
                disk.ReleaseLock();
                VHDMountHelper.MountVHD(path);
                string driveName = MountHelper.WaitForDriveToMount(volumeLabel);
                if (driveName == null)
                {
                    throw new Exception("Timeout waiting for volume to mount");
                }
                bool isErrorFree = ChkdskHelper.Chkdsk(driveName);
                if (!isErrorFree)
                {
                    throw new InvalidDataException("CHKDSK reported errors");
                }
                byte[] bytesRead = File.ReadAllBytes(driveName + directory2Name + "\\" + fileNameAfter);
                if (!ByteUtils.AreByteArraysEqual(fileData, bytesRead))
                {
                    throw new InvalidDataException("Test failed");
                }
                VHDMountHelper.UnmountVHD(path);
                File.Delete(path);

                bytesPerCluster = bytesPerCluster * 2;
            }
        }