2 from __future__
import with_statement
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
):
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")
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
):
42 self
._apply
_step
(step
)
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__':