1# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4; encoding:utf8 -*-
2#
3# Copyright 2002 Ben Escoto <ben@emerose.org>
4# Copyright 2007 Kenneth Loafman <kenneth@loafman.com>
5#
6# This file is part of duplicity.
7#
8# Duplicity is free software; you can redistribute it and/or modify it
9# under the terms of the GNU General Public License as published by the
10# Free Software Foundation; either version 2 of the License, or (at your
11# option) any later version.
12#
13# Duplicity is distributed in the hope that it will be useful, but
14# WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16# General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with duplicity; if not, write to the Free Software Foundation,
20# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22from __future__ import print_function
23from builtins import object
24from future import standard_library
25standard_library.install_aliases()
26
27import unittest
28
29from duplicity import dup_time
30from duplicity import file_naming
31from duplicity import log
32from duplicity import config
33from duplicity import util
34from . import UnitTestCase
35
36
37class Test36(UnitTestCase):
38    def test_base36(self):
39        u"""Test conversion to/from base 36"""
40        numlist = [0, 1, 10, 1313, 34233, 872338, 2342889,
41                   134242234, 1204684368, 34972382455]
42        for n in numlist:
43            b = file_naming.to_base36(n)
44            assert file_naming.from_base36(b) == n, (b, n)
45
46
47class FileNamingBase(object):
48    u"""Holds file naming test functions, for use in subclasses"""
49    def test_basic(self):
50        u"""Check get/parse cycle"""
51        dup_time.setprevtime(10)
52        dup_time.setcurtime(20)
53
54        file_naming.prepare_regex(force=True)
55        filename = file_naming.get(u"inc", volume_number=23)
56        log.Info(u"Inc filename: " + util.fsdecode(filename))
57        pr = file_naming.parse(filename)
58        assert pr and pr.type == u"inc", pr
59        assert pr.start_time == 10
60        assert pr.end_time == 20
61        assert pr.volume_number == 23
62        assert not pr.partial
63
64        filename = file_naming.get(u"full-sig")
65        log.Info(u"Full sig filename: " + util.fsdecode(filename))
66        pr = file_naming.parse(filename)
67        assert pr.type == u"full-sig"
68        assert pr.time == 20
69        assert not pr.partial
70
71        filename = file_naming.get(u"new-sig")
72        pr = file_naming.parse(filename)
73        assert pr.type == u"new-sig"
74        assert pr.start_time == 10
75        assert pr.end_time == 20
76        assert not pr.partial
77
78    def test_suffix(self):
79        u"""Test suffix (encrypt/compressed) encoding and generation"""
80        file_naming.prepare_regex(force=True)
81        filename = file_naming.get(u"inc", manifest=1, gzipped=1)
82        pr = file_naming.parse(filename)
83        assert pr and pr.compressed == 1
84        assert pr.manifest
85
86        filename2 = file_naming.get(u"full", volume_number=23, encrypted=1)
87        pr = file_naming.parse(filename2)
88        assert pr and pr.encrypted == 1
89        assert pr.volume_number == 23
90
91    def test_more(self):
92        u"""More file_parsing tests"""
93        file_naming.prepare_regex(force=True)
94        pr = file_naming.parse(config.file_prefix + config.file_prefix_signature + b"dns.h112bi.h14rg0.st.g")
95        assert pr, pr
96        assert pr.type == u"new-sig"
97        assert pr.end_time == 1029826800
98
99        if not config.short_filenames:
100            pr = file_naming.parse(config.file_prefix + config.file_prefix_signature + b"duplicity-new-signatures.2002-08-18T00:04:30-07:00.to.2002-08-20T00:00:00-07:00.sigtar.gpg")
101            assert pr, pr
102            assert pr.type == u"new-sig"
103            assert pr.end_time == 1029826800
104
105        pr = file_naming.parse(config.file_prefix + config.file_prefix_signature + b"dfs.h5dixs.st.g")
106        assert pr, pr
107        assert pr.type == u"full-sig"
108        assert pr.time == 1036954144, repr(pr.time)
109
110    def test_partial(self):
111        u"""Test addition of partial flag"""
112        file_naming.prepare_regex(force=True)
113        pr = file_naming.parse(config.file_prefix + config.file_prefix_signature + b"dns.h112bi.h14rg0.st.p.g")
114        assert pr, pr
115        assert pr.partial
116        assert pr.type == u"new-sig"
117        assert pr.end_time == 1029826800
118
119        if not config.short_filenames:
120            pr = file_naming.parse(config.file_prefix + config.file_prefix_signature + b"duplicity-new-signatures.2002-08-18T00:04:30-07:00.to.2002-08-20T00:00:00-07:00.sigtar.part.gpg")
121            assert pr, pr
122            assert pr.partial
123            assert pr.type == u"new-sig"
124            assert pr.end_time == 1029826800
125
126        pr = file_naming.parse(config.file_prefix + config.file_prefix_signature + b"dfs.h5dixs.st.p.g")
127        assert pr, pr
128        assert pr.partial
129        assert pr.type == u"full-sig"
130        assert pr.time == 1036954144, repr(pr.time)
131
132
133class FileNamingLong(UnitTestCase, FileNamingBase):
134    u"""Test long filename parsing and generation"""
135    def setUp(self):
136        super(FileNamingLong, self).setUp()
137        self.set_config(u'short_filenames', 0)
138
139
140class FileNamingShort(UnitTestCase, FileNamingBase):
141    u"""Test short filename parsing and generation"""
142    def setUp(self):
143        super(FileNamingShort, self).setUp()
144        self.set_config(u'short_filenames', 1)
145
146
147class FileNamingPrefixes(UnitTestCase, FileNamingBase):
148    u"""Test filename parsing and generation with prefixes"""
149    def setUp(self):
150        super(FileNamingPrefixes, self).setUp()
151        self.set_config(u'file_prefix', b"global-")
152        self.set_config(u'file_prefix_manifest', b"mani-")
153        self.set_config(u'file_prefix_signature', b"sign-")
154        self.set_config(u'file_prefix_archive', b"arch-")
155
156
157if __name__ == u"__main__":
158    unittest.main()
159