Exemplo n.º 1
0
    def test_add(self):
        """Tests adding padding."""
        for size in range(self.tests):
            self._write_data(self.file, self._generate_data(size))

            with self.subTest(size=size):
                padding.add(self.file, self.size)

                self.assertFalse(len(self._read_data(self.file)) % self.size)
Exemplo n.º 2
0
    def test_add(self):
        """Tests adding padding."""
        for size in range(self.tests):
            data = bytearray(self._generate_data(size))

            with self.subTest(size=size):
                padding.add(data, self.size)

                self.assertFalse(len(data) % self.size)
Exemplo n.º 3
0
    def test_remove(self):
        """Tests removing padding."""
        for size in range(self.tests):
            data = bytearray(self._generate_data(size))
            padding.add(data, self.size)

            with self.subTest(size=size):
                padding.remove(data)

                self.assertEqual(size, len(data))
Exemplo n.º 4
0
def encrypt(file_path, key_file):
    """Encrypts data with given key."""
    padding.add(file_path, 4 * constants.NB)

    key = Key.load(key_file)

    offset = 0
    while True:
        state = State.load(file_path, offset)
        if state is None:
            break

        aes.encrypt(state, key)
        state.dump(file_path, offset)

        offset += 4 * constants.NB
Exemplo n.º 5
0
    def test_add_to_file(self):
        """Tests adding padding to file."""
        file_mock = mock.MagicMock(spec=pathlib.Path)

        open_mock = mock.mock_open(file_mock)

        file_handler_mock = open_mock.return_value
        for size in range(self.size):
            file_handler_mock.tell.return_value = size + 1

            with self.subTest(size=size):
                open_mock.reset_mock()

                with mock.patch('builtins.open', open_mock):
                    padding.add(file_mock, self.size)

                open_mock.assert_called_once_with(file_mock, 'ab')
                file_handler_mock.write.assert_any_call(b'\x01')
                file_handler_mock.tell.assert_called_once()
                zeros_padding = b'\x00' * (
                    self.size - file_handler_mock.tell() % self.size)
                file_handler_mock.write.assert_any_call(zeros_padding)