1
1
import os
2
+ import shutil
3
+ import mock
2
4
import unittest
3
5
import tempfile
4
6
import pkg_integrity
5
7
6
8
9
+ TESTDIR = os .path .join (os .getcwd (), "tests/testfiles/pkg_integrity" )
10
+
7
11
PACKAGE_URL = "http://pkgconfig.freedesktop.org/releases/pkg-config-0.29.1.tar.gz"
8
12
XATTR_PKT_URL = "http://pypi.debian.net/xattr/xattr-0.9.1.tar.gz"
9
13
NO_SIGN_PKT_URL = "http://www.ferzkopp.net/Software/SDL_gfx-2.0/SDL_gfx-2.0.25.tar.gz"
10
14
GEM_PKT = "https://rubygems.org/downloads/hoe-debugging-1.2.1.gem"
15
+ NOSIGN_PKT_URL_BAD = "http://gnu.mirrors.pair.com/savannah/savannah/quagga/bad_quagga-1.1.0.tar.gz"
11
16
NOSIGN_PKT_URL = "http://download.savannah.gnu.org/releases/quagga/quagga-1.1.0.tar.gz"
12
17
NOSIGN_SIGN_URL = "http://download.savannah.gnu.org/releases/quagga/quagga-1.1.0.tar.gz.asc"
13
18
PYPI_MD5_ONLY_PKG = "http://pypi.debian.net/tappy/tappy-0.9.2.tar.gz"
14
19
GNOME_SHA256_PKG = "https://download.gnome.org/sources/pygobject/3.24/pygobject-3.24.0.tar.xz"
15
20
KEYID = "EC2392F2EDE74488680DA3CF5F2B4756ED873D23"
16
21
17
22
18
- class TestGPGCli (unittest .TestCase ):
19
-
20
- def test_import_export (self ):
21
- with pkg_integrity .cli_gpg_ctx () as ctx :
22
- err , output = ctx .export_key (KEYID )
23
- self .assertTrue (err is not None )
24
- err , output = ctx .import_key (KEYID )
25
- self .assertTrue (err is None )
26
- err , output = ctx .export_key (KEYID )
27
- self .assertTrue (err is None )
28
- self .assertTrue ('PGP PUBLIC KEY' in output )
29
-
30
- def test_import_non_existing_key (self ):
31
- with pkg_integrity .cli_gpg_ctx () as ctx :
32
- keyid = '0' + KEYID [1 :]
33
- err , output = ctx .import_key (keyid )
34
- self .assertTrue (err is not None )
35
-
36
- def test_display_key_info (self ):
37
- with pkg_integrity .cli_gpg_ctx () as ctx :
38
- err , output = ctx .import_key (KEYID )
39
- self .assertTrue (err is None )
40
- err , output = ctx .export_key (KEYID )
41
- with open ('key_test.pkey' , 'w' ) as out_key :
42
- out_key .write (output )
43
- err , output = ctx .display_keyinfo ('key_test.pkey' )
44
- os .remove ('key_test.pkey' )
45
- self .assertTrue ('keyid' in output )
23
+ def mock_attempt_to_download (path , dest = None ):
24
+ if dest :
25
+ shutil .copyfile (os .path .join (TESTDIR , os .path .basename (path )), dest )
26
+ return 200
27
+
28
+ def mock_head_request (url ):
29
+ bad_sigs = ["http://pkgconfig.freedesktop.org/releases/pkg-config-0.29.1.tar.gz.sig" ,
30
+ "http://www.ferzkopp.net/Software/SDL_gfx-2.0/SDL_gfx-2.0.25.tar.gz.sig" ,
31
+ "http://www.ferzkopp.net/Software/SDL_gfx-2.0/SDL_gfx-2.0.25.tar.gz.asc" ]
46
32
33
+ if url in bad_sigs :
34
+ return 404
35
+ return 200
47
36
37
+
38
+ @mock .patch ('pkg_integrity.attempt_to_download' , mock_attempt_to_download )
39
+ @mock .patch ('pkg_integrity.head_request' , mock_head_request )
48
40
class TestCheckFn (unittest .TestCase ):
49
41
50
42
def setUp (self ):
@@ -53,8 +45,6 @@ def mock_rewrite(path):
53
45
pkg_integrity .config .rewrite_config_opts = mock_rewrite
54
46
pkg_integrity .config .config_opts ['verify_required' ] = False
55
47
56
- @unittest .skipIf ("TRAVIS" in os .environ and os .environ ["TRAVIS" ] == "true" ,
57
- "Skipping this test on Travis CI." )
58
48
def test_check_matching_sign_url (self ):
59
49
with tempfile .TemporaryDirectory () as tmpd :
60
50
out_file = os .path .join (tmpd , os .path .basename (PACKAGE_URL ))
@@ -73,6 +63,7 @@ def test_check_with_existing_sign(self):
73
63
self .assertTrue (result )
74
64
75
65
66
+ @mock .patch ('pkg_integrity.attempt_to_download' , mock_attempt_to_download )
76
67
class TestDomainBasedVerifiers (unittest .TestCase ):
77
68
78
69
def run_test_for_domain (self , Verifier , url ):
@@ -85,8 +76,6 @@ def run_test_for_domain(self, Verifier, url):
85
76
return None
86
77
87
78
88
- @unittest .skipIf ("TRAVIS" in os .environ and os .environ ["TRAVIS" ] == "true" ,
89
- "Skipping this test on Travis CI." )
90
79
def test_pypi (self ):
91
80
result = self .run_test_for_domain (pkg_integrity .PyPiVerifier , PYPI_MD5_ONLY_PKG )
92
81
self .assertTrue (result )
@@ -96,6 +85,7 @@ def test_gnome_org(self):
96
85
self .assertTrue (result )
97
86
98
87
88
+ @mock .patch ('pkg_integrity.attempt_to_download' , mock_attempt_to_download )
99
89
class TestGEMShaVerifier (unittest .TestCase ):
100
90
101
91
def setUp (self ):
@@ -122,6 +112,8 @@ def test_non_matchingsha(self):
122
112
self .assertEqual (a .exception .code , 1 )
123
113
124
114
115
+ @mock .patch ('pkg_integrity.attempt_to_download' , mock_attempt_to_download )
116
+ @mock .patch ('pkg_integrity.head_request' , mock_head_request )
125
117
class TestGPGVerifier (unittest .TestCase ):
126
118
127
119
def setUp (self ):
@@ -130,8 +122,6 @@ def mock_rewrite(path):
130
122
pkg_integrity .config .rewrite_config_opts = mock_rewrite
131
123
pkg_integrity .config .config_opts ['verify_required' ] = False
132
124
133
- @unittest .skipIf ("TRAVIS" in os .environ and os .environ ["TRAVIS" ] == "true" ,
134
- "Skipping this test on Travis CI." )
135
125
def test_from_url (self ):
136
126
with tempfile .TemporaryDirectory () as tmpd :
137
127
out_file = os .path .join (tmpd , os .path .basename (PACKAGE_URL ))
@@ -143,12 +133,14 @@ def test_from_url(self):
143
133
144
134
def test_check_quit (self ):
145
135
with tempfile .TemporaryDirectory () as tmpd :
146
- #with self.assertRaises(SystemExit) as a:
147
- pkg_integrity .check (NO_SIGN_PKT_URL , tmpd , interactive = False )
148
- #self.assertEqual(a.exception.code, 1)
136
+ with self .assertRaises (SystemExit ) as a :
137
+ out_file = os .path .join (tmpd , os .path .basename (NOSIGN_PKT_URL_BAD ))
138
+ pkg_integrity .attempt_to_download (NOSIGN_PKT_URL_BAD , out_file )
139
+ key_file = os .path .join (tmpd , os .path .basename (NOSIGN_PKT_URL_BAD ))
140
+ pkg_integrity .attempt_to_download (NOSIGN_SIGN_URL , key_file + '.asc' )
141
+ result = pkg_integrity .check (NOSIGN_PKT_URL_BAD , tmpd )
142
+ self .assertEqual (a .exception .code , 1 )
149
143
150
- @unittest .skipIf ("TRAVIS" in os .environ and os .environ ["TRAVIS" ] == "true" ,
151
- "Skipping this test on Travis CI." )
152
144
def test_from_disk (self ):
153
145
with tempfile .TemporaryDirectory () as tmpd :
154
146
out_file = os .path .join (tmpd , os .path .basename (PACKAGE_URL ))
@@ -158,8 +150,6 @@ def test_from_disk(self):
158
150
result = pkg_integrity .from_disk (PACKAGE_URL , out_file , out_key )
159
151
self .assertTrue (result )
160
152
161
- @unittest .skipIf ("TRAVIS" in os .environ and os .environ ["TRAVIS" ] == "true" ,
162
- "Skipping this test on Travis CI." )
163
153
def test_non_matchingsig (self ):
164
154
with tempfile .TemporaryDirectory () as tmpd :
165
155
out_file = os .path .join (tmpd , os .path .basename (PACKAGE_URL ))
@@ -174,35 +164,14 @@ def test_result_on_non_existent_pkg_path(self):
174
164
result = pkg_integrity .from_disk ('http://nokey.com/package.tar.gz' ,
175
165
'NonExistentPKG.tar.gz' ,
176
166
'NonExistentKey.asc' )
177
- self .assertTrue (result is None )
167
+ self .assertIsNone (result )
178
168
179
169
def test_result_on_nosign_package (self ):
180
170
with tempfile .TemporaryDirectory () as tmpd :
181
171
out_file = os .path .join (tmpd , os .path .basename (NO_SIGN_PKT_URL ))
182
172
pkg_integrity .attempt_to_download (NO_SIGN_PKT_URL , out_file )
183
173
result = pkg_integrity .check (NO_SIGN_PKT_URL , tmpd )
184
- self .assertTrue (result is None )
185
-
186
- @unittest .skipIf ("TRAVIS" in os .environ and os .environ ["TRAVIS" ] == "true" ,
187
- "Skipping this test on Travis CI." )
188
- def test_pubkey_import (self ):
189
- def say_yes (_ ):
190
- return True
191
- _ = pkg_integrity .InputGetter .get_answer
192
- pkg_integrity .InputGetter .get_answer = say_yes
193
- keyid = '0' + KEYID [1 :]
194
- result = pkg_integrity .attempt_key_import (keyid )
195
- self .assertTrue (result is False )
196
- result = pkg_integrity .attempt_key_import (KEYID )
197
- self .assertTrue (result )
198
- pkg_integrity .InputGetter .get_answer = _
199
- self .removeKey ()
200
-
201
- def removeKey (self ):
202
- key_path = os .path .dirname (os .path .realpath (__file__ ))
203
- key_path = os .path .dirname (key_path ) + '/autospec/keyring/{}.pkey' .format (KEYID )
204
- if os .path .exists (key_path ):
205
- os .unlink (key_path )
174
+ self .assertIsNone (result )
206
175
207
176
208
177
class TestInputGetter (unittest .TestCase ):
@@ -212,12 +181,14 @@ class TestInputGetter(unittest.TestCase):
212
181
def test_timput (self ):
213
182
ig = pkg_integrity .InputGetter (default = 'N' , timeout = 2 )
214
183
answer = ig .get_answer ()
215
- self .assertTrue (answer is None )
184
+ self .assertIsNone (answer )
216
185
ig = pkg_integrity .InputGetter (default = 'Y' , timeout = 2 )
217
186
answer = ig .get_answer ()
218
- self .assertTrue (answer is None )
187
+ self .assertIsNone (answer )
219
188
220
189
190
+ @mock .patch ('pkg_integrity.attempt_to_download' , mock_attempt_to_download )
191
+ @mock .patch ('pkg_integrity.head_request' , mock_head_request )
221
192
class TestUtils (unittest .TestCase ):
222
193
223
194
def setUp (self ):
@@ -252,23 +223,6 @@ def test_get_keyid_none(self):
252
223
false_name = '/false/name'
253
224
self .assertTrue (pkg_integrity .get_keyid (false_name ) is None )
254
225
255
- @unittest .skipIf ("TRAVIS" in os .environ and os .environ ["TRAVIS" ] == "true" ,
256
- "Skipping this test on Travis CI." )
257
- def test_attempt_to_download (self ):
258
- fakeURL = "https://download.my.url.com/file.tar.gz"
259
- realURLnoFile = "http://pypi.debian.net/alembic/alembic-0.8.8.non-existent.tar.gz"
260
- realURL = "http://pypi.debian.net/alembic/alembic-0.8.8.tar.gz"
261
-
262
- tmpf = tempfile .NamedTemporaryFile ()
263
- fname = tmpf .name
264
- tmpf .close ()
265
-
266
- self .assertEqual (pkg_integrity .attempt_to_download (fakeURL , fname ), None )
267
- self .assertEqual (pkg_integrity .attempt_to_download (realURLnoFile , fname ), 404 )
268
- self .assertEqual (pkg_integrity .attempt_to_download (realURL , fname ), 200 )
269
-
270
- os .unlink (fname )
271
-
272
226
def test_get_signature_url (self ):
273
227
274
228
url_from_gnu = "http://ftp.gnu.org/pub/gnu/gperf/gperf-3.0.4.tar.gz"
0 commit comments