1 #!/usr/bin/env python2.3
2 from basetest
import BaseTest
3 import sys
, tempfile
, os
, shutil
5 from logging
import getLogger
, DEBUG
, INFO
6 #getLogger().setLevel(DEBUG)
8 sys
.path
.insert(0, '..')
10 from zeroinstall
.zerostore
import Store
, manifest
, BadDigest
, cli
11 from zeroinstall
import SafeException
, support
13 class TestStore(BaseTest
):
17 path
= tempfile
.mktemp()
19 self
.store
= Store(path
)
21 self
.tmp
= tempfile
.mktemp()
25 BaseTest
.tearDown(self
)
27 support
.ro_rmtree(self
.store
.dir)
28 support
.ro_rmtree(self
.tmp
)
31 assert os
.path
.isdir(self
.store
.dir)
32 self
.assertEquals([], os
.listdir(self
.store
.dir))
34 def testEmptyManifest(self
):
35 lines
= list(manifest
.generate_manifest(self
.tmp
))
36 self
.assertEquals([], lines
)
38 def testSimpleManifest(self
):
39 path
= os
.path
.join(self
.tmp
, 'MyFile')
43 os
.utime(path
, (1, 2))
44 lines
= list(manifest
.generate_manifest(self
.tmp
))
45 self
.assertEquals(['F f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0 2 5 MyFile'],
48 def testLinkManifest(self
):
49 path
= os
.path
.join(self
.tmp
, 'MyLink')
50 os
.symlink('Hello', path
)
51 lines
= list(manifest
.generate_manifest(self
.tmp
))
52 self
.assertEquals(['S f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0 5 MyLink'],
56 path
= os
.path
.join(self
.tmp
, 'MyLink')
57 os
.symlink('Hello', path
)
58 mfile
= os
.path
.join(self
.tmp
, '.manifest')
59 for alg_name
in ['sha1', 'sha256', 'sha1new']:
61 alg
= manifest
.get_algorithm(alg_name
)
62 added_digest
= alg
.getID(manifest
.add_manifest_file(self
.tmp
, alg
))
63 digest
= alg
.new_digest()
64 digest
.update('Hello')
65 self
.assertEquals("S %s 5 MyLink\n" % digest
.hexdigest(),
67 manifest
.verify(self
.tmp
, added_digest
)
68 os
.chmod(self
.tmp
, 0700)
71 raise Exception("%s: %s\n%s" % (alg_name
, ex
, ex
.detail
))
73 def populate_sample(self
, target
):
74 """Create a set of files, links and directories in target for testing."""
75 path
= os
.path
.join(target
, 'MyFile')
79 os
.utime(path
, (1, 2))
81 subdir
= os
.path
.join(target
, 'My Dir')
84 subfile
= os
.path
.join(subdir
, '!a file!')
85 f
= file(subfile
, 'w')
88 os
.utime(subfile
, (1, 2))
91 f
= file(subfile
, 'w')
94 os
.chmod(subfile
, 0500)
95 os
.utime(subfile
, (1, 2))
97 os
.symlink('/the/symlink/target',
98 os
.path
.join(target
, 'a symlink'))
101 sha1
= manifest
.get_algorithm('sha1')
102 sha1new
= manifest
.get_algorithm('sha1new')
103 source
= os
.path
.join(self
.tmp
, 'badname')
106 self
.populate_sample(source
)
108 lines
= list(sha1new
.generate_manifest(source
))
109 self
.assertEquals(['F f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0 2 5 MyFile',
110 'S 570b0ce957ab43e774c82fca0ea3873fc452278b 19 a symlink',
112 'F 0236ef92e1e37c57f0eb161e7e2f8b6a8face705 2 10 !a file!',
113 'X b4ab02f2c791596a980fd35f51f5d92ee0b4705c 2 10 !a file!.exe'],
115 digest
= sha1
.getID(manifest
.add_manifest_file(source
, sha1
))
117 copy
= tempfile
.mktemp()
120 # Source must be in the form alg=value
122 cli
.do_copy([source
, copy
])
124 except BadDigest
, ex
:
125 assert 'badname' in str(ex
)
126 source
, badname
= os
.path
.join(self
.tmp
, digest
), source
127 os
.rename(badname
, source
)
129 # Can't copy sha1 implementations (unsafe)
131 cli
.do_copy([source
, copy
])
132 except SafeException
, ex
:
133 assert 'sha1' in str(ex
)
135 # Already have a .manifest
137 manifest
.add_manifest_file(source
, sha1new
)
139 except SafeException
, ex
:
140 assert '.manifest' in str(ex
)
142 os
.chmod(source
, 0700)
143 os
.unlink(os
.path
.join(source
, '.manifest'))
146 digest
= sha1new
.getID(manifest
.add_manifest_file(source
, sha1new
))
147 source
, badname
= os
.path
.join(self
.tmp
, digest
), source
148 os
.rename(badname
, source
)
150 cli
.do_copy([source
, copy
])
152 self
.assertEquals('Hello', file(os
.path
.join(copy
, digest
, 'MyFile')).read())
154 support
.ro_rmtree(copy
)
156 suite
= unittest
.makeSuite(TestStore
)
157 if __name__
== '__main__':
158 sys
.argv
.append('-v')