Fixed package-implementation scoring
[zeroinstall/solver.git] / tests / testrecipe.py
blob543414a1fd7963af586275a5ba33d81ce2c2d198
1 #!/usr/bin/env python
2 from __future__ import with_statement
3 import unittest
4 import sys
5 import os
6 import tempfile
7 import shutil
8 from basetest import BaseTest
10 sys.path.insert(0, '..')
12 from zeroinstall import SafeException
13 from zeroinstall.injector.fetch import StepRunner
14 from zeroinstall.injector.model import RenameStep
16 class TestRecipe(BaseTest):
17 def setUp(self):
18 super(TestRecipe, self).setUp()
19 self.basedir = tempfile.mkdtemp()
20 self.join = lambda *a: os.path.join(self.basedir, *a)
21 os.makedirs(self.join("dir1"))
22 os.makedirs(self.join("level1", "level2"))
23 with open(self.join("level1", "level2", "level3"), 'w') as f:
24 f.write("level3 contents")
25 with open(self.join("rootfile"), 'w') as f:
26 f.write("rootfile contents")
28 def tearDown(self):
29 shutil.rmtree(self.basedir)
30 super(TestRecipe, self).tearDown()
32 def _apply_step(self, step, **k):
33 if not 'impl_hint' in k: k['impl_hint'] = None
34 cls = StepRunner.class_for(step)
35 runner = cls(step, **k)
36 # NOTE: runner.prepare() is not performed in these tests,
37 # as they test local operations only that require no preparation
38 runner.apply(self.basedir)
40 def _assert_denies_escape(self, step):
41 try:
42 self._apply_step(step)
43 assert False
44 except SafeException as e:
45 if not 'is not within the base directory' in str(e): raise e
47 def testRenameDisallowsEscapingArchiveDirViaSrcSymlink(self):
48 os.symlink("/usr/bin", self.join("bin"))
49 self._assert_denies_escape(RenameStep(source="bin/gpg", dest="gpg"))
51 def testRenameDisallowsEscapingArchiveDirViaDestSymlink(self):
52 os.symlink("/tmp", self.join("tmp"))
53 self._assert_denies_escape(RenameStep(source="rootfile", dest="tmp/surprise"))
55 def testRenameDisallowsEscapingArchiveDirViaSrcRelativePath(self):
56 self._assert_denies_escape(RenameStep(source="../somefile", dest="somefile"))
58 def testRenameDisallowsEscapingArchiveDirViaDestRelativePath(self):
59 self._assert_denies_escape(RenameStep(source="rootfile", dest="../somefile"))
61 def testRenameDisallowsEscapingArchiveDirViaSrcAbsolutePath(self):
62 self._assert_denies_escape(RenameStep(source="/usr/bin/gpg", dest="gpg"))
64 def testRenameDisallowsEscapingArchiveDirViaDestAbsolutePath(self):
65 self._assert_denies_escape(RenameStep(source="rootfile", dest="/tmp/rootfile"))
67 if __name__ == '__main__':
68 unittest.main()