When creating a temporary directory for extracting an archive, always clear
[zeroinstall.git] / tests / teststore.py
blob9cd81f1c6ef9bf990dcb67f41613429fc5d35189
1 #!/usr/bin/env python2.3
2 from basetest import BaseTest
3 import sys, tempfile, os, shutil
4 import unittest
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):
14 def setUp(self):
15 BaseTest.setUp(self)
17 path = tempfile.mktemp()
18 os.mkdir(path, 0700)
19 self.store = Store(path)
21 self.tmp = tempfile.mktemp()
22 os.mkdir(self.tmp)
24 def tearDown(self):
25 BaseTest.tearDown(self)
27 support.ro_rmtree(self.store.dir)
28 support.ro_rmtree(self.tmp)
30 def testInit(self):
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')
40 f = file(path, 'w')
41 f.write('Hello')
42 f.close()
43 os.utime(path, (1, 2))
44 lines = list(manifest.generate_manifest(self.tmp))
45 self.assertEquals(['F f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0 2 5 MyFile'],
46 lines)
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'],
53 lines)
55 def testVerify(self):
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']:
60 try:
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(),
66 file(mfile).read())
67 manifest.verify(self.tmp, added_digest)
68 os.chmod(self.tmp, 0700)
69 os.unlink(mfile)
70 except BadDigest, ex:
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')
76 f = file(path, 'w')
77 f.write('Hello')
78 f.close()
79 os.utime(path, (1, 2))
81 subdir = os.path.join(target, 'My Dir')
82 os.mkdir(subdir)
84 subfile = os.path.join(subdir, '!a file!')
85 f = file(subfile, 'w')
86 f.write('Some data.')
87 f.close()
88 os.utime(subfile, (1, 2))
90 subfile += '.exe'
91 f = file(subfile, 'w')
92 f.write('Some code.')
93 f.close()
94 os.chmod(subfile, 0500)
95 os.utime(subfile, (1, 2))
97 os.symlink('/the/symlink/target',
98 os.path.join(target, 'a symlink'))
100 def testCopy(self):
101 sha1 = manifest.get_algorithm('sha1')
102 sha1new = manifest.get_algorithm('sha1new')
103 source = os.path.join(self.tmp, 'badname')
104 os.mkdir(source)
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',
111 'D /My Dir',
112 'F 0236ef92e1e37c57f0eb161e7e2f8b6a8face705 2 10 !a file!',
113 'X b4ab02f2c791596a980fd35f51f5d92ee0b4705c 2 10 !a file!.exe'],
114 lines)
115 digest = sha1.getID(manifest.add_manifest_file(source, sha1))
117 copy = tempfile.mktemp()
118 os.mkdir(copy)
119 try:
120 # Source must be in the form alg=value
121 try:
122 cli.do_copy([source, copy])
123 assert 0
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)
130 try:
131 cli.do_copy([source, copy])
132 except SafeException, ex:
133 assert 'sha1' in str(ex)
135 # Already have a .manifest
136 try:
137 manifest.add_manifest_file(source, sha1new)
138 assert 0
139 except SafeException, ex:
140 assert '.manifest' in str(ex)
142 os.chmod(source, 0700)
143 os.unlink(os.path.join(source, '.manifest'))
145 # Switch to sha1new
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())
153 finally:
154 support.ro_rmtree(copy)
156 suite = unittest.makeSuite(TestStore)
157 if __name__ == '__main__':
158 sys.argv.append('-v')
159 unittest.main()