~ubuntu-branches/ubuntu/karmic/zeroinstall-injector/karmic

« back to all changes in this revision

Viewing changes to tests/testifacecache.py

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Leonard
  • Date: 2007-01-23 21:50:46 UTC
  • Revision ID: james.westby@ubuntu.com-20070123215046-3ya2x81i99m5ya8r
Tags: upstream-0.25
ImportĀ upstreamĀ versionĀ 0.25

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python2.3
 
2
import sys, tempfile, os, shutil
 
3
import unittest
 
4
import data
 
5
from logging import getLogger, DEBUG, INFO
 
6
#getLogger().setLevel(DEBUG)
 
7
 
 
8
sys.path.insert(0, '..')
 
9
from zeroinstall.injector import basedir, download, model, gpg, trust
 
10
from zeroinstall.injector.namespaces import *
 
11
from zeroinstall.injector.iface_cache import iface_cache, PendingFeed
 
12
 
 
13
class TestIfaceCache(unittest.TestCase):
 
14
        def setUp(self):
 
15
                self.config_home = tempfile.mktemp()
 
16
                self.cache_home = tempfile.mktemp()
 
17
                self.gnupg_home = tempfile.mktemp()
 
18
                os.environ['XDG_CONFIG_HOME'] = self.config_home
 
19
                os.environ['XDG_CACHE_HOME'] = self.cache_home
 
20
                os.environ['XDG_CACHE_DIRS'] = ''
 
21
                os.environ['GNUPGHOME'] = self.gnupg_home
 
22
                reload(basedir)
 
23
 
 
24
                os.mkdir(self.config_home, 0700)
 
25
                os.mkdir(self.cache_home, 0700)
 
26
                os.mkdir(self.gnupg_home, 0700)
 
27
 
 
28
                iface_cache.__init__()
 
29
        
 
30
        def tearDown(self):
 
31
                shutil.rmtree(self.config_home)
 
32
                shutil.rmtree(self.cache_home)
 
33
                shutil.rmtree(self.gnupg_home)
 
34
 
 
35
        def testList(self):
 
36
                self.assertEquals([], iface_cache.list_all_interfaces())
 
37
                iface_dir = basedir.save_cache_path(config_site, 'interfaces')
 
38
                file(os.path.join(iface_dir, 'http%3a%2f%2ffoo'), 'w').close()
 
39
                self.assertEquals(['http://foo'],
 
40
                                iface_cache.list_all_interfaces())
 
41
                # TODO: test overrides
 
42
 
 
43
        def testCheckSigned(self):
 
44
                trust.trust_db.trust_key(
 
45
                        '92429807C9853C0744A68B9AAE07828059A53CC1')
 
46
                iface = iface_cache.get_interface('http://foo')
 
47
                src = tempfile.TemporaryFile()
 
48
 
 
49
                # Unsigned
 
50
                src.write("hello")
 
51
                src.flush()
 
52
                src.seek(0)
 
53
                try:
 
54
                        PendingFeed(iface.uri, src)
 
55
                        assert 0
 
56
                except model.SafeException:
 
57
                        pass
 
58
 
 
59
                stream = tempfile.TemporaryFile()
 
60
                stream.write(data.thomas_key)
 
61
                stream.seek(0)
 
62
 
 
63
                gpg.import_key(stream)
 
64
 
 
65
                # Signed
 
66
                src.seek(0)
 
67
                src.write(data.foo_signed)
 
68
                src.flush()
 
69
                src.seek(0)
 
70
 
 
71
                pending = PendingFeed(iface.uri, src)
 
72
                iface_cache.add_pending(pending)
 
73
                assert iface_cache.update_interface_if_trusted(iface, pending.sigs, pending.new_xml)
 
74
 
 
75
                self.assertEquals(['http://foo'],
 
76
                                iface_cache.list_all_interfaces())
 
77
 
 
78
                self.assertEquals(1116788178,  iface.last_modified)
 
79
 
 
80
                # mtimes are unreliable because copying often changes them -
 
81
                # check that we stored the mtime in an attribute
 
82
                upstream_dir = basedir.save_cache_path(config_site, 'interfaces')
 
83
                cached = os.path.join(upstream_dir, model.escape(iface.uri))
 
84
                os.utime(cached, None)
 
85
 
 
86
                iface_cache.__init__()
 
87
                iface = iface_cache.get_interface('http://foo')
 
88
                self.assertEquals(1116788178,  iface.last_modified)
 
89
 
 
90
        def testXMLupdate(self):
 
91
                trust.trust_db.trust_key(
 
92
                        '92429807C9853C0744A68B9AAE07828059A53CC1')
 
93
                stream = tempfile.TemporaryFile()
 
94
                stream.write(data.thomas_key)
 
95
                stream.seek(0)
 
96
                gpg.import_key(stream)
 
97
 
 
98
                iface = iface_cache.get_interface('http://foo')
 
99
                src = tempfile.TemporaryFile()
 
100
                src.write(data.foo_signed_xml)
 
101
                src.seek(0)
 
102
                pending = PendingFeed(iface.uri, src)
 
103
                iface_cache.add_pending(pending)
 
104
                assert iface_cache.update_interface_if_trusted(iface, pending.sigs, pending.new_xml)
 
105
 
 
106
                iface_cache.__init__()
 
107
                iface = iface_cache.get_interface('http://foo')
 
108
                assert iface.last_modified == 1154850229
 
109
 
 
110
                # mtimes are unreliable because copying often changes them -
 
111
                # check that we extract the time from the signature when upgrading
 
112
                upstream_dir = basedir.save_cache_path(config_site, 'interfaces')
 
113
                cached = os.path.join(upstream_dir, model.escape(iface.uri))
 
114
                os.utime(cached, None)
 
115
 
 
116
                iface_cache.__init__()
 
117
                iface = iface_cache.get_interface('http://foo')
 
118
                assert iface.last_modified > 1154850229
 
119
 
 
120
                src = tempfile.TemporaryFile()
 
121
                src.write(data.new_foo_signed_xml)
 
122
                src.seek(0)
 
123
 
 
124
                pending = PendingFeed(iface.uri, src)
 
125
                iface_cache.add_pending(pending)
 
126
                assert iface_cache.update_interface_if_trusted(iface, pending.sigs, pending.new_xml)
 
127
 
 
128
                # Can't 'update' to an older copy
 
129
                src = tempfile.TemporaryFile()
 
130
                src.write(data.foo_signed_xml)
 
131
                src.seek(0)
 
132
                try:
 
133
                        pending = PendingFeed(iface.uri, src)
 
134
                        iface_cache.add_pending(pending)
 
135
                        assert iface_cache.update_interface_if_trusted(iface, pending.sigs, pending.new_xml)
 
136
 
 
137
                        assert 0
 
138
                except model.SafeException:
 
139
                        pass
 
140
 
 
141
        def testTimes(self):
 
142
                stream = tempfile.TemporaryFile()
 
143
                stream.write(data.thomas_key)
 
144
                stream.seek(0)
 
145
                gpg.import_key(stream)
 
146
 
 
147
                upstream_dir = basedir.save_cache_path(config_site, 'interfaces')
 
148
                cached = os.path.join(upstream_dir, model.escape('http://foo'))
 
149
 
 
150
                stream = file(cached, 'w')
 
151
                stream.write(data.foo_signed_xml)
 
152
                stream.close()
 
153
 
 
154
                signed = iface_cache._get_signature_date('http://foo')
 
155
                assert signed == None
 
156
 
 
157
                trust.trust_db.trust_key(
 
158
                        '92429807C9853C0744A68B9AAE07828059A53CC1')
 
159
 
 
160
                signed = iface_cache._get_signature_date('http://foo')
 
161
                assert signed == 1154850229
 
162
 
 
163
                stream = file(cached, 'w+')
 
164
                stream.seek(0)
 
165
                stream.write('Hello')
 
166
                stream.close()
 
167
 
 
168
                # When the signature is invalid, we just return None.
 
169
                # This is because versions < 0.22 used to corrupt the signatue
 
170
                # by adding an attribute to the XML
 
171
                signed = iface_cache._get_signature_date('http://foo')
 
172
                assert signed == None
 
173
 
 
174
suite = unittest.makeSuite(TestIfaceCache)
 
175
if __name__ == '__main__':
 
176
        sys.argv.append('-v')
 
177
        unittest.main()