From 531aaba3a0acab82b6b5280271118d9585729b29 Mon Sep 17 00:00:00 2001 From: Stefano Rivera Date: Fri, 31 Dec 2010 17:37:24 +0200 Subject: [PATCH] Basic test suite for ubuntutools.archive --- test-data/example_1.0-1.debian.tar.gz | Bin 0 -> 1170 bytes test-data/example_1.0-1.dsc | 17 ++++ test-data/example_1.0.orig.tar.gz | Bin 0 -> 170 bytes ubuntutools/test/test_archive.py | 135 ++++++++++++++++++++++++++ 4 files changed, 152 insertions(+) create mode 100644 test-data/example_1.0-1.debian.tar.gz create mode 100644 test-data/example_1.0-1.dsc create mode 100644 test-data/example_1.0.orig.tar.gz create mode 100644 ubuntutools/test/test_archive.py diff --git a/test-data/example_1.0-1.debian.tar.gz b/test-data/example_1.0-1.debian.tar.gz new file mode 100644 index 0000000000000000000000000000000000000000..0dfd3a7e1658ee5c3f79f35e259b6ab629b90418 GIT binary patch literal 1170 zcmV;D1a12tiwFP!000021MOIClbbdW_SfoHY}2%pq&5cInAYtj;cR@z3|G&il zfj&O+e~dlFe|bR$_= zOkmH%hAcTNIg>Z)f5{WFNLV59OZnuhm`~2JvPgR~TG1jE6Tsb;(Y&G@Uc5d7OVc$| z|NAA0WKyIFPetz?<)Se^)PB|sLYB$qW3v0iKbvndCeg{7<7krSeqUS_ z+F?s*CExY0vENT=)xV}|)e7}vYWwuMcvDtqTb9!wIK4VE%mF!O5~WF?sywNSO;#nT zR#4eLB!yl|uX?g9b3w=)g^{(d$n?1PCQWV7{kqKJu?;N*+H<2RfhFb0bd@?lmWrTV zP+PhS`!Z!)Y%#&~k>4p3lCyO!k+~TYAj+-0PWXZRNpVAt%!g|xGr&RBmbHW}rGNq% zuQ*sSh-J^ocy6gMF&R6?M=v=6Lw(&EK-N09~(P|vp$_3xK@Z1Z>pE@W=AZVt?L5MR~(XB)m?9h#r z&0rNP@u1O&N_^*_QmuKj4CqFqXX%8v01La(JO6hK9_guZoBw%@WPG&uzeDRj|BuJ2 z{u`KW{dW?4_ieuxyuW5ef1iA$(AyI7{e*mzX7Kziv +Standards-Version: 3.9.1 +Build-Depends: debhelper (>= 7.0.50~) +Checksums-Sha1: + 13c309f70155ab5be50553ce72f3344c48851013 170 example_1.0.orig.tar.gz + ae9dcd225000d24128b8eccbdc472abcfa9b60ee 1170 example_1.0-1.debian.tar.gz +Checksums-Sha256: + 4908cb2ce47c9835fb74476ab5dba0298da7a56a389328b845d07575f5ea6276 170 example_1.0.orig.tar.gz + ca366dc1ae0efa1df6537f6882e87134a0409e1589ca18daf56d065beb9cfce3 1170 example_1.0-1.debian.tar.gz +Files: + 8054fdf3653160e13d9539ca36c2e311 170 example_1.0.orig.tar.gz + 8538e8b6cb8162d5c6886b18c132a1c8 1170 example_1.0-1.debian.tar.gz diff --git a/test-data/example_1.0.orig.tar.gz b/test-data/example_1.0.orig.tar.gz new file mode 100644 index 0000000000000000000000000000000000000000..1d3482e74e5d142569d08e073d34700829f1d6e1 GIT binary patch literal 170 zcmb2|=3odpBJ0b*{Pvsv+^E0^~y8u$LD{?^G?)ueSLS^ TkpUUhykT4X#;A`$gMk46H^NWl literal 0 HcmV?d00001 diff --git a/ubuntutools/test/test_archive.py b/ubuntutools/test/test_archive.py new file mode 100644 index 0000000..cafa1c9 --- /dev/null +++ b/ubuntutools/test/test_archive.py @@ -0,0 +1,135 @@ +# test_archive.py - Test suite for ubuntutools.archive +# +# Copyright (C) 2010, Stefano Rivera +# +# 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. + +from __future__ import with_statement + +import __builtin__ +import os.path +import shutil +import StringIO +import subprocess +import sys +import tempfile +import urllib2 +import urlparse + +import mox + +from ubuntutools.archive import Dsc, DebianSourcePackage, UbuntuSourcePackage +from ubuntutools.logger import Logger +from ubuntutools.test import unittest + +class DscVerificationTestCase(mox.MoxTestBase, unittest.TestCase): + def setUp(self): + super(DscVerificationTestCase, self).setUp() + with open('test-data/example_1.0-1.dsc', 'rb') as f: + self.dsc = Dsc(f.read()) + + def tearDown(self): + super(DscVerificationTestCase, self).tearDown() + + def test_good(self): + self.assertTrue(self.dsc.verify_file( + 'test-data/example_1.0.orig.tar.gz')) + self.assertTrue(self.dsc.verify_file( + 'test-data/example_1.0-1.debian.tar.gz')) + + def test_missing(self): + self.assertFalse(self.dsc.verify_file( + 'test-data/does.not.exist')) + + def test_bad(self): + fn = 'test-data/example_1.0.orig.tar.gz' + with open(fn, 'rb') as f: + data = f.read() + data = data[:-1] + chr(ord(data[-1]) ^ 8) + self.mox.StubOutWithMock(__builtin__, 'open') + open(fn, 'rb').AndReturn(StringIO.StringIO(data)) + self.mox.ReplayAll() + self.assertFalse(self.dsc.verify_file(fn)) + + 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() + + +class LocalSourcePackageTestCase(mox.MoxTestBase, unittest.TestCase): + SourcePackage = DebianSourcePackage + + def setUp(self): + super(LocalSourcePackageTestCase, self).setUp() + self.workdir = tempfile.mkdtemp(prefix='udt-test') + + for funcname in ('ubuntutools.archive.Distribution', + 'ubuntutools.archive.rmadison', + 'urllib2.urlopen', + ): + mod, func = funcname.rsplit('.', 1) + setattr(self, 'o_' + func, getattr(sys.modules[mod], func)) + self.mox.StubOutWithMock(sys.modules[mod], func) + self.mox.StubOutWithMock(Logger, 'stdout') + + def tearDown(self): + super(LocalSourcePackageTestCase, self).tearDown() + shutil.rmtree(self.workdir) + + def test_local_copy(self): + urllib2.urlopen(mox.Regex('^file://.*\.dsc$') + ).WithSideEffects(self.o_urlopen) + urllib2.urlopen(mox.Regex('^file://.*\.orig\.tar\.gz$') + ).WithSideEffects(self.o_urlopen) + urllib2.urlopen(mox.Regex('^file://.*\.debian\.tar\.gz$') + ).WithSideEffects(self.o_urlopen) + Logger.stdout.write(mox.IsA(basestring)).MultipleTimes() + Logger.stdout.flush().MultipleTimes() + self.mox.ReplayAll() + + pkg = self.SourcePackage('example', '1.0-1', 'main', + dscfile='test-data/example_1.0-1.dsc', + workdir=self.workdir) + pkg.pull() + pkg.unpack() + + def test_verification(self): + shutil.copy2('test-data/example_1.0-1.dsc', self.workdir) + shutil.copy2('test-data/example_1.0.orig.tar.gz', self.workdir) + shutil.copy2('test-data/example_1.0-1.debian.tar.gz', self.workdir) + with open(os.path.join(self.workdir, 'example_1.0-1.debian.tar.gz'), + 'r+b') as f: + f.write('CORRUPTION') + urllib2.urlopen(mox.Regex('^file://.*\.dsc$') + ).WithSideEffects(self.o_urlopen) + urllib2.urlopen(mox.Regex('^file://.*\.debian\.tar\.gz$') + ).WithSideEffects(self.o_urlopen) + Logger.stdout.write(mox.IsA(basestring)).MultipleTimes() + Logger.stdout.flush().MultipleTimes() + self.mox.ReplayAll() + + pkg = self.SourcePackage('example', '1.0-1', 'main', + dscfile='test-data/example_1.0-1.dsc', + workdir=self.workdir) + pkg.pull() + pkg.unpack() + +class UbuntuLocalSourcePackageTestCase(LocalSourcePackageTestCase): + SourcePackage = UbuntuSourcePackage