def test_dir_full_pipeline(self): """Basic test of a single directory archive, encrypt and backup.""" try: # Setup the test state. tempdir, in_dir, out_dir = self._create_tempdir_structure('input', \ 'output') in_struct, out_struct, cfg_file = self._create_single_dir_test(\ 'struct', 'struct.tar.xz.gpg', tempdir, in_dir, tempdir, \ [_ConfigSection('input/struct', tempdir, archive='yes', compress='yes', encrypt='yes')]) in_dir_hash = test.get_dir_md5(in_dir) # Run backup. with redir_stdstreams() as (stdout, stderr): backup.main(['--config', cfg_file, '--gpg-home', test.GPG_HOME]) self.assertEqual('', stdout.getvalue().strip()) self.assertEqual('', stderr.getvalue().strip()) # Assert the output state looks as we expect. self.assertTrue(os.path.isdir(in_struct)) self.assertTrue(os.path.isfile(out_struct)) self.assertEqual(in_dir_hash, test.get_dir_md5(in_dir)) undo_out_file = os.path.join(out_dir, 'struct') backup.unencrypt_path(os.path.join(tempdir, 'struct.tar.xz'), out_struct, homedir=test.GPG_HOME) backup.uncompress_path(os.path.join(tempdir, 'struct.tar'), os.path.join(tempdir, 'struct.tar.xz')) backup.unarchive_path(out_dir, os.path.join(tempdir, 'struct.tar')) self.assertTrue(os.path.isdir(undo_out_file)) self.assertEqual(in_dir_hash, test.get_dir_md5(out_dir)) finally: shutil.rmtree(tempdir)
def test_file_encrypt(self): """Basic test of a single file encrypt and backup.""" try: # Setup the test state. tempdir, in_dir, out_dir = self._create_tempdir_structure('input', \ 'output') in_file, out_file, cfg_file = self._create_single_file_test(\ 'file.txt', 'file.txt.gpg', tempdir, in_dir, out_dir, \ [_ConfigSection('input/file.txt', out_dir, encrypt='yes')]) in_dir_hash = test.get_dir_md5(in_dir) in_file_hash = test.get_file_md5(in_file) # Run backup. with redir_stdstreams() as (stdout, stderr): backup.main(['--config', cfg_file, '--gpg-home', test.GPG_HOME]) self.assertEqual('', stdout.getvalue().strip()) self.assertEqual('', stderr.getvalue().strip()) # Assert the output state looks as we expect. self.assertTrue(os.path.isfile(in_file)) self.assertTrue(os.path.isfile(out_file)) self.assertEqual(in_dir_hash, test.get_dir_md5(in_dir)) undo_out_file = os.path.join(tempdir, 'file.txt') backup.unencrypt_path(undo_out_file, out_file, homedir=test.GPG_HOME) self.assertTrue(os.path.isfile(undo_out_file)) self.assertEqual(in_file_hash, test.get_file_md5(undo_out_file)) finally: shutil.rmtree(tempdir)
def test_encrypt_path_binary_file(self): """Test the encrypt methods with a binary file path argument.""" # Wrap the backup.xcrypt_path functions so we can inject the unittesting # homedir and use it's key (which should never be used for anything as # it's completely insecure) rather than the keys of the current user. encrypt = lambda d, s: backup.encrypt_path(d, s, homedir=test.GPG_HOME) unencrypt = lambda d, s: backup.unencrypt_path( d, s, homedir=test.GPG_HOME) self._assert_file_processing( encrypt, unencrypt, [self._FILE_TYPE_GPG, self._FILE_TYPE_DATA], 'testfile.bin', 'encrypted.gpg', 'testfile.bin', False)