Start development series 0.46-post
[zeroinstall/solver.git] / tests / testmanifest.py
blob41666d94975724f3b471cbe3ffb65c50942fb8a3
1 #!/usr/bin/env python
2 from basetest import BaseTest
3 import sys, tempfile, os, shutil
4 import unittest
6 sys.path.insert(0, '..')
7 from zeroinstall.zerostore import manifest, BadDigest
9 class TestManifest(BaseTest):
10 def setUp(self):
11 BaseTest.setUp(self)
12 self.tmpdir = tempfile.mkdtemp(prefix = 'test-manifest')
14 def tearDown(self):
15 BaseTest.tearDown(self)
16 shutil.rmtree(self.tmpdir)
18 def testUnknownAlgorithm(self):
19 try:
20 manifest.get_algorithm('unknown')
21 assert False
22 except BadDigest:
23 pass
25 def testEmpty(self):
26 self.assertEquals('',
27 '\n'.join(manifest.generate_manifest(self.tmpdir)))
29 def write(self, path, contents, time = None):
30 assert not path.startswith('/')
31 myfile = os.path.join(self.tmpdir, path)
32 stream = file(myfile, 'w')
33 stream.write(contents)
34 stream.close()
35 if time is not None:
36 os.utime(myfile, (time, time))
37 return myfile
39 def testOldSHA(self):
40 mydir = os.path.join(self.tmpdir, 'MyDir')
41 os.mkdir(mydir)
42 myfile = self.write('MyDir/Hello', 'Hello World', 30)
43 myexec = self.write('MyDir/Run me', 'Bang!', 40)
44 os.symlink('Hello', os.path.join(self.tmpdir, 'MyDir/Sym link'))
45 os.chmod(myexec, 0700)
46 os.utime(mydir, (10, 20))
47 self.assertEquals([
48 'D 20 /MyDir',
49 'F 0a4d55a8d778e5022fab701977c5d840bbc486d0 30 11 Hello',
50 'X 4001b8c42ddfb61c453d04930e8ce78fb3a40bc8 40 5 Run me',
51 'S f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0 5 Sym link'],
52 list(manifest.generate_manifest(self.tmpdir)))
54 def testNewSHA1(self):
55 mydir = os.path.join(self.tmpdir, 'MyDir')
56 os.mkdir(mydir)
57 myfile = self.write('MyDir/Hello', 'Hello World', 30)
58 myexec = self.write('MyDir/Run me', 'Bang!', 40)
59 os.symlink('Hello', os.path.join(self.tmpdir, 'MyDir/Sym link'))
60 os.chmod(myexec, 0700)
61 os.utime(mydir, (10, 20))
62 self.assertEquals([
63 'D /MyDir',
64 'F 0a4d55a8d778e5022fab701977c5d840bbc486d0 30 11 Hello',
65 'X 4001b8c42ddfb61c453d04930e8ce78fb3a40bc8 40 5 Run me',
66 'S f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0 5 Sym link'],
67 list(manifest.generate_manifest(self.tmpdir, alg = 'sha1new')))
69 def testOrderingSHA1(self):
70 mydir = os.path.join(self.tmpdir, 'Dir')
71 os.mkdir(mydir)
72 myfile = self.write('Hello', 'Hello World', 30)
73 myfile = self.write('Dir/Hello', 'Hello World', 30)
74 os.utime(mydir, (10, 20))
75 self.assertEquals([
76 'F 0a4d55a8d778e5022fab701977c5d840bbc486d0 30 11 Hello',
77 'D /Dir',
78 'F 0a4d55a8d778e5022fab701977c5d840bbc486d0 30 11 Hello'],
79 list(manifest.generate_manifest(self.tmpdir, alg = 'sha1new')))
81 def testNewSHA256(self):
82 mydir = os.path.join(self.tmpdir, 'MyDir')
83 os.mkdir(mydir)
84 myfile = self.write('MyDir/Hello', 'Hello World', 30)
85 myexec = self.write('MyDir/Run me', 'Bang!', 40)
86 os.symlink('Hello', os.path.join(self.tmpdir, 'MyDir/Sym link'))
87 os.chmod(myexec, 0700)
88 self.assertEquals([
89 'D /MyDir',
90 'F a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e 30 11 Hello',
91 'X 640628586b08f8ed3910bd1e75ba02818959e843b54efafb9c2260a1f77e3ddf 40 5 Run me',
92 'S 185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969 5 Sym link'],
93 list(manifest.generate_manifest(self.tmpdir, alg = 'sha256')))
95 def testOrdering(self):
96 mydir = os.path.join(self.tmpdir, 'Dir')
97 os.mkdir(mydir)
98 myfile = self.write('Hello', 'Hello World', 30)
99 os.utime(mydir, (10, 20))
100 self.assertEquals([
101 'F a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e 30 11 Hello',
102 'D /Dir'],
103 list(manifest.generate_manifest(self.tmpdir, alg='sha256')))
105 def testParseManifest(self):
106 self.assertEquals({}, manifest._parse_manifest(''))
107 parsed = manifest._parse_manifest('F e3d5983c3dfd415af24772b48276d16122fe5a87 1172429666 2980 README\n'
108 'X 8a1f3c5f416f0e63140928102c44cd16ec2c6100 1172429666 5816 install.sh\n'
109 'D /0install\n'
110 'S 2b37e4457a1a38cfab89391ce1bfbe4dc5473fc3 26 mime-application:x-java-archive.png\n')
111 keys = parsed.keys()
112 keys.sort()
113 assert keys == ['0install', '0install/mime-application:x-java-archive.png', 'README', 'install.sh']
114 for bad in ['Hello', 'D bob\n', 'D /bob\nD /bob\n']:
115 try:
116 manifest._parse_manifest(bad)
117 assert False
118 except BadDigest:
119 pass
121 suite = unittest.makeSuite(TestManifest)
122 if __name__ == '__main__':
123 sys.argv.append('-v')
124 unittest.main()