Browse Source

Merge pull request #2758 from enkore/f/missing-tests

Add some missing tests
enkore 8 years ago
parent
commit
d0c1ea5935

+ 1 - 0
src/borg/crypto/keymanager.py

@@ -164,6 +164,7 @@ class KeyManager:
                         (id_lines, id_repoid, id_complete_checksum) = data.split('/')
                     except ValueError:
                         print("the id line must contain exactly three '/', try again")
+                        continue
                     if sha256_truncated(data.lower().encode('ascii'), 2) != checksum:
                         print('line checksum did not match, try same line again')
                         continue

+ 48 - 3
src/borg/testsuite/archiver.py

@@ -61,7 +61,7 @@ from . import key
 src_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
 
 
-def exec_cmd(*args, archiver=None, fork=False, exe=None, **kw):
+def exec_cmd(*args, archiver=None, fork=False, exe=None, input=b'', **kw):
     if fork:
         try:
             if exe is None:
@@ -70,7 +70,7 @@ def exec_cmd(*args, archiver=None, fork=False, exe=None, **kw):
                 borg = (exe, )
             elif not isinstance(exe, tuple):
                 raise ValueError('exe must be None, a tuple or a str')
-            output = subprocess.check_output(borg + args, stderr=subprocess.STDOUT)
+            output = subprocess.check_output(borg + args, stderr=subprocess.STDOUT, input=input)
             ret = 0
         except subprocess.CalledProcessError as e:
             output = e.output
@@ -82,7 +82,7 @@ def exec_cmd(*args, archiver=None, fork=False, exe=None, **kw):
     else:
         stdin, stdout, stderr = sys.stdin, sys.stdout, sys.stderr
         try:
-            sys.stdin = StringIO()
+            sys.stdin = StringIO(input.decode())
             sys.stdout = sys.stderr = output = StringIO()
             if archiver is None:
                 archiver = Archiver()
@@ -2533,6 +2533,51 @@ id: 2 / e29442 3506da 4e1ea7 / 25f62a 5a3d41 - 02
  2: 737475 - 88
 """
 
+    def test_key_import_paperkey(self):
+        repo_id = 'e294423506da4e1ea76e8dcdf1a3919624ae3ae496fddf905610c351d3f09239'
+        self.cmd('init', self.repository_location, '--encryption', 'keyfile')
+        self._set_repository_id(self.repository_path, unhexlify(repo_id))
+
+        key_file = self.keys_path + '/' + os.listdir(self.keys_path)[0]
+        with open(key_file, 'w') as fd:
+            fd.write(KeyfileKey.FILE_ID + ' ' + repo_id + '\n')
+            fd.write(b2a_base64(b'abcdefghijklmnopqrstu').decode())
+
+        typed_input = (
+            b'2 / e29442 3506da 4e1ea7 / 25f62a 5a3d41  02\n'   # Forgot to type "-"
+            b'2 / e29442 3506da 4e1ea7  25f62a 5a3d41 - 02\n'   # Forgot to type second "/"
+            b'2 / e29442 3506da 4e1ea7 / 25f62a 5a3d42 - 02\n'  # Typo (..42 not ..41)
+            b'2 / e29442 3506da 4e1ea7 / 25f62a 5a3d41 - 02\n'  # Correct! Congratulations
+            b'616263 646566 676869 6a6b6c 6d6e6f 707172 - 6d\n'
+            b'\n\n'  # Abort [yN] => N
+            b'737475 88\n'  # missing "-"
+            b'73747i - 88\n'  # typo
+            b'73747 - 88\n'  # missing nibble
+            b'73 74 75  -  89\n'  # line checksum mismatch
+            b'00a1 - 88\n'  # line hash collision - overall hash mismatch, have to start over
+
+            b'2 / e29442 3506da 4e1ea7 / 25f62a 5a3d41 - 02\n'
+            b'616263 646566 676869 6a6b6c 6d6e6f 707172 - 6d\n'
+            b'73 74 75  -  88\n'
+        )
+
+        # In case that this has to change, here is a quick way to find a colliding line hash:
+        #
+        # from hashlib import sha256
+        # hash_fn = lambda x: sha256(b'\x00\x02' + x).hexdigest()[:2]
+        # for i in range(1000):
+        #     if hash_fn(i.to_bytes(2, byteorder='big')) == '88':  # 88 = line hash
+        #         print(i.to_bytes(2, 'big'))
+        #         break
+
+        self.cmd('key', 'import', '--paper', self.repository_location, input=typed_input)
+
+        # Test abort paths
+        typed_input = b'\ny\n'
+        self.cmd('key', 'import', '--paper', self.repository_location, input=typed_input)
+        typed_input = b'2 / e29442 3506da 4e1ea7 / 25f62a 5a3d41 - 02\n\ny\n'
+        self.cmd('key', 'import', '--paper', self.repository_location, input=typed_input)
+
     def test_debug_dump_manifest(self):
         self.create_regular_file('file1', size=1024 * 80)
         self.cmd('init', '--encryption=repokey', self.repository_location)

+ 16 - 1
src/borg/testsuite/version.py

@@ -1,6 +1,6 @@
 import pytest
 
-from ..version import parse_version
+from ..version import parse_version, format_version
 
 
 @pytest.mark.parametrize("version_str, version_tuple", [
@@ -36,3 +36,18 @@ def test_parse_version_invalid():
         assert parse_version('1.2')  # we require x.y.z versions
     with pytest.raises(ValueError):
         assert parse_version('crap')
+
+
+@pytest.mark.parametrize("version_str, version_tuple", [
+    ('1.0.0a1', (1, 0, 0, -4, 1)),
+    ('1.0.0', (1, 0, 0, -1)),
+    ('1.0.0a2', (1, 0, 0, -4, 2)),
+    ('1.0.0b3', (1, 0, 0, -3, 3)),
+    ('1.0.0rc4', (1, 0, 0, -2, 4)),
+    ('0.0.0', (0, 0, 0, -1)),
+    ('0.0.11', (0, 0, 11, -1)),
+    ('0.11.0', (0, 11, 0, -1)),
+    ('11.0.0', (11, 0, 0, -1)),
+])
+def test_format_version(version_str, version_tuple):
+    assert format_version(version_tuple) == version_str

+ 13 - 1
src/borg/testsuite/xattr.py

@@ -2,7 +2,9 @@ import os
 import tempfile
 import unittest
 
-from ..xattr import is_enabled, getxattr, setxattr, listxattr, buffer
+import pytest
+
+from ..xattr import is_enabled, getxattr, setxattr, listxattr, buffer, split_lstring
 from . import BaseTestCase
 
 
@@ -58,3 +60,13 @@ class XattrTestCase(BaseTestCase):
         got_value = getxattr(self.tmpfile.name, 'user.big')
         self.assert_equal(value, got_value)
         self.assert_equal(len(buffer), 128)
+
+
+@pytest.mark.parametrize('lstring, splitted', (
+    (b'', []),
+    (b'\x00', [b'']),
+    (b'\x01a', [b'a']),
+    (b'\x01a\x02cd', [b'a', b'cd']),
+))
+def test_split_lstring(lstring, splitted):
+    assert split_lstring(lstring) == splitted

+ 1 - 1
src/borg/version.py

@@ -40,7 +40,7 @@ def format_version(version):
     while True:
         part = next(it)
         if part >= 0:
-            f += str(part)
+            f.append(str(part))
         elif part == -1:
             break
         else: