2010-12-31 17:37:24 +02:00
|
|
|
# test_archive.py - Test suite for ubuntutools.archive
|
|
|
|
#
|
2012-03-26 10:00:46 +02:00
|
|
|
# Copyright (C) 2010-2012, Stefano Rivera <stefanor@ubuntu.com>
|
2010-12-31 17:37:24 +02:00
|
|
|
#
|
|
|
|
# Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
# purpose with or without fee is hereby granted, provided that the above
|
|
|
|
# copyright notice and this permission notice appear in all copies.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
|
|
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
|
|
# AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
|
|
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
|
|
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
|
|
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
# PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
|
|
|
|
|
|
|
import os.path
|
|
|
|
import shutil
|
|
|
|
import tempfile
|
2020-06-15 18:28:29 -04:00
|
|
|
import unittest
|
|
|
|
|
2017-05-01 00:20:03 +02:00
|
|
|
from io import BytesIO
|
2020-06-15 18:20:51 -04:00
|
|
|
from unittest import mock
|
2010-12-31 18:52:22 +02:00
|
|
|
import ubuntutools.archive
|
2011-01-15 19:47:13 +02:00
|
|
|
|
2011-01-15 17:56:48 +02:00
|
|
|
from ubuntutools.test.example_package import ExamplePackage
|
|
|
|
|
2012-03-26 10:00:46 +02:00
|
|
|
|
2011-01-15 17:56:48 +02:00
|
|
|
def setUpModule():
|
2011-01-15 19:47:13 +02:00
|
|
|
if not os.path.exists('test-data/example-0.1-1.dsc'):
|
|
|
|
ex_pkg = ExamplePackage()
|
|
|
|
ex_pkg.create_orig()
|
|
|
|
ex_pkg.create()
|
|
|
|
ex_pkg.cleanup()
|
2011-01-15 17:56:48 +02:00
|
|
|
|
2010-12-31 17:37:24 +02:00
|
|
|
|
2014-12-15 03:25:41 +00:00
|
|
|
class DscVerificationTestCase(unittest.TestCase):
|
2010-12-31 17:37:24 +02:00
|
|
|
def setUp(self):
|
2011-01-15 19:47:13 +02:00
|
|
|
with open('test-data/example_1.0-1.dsc', 'rb') as f:
|
2010-12-31 18:52:22 +02:00
|
|
|
self.dsc = ubuntutools.archive.Dsc(f.read())
|
2010-12-31 17:37:24 +02:00
|
|
|
|
|
|
|
def test_good(self):
|
2011-01-15 19:47:13 +02:00
|
|
|
self.assertTrue(self.dsc.verify_file(
|
|
|
|
'test-data/example_1.0.orig.tar.gz'))
|
|
|
|
self.assertTrue(self.dsc.verify_file(
|
2014-02-25 22:46:10 +02:00
|
|
|
'test-data/example_1.0-1.debian.tar.xz'))
|
2010-12-31 17:37:24 +02:00
|
|
|
|
|
|
|
def test_missing(self):
|
2011-01-15 19:47:13 +02:00
|
|
|
self.assertFalse(self.dsc.verify_file(
|
|
|
|
'test-data/does.not.exist'))
|
2010-12-31 17:37:24 +02:00
|
|
|
|
|
|
|
def test_bad(self):
|
2011-01-15 19:47:13 +02:00
|
|
|
fn = 'test-data/example_1.0.orig.tar.gz'
|
2010-12-31 17:37:24 +02:00
|
|
|
with open(fn, 'rb') as f:
|
|
|
|
data = f.read()
|
2019-09-04 19:17:00 -03:00
|
|
|
last_byte = chr(data[-1] ^ 8).encode()
|
2014-12-18 23:03:23 +00:00
|
|
|
data = data[:-1] + last_byte
|
2014-12-15 03:25:41 +00:00
|
|
|
m = mock.MagicMock(name='open', spec=open)
|
|
|
|
m.return_value = BytesIO(data)
|
2019-09-04 19:17:00 -03:00
|
|
|
with mock.patch('builtins.open', m):
|
2014-12-15 03:25:41 +00:00
|
|
|
self.assertFalse(self.dsc.verify_file(fn))
|
2010-12-31 17:37:24 +02:00
|
|
|
|
|
|
|
def test_sha1(self):
|
|
|
|
del self.dsc['Checksums-Sha256']
|
|
|
|
self.test_good()
|
|
|
|
self.test_bad()
|
|
|
|
|
|
|
|
def test_md5(self):
|
|
|
|
del self.dsc['Checksums-Sha256']
|
|
|
|
del self.dsc['Checksums-Sha1']
|
|
|
|
self.test_good()
|
|
|
|
self.test_bad()
|
|
|
|
|
|
|
|
|
2014-12-16 01:44:13 +00:00
|
|
|
class LocalSourcePackageTestCase(unittest.TestCase):
|
2010-12-31 21:08:16 +02:00
|
|
|
SourcePackage = ubuntutools.archive.UbuntuSourcePackage
|
2010-12-31 17:37:24 +02:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.workdir = tempfile.mkdtemp(prefix='udt-test')
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
shutil.rmtree(self.workdir)
|
|
|
|
|
2011-11-22 15:57:02 +02:00
|
|
|
def test_local_copy(self):
|
2018-02-28 15:47:53 -05:00
|
|
|
pkg = self.SourcePackage(package='example',
|
|
|
|
version='1.0-1',
|
|
|
|
component='main',
|
2011-01-15 19:47:13 +02:00
|
|
|
dscfile='test-data/example_1.0-1.dsc',
|
2018-02-28 15:47:53 -05:00
|
|
|
workdir=self.workdir,
|
|
|
|
verify_signature=False)
|
|
|
|
pkg.pull()
|
2010-12-31 17:37:24 +02:00
|
|
|
pkg.unpack()
|
|
|
|
|
2011-01-22 23:19:31 +02:00
|
|
|
def test_workdir_srcpkg_noinfo(self):
|
|
|
|
shutil.copy2('test-data/example_1.0-1.dsc', self.workdir)
|
|
|
|
shutil.copy2('test-data/example_1.0.orig.tar.gz', self.workdir)
|
2014-02-25 22:46:10 +02:00
|
|
|
shutil.copy2('test-data/example_1.0-1.debian.tar.xz', self.workdir)
|
2011-09-04 19:01:01 +02:00
|
|
|
|
2011-01-22 23:19:31 +02:00
|
|
|
pkg = self.SourcePackage(dscfile=os.path.join(self.workdir,
|
|
|
|
'example_1.0-1.dsc'),
|
2018-02-28 15:47:53 -05:00
|
|
|
workdir=self.workdir,
|
|
|
|
verify_signature=False)
|
|
|
|
pkg.pull()
|
2011-01-22 23:19:31 +02:00
|
|
|
pkg.unpack()
|
|
|
|
|
|
|
|
def test_workdir_srcpkg_info(self):
|
|
|
|
shutil.copy2('test-data/example_1.0-1.dsc', self.workdir)
|
|
|
|
shutil.copy2('test-data/example_1.0.orig.tar.gz', self.workdir)
|
2014-02-25 22:46:10 +02:00
|
|
|
shutil.copy2('test-data/example_1.0-1.debian.tar.xz', self.workdir)
|
2011-09-04 19:01:01 +02:00
|
|
|
|
2018-02-28 15:47:53 -05:00
|
|
|
pkg = self.SourcePackage(package='example', version='1.0-1',
|
|
|
|
component='main',
|
2011-01-22 23:19:31 +02:00
|
|
|
dscfile=os.path.join(self.workdir,
|
|
|
|
'example_1.0-1.dsc'),
|
2018-02-28 15:47:53 -05:00
|
|
|
workdir=self.workdir,
|
|
|
|
verify_signature=False)
|
|
|
|
pkg.pull()
|
2011-01-22 23:19:31 +02:00
|
|
|
pkg.unpack()
|
|
|
|
|
2010-12-31 17:37:24 +02:00
|
|
|
def test_verification(self):
|
2011-01-15 19:47:13 +02:00
|
|
|
shutil.copy2('test-data/example_1.0-1.dsc', self.workdir)
|
|
|
|
shutil.copy2('test-data/example_1.0.orig.tar.gz', self.workdir)
|
2014-02-25 22:46:10 +02:00
|
|
|
shutil.copy2('test-data/example_1.0-1.debian.tar.xz', self.workdir)
|
|
|
|
with open(os.path.join(self.workdir, 'example_1.0-1.debian.tar.xz'),
|
2010-12-31 17:37:24 +02:00
|
|
|
'r+b') as f:
|
2014-12-18 23:03:23 +00:00
|
|
|
f.write(b'CORRUPTION')
|
2010-12-31 17:37:24 +02:00
|
|
|
|
2018-02-28 15:47:53 -05:00
|
|
|
pkg = self.SourcePackage(package='example',
|
|
|
|
version='1.0-1',
|
|
|
|
component='main',
|
2011-01-15 19:47:13 +02:00
|
|
|
dscfile='test-data/example_1.0-1.dsc',
|
2018-02-28 15:47:53 -05:00
|
|
|
workdir=self.workdir,
|
|
|
|
verify_signature=False)
|
|
|
|
pkg.pull()
|