<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> from __future__ import absolute_import <EOL> from __future__ import division <EOL> from __future__ import unicode_literals <EOL> import hashlib <EOL> import logging <EOL> import os <EOL> import random <EOL> import subprocess <EOL> import time <EOL> import unittest <EOL> import tuf <EOL> import tuf . conf <EOL> import tuf . download as download <EOL> import tuf . log <EOL> import tuf . unittest_toolbox as unittest_toolbox <EOL> import six <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> class TestDownload ( unittest_toolbox . Modified_TestCase ) : <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> unittest_toolbox . Modified_TestCase . setUp ( self ) <EOL> current_dir = os . getcwd ( ) <EOL> target_filepath = self . make_temp_data_file ( directory = current_dir ) <EOL> self . target_fileobj = open ( target_filepath , '<STR_LIT:r>' ) <EOL> self . target_data = self . target_fileobj . read ( ) <EOL> self . target_data_length = len ( self . target_data ) <EOL> self . PORT = random . randint ( <NUM_LIT> , <NUM_LIT> ) <EOL> command = [ '<STR_LIT>' , '<STR_LIT>' , str ( self . PORT ) ] <EOL> self . server_proc = subprocess . Popen ( command , stderr = subprocess . PIPE ) <EOL> logger . info ( '<STR_LIT>' ) <EOL> logger . info ( '<STR_LIT>' + str ( self . server_proc . pid ) ) <EOL> logger . info ( '<STR_LIT>' + str ( self . PORT ) ) <EOL> junk , rel_target_filepath = os . path . split ( target_filepath ) <EOL> self . url = '<STR_LIT>' + str ( self . PORT ) + '<STR_LIT:/>' + rel_target_filepath <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> m = hashlib . md5 ( ) <EOL> m . update ( self . target_data . encode ( '<STR_LIT:utf-8>' ) ) <EOL> digest = m . hexdigest ( ) <EOL> self . target_hash = { '<STR_LIT>' : digest } <EOL> def tearDown ( self ) : <EOL> unittest_toolbox . Modified_TestCase . tearDown ( self ) <EOL> if self . server_proc . returncode is None : <EOL> logger . info ( '<STR_LIT>' + str ( self . server_proc . pid ) + '<STR_LIT>' ) <EOL> self . server_proc . kill ( ) <EOL> self . target_fileobj . close ( ) <EOL> def test_download_url_to_tempfileobj ( self ) : <EOL> download_file = download . safe_download <EOL> temp_fileobj = download_file ( self . url , self . target_data_length ) <EOL> self . assertEqual ( self . target_data , temp_fileobj . read ( ) . decode ( '<STR_LIT:utf-8>' ) ) <EOL> self . assertEqual ( self . target_data_length , len ( temp_fileobj . read ( ) ) ) <EOL> temp_fileobj . close_temp_file ( ) <EOL> def test_download_url_to_tempfileobj_and_lengths ( self ) : <EOL> download . safe_download ( self . url , self . target_data_length - <NUM_LIT:4> ) <EOL> download . unsafe_download ( self . url , self . target_data_length - <NUM_LIT:4> ) <EOL> self . assertRaises ( tuf . DownloadLengthMismatchError , download . safe_download , <EOL> self . url , self . target_data_length + <NUM_LIT:1> ) <EOL> temp_fileobj = download . unsafe_download ( self . url , self . target_data_length + <NUM_LIT:1> ) <EOL> self . assertEqual ( self . target_data , temp_fileobj . read ( ) . decode ( '<STR_LIT:utf-8>' ) ) <EOL> self . assertEqual ( self . target_data_length , len ( temp_fileobj . read ( ) ) ) <EOL> temp_fileobj . close_temp_file ( ) <EOL> def test_download_url_to_tempfileobj_and_performance ( self ) : <EOL> """<STR_LIT>""" <EOL> def test_download_url_to_tempfileobj_and_urls ( self ) : <EOL> download_file = download . safe_download <EOL> unsafe_download_file = download . unsafe_download <EOL> self . assertRaises ( tuf . FormatError , <EOL> download_file , None , self . target_data_length ) <EOL> self . assertRaises ( tuf . FormatError , <EOL> download_file , <EOL> self . random_string ( ) , self . target_data_length ) <EOL> self . assertRaises ( six . moves . urllib . error . HTTPError , <EOL> download_file , <EOL> '<STR_LIT>' + str ( self . PORT ) + '<STR_LIT:/>' + self . random_string ( ) , <EOL> self . target_data_length ) <EOL> self . assertRaises ( six . moves . urllib . error . URLError , <EOL> download_file , <EOL> '<STR_LIT>' + str ( self . PORT + <NUM_LIT:1> ) + '<STR_LIT:/>' + self . random_string ( ) , <EOL> self . target_data_length ) <EOL> url_with_unsupported_uri = self . url . replace ( '<STR_LIT:http>' , '<STR_LIT:file>' ) <EOL> self . assertRaises ( tuf . FormatError , download_file , url_with_unsupported_uri , <EOL> self . target_data_length ) <EOL> self . assertRaises ( tuf . FormatError , unsafe_download_file , <EOL> url_with_unsupported_uri , self . target_data_length ) <EOL> def test__get_opener ( self ) : <EOL> fake_cacert = self . make_temp_data_file ( ) <EOL> with open ( fake_cacert , '<STR_LIT>' ) as file_object : <EOL> file_object . write ( '<STR_LIT>' ) <EOL> tuf . conf . ssl_certificates = fake_cacert <EOL> tuf . download . _get_opener ( '<STR_LIT>' ) <EOL> tuf . conf . ssl_certificates = None <EOL> def test_https_connection ( self ) : <EOL> current_directory = os . getcwd ( ) <EOL> target_filepath = self . make_temp_data_file ( directory = current_directory ) <EOL> target_data = None <EOL> target_data_length = <NUM_LIT:0> <EOL> with open ( target_filepath , '<STR_LIT:r>' ) as target_file_object : <EOL> target_data = target_file_object . read ( ) <EOL> target_data_length = len ( target_data ) <EOL> port = random . randint ( <NUM_LIT> , <NUM_LIT> ) <EOL> command = [ '<STR_LIT>' , '<STR_LIT>' , str ( port ) ] <EOL> https_server_process = subprocess . Popen ( command , stderr = subprocess . PIPE ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> junk , relative_target_filepath = os . path . split ( target_filepath ) <EOL> https_url = '<STR_LIT>' + str ( port ) + '<STR_LIT:/>' + relative_target_filepath <EOL> tuf . conf . ssl_certificates = '<STR_LIT>' <EOL> message = '<STR_LIT>' + https_url <EOL> logger . info ( message ) <EOL> try : <EOL> download . safe_download ( https_url , target_data_length ) <EOL> download . unsafe_download ( https_url , target_data_length ) <EOL> finally : <EOL> https_server_process <EOL> if https_server_process . returncode is None : <EOL> message = '<STR_LIT>' + str ( https_server_process . pid ) + '<STR_LIT>' <EOL> logger . info ( message ) <EOL> self . server_proc . kill ( ) <EOL> def test__get_content_length ( self ) : <EOL> content_length = tuf . download . _get_content_length ( { '<STR_LIT>' : <NUM_LIT:8> } ) <EOL> self . assertEqual ( content_length , None ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
