Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_extract_zip_with_timezone(self):
test_file = self.get_test_loc('archive/zip/timezone/c.zip')
test_dir = self.get_temp_dir()
archive.extract_zip(test_file, test_dir)
expected = [
(os.path.join(test_dir, 'c/a/a.txt'), '2008-07-29'),
(os.path.join(test_dir, 'c/b/a.txt'), '2008-07-29'),
(os.path.join(test_dir, 'c/c/a.txt'), '2008-07-29'),
]
# DST sends a monkey wrench.... so we only test the date, not the time
for loc, expected_date in expected:
result = commoncode.date.get_file_mtime(loc)
assert result.startswith(expected_date)
# f.close()
# f = ZipFile(os.path.join(self.get_test_loc('archive'), 'relative_parent_folders.zip'), 'w')
# f.write('/tmp/a.txt', '../a_parent_folder.txt')
# f.write('/tmp/b.txt', '../../another_folder/b_two_root.txt')
# f.write('/tmp/b.txt', '../folder/subfolder/b_subfolder.txt')
# f.close()
# f = ZipFile(os.path.join(self.get_test_loc('archive'), 'high_ancest.zip'), 'w')
# f.write('/tmp/a.txt', ('../' * 12) + 'a_parent_folder.txt')
# f.write('/tmp/a.txt', ('../' * 12) + ('sub/' * 6) + 'a_parent_folder_in_sub_1.txt')
# f.write('/tmp/a.txt', ('../' * 6) + ('sub/' * 12) + 'a_parent_folder_in_sub_2.txt')
# f.write('/tmp/a.txt', ('../' * 12) + ('sub/' * 12) + 'a_parent_folder_in_sub_3.txt')
# f.close()
test_file = self.get_test_loc('archive/zip/relative_parent_folders.zip')
test_dir = self.get_temp_dir()
archive.extract_zip(test_file, test_dir)
abs_path = os.path.join(test_dir , '../a_parent_folder.txt')
assert not os.path.exists(abs_path)
result = self.collect_extracted_path(test_dir)
expected = [
'/dotdot/',
'/dotdot/a_parent_folder.txt',
'/dotdot/dotdot/',
'/dotdot/dotdot/another_folder/',
'/dotdot/dotdot/another_folder/b_two_root.txt',
'/dotdot/folder/',
'/dotdot/folder/subfolder/',
'/dotdot/folder/subfolder/b_subfolder.txt'
]
assert expected == result
def test_extract_cbz_basic_with_weird_filename_extension(self):
test_file = self.get_test_loc('archive/cbz/t.cbz.foo')
test_dir = self.get_temp_dir()
archive.extract_zip(test_file, test_dir)
extracted = self.collect_extracted_path(test_dir)
expected = ['/t/', '/t/t.txt']
assert expected == extracted
def test_extract_zip_with_password(self):
test_file = self.get_test_loc('archive/zip/zip_password_nexb.zip')
test_dir = self.get_temp_dir()
try:
archive.extract_zip(test_file, test_dir)
except Exception, e:
assert isinstance(e, ExtractErrorFailedToExtract)
assert 'Password protected archive, unable to extract' in str(e)
def test_extract_zip_java_jar(self):
test_file = self.get_test_loc('archive/zip/jar/simple.jar')
test_dir = self.get_temp_dir()
archive.extract_zip(test_file, test_dir)
extracted = self.collect_extracted_path(test_dir)
expected = [
'/META-INF/',
'/META-INF/MANIFEST.MF',
'/org/',
'/org/jvnet/',
'/org/jvnet/glassfish/',
'/org/jvnet/glassfish/comms/',
'/org/jvnet/glassfish/comms/sipagent/',
'/org/jvnet/glassfish/comms/sipagent/actions/',
'/org/jvnet/glassfish/comms/sipagent/actions/Bundle.properties',
'/org/jvnet/glassfish/comms/sipagent/actions/SipAgentCookieAction.class',
'/org/jvnet/glassfish/comms/sipagent/actions/bd.png',
'/org/jvnet/glassfish/comms/sipagent/actions/bd24.png',
'/org/jvnet/glassfish/comms/sipagent/org-jvnet-glassfish-comms-sipagent-actions-SipAgentCookieAction.instance',
'/org/jvnet/glassfish/comms/sipagent/org-jvnet-glassfish-comms-sipagent-actions-SipAgentCookieAction_1.instance'
def test_extract_zip_with_backslash_in_path_1(self):
test_file = self.get_test_loc('archive/zip/backslash/backslash1.zip')
test_dir = self.get_temp_dir()
archive.extract_zip(test_file, test_dir)
# Info-ZIP 'zip' displays:
# warning: booxw-1202-bin.distribution.zip appears to use
# backslashes as path separators (which is the right thing to do)
expected = ['scripts/AutomaticClose.int']
check_files(test_dir, expected)
result = os.path.join(test_dir, 'scripts/AutomaticClose.int')
assert os.path.exists(result)
def test_extract_zip_with_relative_path_deeply_nested(self):
test_file = self.get_test_loc('archive/zip/relative_nested.zip')
test_dir = self.get_temp_dir()
archive.extract_zip(test_file, test_dir)
result = self.collect_extracted_path(test_dir)
expected = [
'/dotdot/',
'/dotdot/dotdot/',
'/dotdot/dotdot/dotdot/',
'/dotdot/dotdot/dotdot/dotdot/',
'/dotdot/dotdot/dotdot/dotdot/dotdot/',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/a_parent_folder.txt',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/sub/',
def test_extract_zip_with_unicode_path_should_extract_without_error(self):
test_file = self.get_test_loc('archive/zip/zip_unicode.zip')
test_dir = self.get_temp_dir()
result = archive.extract_zip(test_file, test_dir)
assert [] == result
assert os.listdir(test_dir)
def test_extract_zip_basic(self):
test_file = self.get_test_loc('archive/zip/basic.zip')
test_dir = self.get_temp_dir()
result = archive.extract_zip(test_file, test_dir)
assert [] == result
expected = ['c/a/a.txt', 'c/b/a.txt', 'c/c/a.txt']
check_files(test_dir, expected)
def test_extract_zip_can_extract_zip_with_directory_not_marked_with_trailing_slash(self):
test_file = self.get_test_loc('archive/zip/directory-with-no-trailing-slash.zip')
test_dir = self.get_temp_dir()
result = archive.extract_zip(test_file, test_dir)
assert [] == result
expected = ['online_upgrade_img/machine_type']
check_files(test_dir, expected)