~mterry/duplicity/use-gpg-options-0.7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
#
# Copyright 2002 Ben Escoto <ben@emerose.org>
# Copyright 2007 Kenneth Loafman <kenneth@loafman.com>
#
# This file is part of duplicity.
#
# Duplicity is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# Duplicity is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with duplicity; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

import config
import sys, os, unittest
sys.path.insert(0, "../")

from duplicity.path import *
from duplicity import diffdir
from duplicity import selection
from duplicity import tarfile
from duplicity import log

config.setup()

class DDTest(unittest.TestCase):
    """Test functions in diffdir.py"""
    def setUp(self):
        assert not os.system("tar xzf testfiles.tar.gz > /dev/null 2>&1")

    def tearDown(self):
        assert not os.system("rm -rf testfiles tempdir temp2.tar")

    def copyfileobj(self, infp, outfp):
        """Copy in fileobj to out, closing afterwards"""
        blocksize = 32 * 1024
        while 1:
            buf = infp.read(blocksize)
            if not buf: break
            outfp.write(buf)
        assert not infp.close()
        assert not outfp.close()

    def deltmp(self):
        """Delete temporary directories"""
        assert not os.system("rm -rf testfiles/output")
        os.mkdir("testfiles/output")

    def testsig(self):
        """Test producing tar signature of various file types"""
        self.deltmp()
        select = selection.Select(Path("testfiles/various_file_types"))
        select.set_iter()
        sigtar = diffdir.SigTarBlockIter(select)
        diffdir.write_block_iter(sigtar, "testfiles/output/sigtar")

        i = 0
        for tarinfo in tarfile.TarFile("testfiles/output/sigtar", "r"):
            i += 1
        assert i >= 5, "There should be at least 5 files in sigtar"

    def empty_diff_schema(self, dirname):
        """Given directory name, make sure can tell when nothing changes"""
        self.deltmp()
        select = selection.Select(Path(dirname))
        select.set_iter()
        sigtar = diffdir.SigTarBlockIter(select)
        diffdir.write_block_iter(sigtar, "testfiles/output/sigtar")

        sigtar_fp = open("testfiles/output/sigtar")
        select2 = selection.Select(Path(dirname))
        select2.set_iter()
        diffdir.write_block_iter(diffdir.DirDelta(select2, sigtar_fp),
                                 "testfiles/output/difftar")

        size = os.stat("testfiles/output/difftar").st_size
        assert size == 0 or size == 10240, size # 10240 is size of one record
        if size != 0:
            fin = open("testfiles/output/difftar", "rb")
            diff_buf = fin.read()
            assert not fin.close()
            assert diff_buf == '\0'*10240

    def test_empty_diff(self):
        """Test producing a diff against same sig; should be len 0"""
        self.empty_diff_schema("testfiles/various_file_types")
        return

        self.deltmp()
        select = selection.Select(Path("testfiles/various_file_types"))
        select.set_iter()
        sigtar = diffdir.SigTarBlockIter(select)
        diffdir.write_block_iter(sigtar, "testfiles/output/sigtar")

        sigtar_fp = open("testfiles/output/sigtar")
        select2 = selection.Select(Path("testfiles/various_file_types"))
        select2.set_iter()
        diffdir.write_block_iter(diffdir.DirDelta(select2, sigtar_fp),
                                 "testfiles/output/difftar")

        size = os.stat("testfiles/output/difftar").st_size

    def test_empty_diff2(self):
        """Test producing diff against directories of special files"""
        self.empty_diff_schema("testfiles/special_cases/neg_mtime")
        self.empty_diff_schema("testfiles/special_cases/no_uname")

    def test_diff(self):
        """Test making a diff"""
        self.deltmp()
        sel1 = selection.Select(Path("testfiles/dir1"))
        diffdir.write_block_iter(diffdir.SigTarBlockIter(sel1.set_iter()),
                                 "testfiles/output/dir1.sigtar")

        sigtar_fp = open("testfiles/output/dir1.sigtar", "rb")
        sel2 = selection.Select(Path("testfiles/dir2"))
        delta_tar = diffdir.DirDelta(sel2.set_iter(), sigtar_fp)
        diffdir.write_block_iter(delta_tar,
                                 "testfiles/output/dir1dir2.difftar")

        changed_files = ["diff/changeable_permission",
                         "diff/regular_file",
                         "snapshot/symbolic_link/",
                         "deleted/deleted_file",
                         "snapshot/directory_to_file",
                         "snapshot/file_to_directory/"]
        for tarinfo in tarfile.TarFile("testfiles/output/dir1dir2.difftar",
                                       "r"):
            if tarinfo.name in changed_files:
                changed_files.remove(tarinfo.name)
        assert not changed_files, ("Following files not found:\n"
                                   + "\n".join(changed_files))

    def test_diff2(self):
        """Another diff test - this one involves multivol support"""
        self.deltmp()
        sel1 = selection.Select(Path("testfiles/dir2"))
        diffdir.write_block_iter(diffdir.SigTarBlockIter(sel1.set_iter()),
                                 "testfiles/output/dir2.sigtar")

        sigtar_fp = open("testfiles/output/dir2.sigtar", "rb")
        sel2 = selection.Select(Path("testfiles/dir3"))
        delta_tar = diffdir.DirDelta(sel2.set_iter(), sigtar_fp)
        diffdir.write_block_iter(delta_tar,
                                 "testfiles/output/dir2dir3.difftar")

        buffer = ""
        tf = tarfile.TarFile("testfiles/output/dir2dir3.difftar", "r")
        for tarinfo in tf:
            if tarinfo.name.startswith("multivol_diff/"):
                buffer += tf.extractfile(tarinfo).read()
        assert 3000000 < len(buffer) < 4000000
        fout = open("testfiles/output/largefile.delta", "wb")
        fout.write(buffer)
        fout.close()
        assert not os.system("rdiff patch testfiles/dir2/largefile "
                             "testfiles/output/largefile.delta "
                             "testfiles/output/largefile.patched")
        dir3large = open("testfiles/dir3/largefile", "rb").read()
        patchedlarge = open("testfiles/output/largefile.patched", "rb").read()
        assert dir3large == patchedlarge

    def test_dirdelta_write_sig(self):
        """Test simultaneous delta and sig generation

        Generate signatures and deltas of dirs1, 2, 3, 4 and compare
        those produced by DirDelta_WriteSig and other methods.

        """
        self.deltmp()
        deltadir1 = Path("testfiles/output/dir.deltatar1")
        deltadir2 = Path("testfiles/output/dir.deltatar2")
        cur_full_sigs = Path("testfiles/output/fullsig.dir1")

        cur_dir = Path("testfiles/dir1")
        get_sel = lambda cur_dir: selection.Select(cur_dir).set_iter()
        diffdir.write_block_iter(diffdir.SigTarBlockIter(get_sel(cur_dir)),
                                 cur_full_sigs)

        sigstack = [cur_full_sigs]
        for dirname in ['dir2', 'dir3', 'dir4']:
            #print "Processing ", dirname
            old_dir = cur_dir
            cur_dir = Path("testfiles/" + dirname)

            old_full_sigs = cur_full_sigs
            cur_full_sigs = Path("testfiles/output/fullsig." + dirname)

            delta1 = Path("testfiles/output/delta1." + dirname)
            delta2 = Path("testfiles/output/delta2." + dirname)
            incsig = Path("testfiles/output/incsig." + dirname)

            # Write old-style delta to deltadir1
            diffdir.write_block_iter(diffdir.DirDelta(get_sel(cur_dir),
                                               old_full_sigs.open("rb")),
                                     delta1)

            # Write new signature and delta to deltadir2 and sigdir2, compare
            block_iter = diffdir.DirDelta_WriteSig(get_sel(cur_dir),
                           map(lambda p: p.open("rb"), sigstack),
                                                   incsig.open("wb"))
            sigstack.append(incsig)
            diffdir.write_block_iter(block_iter, delta2)

            #print delta1.name, delta2.name
            compare_tar(delta1.open("rb"), delta2.open("rb"))
            assert not os.system("cmp %s %s" % (delta1.name, delta2.name))

            # Write old-style signature to cur_full_sigs
            diffdir.write_block_iter(diffdir.SigTarBlockIter(get_sel(cur_dir)),
                                     cur_full_sigs)

    def test_combine_path_iters(self):
        """Test diffdir.combine_path_iters"""
        class Dummy:
            def __init__(self, index, other = None):
                self.index = index
                self.other = other
            def __repr__(self): return "(%s %s)" % (self.index, self.other)

        def get_iter1():
            yield Dummy(())
            yield Dummy((1,))
            yield Dummy((1, 5), 2)

        def get_iter2():
            yield Dummy((), 2)
            yield Dummy((1,5))
            yield Dummy((2,))

        def get_iter3():
            yield Dummy((), 3)
            yield Dummy((2,), 1)

        result = diffdir.combine_path_iters([get_iter1(),
                                             get_iter2(),
                                             get_iter3()])
        elem1 = result.next()
        assert elem1.index == () and elem1.other == 3, elem1
        elem2 = result.next()
        assert elem2.index == (1,) and elem2.other is None, elem2
        elem3 = result.next()
        assert elem3.index == (1,5) and elem3.other is None
        elem4 = result.next()
        assert elem4.index == (2,) and elem4.other == 1
        try: elem5 = result.next()
        except StopIteration: pass
        else: assert 0, elem5


def compare_tar(tarfile1, tarfile2):
    """Compare two tarfiles"""
    tf1 = tarfile.TarFile("none", "r", tarfile1)
    tf2 = tarfile.TarFile("none", "r", tarfile2)
    tf2_iter = iter(tf2)

    for ti1 in tf1:
        try: ti2 = tf2_iter.next()
        except StopIteration:
            assert 0, ("Premature end to second tarfile, "
                       "ti1.name = %s" % ti1.name)
        #print "Comparing ", ti1.name, ti2.name
        assert tarinfo_eq(ti1, ti2), "%s %s" % (ti1.name, ti2.name)
        if ti1.size != 0:
            fp1 = tf1.extractfile(ti1)
            buf1 = fp1.read()
            fp1.close()
            fp2 = tf2.extractfile(ti2)
            buf2 = fp2.read()
            fp2.close()
            assert buf1 == buf2
    try: ti2 = tf2_iter.next()
    except StopIteration: pass
    else: assert 0, ("Premature end to first tarfile, "
                     "ti2.name = %s" % ti2.name)

    tarfile1.close()
    tarfile2.close()

def tarinfo_eq(ti1, ti2):
    if ti1.name != ti2.name:
        print "Name:", ti1.name, ti2.name
        return 0
    if ti1.size != ti2.size:
        print "Size:", ti1.size, ti2.size
        return 0
    if ti1.mtime != ti2.mtime:
        print "Mtime:", ti1.mtime, ti2.mtime
        return 0
    if ti1.mode != ti2.mode:
        print "Mode:", ti1.mode, ti2.mode
        return 0
    if ti1.type != ti2.type:
        print "Type:", ti1.type, ti2.type
        return 0
    if ti1.issym() or ti1.islnk():
        if ti1.linkname != ti2.linkname:
            print "Linkname:", ti1.linkname, ti2.linkname
            return 0
    if ti1.uid != ti2.uid or ti1.gid != ti2.gid:
        print "IDs:", ti1.uid, ti2.uid, ti1.gid, ti2.gid
        return 0
    if ti1.uname != ti2.uname or ti1.gname != ti2.gname:
        print "Owner names:", ti1.uname, ti2.uname, ti1.gname, ti2.gname
        return 0
    return 1

if __name__ == "__main__":
    unittest.main()