예제 #1
0
 def test_open(self):
     """test_open
        Given a valid mtd device and file pointer
        When I try to read from file pointer
        Then I get a valid file pointer returned
        When I try to read from an invalid mtd device
        Then I get an IOError
     """
     with mtd('/dev/mtd0').open('rb') as dev:
         self.assertIsNotNone(dev)
     with self.assertRaises(IOError):
         with mtd('/dev/mtd1').open('rb') as dev:
             pass
예제 #2
0
    def process_flash_item(self, controls, flash):
        # Find the flash_control object that matches the
        # 'type' encoded in the flash entry.
        ctrls = [c for c in controls if c.name == flash['type']]

        if not ctrls:
            msg = 'flash type "{}" not found'.format(flash['type'])
            self.error(msg)
            raise AttributeError(msg)

        with ctrls[0] as ctrl:
            with mtd(ctrl.devpath).open('r+b') as mtd_dev:
                try:
                    if flash.get('read-modify-write', False):
                        self.read_modify_write(flash, mtd_dev)
                    else:
                        self.erase(flash, mtd_dev)
                        self.write(flash, mtd_dev)
                except IOError as eexc:
                    LOG.exception(eexc)
                    self.error(eexc)
                    return False

            for nested in flash.get('flash', []):
                if not self.process_flash_item(controls, nested):
                    return False

        return self.error_count == 0
예제 #3
0
 def test_fileno(self):
     """test_fileno
        Given a valid mtd device and its file pointer 
        When I retrieve the fileno from the file object
        Then the returned fileno equals to an empty file pointer
     """
     with mock.patch(open_string, return_value=self.empty_io):
         with mtd('/dev/mtd0').open('rb') as dev:
             self.assertEqual(dev.fileno(), self.empty_io.fileno())
예제 #4
0
 def test_fileno_closed(self):
     """test_fileno_closed
        Given a valid mtd device and its file pointer
        When I exit the context manager and close the file pointer
        Then I try to retrieve the same file pointer
        It returns -1
     """
     with mock.patch(open_string, return_value=self.empty_io):
         with mtd('/dev/mtd0') as dev:
             pass
         fno = dev.fileno()
         self.assertEqual(fno, -1)
예제 #5
0
 def test_load_no_file(self):
     """test_load_no_file
        Given a valid mtd device and its file pointer
        When I try to load a non-exist file to the mtd device
        Then it fails to locate file to extract the content
        And I get an OSError exception
     """
     with mock.patch(open_string, return_value=self.empty_io):
         with mtd('/dev/mtd0').open('rb') as dev:
             with self.assertRaises(OSError):
                 dev.load("invalid_file")
                 pass
예제 #6
0
 def test_load(self):
     """test_load
        Given a valid mtd device and its file pointer
        When I try to load the empty new file into the mtd device
        Then I get a ValueError exception
     """
     with mock.patch(open_string, return_value=self.empty_io):
         with mtd('/dev/mtd0').open('rb') as dev:
             with tempfile.NamedTemporaryFile() as tmp_file:
                 with self.assertRaises(ValueError):
                     dev.load(tmp_file.name)
                     pass
예제 #7
0
 def test_copy_from_closed_fd(self):
     """test_copy_from_closed_fd
        Given a valid mtd open and close of mtd device
        When I copy the entire content of the file to the mtd device
        It returns IOError exception
     """
     with mock.patch(open_string, return_value=self.empty_io):
         with mtd('/dev/mtd0').open('rb') as dev:
             pass
         with self.assertRaises(IOError):
             dev.copy_from(self.random_io,
                           self.TEST_SIZE,
                           chunked=32,
                           progress=sys.stdout)
예제 #8
0
    def test_size(self):
        """test_size
           Given a valid registered ioctl
           When I open the mtd device
           Then the read device size equals to the registered dummy size
        """
        dummy_size = 0x101011

        def mtd_info(fd, req, buf, *args):
            return struct.pack('BIQIIQ', 0, 0, 0x101011, 0, 0, 0)

        with self.ioctl_handler.register(mtd.IOCTL_MTD_MEMGETINFO,
                                         mtd_info) as ioctl:
            with mock.patch('fcntl.ioctl', new=ioctl):
                dev = mtd('/dev/mtd0')
                dev.open('rb')
                self.assertEquals(dev.size, dummy_size)
예제 #9
0
    def test_erase(self):
        """test_erase
           Given a valid registered ioctl 
           When I open the mtd device
           Then I try erasing bytes from the mtd device
           And the packed data equals to the unpacked data from the erase
        """
        erase_data = 0x101011

        def mtd_erase(fd, req, buf, *args):
            self.assertEqual(req, mtd.IOCTL_MTD_MEMERASE)
            data = struct.unpack('II', buf)
            self.assertEquals(data, (0, erase_data))

        with self.ioctl_handler.register(mtd.IOCTL_MTD_MEMERASE,
                                         mtd_erase) as ioctl:
            with mock.patch('fcntl.ioctl', new=ioctl):
                dev = mtd('/dev/mtd0')
                dev.open('wb')
                dev.erase(0, erase_data)
예제 #10
0
 def test_copy_to(self):
     """test_copy_to
        Given a valid open mtd device and its file pointer
        And a non-empy data file
        When I copy the content of the mtd device to an object file
        Then the buffer copied from the mtd file pointer equals the buffer to the src file pointer
        When I close the device
        And I try to read from its file pointer
        I get a ValueError exception
     """
     self.assertNotEqual(get_buffer(self.empty_io),
                         get_buffer(self.random_io))
     with mock.patch(open_string, return_value=self.random_io):
         with mtd('/dev/mtd0').open('wb') as dev:
             dev.copy_to(self.empty_io,
                         self.TEST_SIZE,
                         chunked=32,
                         progress=sys.stdout)
             self.assertEqual(get_buffer(self.random_io),
                              get_buffer(self.empty_io))
         with self.assertRaises(ValueError):
             self.random_io.read(1)