def setUp(self): # Start both an HTTP and an HTTPS server running. The former is for # the zip files and the latter is for everything else. Vend them out # of a temporary directory which we load up with the right files. self._stack = ExitStack() try: self._serverdir = self._stack.enter_context(temporary_directory()) copy('winner.channels_01.json', self._serverdir, 'channels.json') sign(os.path.join(self._serverdir, 'channels.json'), 'image-signing.gpg') # Path B will win, with no bootme flags. self._indexpath = os.path.join('stable', 'nexus7', 'index.json') copy('winner.index_02.json', self._serverdir, self._indexpath) sign(os.path.join(self._serverdir, self._indexpath), 'image-signing.gpg') # Create every file in path B. The file contents will be the # checksum value. We need to create the signatures on the fly. setup_index('winner.index_02.json', self._serverdir, 'image-signing.gpg') self._stack.push( make_http_server(self._serverdir, 8943, 'cert.pem', 'key.pem')) self._stack.push(make_http_server(self._serverdir, 8980)) except: self._stack.close() raise
def test_cancel(self): # Try to cancel the download of a big file. self.assertEqual(os.listdir(config.tempdir), []) with ExitStack() as stack: serverdir = stack.enter_context(temporary_directory()) stack.push(make_http_server(serverdir, 8980)) # Create a couple of big files to download. write_bytes(os.path.join(serverdir, 'bigfile_1.dat'), 10) write_bytes(os.path.join(serverdir, 'bigfile_2.dat'), 10) # The download service doesn't provide reliable cancel # granularity, so instead, we mock the 'started' signal to # immediately cancel the download. downloader = get_download_manager() def cancel_on_start(self, signal, path, started): if started: downloader.cancel() stack.enter_context(patch( 'systemimage.udm.DownloadReactor._do_started', cancel_on_start)) self.assertRaises( Canceled, downloader.get_files, _http_pathify([ ('bigfile_1.dat', 'bigfile_1.dat'), ('bigfile_2.dat', 'bigfile_2.dat'), ])) self.assertEqual(os.listdir(config.tempdir), [])
def setUp(self): super().setUp() # Patch this method so that we can verify both the value of the flag # that system-image sets and the value that u-d-m's group downloader # records and uses. This is the only thing we can really # automatically test given that e.g. we won't have GSM in development. self._gsm_set_flag = None self._gsm_get_flag = None self._original = None def set_gsm(iface, *, allow_gsm): self._gsm_set_flag = allow_gsm self._original(iface, allow_gsm=allow_gsm) self._gsm_get_flag = iface.isGSMDownloadAllowed() self._resources = ExitStack() try: # Start the HTTP server running, vending files out of our test # data directory. directory = os.path.dirname(data_path('__init__.py')) self._resources.push(make_http_server(directory, 8980)) # Patch the GSM setting method to capture what actually happens. self._original = getattr(UDMDownloadManager, '_set_gsm') self._resources.enter_context(patch( 'systemimage.udm.UDMDownloadManager._set_gsm', set_gsm)) self._resources.callback(setattr, self, '_original', None) except: self._resources.close() raise
def setUp(self): self._stack = ExitStack() try: self._serverdir = self._stack.enter_context(temporary_directory()) self._stack.push( make_http_server(self._serverdir, 8943, 'cert.pem', 'key.pem')) except: self._stack.close() raise
def test_load_channel_over_https_port_with_http_fails(self): # We maliciously put an HTTP server on the HTTPS port. setup_keyrings() state = State() # Try to get the blacklist. This will fail silently since it's okay # not to find a blacklist. state.run_thru('get_blacklist_1') # This will fail to get the channels.json file. with make_http_server(self._serverdir, 8943): self.assertRaises(FileNotFoundError, next, state)
def setUp(self): super().setUp() self._resources = ExitStack() try: self._serverdir = self._resources.enter_context( temporary_directory()) self._resources.push(make_http_server(self._serverdir, 8980)) except: self._resources.close() raise
def test_https_cert_not_in_capath(self): # The self-signed certificate fails because it's not in the system's # CApath (no known-good CA). with make_http_server(self._directory, 8943, 'cert.pem', 'key.pem'): self.assertRaises( FileNotFoundError, get_download_manager().get_files, _https_pathify([ ('channel.channels_05.json', 'channels.json'), ]))
def setUp(self): # Start the HTTPS server running. Vend it out of a temporary # directory which we load up with the right files. self._stack = ExitStack() try: self._serverdir = self._stack.enter_context(temporary_directory()) self._stack.push( make_http_server(self._serverdir, 8943, 'cert.pem', 'key.pem')) except: self._stack.close() raise
def setUp(self): super().setUp() self._resources = ExitStack() try: # Start the HTTP server running, vending files out of our test # data directory. directory = os.path.dirname(data_path('__init__.py')) self._resources.push(make_http_server(directory, 8980)) except: self._resources.close() raise
def test_bad_host(self): # The HTTPS server has a certificate with a non-matching hostname # (mocked so that its CA is in the system's trusted path). with ExitStack() as stack: stack.push(make_http_server( self._directory, 8943, 'nasty_cert.pem', 'nasty_key.pem')) self.assertRaises( FileNotFoundError, get_download_manager().get_files, _https_pathify([ ('channel.channels_05.json', 'channels.json'), ]))
def test_expired(self): # The HTTPS server has an expired certificate (mocked so that its CA # is in the system's trusted path). with ExitStack() as stack: stack.push(make_http_server( self._directory, 8943, 'expired_cert.pem', 'expired_key.pem')) self.assertRaises( FileNotFoundError, get_download_manager().get_files, _https_pathify([ ('channel.channels_05.json', 'channels.json'), ]))
def test_good_path(self): # The HTTPS server has a valid self-signed certificate, so downloading # over https succeeds. with ExitStack() as stack: stack.push(make_http_server( self._directory, 8943, 'cert.pem', 'key.pem')) get_download_manager().get_files(_https_pathify([ ('channel.channels_05.json', 'channels.json'), ])) self.assertEqual( set(os.listdir(config.tempdir)), set(['channels.json']))
def setUp(self): self._stack = ExitStack() self._state = State() try: self._serverdir = self._stack.enter_context(temporary_directory()) self._stack.push( make_http_server(self._serverdir, 8943, 'cert.pem', 'key.pem')) copy('channel.channels_01.json', self._serverdir, 'channels.json') self._channels_path = os.path.join(self._serverdir, 'channels.json') except: self._stack.close() raise
def test_http_masquerades_as_https(self): # There's an HTTP server pretending to be an HTTPS server. This # should fail to download over https URLs. with ExitStack() as stack: # By not providing an SSL context wrapped socket, this isn't # really an https server. stack.push(make_http_server(self._directory, 8943)) self.assertRaises( FileNotFoundError, get_download_manager().get_files, _https_pathify([ ('channel.channels_05.json', 'channels.json'), ]))
def test_download_404(self): # Start a group download of some big files. One of the files won't # exist, so the entire group download should fail, and none of the # files should exist in the destination. self.assertEqual(os.listdir(config.tempdir), []) with ExitStack() as stack: serverdir = stack.enter_context(temporary_directory()) stack.push(make_http_server(serverdir, 8980)) # Create a couple of big files to download. write_bytes(os.path.join(serverdir, 'bigfile_1.dat'), 10) write_bytes(os.path.join(serverdir, 'bigfile_2.dat'), 10) write_bytes(os.path.join(serverdir, 'bigfile_3.dat'), 10) downloads = _http_pathify([ ('bigfile_1.dat', 'bigfile_1.dat'), ('bigfile_2.dat', 'bigfile_2.dat'), ('bigfile_3.dat', 'bigfile_3.dat'), ('missing.txt', 'missing.txt'), ]) self.assertRaises(FileNotFoundError, get_download_manager().get_files, downloads) # The temporary directory is empty. self.assertEqual(os.listdir(config.tempdir), [])