Commit | Line | Data |
---|---|---|
866c42e6 DGM |
1 | # Copyright (C) 2013 Intra2net AG |
2 | # | |
494b38aa DGM |
3 | # This program is free software; you can redistribute it and/or modify |
4 | # it under the terms of the GNU Lesser General Public License as published | |
5 | # by the Free Software Foundation; either version 3 of the License, or | |
866c42e6 DGM |
6 | # (at your option) any later version. |
7 | # | |
8 | # This program is distributed in the hope that it will be useful, | |
9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
494b38aa | 11 | # GNU Lesser General Public License for more details. |
866c42e6 DGM |
12 | # |
13 | # You should have received a copy of the GNU General Public License | |
494b38aa DGM |
14 | # along with this program. If not, see |
15 | # <http://www.gnu.org/licenses/lgpl-3.0.html> | |
866c42e6 DGM |
16 | |
17 | ||
0112ba0d ERE |
18 | import os, unittest, hashlib, string |
19 | import random | |
20 | ||
cb7a3911 PG |
21 | from deltatar import crypto |
22 | ||
fbfda3d4 PG |
23 | import sys |
24 | ||
25 | def new_volume_handler(tarobj, base_name, volume_number, encryption=None): | |
26fa5ad5 ERE |
26 | ''' |
27 | Handles the new volumes | |
28 | ''' | |
29 | volume_path = "%s.%d" % (base_name, volume_number) | |
fbfda3d4 PG |
30 | tarobj.open_volume(volume_path, encryption=encryption) |
31 | ||
32 | def make_new_encryption_volume_handler(encryption): | |
33 | ''' | |
34 | Handles the new volumes using the right crypto context. | |
35 | ''' | |
36 | return lambda tarobj, base_name, volume_number: \ | |
37 | new_volume_handler (tarobj, base_name, volume_number, | |
38 | encryption=encryption) | |
26fa5ad5 | 39 | |
c7609167 ERE |
40 | def closing_new_volume_handler(tarobj, base_name, volume_number): |
41 | ''' | |
42 | Handles the new volumes | |
43 | ''' | |
44 | volume_path = "%s.%d" % (base_name, volume_number) | |
45 | tarobj.fileobj.close() | |
46 | tarobj.open_volume(volume_path) | |
47 | ||
0112ba0d ERE |
48 | class BaseTest(unittest.TestCase): |
49 | """ | |
50 | Test concatenated compression in tarfiles | |
51 | """ | |
52 | ||
53 | def tearDown(self): | |
54 | ''' | |
55 | Remove temporal files created by unit tests | |
56 | ''' | |
a0873dcc | 57 | os.system("rm -rf big big2 small small2 sample.* pdtcrypt-object-*.bin") |
0112ba0d ERE |
58 | |
59 | def create_file(self, path, length): | |
60 | ''' | |
61 | Creates a file with some gibberish inside, returning the md5sum of that | |
62 | file. File path and length are specified as function arguments. | |
63 | ''' | |
25d64d27 CH |
64 | data = string.ascii_lowercase + string.digits + "\n" |
65 | ||
66 | # determine how often need to repeat data and how much part of data is | |
67 | # left in the end to fill file up to length | |
68 | n_blocks, remainder = divmod(length, len(data)) | |
69 | with open(path, 'w') as write_handle: | |
70 | for _ in range(n_blocks): | |
71 | write_handle.write(data) | |
72 | write_handle.write(data[:remainder]) | |
0112ba0d ERE |
73 | return self.md5sum(path) |
74 | ||
f5d9144b PG |
75 | def create_symlink(self, linkname, path): |
76 | os.symlink(linkname, path) | |
77 | return self.md5sum(path, linkname=linkname) | |
78 | ||
79 | def md5sum(self, filename, linkname=None): | |
0112ba0d | 80 | ''' |
f5d9144b PG |
81 | Returns the md5sum of a file specified by its filename/path or, if |
82 | ``linkname`` is specified, the hash of both paths (for symlinks). | |
0112ba0d ERE |
83 | ''' |
84 | md5 = hashlib.md5() | |
f5d9144b PG |
85 | if linkname is None: |
86 | with open(filename,'rb') as f: | |
87 | for chunk in iter(lambda: f.read(128*md5.block_size), b''): | |
88 | md5.update(chunk) | |
89 | else: # symlink; hash paths | |
90 | md5.update(filename.encode("UTF-8")) | |
91 | md5.update(linkname.encode("UTF-8")) | |
26fa5ad5 | 92 | return md5.hexdigest() |