~ed.so/duplicity/webdav200fix-0.7

« back to all changes in this revision

Viewing changes to testing/unit/test_gpg.py

  • Committer: Kenneth Loafman
  • Date: 2014-04-20 15:32:17 UTC
  • mfrom: (977.1.8 test-reorg)
  • Revision ID: kenneth@loafman.com-20140420153217-n4jt28oo4q7d6bzu
# Merged in lp:~mterry/duplicity/more-test-reorg
  - Here's another test reorganization / modernization branch. It does the
    following things:
    - Drop duplicity/misc.py. It is confusing to have both misc.py and util.py,
      and most of the code in misc.py was no longer used. I moved the one
      function that was still used into util.py.
    - Consolidated the various ways to run tests into just one. I made tox runs
      go through ./setup.py test, rather than nosetests. And I made the
      ./testing/run-tests scripts just call tox. Now we no longer need nosetests
      as a test dependency (although you can still use it if you want).
    - Added two more code quality automated tests: a pep8 one and a pylint one.
      I disabled almost all checks in each program that gave a warning. These
      tests just establish a baseline for future improvement.
    - Moved the test helper code into TestCase subclasses that all tests can
      use. And used more code sharing and setUp/tearDown cleverness to remove
      duplicated code.
    - Reorganized the tests in ./testing/tests into ./testing/functional and
      ./testing/unit -- for whether they drive duplicity as a subprocess or
      whether they import and test code directly. Each dir can have specialized
      TestCase subclasses now.
    - Renamed the files in ./testing/unit to more clearly indicate which file
      in ./duplicity they are unit testing.
    - Added some helper methods for tests to set environment and globals.*
      parameters more safely (i.e. without affecting other tests) by
      automatically cleaning up any such changes during test tearDown.
    - Removed test_unicode.py, since it is kind of dumb. It used to be more
      useful, but now with py2.6, we are just testing that one line of code
      in it is actually there.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
# along with duplicity; if not, write to the Free Software Foundation,
20
20
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
21
 
22
 
import helper
23
22
import sys, os, unittest, random
24
23
 
25
24
from duplicity import gpg
26
25
from duplicity import path
27
 
 
28
 
helper.setup()
29
 
 
30
 
class GPGTest(unittest.TestCase):
 
26
from . import UnitTestCase
 
27
 
 
28
 
 
29
class GPGTest(UnitTestCase):
31
30
    """Test GPGFile"""
32
31
    def setUp(self):
33
 
        assert not os.system("tar xzf testfiles.tar.gz > /dev/null 2>&1")
 
32
        super(GPGTest, self).setUp()
 
33
        self.unpack_testfiles()
34
34
        self.default_profile = gpg.GPGProfile(passphrase = "foobar")
35
35
 
36
 
    def tearDown(self):
37
 
        assert not os.system("rm -rf testfiles tempdir temp2.tar")
38
 
 
39
 
    def deltmp(self):
40
 
        """Delete testfiles/output and recreate"""
41
 
        assert not os.system("rm -rf testfiles/output")
42
 
        assert not os.system("mkdir testfiles/output")
43
 
 
44
36
    def gpg_cycle(self, s, profile = None):
45
37
        """Test encryption/decryption cycle on string s"""
46
 
        self.deltmp()
47
38
        epath = path.Path("testfiles/output/encrypted_file")
48
39
        if not profile:
49
40
            profile = self.default_profile
77
68
 
78
69
    def test_gpg_asym(self):
79
70
        """Test GPG asymmetric encryption"""
80
 
        profile = gpg.GPGProfile(passphrase = helper.sign_passphrase,
81
 
                                 recipients = [helper.encrypt_key1,
82
 
                                               helper.encrypt_key2])
 
71
        profile = gpg.GPGProfile(passphrase = self.sign_passphrase,
 
72
                                 recipients = [self.encrypt_key1,
 
73
                                               self.encrypt_key2])
83
74
        self.gpg_cycle("aoensutha aonetuh saoe", profile)
84
75
 
85
 
        profile2 = gpg.GPGProfile(passphrase = helper.sign_passphrase,
86
 
                                  recipients = [helper.encrypt_key1])
 
76
        profile2 = gpg.GPGProfile(passphrase = self.sign_passphrase,
 
77
                                  recipients = [self.encrypt_key1])
87
78
        self.gpg_cycle("aoeu" * 10000, profile2)
88
79
 
89
80
    def test_gpg_hidden_asym(self):
90
81
        """Test GPG asymmetric encryption with hidden key id"""
91
 
        profile = gpg.GPGProfile(passphrase = helper.sign_passphrase,
92
 
                                 hidden_recipients = [helper.encrypt_key1,
93
 
                                               helper.encrypt_key2])
 
82
        profile = gpg.GPGProfile(passphrase = self.sign_passphrase,
 
83
                                 hidden_recipients = [self.encrypt_key1,
 
84
                                                      self.encrypt_key2])
94
85
        self.gpg_cycle("aoensutha aonetuh saoe", profile)
95
86
 
96
 
        profile2 = gpg.GPGProfile(passphrase = helper.sign_passphrase,
97
 
                                  hidden_recipients = [helper.encrypt_key1])
 
87
        profile2 = gpg.GPGProfile(passphrase = self.sign_passphrase,
 
88
                                  hidden_recipients = [self.encrypt_key1])
98
89
        self.gpg_cycle("aoeu" * 10000, profile2)
99
90
 
100
91
    def test_gpg_signing(self):
101
92
        """Test to make sure GPG reports the proper signature key"""
102
 
        self.deltmp()
103
93
        plaintext = "hello" * 50000
104
94
 
105
 
        signing_profile = gpg.GPGProfile(passphrase = helper.sign_passphrase,
106
 
                                         sign_key = helper.sign_key,
107
 
                                         recipients = [helper.encrypt_key1])
 
95
        signing_profile = gpg.GPGProfile(passphrase = self.sign_passphrase,
 
96
                                         sign_key = self.sign_key,
 
97
                                         recipients = [self.encrypt_key1])
108
98
 
109
99
        epath = path.Path("testfiles/output/encrypted_file")
110
100
        encrypted_signed_file = gpg.GPGFile(1, epath, signing_profile)
115
105
        assert decrypted_file.read() == plaintext
116
106
        decrypted_file.close()
117
107
        sig = decrypted_file.get_signature()
118
 
        assert sig == helper.sign_key, sig
 
108
        assert sig == self.sign_key, sig
119
109
 
120
110
    def test_gpg_signing_and_hidden_encryption(self):
121
111
        """Test to make sure GPG reports the proper signature key even with hidden encryption key id"""
122
 
        self.deltmp()
123
112
        plaintext = "hello" * 50000
124
113
 
125
 
        signing_profile = gpg.GPGProfile(passphrase = helper.sign_passphrase,
126
 
                                         sign_key = helper.sign_key,
127
 
                                         hidden_recipients = [helper.encrypt_key1])
 
114
        signing_profile = gpg.GPGProfile(passphrase = self.sign_passphrase,
 
115
                                         sign_key = self.sign_key,
 
116
                                         hidden_recipients = [self.encrypt_key1])
128
117
 
129
118
        epath = path.Path("testfiles/output/encrypted_file")
130
119
        encrypted_signed_file = gpg.GPGFile(1, epath, signing_profile)
135
124
        assert decrypted_file.read() == plaintext
136
125
        decrypted_file.close()
137
126
        sig = decrypted_file.get_signature()
138
 
        assert sig == helper.sign_key, sig
 
127
        assert sig == self.sign_key, sig
139
128
 
140
129
    def test_GPGWriteFile(self):
141
130
        """Test GPGWriteFile"""
142
 
        self.deltmp()
143
131
        size = 400 * 1000
144
132
        gwfh = GPGWriteFile_Helper()
145
133
        profile = gpg.GPGProfile(passphrase = "foobar")
155
143
 
156
144
    def test_GzipWriteFile(self):
157
145
        """Test GzipWriteFile"""
158
 
        self.deltmp()
159
146
        size = 400 * 1000
160
147
        gwfh = GPGWriteFile_Helper()
161
148
        for i in range(10): #@UnusedVariable
171
158
class GPGWriteHelper2:
172
159
    def __init__(self, data): self.data = data
173
160
 
 
161
 
174
162
class GPGWriteFile_Helper:
175
163
    """Used in test_GPGWriteFile above"""
176
164
    def __init__(self):
203
191
        return "e" * random.randrange(0, 15000)
204
192
 
205
193
 
206
 
class SHATest(unittest.TestCase):
 
194
class SHATest(UnitTestCase):
207
195
    """Test making sha signatures"""
208
196
    def setUp(self):
209
 
        assert not os.system("tar xzf testfiles.tar.gz > /dev/null 2>&1")
210
 
 
211
 
    def tearDown(self):
212
 
        assert not os.system("rm -rf testfiles tempdir temp2.tar")
 
197
        super(SHATest, self).setUp()
 
198
        self.unpack_testfiles()
213
199
 
214
200
    def test_sha(self):
215
201
        hash = gpg.get_hash("SHA1", path.Path("testfiles/various_file_types/regular_file"))