コード例 #1
0
 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)
コード例 #2
0
    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)
コード例 #3
0
ファイル: utils.py プロジェクト: angadgill/unix-fs-py
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()
コード例 #4
0
 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)
コード例 #5
0
 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)
コード例 #6
0
 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())
コード例 #7
0
 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()
コード例 #8
0
 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)
コード例 #9
0
 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)
コード例 #10
0
 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)
コード例 #11
0
 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)
コード例 #12
0
 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)
コード例 #13
0
 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)
コード例 #14
0
 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)
コード例 #15
0
 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])
コード例 #16
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()
コード例 #17
0
 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)
コード例 #18
0
 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)
コード例 #19
0
 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)
コード例 #20
0
    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)))
コード例 #21
0
 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)
コード例 #22
0
 def setUp(self):
     open(PATH, 'a').close()
     utils.makefs(PATH)
     self.cls = system.Directory(device=device_io.Disk(PATH))
コード例 #23
0
 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)
コード例 #24
0
 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)