~duplicity-team/duplicity/0.7-series

« back to all changes in this revision

Viewing changes to testing/unit/test_gpg.py

  • Committer: Kenneth Loafman
  • Date: 2014-12-12 14:39:54 UTC
  • Revision ID: kenneth@loafman.com-20141212143954-wyln65yd1ynzsrlx
* Source formatted, using PyDev, all source files to fix some easily fixed
  PEP8 issues. Use ignore space when comparing against previous versions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
    def setUp(self):
32
32
        super(GPGTest, self).setUp()
33
33
        self.unpack_testfiles()
34
 
        self.default_profile = gpg.GPGProfile(passphrase = "foobar")
 
34
        self.default_profile = gpg.GPGProfile(passphrase="foobar")
35
35
 
36
 
    def gpg_cycle(self, s, profile = None):
 
36
    def gpg_cycle(self, s, profile=None):
37
37
        """Test encryption/decryption cycle on string s"""
38
38
        epath = path.Path("testfiles/output/encrypted_file")
39
39
        if not profile:
68
68
 
69
69
    def test_gpg_asym(self):
70
70
        """Test GPG asymmetric encryption"""
71
 
        profile = gpg.GPGProfile(passphrase = self.sign_passphrase,
72
 
                                 recipients = [self.encrypt_key1,
 
71
        profile = gpg.GPGProfile(passphrase=self.sign_passphrase,
 
72
                                 recipients=[self.encrypt_key1,
73
73
                                               self.encrypt_key2])
74
74
        self.gpg_cycle("aoensutha aonetuh saoe", profile)
75
75
 
76
 
        profile2 = gpg.GPGProfile(passphrase = self.sign_passphrase,
77
 
                                  recipients = [self.encrypt_key1])
 
76
        profile2 = gpg.GPGProfile(passphrase=self.sign_passphrase,
 
77
                                  recipients=[self.encrypt_key1])
78
78
        self.gpg_cycle("aoeu" * 10000, profile2)
79
79
 
80
80
    def test_gpg_hidden_asym(self):
81
81
        """Test GPG asymmetric encryption with hidden key id"""
82
 
        profile = gpg.GPGProfile(passphrase = self.sign_passphrase,
83
 
                                 hidden_recipients = [self.encrypt_key1,
 
82
        profile = gpg.GPGProfile(passphrase=self.sign_passphrase,
 
83
                                 hidden_recipients=[self.encrypt_key1,
84
84
                                                      self.encrypt_key2])
85
85
        self.gpg_cycle("aoensutha aonetuh saoe", profile)
86
86
 
87
 
        profile2 = gpg.GPGProfile(passphrase = self.sign_passphrase,
88
 
                                  hidden_recipients = [self.encrypt_key1])
 
87
        profile2 = gpg.GPGProfile(passphrase=self.sign_passphrase,
 
88
                                  hidden_recipients=[self.encrypt_key1])
89
89
        self.gpg_cycle("aoeu" * 10000, profile2)
90
90
 
91
91
    def test_gpg_signing(self):
92
92
        """Test to make sure GPG reports the proper signature key"""
93
93
        plaintext = "hello" * 50000
94
94
 
95
 
        signing_profile = gpg.GPGProfile(passphrase = self.sign_passphrase,
96
 
                                         sign_key = self.sign_key,
97
 
                                         recipients = [self.encrypt_key1])
 
95
        signing_profile = gpg.GPGProfile(passphrase=self.sign_passphrase,
 
96
                                         sign_key=self.sign_key,
 
97
                                         recipients=[self.encrypt_key1])
98
98
 
99
99
        epath = path.Path("testfiles/output/encrypted_file")
100
100
        encrypted_signed_file = gpg.GPGFile(1, epath, signing_profile)
111
111
        """Test to make sure GPG reports the proper signature key even with hidden encryption key id"""
112
112
        plaintext = "hello" * 50000
113
113
 
114
 
        signing_profile = gpg.GPGProfile(passphrase = self.sign_passphrase,
115
 
                                         sign_key = self.sign_key,
116
 
                                         hidden_recipients = [self.encrypt_key1])
 
114
        signing_profile = gpg.GPGProfile(passphrase=self.sign_passphrase,
 
115
                                         sign_key=self.sign_key,
 
116
                                         hidden_recipients=[self.encrypt_key1])
117
117
 
118
118
        epath = path.Path("testfiles/output/encrypted_file")
119
119
        encrypted_signed_file = gpg.GPGFile(1, epath, signing_profile)
130
130
        """Test GPGWriteFile"""
131
131
        size = 400 * 1000
132
132
        gwfh = GPGWriteFile_Helper()
133
 
        profile = gpg.GPGProfile(passphrase = "foobar")
134
 
        for i in range(10): #@UnusedVariable
 
133
        profile = gpg.GPGProfile(passphrase="foobar")
 
134
        for i in range(10):  # @UnusedVariable
135
135
            gpg.GPGWriteFile(gwfh, "testfiles/output/gpgwrite.gpg",
136
 
                             profile, size = size)
137
 
            #print os.stat("testfiles/output/gpgwrite.gpg").st_size-size
 
136
                             profile, size=size)
 
137
            # print os.stat("testfiles/output/gpgwrite.gpg").st_size-size
138
138
            assert size - 64 * 1024 <= os.stat("testfiles/output/gpgwrite.gpg").st_size <= size + 64 * 1024
139
139
        gwfh.set_at_end()
140
140
        gpg.GPGWriteFile(gwfh, "testfiles/output/gpgwrite.gpg",
141
 
                         profile, size = size)
142
 
        #print os.stat("testfiles/output/gpgwrite.gpg").st_size
 
141
                         profile, size=size)
 
142
        # print os.stat("testfiles/output/gpgwrite.gpg").st_size
143
143
 
144
144
    def test_GzipWriteFile(self):
145
145
        """Test GzipWriteFile"""
146
146
        size = 400 * 1000
147
147
        gwfh = GPGWriteFile_Helper()
148
 
        for i in range(10): #@UnusedVariable
 
148
        for i in range(10):  # @UnusedVariable
149
149
            gpg.GzipWriteFile(gwfh, "testfiles/output/gzwrite.gz",
150
 
                              size = size)
151
 
            #print os.stat("testfiles/output/gzwrite.gz").st_size-size
 
150
                              size=size)
 
151
            # print os.stat("testfiles/output/gzwrite.gz").st_size-size
152
152
            assert size - 64 * 1024 <= os.stat("testfiles/output/gzwrite.gz").st_size <= size + 64 * 1024
153
153
        gwfh.set_at_end()
154
 
        gpg.GzipWriteFile(gwfh, "testfiles/output/gzwrite.gz", size = size)
155
 
        #print os.stat("testfiles/output/gzwrite.gz").st_size
 
154
        gpg.GzipWriteFile(gwfh, "testfiles/output/gzwrite.gz", size=size)
 
155
        # print os.stat("testfiles/output/gzwrite.gz").st_size
156
156
 
157
157
 
158
158
class GPGWriteHelper2:
171
171
 
172
172
    def get_buffer(self, size):
173
173
        """Return buffer of size size, consisting of half random data"""
174
 
        s1 = size/2
 
174
        s1 = size / 2
175
175
        s2 = size - s1
176
176
        return "a"*s1 + self.from_random_fp.read(s2)
177
177