1
0
Fork 0
mirror of https://github.com/borgbackup/borg.git synced 2025-01-01 12:45:34 +00:00

testsuite/key: Use pytest built-in fixture

This commit is contained in:
Marian Beermann 2016-05-13 22:06:38 +02:00
parent 6a15e8d138
commit ad5ae32cc4
No known key found for this signature in database
GPG key ID: 9B8450B91D1362C1

View file

@ -38,15 +38,10 @@ class MockArgs:
""")) """))
keyfile2_id = unhexlify('c3fbf14bc001ebcc3cd86e696c13482ed071740927cd7cbe1b01b4bfcee49314') keyfile2_id = unhexlify('c3fbf14bc001ebcc3cd86e696c13482ed071740927cd7cbe1b01b4bfcee49314')
@pytest.fixture(autouse=True) @pytest.fixture
def keys_dir(self, request, monkeypatch): def keys_dir(self, request, monkeypatch, tmpdir):
tmp_keys_dir = tempfile.mkdtemp() monkeypatch.setenv('BORG_KEYS_DIR', tmpdir)
return tmpdir
def finalize():
shutil.rmtree(tmp_keys_dir)
request.addfinalizer(finalize)
monkeypatch.setenv('BORG_KEYS_DIR', tmp_keys_dir)
class MockRepository: class MockRepository:
class _Location: class _Location:
@ -62,7 +57,7 @@ def test_plaintext(self):
assert hexlify(key.id_hash(chunk.data)) == b'2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae' assert hexlify(key.id_hash(chunk.data)) == b'2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae'
assert chunk == key.decrypt(key.id_hash(chunk.data), key.encrypt(chunk)) assert chunk == key.decrypt(key.id_hash(chunk.data), key.encrypt(chunk))
def test_keyfile(self, monkeypatch): def test_keyfile(self, monkeypatch, keys_dir):
monkeypatch.setenv('BORG_PASSPHRASE', 'test') monkeypatch.setenv('BORG_PASSPHRASE', 'test')
key = KeyfileKey.create(self.MockRepository(), self.MockArgs()) key = KeyfileKey.create(self.MockRepository(), self.MockArgs())
assert bytes_to_long(key.enc_cipher.iv, 8) == 0 assert bytes_to_long(key.enc_cipher.iv, 8) == 0
@ -97,8 +92,8 @@ def test_keyfile_kfenv(self, tmpdir, monkeypatch):
with pytest.raises(FileNotFoundError): with pytest.raises(FileNotFoundError):
KeyfileKey.detect(self.MockRepository(), chunk_cdata) KeyfileKey.detect(self.MockRepository(), chunk_cdata)
def test_keyfile2(self, monkeypatch): def test_keyfile2(self, monkeypatch, keys_dir):
with open(os.path.join(os.environ['BORG_KEYS_DIR'], 'keyfile'), 'w') as fd: with keys_dir.join('keyfile').open('w') as fd:
fd.write(self.keyfile2_key_file) fd.write(self.keyfile2_key_file)
monkeypatch.setenv('BORG_PASSPHRASE', 'passphrase') monkeypatch.setenv('BORG_PASSPHRASE', 'passphrase')
key = KeyfileKey.detect(self.MockRepository(), self.keyfile2_cdata) key = KeyfileKey.detect(self.MockRepository(), self.keyfile2_cdata)
@ -144,8 +139,8 @@ def _corrupt_byte(self, key, data, offset):
with pytest.raises(IntegrityError): with pytest.raises(IntegrityError):
key.decrypt("", data) key.decrypt("", data)
def test_decrypt_integrity(self, monkeypatch): def test_decrypt_integrity(self, monkeypatch, keys_dir):
with open(os.path.join(os.environ['BORG_KEYS_DIR'], 'keyfile'), 'w') as fd: with keys_dir.join('keyfile').open('w') as fd:
fd.write(self.keyfile2_key_file) fd.write(self.keyfile2_key_file)
monkeypatch.setenv('BORG_PASSPHRASE', 'passphrase') monkeypatch.setenv('BORG_PASSPHRASE', 'passphrase')
key = KeyfileKey.detect(self.MockRepository(), self.keyfile2_cdata) key = KeyfileKey.detect(self.MockRepository(), self.keyfile2_cdata)