예제 #1
0
    def test_seek_offset(self):
        """Tests the seek_offset function."""
        test_source = unittest.source
        if not test_source:
            raise unittest.SkipTest("missing source")

        vsmbr_partition = pyvsmbr.partition()

        vsmbr_partition.open(test_source)

        size = vsmbr_partition.get_size()

        vsmbr_partition.seek_offset(16, os.SEEK_SET)

        offset = vsmbr_partition.get_offset()
        self.assertEqual(offset, 16)

        vsmbr_partition.seek_offset(16, os.SEEK_CUR)

        offset = vsmbr_partition.get_offset()
        self.assertEqual(offset, 32)

        vsmbr_partition.seek_offset(-16, os.SEEK_CUR)

        offset = vsmbr_partition.get_offset()
        self.assertEqual(offset, 16)

        if size > 16:
            vsmbr_partition.seek_offset(-16, os.SEEK_END)

            offset = vsmbr_partition.get_offset()
            self.assertEqual(offset, size - 16)

        vsmbr_partition.seek_offset(16, os.SEEK_END)

        offset = vsmbr_partition.get_offset()
        self.assertEqual(offset, size + 16)

        # TODO: change IOError into ValueError
        with self.assertRaises(IOError):
            vsmbr_partition.seek_offset(-1, os.SEEK_SET)

        # TODO: change IOError into ValueError
        with self.assertRaises(IOError):
            vsmbr_partition.seek_offset(-32 - size, os.SEEK_CUR)

        # TODO: change IOError into ValueError
        with self.assertRaises(IOError):
            vsmbr_partition.seek_offset(-32 - size, os.SEEK_END)

        # TODO: change IOError into ValueError
        with self.assertRaises(IOError):
            vsmbr_partition.seek_offset(0, -1)

        vsmbr_partition.close()

        # Test the seek without open.
        with self.assertRaises(IOError):
            vsmbr_partition.seek_offset(16, os.SEEK_SET)
예제 #2
0
    def test_get_offset(self):
        """Tests the get_offset function."""
        test_source = unittest.source
        if not test_source:
            raise unittest.SkipTest("missing source")

        vsmbr_partition = pyvsmbr.partition()

        vsmbr_partition.open(test_source)

        offset = vsmbr_partition.get_offset()
        self.assertIsNotNone(offset)

        vsmbr_partition.close()
예제 #3
0
    def test_get_size(self):
        """Tests the get_size function and size property."""
        test_source = unittest.source
        if not test_source:
            raise unittest.SkipTest("missing source")

        vsmbr_partition = pyvsmbr.partition()

        vsmbr_partition.open(test_source)

        size = vsmbr_partition.get_size()
        self.assertIsNotNone(size)

        self.assertIsNotNone(vsmbr_partition.size)

        vsmbr_partition.close()
예제 #4
0
    def test_read_buffer_file_object(self):
        """Tests the read_buffer function on a file-like object."""
        test_source = unittest.source
        if not test_source:
            raise unittest.SkipTest("missing source")

        if not os.path.isfile(test_source):
            raise unittest.SkipTest("source not a regular file")

        vsmbr_partition = pyvsmbr.partition()

        with open(test_source, "rb") as file_object:
            vsmbr_partition.open_file_object(file_object)

            size = vsmbr_partition.get_size()

            # Test normal read.
            data = vsmbr_partition.read_buffer(size=4096)

            self.assertIsNotNone(data)
            self.assertEqual(len(data), min(size, 4096))

            vsmbr_partition.close()
예제 #5
0
    def test_read_buffer(self):
        """Tests the read_buffer function."""
        test_source = unittest.source
        if not test_source:
            raise unittest.SkipTest("missing source")

        vsmbr_partition = pyvsmbr.partition()

        vsmbr_partition.open(test_source)

        size = vsmbr_partition.get_size()

        if size < 4096:
            # Test read without maximum size.
            vsmbr_partition.seek_offset(0, os.SEEK_SET)

            data = vsmbr_partition.read_buffer()

            self.assertIsNotNone(data)
            self.assertEqual(len(data), size)

        # Test read with maximum size.
        vsmbr_partition.seek_offset(0, os.SEEK_SET)

        data = vsmbr_partition.read_buffer(size=4096)

        self.assertIsNotNone(data)
        self.assertEqual(len(data), min(size, 4096))

        if size > 8:
            vsmbr_partition.seek_offset(-8, os.SEEK_END)

            # Read buffer on size boundary.
            data = vsmbr_partition.read_buffer(size=4096)

            self.assertIsNotNone(data)
            self.assertEqual(len(data), 8)

            # Read buffer beyond size boundary.
            data = vsmbr_partition.read_buffer(size=4096)

            self.assertIsNotNone(data)
            self.assertEqual(len(data), 0)

        # Stress test read buffer.
        vsmbr_partition.seek_offset(0, os.SEEK_SET)

        remaining_size = size

        for _ in range(1024):
            read_size = int(random.random() * 4096)

            data = vsmbr_partition.read_buffer(size=read_size)

            self.assertIsNotNone(data)

            data_size = len(data)

            if read_size > remaining_size:
                read_size = remaining_size

            self.assertEqual(data_size, read_size)

            remaining_size -= data_size

            if not remaining_size:
                vsmbr_partition.seek_offset(0, os.SEEK_SET)

                remaining_size = size

        with self.assertRaises(ValueError):
            vsmbr_partition.read_buffer(size=-1)

        vsmbr_partition.close()

        # Test the read without open.
        with self.assertRaises(IOError):
            vsmbr_partition.read_buffer(size=4096)
예제 #6
0
    def test_read_buffer_at_offset(self):
        """Tests the read_buffer_at_offset function."""
        test_source = unittest.source
        if not test_source:
            raise unittest.SkipTest("missing source")

        vsmbr_partition = pyvsmbr.partition()

        vsmbr_partition.open(test_source)

        size = vsmbr_partition.get_size()

        # Test normal read.
        data = vsmbr_partition.read_buffer_at_offset(4096, 0)

        self.assertIsNotNone(data)
        self.assertEqual(len(data), min(size, 4096))

        if size > 8:
            # Read buffer on size boundary.
            data = vsmbr_partition.read_buffer_at_offset(4096, size - 8)

            self.assertIsNotNone(data)
            self.assertEqual(len(data), 8)

            # Read buffer beyond size boundary.
            data = vsmbr_partition.read_buffer_at_offset(4096, size + 8)

            self.assertIsNotNone(data)
            self.assertEqual(len(data), 0)

        # Stress test read buffer.
        for _ in range(1024):
            random_number = random.random()

            media_offset = int(random_number * size)
            read_size = int(random_number * 4096)

            data = vsmbr_partition.read_buffer_at_offset(
                read_size, media_offset)

            self.assertIsNotNone(data)

            remaining_size = size - media_offset

            data_size = len(data)

            if read_size > remaining_size:
                read_size = remaining_size

            self.assertEqual(data_size, read_size)

            remaining_size -= data_size

            if not remaining_size:
                vsmbr_partition.seek_offset(0, os.SEEK_SET)

        with self.assertRaises(ValueError):
            vsmbr_partition.read_buffer_at_offset(-1, 0)

        with self.assertRaises(ValueError):
            vsmbr_partition.read_buffer_at_offset(4096, -1)

        vsmbr_partition.close()

        # Test the read without open.
        with self.assertRaises(IOError):
            vsmbr_partition.read_buffer_at_offset(4096, 0)