def test_add_few__with_device(self): expected = (['test1', 'test2'], [1, 2]) self.cls.add('test1', 1) self.cls.add('test2', 2) block = ds.DirectoryBlock(device=self.cls._device, index=1) self.cls = system.Directory(device=device_io.Disk(PATH), index=0) self.assertEqual(self.cls.read(), expected)
def test_write_2(self): write_data = bytes(ds.BLOCK_SIZE * 3) + \ b'\x00\x00\x00\x00\x00\x00\x00\x00' + \ b'\x00\x00\x00\x00\x00\x00\x00\x00' + \ b'\x00\x00\x00\x00\x00\x00\x00\x00' + \ b'\x00\x00\x00\x00\x00\x00\x00\x00' + \ b'\x00\x00\x00\x00\x00\x00\x00\x00' + \ b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' with open(PATH, 'wb') as f: f.write(write_data) expected = bytes(ds.BLOCK_SIZE * 3)+ \ b'\x01\x00\x00\x00\x00\x00\x00\x00' + \ b'\x01\x00\x00\x00\x00\x00\x00\x00' + \ b'\x02\x00\x00\x00\x00\x00\x00\x00' + \ b'\x03\x00\x00\x00\x00\x00\x00\x00' + \ b'\x04\x00\x00\x00\x00\x00\x00\x00' + \ b'\x05\x00\x00\x00\x00\x00\x00\x00\x00\x00' self.cls = ds.Inode(device=device_io.Disk(PATH), index=2) self.cls.i_type = 1 self.cls.address_direct = [1, 2, 3, 4, 5] self.cls.__write__() with open(PATH, 'rb') as f: output = f.read() self.assertEqual(output, expected)
def makefs(root_path, verbose=False): """ Layout: Superblock All Inodes Inode Freelist Block Freelist Root Directory """ if verbose: print("Creating file system at {}".format(root_path)) bootstrap_data = bytes(ds.SuperBlock()) bootstrap_data += bytes(ds.BLOCK_SIZE * ds.NUM_INODES) bootstrap_data += bytes(ds.InodeFreeList()) bootstrap_data += bytes(ds.DataBlockFreeList()) # TODO: Update write a real root directory bootstrap_data += bytes(ds.DirectoryBlock()) bootstrap_data += bytes(ds.BLOCK_SIZE * ds.NUM_DATA_BLOCKS) disk = device_io.Disk(root_path) disk.open() disk.seek(0) disk.write(bootstrap_data) # TODO: remove this hack when real root directory is written ds.DataBlockFreeList(device=disk).allocate() disk.close()
def test_write(self): expected = b'\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01' + \ b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' self.cls._device = device_io.Disk(PATH) self.cls.__write__() with open(PATH, 'rb') as f: output = f.read() self.assertEqual(output, expected)
def test_multiple_write_partial_block_read(self): input_text1 = ''.join(['t' for _ in range(int(ds.BLOCK_SIZE / 2))]) input_text2 = ''.join(['t' for _ in range(ds.BLOCK_SIZE * 2)]) self.cls.write(input_text1) self.cls.write(input_text2) self.cls = system.File(device=device_io.Disk(PATH), index=0) output = self.cls.read() self.assertEqual(output, input_text1 + input_text2)
def test_is_full_false(self): self.cls = ds.DataBlock(index=0) input_data = bytes(self.cls.address * ds.BLOCK_SIZE) + \ b'this is test data\x00\x00\x00' with open(PATH, 'wb') as f: f.write(input_data) self.cls = ds.DataBlock(device=device_io.Disk(PATH), index=0) self.assertFalse(self.cls.is_full())
def setUp(self): """ Executed before each test case """ open(PATH, 'a').close() # create file b = bytearray([12, 12, 12]) f = device_io.Disk(PATH) f.open() f.write(b) f.close()
def test_read(self): input_data = b'\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01' + \ b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' with open(PATH, 'wb') as f: f.write(input_data) self.cls = ds.FreeList(n=10, device=device_io.Disk(PATH)) output = self.cls.list expected = [True] * 10 self.assertEqual(output, expected)
def test_read_2(self): input_data = b'\x0A\x00\x00\x00\x00\x00\x00\x00' + \ b'\x0A\x00\x00\x00\x00\x00\x00\x00' + \ b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' with open(PATH, 'wb') as f: f.write(input_data) device = device_io.Disk(PATH) self.cls = ds.SuperBlock(device) self.assertEqual(self.cls.block_size, 10) self.assertEqual(self.cls.num_inodes, 10)
def test_read(self): self.cls = ds.DataBlock(index=0) input_data = bytes(self.cls.address * ds.BLOCK_SIZE) + \ b'this is test data\x00\x00\x00' with open(PATH, 'wb') as f: f.write(input_data) self.cls = ds.DataBlock(device=device_io.Disk(PATH), index=0) expected = 'this is test data' output = self.cls.data self.assertEqual(output, expected)
def test_deallocate_with_device(self): input_data = b'\x00\x00\x00\x00\x01\x01\x01\x01\x01\x01' + \ b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' with open(PATH, 'wb') as f: f.write(input_data) self.cls = ds.FreeList(n=10, device=device_io.Disk(PATH)) self.cls.deallocate(index=1) output = self.cls.list expected = [False, True, False, False] + [True] * 6 self.assertEqual(output, expected)
def test_write(self): self.cls = ds.DataBlock(index=0) expected = bytes(self.cls.address * ds.BLOCK_SIZE) + \ b'this is test data\x00\x00\x00' self.cls._device = device_io.Disk(PATH) self.cls.data = 'this is test data' self.cls.__write__() with open(PATH, 'rb') as f: output = f.read() self.assertEqual(output, expected)
def test_allocate_overflow_with_device(self): input_data = bytes(self.cls.address * ds.BLOCK_SIZE) + \ b'\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01' + \ b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' with open(PATH, 'wb') as f: f.write(input_data) self.cls = ds.DataBlockFreeList(device=device_io.Disk(PATH)) with self.assertRaises(Exception): for i in range(11): _ = self.cls.allocate(write_through=False)
def test_allocate_with_device(self): input_data = b'\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01' + \ b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' with open(PATH, 'wb') as f: f.write(input_data) self.cls = ds.FreeList(n=10, device=device_io.Disk(PATH)) for i in range(3): _ = self.cls.allocate(write_through=False) output = self.cls.list expected = [False] * 3 + [True] * 7 self.assertEqual(output, expected)
def test_add_write_read(self): reload(ds) ds.NUM_FILES_PER_DIR_BLOCK = 5 reload(utils) reload(device_io) utils.makefs(PATH) self.cls = ds.DirectoryBlock(device=device_io.Disk(PATH)) self.cls.add_entry('f1', 1) self.cls = ds.DirectoryBlock(device=self.cls._device, index=1) self.assertEqual(self.cls.entry_names, ['f1', '', '', '', '']) self.assertEqual(self.cls.entry_inode_indices, [1, 0, 0, 0, 0])
def test_allocate_with_device(self): input_data = bytes(ds.SuperBlock()) + \ bytes(ds.NUM_INODES * ds.BLOCK_SIZE) + \ bytes(ds.InodeFreeList()) with open(PATH, 'wb') as f: f.write(input_data) self.cls = ds.Inode(device=device_io.Disk(PATH)) # self.assertEqual(self.cls.index, None) # New inode should have a None index # self.cls.allocate() self.assertEqual(self.cls.index, 0) with self.assertRaises(Exception): self.cls.allocate()
def test_write_1(self): ds.BLOCK_SIZE = 30 device_io.BLOCK_SIZE = 30 ds.NUM_INODES = 10 expected = b'\x1e\x00\x00\x00\x00\x00\x00\x00' + \ b'\x0A\x00\x00\x00\x00\x00\x00\x00' + \ b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' self.cls = ds.SuperBlock(None) self.cls._device = device_io.Disk(PATH) self.cls.__write__() with open(PATH, 'rb') as f: output = f.read() self.assertEqual(output, expected)
def test_read_2(self): input_data = bytes(ds.BLOCK_SIZE * 3) + \ b'\x01\x00\x00\x00\x00\x00\x00\x00' + \ b'\x01\x00\x00\x00\x00\x00\x00\x00' + \ b'\x02\x00\x00\x00\x00\x00\x00\x00' + \ b'\x03\x00\x00\x00\x00\x00\x00\x00' + \ b'\x04\x00\x00\x00\x00\x00\x00\x00' + \ b'\x05\x00\x00\x00\x00\x00\x00\x00\x00\x00' expected = [1, 1, 2, 3, 4, 5] with open(PATH, 'wb') as f: f.write(input_data) self.cls = ds.Inode(device=device_io.Disk(PATH), index=2) output = self.cls._items self.assertEqual(output, expected)
def test_add_item_with_device(self): self.cls = ds.DirectoryBlock(index=0) self.cls._device = device_io.Disk(PATH) self.cls.name = 'test' self.cls.add_entry('f1', 1) # self.cls.__write__() expected = bytes(self.cls.address * ds.BLOCK_SIZE) + \ b'test\x00' \ b'f1\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' \ b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' \ b'\x00\x00' \ b'\x01\x00\x00\x00\x00\x00\x00\x00' \ b'\x00\x00\x00\x00\x00\x00\x00\x00' \ b'\x00\x00\x00\x00\x00\x00\x00\x00' \ b'\x00\x00\x00\x00\x00\x00\x00\x00' \ b'\x00\x00\x00\x00\x00\x00\x00\x00' with open(PATH, 'rb') as f: output = f.read() self.assertEqual(output, expected)
def test_read(self): self.cls = ds.DirectoryBlock(index=0) input_data = bytes(self.cls.address * ds.BLOCK_SIZE) + \ b'test\x00' \ b'f0\x00\x00\x00f1\x00\x00\x00f2\x00\x00\x00f3\x00\x00\x00f4\x00\x00\x00' + \ b'\x00\x00' \ b'\x00\x00\x00\x00\x00\x00\x00\x00' \ b'\x01\x00\x00\x00\x00\x00\x00\x00' \ b'\x02\x00\x00\x00\x00\x00\x00\x00' \ b'\x03\x00\x00\x00\x00\x00\x00\x00' \ b'\x04\x00\x00\x00\x00\x00\x00\x00' with open(PATH, 'wb') as f: f.write(input_data) self.cls = ds.DirectoryBlock(device=device_io.Disk(PATH), index=0) self.assertEqual(self.cls.name, 'test') self.assertEqual(self.cls.entry_names, ['f{}'.format(i) for i in range(5)]) self.assertEqual(self.cls.entry_inode_indices, list(range(5)))
def test_write(self): self.cls = ds.DirectoryBlock(index=0) self.cls._device = device_io.Disk(PATH) self.cls.name = 'test' self.cls.entry_names = ['f{}'.format(i) for i in range(5)] self.cls.entry_inode_indices = list(range(5)) self.cls.__write__() expected = bytes(self.cls.address * ds.BLOCK_SIZE) + \ b'test\x00' \ b'f0\x00\x00\x00f1\x00\x00\x00f2\x00\x00\x00f3\x00\x00\x00f4\x00\x00\x00' \ b'\x00\x00' \ b'\x00\x00\x00\x00\x00\x00\x00\x00' \ b'\x01\x00\x00\x00\x00\x00\x00\x00' \ b'\x02\x00\x00\x00\x00\x00\x00\x00' \ b'\x03\x00\x00\x00\x00\x00\x00\x00' \ b'\x04\x00\x00\x00\x00\x00\x00\x00' with open(PATH, 'rb') as f: output = f.read() self.assertEqual(output, expected)
def setUp(self): open(PATH, 'a').close() utils.makefs(PATH) self.cls = system.Directory(device=device_io.Disk(PATH))
def test_write_read_long(self): input_text = ''.join(['t' for _ in range(ds.BLOCK_SIZE * 2)]) self.cls.write(input_text) self.cls = system.File(device=device_io.Disk(PATH), index=0) self.assertEqual(self.cls.read(), input_text)
def test_add_with_device(self): expected = (['test1'], [1]) self.cls.add('test1', 1) self.cls = system.Directory(device=device_io.Disk(PATH), index=0) self.assertEqual(self.cls.read(), expected)