public void Format(DiskDriver disk) { //wipe the disk FREE_SECTOR freeSector = new FREE_SECTOR(disk.BytesPerSector); for (int i = 0; i < disk.SectorCount; i++) { disk.WriteSector(i, freeSector.RawBytes); } //format it DRIVE_INFO driveInfo = new DRIVE_INFO(disk.BytesPerSector, Constants.ROOT_DIR_SECTOR); disk.WriteSector(Constants.DRIVE_INFO_SECTOR, driveInfo.RawBytes); DIR_NODE rootNode = new DIR_NODE(disk.BytesPerSector, Constants.ROOT_DATA_SECTOR, "/", 0); disk.WriteSector(Constants.ROOT_DIR_SECTOR, rootNode.RawBytes); DATA_SECTOR rootData = new DATA_SECTOR(disk.BytesPerSector, 0, new byte[1]); disk.WriteSector(Constants.ROOT_DATA_SECTOR, rootData.RawBytes); }
public void Format(DiskDriver disk) { // wipe all sectors of disk and create minimum required DRIVE_INFO, DIR_NODE and DATA_SECTOR // Wipe all sectors (replace with "zeroes" are FREE_SECTOR) int bps = disk.BytesPerSector; FREE_SECTOR free = new FREE_SECTOR(bps); for (int i = 0; i < disk.SectorCount; i++) { disk.WriteSector(i, free.RawBytes); } // Create DRIVE_INFO DRIVE_INFO di = new DRIVE_INFO(bps, ROOT_DIR_SECTOR); disk.WriteSector(DRIVE_INFO_SECTOR, di.RawBytes); // Create and write the DIR_NODE for the root node... DIR_NODE dn = new DIR_NODE(bps, ROOT_DATA_SECTOR, FSConstants.ROOT_DIR_NAME, 0); disk.WriteSector(ROOT_DIR_SECTOR, dn.RawBytes); // ... and an empty DATA_SECTOR DATA_SECTOR ds = new DATA_SECTOR(bps, 0, null); // 0 = no next data sector, nul = empty set disk.WriteSector(ROOT_DATA_SECTOR, ds.RawBytes); }
public void Delete_ForDirectory_Recurse_DirectoryNotEmpty_CanDelete() { // arrange using (var driver = new DiskDriver()) { driver.CreateOrGetFile(@"c:\folder\subfolder\file.txt"); driver.CreateOrGetFile(@"c:\folder\file.txt"); driver.CreateOrGetFile(@"c:\other_folder\file.txt"); // act var folder = driver.GetDirectory(@"c:\folder"); folder.Delete(true); // assert folder.Refresh(); Assert.IsFalse(folder.Exists); Assert.IsFalse(driver.GetDirectory(@"c:\folder\subfolder").Exists); Assert.IsFalse(driver.GetFile(@"c:\folder\subfolder\file.txt").Exists); var c = driver.GetDirectory(@"c:\"); var filesAfterDelete = c.GetFiles("*", SearchOption.AllDirectories); Assert.AreEqual(1, filesAfterDelete.Length); Assert.AreEqual(@"c:\other_folder\file.txt", filesAfterDelete[0].FullName); var directoriesAfterDelete = c.GetDirectories("*", SearchOption.AllDirectories); Assert.AreEqual(1, directoriesAfterDelete.Length); Assert.AreEqual(@"c:\other_folder", directoriesAfterDelete[0].FullName); } }
public void OpenFile_CreateMode_FileExists_CanOverwriteFile() { // arrange using (var driver = new DiskDriver()) { driver.CreateOrGetDirectory(@"c:\temp"); var file = driver.GetFile(@"c:\temp\file.txt"); using (var writer = new StreamWriter(file.Open(FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))) { writer.Write("1"); } // act using (var writer = new StreamWriter(file.Open(FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))) { writer.Write("2"); } // assert using (var reader = new StreamReader(file.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite))) { Assert.AreEqual("2", reader.ReadToEnd()); } } }
public void MoveTo_TargetIsNewFile_CanMoveAndChangeName() { // arrange using (var driver = new DiskDriver()) { var src = driver.CreateOrGetFile(@"c:\src\file.txt"); driver.CreateOrGetDirectory(@"c:\targ"); using (var t = src.CreateText()) { t.Write("CONTENT"); } // act src.MoveTo(@"c:\targ\moved.txt"); src.Refresh(); // assert var target = driver.GetFile(@"c:\targ\moved.txt"); Assert.AreEqual(@"c:\targ\moved.txt", src.FullName); Assert.IsFalse(driver.GetFile(@"c:\src\file.txt").Exists); Assert.IsTrue(target.Exists); using (var t = target.OpenText()) { Assert.AreEqual("CONTENT", t.ReadToEnd()); } } }
private DRIVE_INFO sector; // caching entire sector for now public VirtualDrive(DiskDriver disk, int driveInfoSector, DRIVE_INFO sector) { this.disk = disk; this.driveInfoSector = driveInfoSector; this.bytesPerDataSector = DATA_SECTOR.MaxDataLength(disk.BytesPerSector); this.sector = sector; }
public VirtualDrive(DiskDriver disk, int driveInfoSector, DRIVE_INFO sector) { mDisk = disk; mDriveInfoSector = driveInfoSector; mBytesPerDataSector = DATA_SECTOR.MaxDataLength(disk.BytesPerSector); mSector = sector; }
public void Format(DiskDriver disk) { // wipe all sectors of disk and create minimum required DRIVE_INFO, DIR_NODE and DATA_SECTOR FREE_SECTOR free = new FREE_SECTOR(disk.BytesPerSector); for (int i = 0; i < disk.SectorCount; i++) { disk.WriteSector(i, free.RawBytes); } // DRIVE_INFO DRIVE_INFO drive = new DRIVE_INFO(disk.BytesPerSector, ROOT_DIR_SECTOR); disk.WriteSector(DRIVE_INFO_SECTOR, drive.RawBytes); // DIR_NODE for root node DIR_NODE rootDir = new DIR_NODE(disk.BytesPerSector, ROOT_DATA_SECTOR, FSConstants.PATH_SEPARATOR.ToString(), 0); disk.WriteSector(ROOT_DIR_SECTOR, rootDir.RawBytes); // DATA_SECTOR for root node DATA_SECTOR data = new DATA_SECTOR(disk.BytesPerSector, 0, new byte[] { 0 }); disk.WriteSector(ROOT_DATA_SECTOR, data.RawBytes); }
public void Timestamps_DeleteDirectory_UpdateTimestamps() { // arrange using (var driver = new DiskDriver()) { var subdir = driver.CreateOrGetDirectory(@"c:\folder\subdir"); var folder = driver.GetDirectory(@"c:\folder"); var beforeDelete = DateTime.Now; var old = beforeDelete.AddMinutes(-5); folder.LastAccessTime = folder.LastWriteTime = folder.CreationTime = old; Assert.IsTrue(folder.LastAccessTime == old); Assert.IsTrue(folder.LastWriteTime == old); Assert.IsTrue(folder.CreationTime == old); // act subdir.Delete(); folder.Refresh(); // Assert Assert.IsTrue(folder.LastAccessTime >= beforeDelete); Assert.IsTrue(folder.LastWriteTime >= beforeDelete); Assert.IsTrue(folder.CreationTime == old); } }
public void Timestamp_DeleteDirectory_GrandparentDirectoryIsntUpdated() { // arrange using (var driver = new DiskDriver()) { var beforeCreate = DateTime.Now; var f = driver.CreateOrGetDirectory(@"c:\parent\subfolder\folder"); var parent = driver.GetDirectory(@"c:\parent"); var expected = beforeCreate.AddMinutes(-5); parent.LastAccessTime = parent.LastWriteTime = parent.CreationTime = expected; Assert.IsTrue(parent.LastAccessTime == expected); Assert.IsTrue(parent.LastWriteTime == expected); Assert.IsTrue(parent.CreationTime == expected); // act var folder = driver.GetDirectory(@"c:\parent\subfolder\folder"); folder.Delete(); // Assert Assert.IsTrue(parent.LastAccessTime == expected); Assert.IsTrue(parent.LastWriteTime == expected); Assert.IsTrue(parent.CreationTime == expected); } }
public void Timestamps_CreateOrGetFile_CanUpdatestamps() { // arrange using (var driver = new DiskDriver()) { var beforeCreate = DateTime.Now; var f = driver.CreateOrGetDirectory(@"c:\folder"); var old = beforeCreate.AddMinutes(-5); f.LastAccessTime = f.LastWriteTime = f.CreationTime = old; Assert.IsTrue(f.LastAccessTime == old); Assert.IsTrue(f.LastWriteTime == old); Assert.IsTrue(f.CreationTime == old); // act var file = driver.CreateOrGetFile(@"c:\folder\file.txt"); f.Refresh(); // Assert Assert.IsTrue(f.LastAccessTime >= beforeCreate); Assert.IsTrue(f.LastWriteTime >= beforeCreate); Assert.IsTrue(f.CreationTime == old); } }
public void Timestamps_DeleteFile_GrandparentDirectoryIsntUpdated() { // arrange using (var driver = new DiskDriver()) { driver.CreateOrGetFile(@"c:\parent\folder\file.txt"); var parent = driver.GetDirectory(@"c:\parent"); var beforeDelete = DateTime.Now; var old = beforeDelete.AddMinutes(-5); parent.LastAccessTime = parent.LastWriteTime = parent.CreationTime = old; Assert.IsTrue(parent.LastAccessTime == old); Assert.IsTrue(parent.LastWriteTime == old); Assert.IsTrue(parent.CreationTime == old); // act var file = driver.GetFile(@"c:\parent\folder\file.txt"); file.Delete(); parent.Refresh(); // Assert Assert.IsTrue(parent.LastAccessTime == old); Assert.IsTrue(parent.LastWriteTime == old); Assert.IsTrue(parent.CreationTime == old); } }
public void CopyTo_TargetIsExistingFile_Overwrite_CanCopy() { // arrange using (var driver = new DiskDriver()) { var src = driver.CreateOrGetFile(@"c:\src\file.txt"); var targ = driver.CreateOrGetFile(@"c:\targ\existing_file.txt"); using (var t = src.CreateText()) { t.Write("CONTENT"); } using (var t = targ.CreateText()) { t.Write("OLD_CONTENT"); } // act var result = src.CopyTo(@"c:\targ\existing_file.txt", true); // assert using (var t = result.OpenText()) { Assert.AreEqual("CONTENT", t.ReadToEnd()); } } }
public void CopyTo_CanCreateNewOnwership() { // arrange using (var driver = new DiskDriver()) { var srcDir = driver.CreateOrGetDirectory(@"c:\src"); var srcFile = driver.CreateOrGetFile(@"c:\src\file.txt"); driver.CreateOrGetFile(@"c:\src\file_2.txt"); var targetDir = driver.CreateOrGetDirectory(@"c:\targ"); // act srcFile.CopyTo(@"c:\targ\file.txt", true); // assert var targetFiles = targetDir.GetFiles(); var srcFiles = srcDir.GetFiles(); Assert.AreEqual(2, srcFiles.Length); Assert.AreEqual(1, srcFiles.Where(i => i.FullName == @"c:\src\file.txt").Count()); Assert.AreEqual(1, srcFiles.Where(i => i.FullName == @"c:\src\file_2.txt").Count()); Assert.AreEqual(1, targetFiles.Length); Assert.AreEqual(@"c:\targ\file.txt", targetFiles[0].FullName); } }
public void GetDirectories_Recurse_Wilchars_CanFind() { // arrange using (var driver = new DiskDriver()) { driver.CreateOrGetDirectory(@"c:\root\folder_1\subfolder_1"); driver.CreateOrGetDirectory(@"c:\root\folder_1\subfolder_2"); driver.CreateOrGetDirectory(@"c:\root\folder_1\subfolder1"); // to check if they are excluded from result driver.CreateOrGetDirectory(@"c:\root\folder_1\subfolder1"); // to check if they are excluded from result driver.CreateOrGetDirectory(@"c:\root\folder_2\subfolder_3"); driver.CreateOrGetDirectory(@"c:\root\folder_2\subfolder_4"); driver.CreateOrGetDirectory(@"c:\root\folder_1\subfolder3"); // to check if they are excluded from result driver.CreateOrGetDirectory(@"c:\root\folder_1\subfolder4"); // to check if they are excluded from result var root = driver.GetDirectory(@"c:\root"); // act var result = root.GetDirectories(@"subfolder_*", SearchOption.AllDirectories); // assert Assert.AreEqual(4, result.Length); Assert.AreEqual(1, result.Where(i => i.FullName == @"c:\root\folder_1\subfolder_1").Count()); Assert.AreEqual(1, result.Where(i => i.FullName == @"c:\root\folder_1\subfolder_2").Count()); Assert.AreEqual(1, result.Where(i => i.FullName == @"c:\root\folder_2\subfolder_3").Count()); Assert.AreEqual(1, result.Where(i => i.FullName == @"c:\root\folder_2\subfolder_4").Count()); } }
public void Format(DiskDriver disk) { // wipe all sectors of disk and create minimum required DRIVE_INFO, DIR_NODE and DATA_SECTOR //how many sectors int numSectors = disk.SectorCount; FREE_SECTOR freeSector = new FREE_SECTOR(disk.BytesPerSector); for (int lba = 0; lba < numSectors; lba++) { disk.WriteSector(lba, freeSector.RawBytes); } DRIVE_INFO diSector1 = new DRIVE_INFO(disk.BytesPerSector, ROOT_DIR_SECTOR); disk.WriteSector(0, diSector1.RawBytes); DIR_NODE rootDirSector = new DIR_NODE(disk.BytesPerSector, ROOT_DATA_SECTOR, FSConstants.ROOT_DIR_NAME, 0); disk.WriteSector(ROOT_DIR_SECTOR, rootDirSector.RawBytes); DATA_SECTOR rootDataSector = new DATA_SECTOR(disk.BytesPerSector, 0, new byte[DATA_SECTOR.MaxDataLength(disk.BytesPerSector)]); disk.WriteSector(ROOT_DATA_SECTOR, rootDataSector.RawBytes); }
public void Move_CanUpdateStubs() { // arrange using (var driver = new DiskDriver()) { driver.CreateOrGetFile(@"c:\src\folder_1\subfolder_1\file_1.txt"); driver.CreateOrGetFile(@"c:\src\folder_1\subfolder_1\file_2.txt"); driver.CreateOrGetFile(@"c:\src\folder_1\subfolder_2\file_1.txt"); driver.CreateOrGetFile(@"c:\src\folder_1\subfolder_2\file_2.txt"); driver.CreateOrGetFile(@"c:\src\folder_2\subfolder_1\file_1.txt"); driver.CreateOrGetFile(@"c:\src\folder_2\subfolder_1\file_2.txt"); driver.CreateOrGetFile(@"c:\src\folder_2\subfolder_2\file_1.txt"); driver.CreateOrGetFile(@"c:\src\folder_2\subfolder_2\file_2.txt"); driver.CreateOrGetDirectory(@"c:\targ"); var folder_1 = driver.GetDirectory(@"c:\src\folder_1"); // act folder_1.MoveTo(@"c:\targ\folder_1"); // assert var fileStub = driver.GetFile(@"c:\targ\folder_1\subfolder_1\file_1.txt"); Assert.AreEqual(@"c:\targ\folder_1\subfolder_1\file_1.txt", fileStub.FullName); fileStub = driver.GetFile(@"c:\targ\folder_1\subfolder_1\file_2.txt"); Assert.AreEqual(@"c:\targ\folder_1\subfolder_1\file_2.txt", fileStub.FullName); fileStub = driver.GetFile(@"c:\targ\folder_1\subfolder_2\file_1.txt"); Assert.AreEqual(@"c:\targ\folder_1\subfolder_2\file_1.txt", fileStub.FullName); fileStub = driver.GetFile(@"c:\targ\folder_1\subfolder_2\file_2.txt"); Assert.AreEqual(@"c:\targ\folder_1\subfolder_2\file_2.txt", fileStub.FullName); fileStub = driver.GetFile(@"c:\src\folder_2\subfolder_1\file_1.txt"); Assert.AreEqual(@"c:\src\folder_2\subfolder_1\file_1.txt", fileStub.FullName); fileStub = driver.GetFile(@"c:\src\folder_2\subfolder_1\file_2.txt"); Assert.AreEqual(@"c:\src\folder_2\subfolder_1\file_2.txt", fileStub.FullName); fileStub = driver.GetFile(@"c:\src\folder_2\subfolder_2\file_1.txt"); Assert.AreEqual(@"c:\src\folder_2\subfolder_2\file_1.txt", fileStub.FullName); fileStub = driver.GetFile(@"c:\src\folder_2\subfolder_2\file_2.txt"); Assert.AreEqual(@"c:\src\folder_2\subfolder_2\file_2.txt", fileStub.FullName); var dirStub = driver.GetDirectory(@"c:\targ"); Assert.AreEqual(@"c:\targ", dirStub.FullName); dirStub = driver.GetDirectory(@"c:\targ\folder_1"); Assert.AreEqual(@"c:\targ\folder_1", dirStub.FullName); dirStub = driver.GetDirectory(@"c:\targ\folder_1\subfolder_1"); Assert.AreEqual(@"c:\targ\folder_1\subfolder_1", dirStub.FullName); dirStub = driver.GetDirectory(@"c:\targ\folder_1\subfolder_2"); Assert.AreEqual(@"c:\targ\folder_1\subfolder_2", dirStub.FullName); dirStub = driver.GetDirectory(@"c:\src"); Assert.AreEqual(@"c:\src", dirStub.FullName); dirStub = driver.GetDirectory(@"c:\src\folder_2"); Assert.AreEqual(@"c:\src\folder_2", dirStub.FullName); dirStub = driver.GetDirectory(@"c:\src\folder_2\subfolder_1"); Assert.AreEqual(@"c:\src\folder_2\subfolder_1", dirStub.FullName); dirStub = driver.GetDirectory(@"c:\src\folder_2\subfolder_2"); Assert.AreEqual(@"c:\src\folder_2\subfolder_2", dirStub.FullName); } }
static bool TestSector(DiskDriver disk, int lba, byte[] testdata) { disk.WriteSector(lba, testdata); byte[] s = disk.ReadSector(lba); bool success = Compare(testdata, s); Console.WriteLine("Compare " + success.ToString()); return(success); }
public void Move_PartOfTargetDoesntExists_Throws() { // arrange using (var driver = new DiskDriver()) { var dir = driver.CreateOrGetDirectory(@"c:\dir"); // act/assert Assert.ThrowsException <DirectoryNotFoundException>(() => dir.MoveTo(@"c:\temp\dir")); } }
public void GetLength_DirectoryDoesntExist_Throws() { // arrange using (var driver = new DiskDriver()) { var file = driver.GetFile(@"c:\temp\file.txt"); // act/assert Assert.ThrowsException <DirectoryNotFoundException>(() => file.Length); } }
public void Parent_DirectoryIsRoot_ReturnsNull() { // arrange using (var driver = new DiskDriver()) { var c = driver.CreateOrGetDirectory(@"c:\"); // assert Assert.IsNull(c.Parent); } }
public void SetCreationTime_DirectoryDoesntExist_Throws() { // arrange using (var driver = new DiskDriver()) { var file = driver.GetFile(@"c:\temp\file.txt"); // act/assert Assert.ThrowsException <DirectoryNotFoundException>(() => file.CreationTime = DateTime.Now); } }
public void GetIsReadOnly_FileDoesntExist_ReturnsTrue() { // arrange using (var driver = new DiskDriver()) { var file = driver.GetFile(@"c:\temp\file.txt"); // act/assert Assert.IsTrue(file.IsReadOnly); } }
public void SetIsReadOnly_DirDoesntExist_Throws() { // arrange using (var driver = new DiskDriver()) { var file = driver.GetFile(@"c:\temp\file.txt"); // act/assert Assert.ThrowsException <DirectoryNotFoundException>(() => file.IsReadOnly = true); } }
public void SetAttributes_DirectoryDoesntExist_Throws() { // arrange using (var driver = new DiskDriver()) { var file = driver.GetFile(@"c:\temp\file.txt"); // act/assert Assert.ThrowsException <DirectoryNotFoundException>(() => file.Attributes = FileAttributes.Hidden); } }
public void Root_ReturnsRoot() { // arrange using (var driver = new DiskDriver()) { var f = driver.CreateOrGetDirectory(@"c:\folder\subfolder"); // assert Assert.AreEqual(@"c:\", f.Root.FullName); } }
public void CopyTo_SourceDirDoesntExist_Throws() { // arrange using (var driver = new DiskDriver()) { var src = driver.GetFile(@"c:\src\file.txt"); // act/assert Assert.ThrowsException <DirectoryNotFoundException>(() => src.CopyTo(@"c:\targ\file.txt", true)); } }
public void CreateSubdirectory_PathIsAbsolute_Throws() { // arrange using (var driver = new DiskDriver()) { var folder = driver.CreateOrGetDirectory(@"c:\folder"); // act/assert Assert.ThrowsException <ArgumentException>(() => folder.CreateSubdirectory(@"c:\folder\subfolder")); } }
public void MoveTo_TargetDirectoryDoesntExist_Throws() { // arrange using (var driver = new DiskDriver()) { var src = driver.CreateOrGetFile(@"c:\src\file.txt"); // act/assert Assert.ThrowsException <DirectoryNotFoundException>(() => src.MoveTo(@"c:\targ\file.txt")); } }
public void GetFiles_DirectoryDoesntExist_Throws() { // arrange using (var driver = new DiskDriver()) { var root = driver.GetDirectory(@"c:\root"); // act/assert Assert.ThrowsException <DirectoryNotFoundException>(() => root.GetFiles(@"file.txt", SearchOption.AllDirectories)); } }