This repository has been archived by the owner on Sep 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathautoupdate_unittest.py
executable file
·326 lines (274 loc) · 12.8 KB
/
autoupdate_unittest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/usr/bin/python
# Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Unit tests for autoupdate.py."""
import json
import os
import shutil
import socket
import unittest
import cherrypy
import mox
import autoupdate
import autoupdate_lib
import common_util
_TEST_REQUEST = """
<client_test xmlns:o="http://www.google.com/update2/request" updaterversion="%(client)s" protocol="3.0">
<app version="%(version)s" track="%(track)s" board="%(board)s" />
<updatecheck />
<event eventresult="%(event_result)d" eventtype="%(event_type)d" />
</client_test>"""
#pylint: disable=W0212
class AutoupdateTest(mox.MoxTestBase):
def setUp(self):
mox.MoxTestBase.setUp(self)
self.mox.StubOutWithMock(common_util, 'GetFileSize')
self.mox.StubOutWithMock(common_util, 'GetFileSha1')
self.mox.StubOutWithMock(common_util, 'GetFileSha256')
self.mox.StubOutWithMock(autoupdate_lib, 'GetUpdateResponse')
self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetLatestImageDir')
self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetRemotePayloadAttrs')
self.port = 8080
self.test_board = 'test-board'
self.build_root = '/src_path/build/images'
self.latest_dir = '12345_af_12-a1'
self.latest_verision = '12345_af_12'
self.static_image_dir = '/tmp/static-dir/'
self.hostname = '%s:%s' % (socket.gethostname(), self.port)
self.test_dict = {
'client': 'ChromeOSUpdateEngine-1.0',
'version': 'ForcedUpdate',
'track': 'unused_var',
'board': self.test_board,
'event_result': 2,
'event_type': 3
}
self.test_data = _TEST_REQUEST % self.test_dict
self.forced_image_path = '/path_to_force/coreos_developer_image.bin'
self.sha1 = 12345
self.size = 54321
self.url = 'http://%s/static/update.gz' % self.hostname
self.payload = 'My payload'
self.sha256 = 'SHA LA LA'
cherrypy.request.base = 'http://%s' % self.hostname
os.makedirs(self.static_image_dir)
def tearDown(self):
shutil.rmtree(self.static_image_dir)
def _DummyAutoupdateConstructor(self, **kwargs):
"""Creates a dummy autoupdater. Used to avoid using constructor."""
dummy = autoupdate.Autoupdate(static_dir=self.static_image_dir, **kwargs)
return dummy
def testGetRightSignedDeltaPayloadDir(self):
"""Test that our directory is what we expect it to be for signed updates."""
self.mox.StubOutWithMock(common_util, 'GetFileMd5')
key_path = 'test_key_path'
src_image = 'test_src_image'
target_image = 'test_target_image'
src_hash = '12345'
target_hash = '67890'
key_hash = 'abcde'
common_util.GetFileMd5(src_image).AndReturn(src_hash)
common_util.GetFileMd5(target_image).AndReturn(target_hash)
common_util.GetFileMd5(key_path).AndReturn(key_hash)
self.mox.ReplayAll()
au_mock = self._DummyAutoupdateConstructor()
au_mock.private_key = key_path
update_dir = au_mock.FindCachedUpdateImageSubDir(src_image, target_image)
self.assertEqual(os.path.basename(update_dir),
'%s_%s+%s+patched_kernel' %
(src_hash, target_hash, key_hash))
self.mox.VerifyAll()
def testGenerateLatestUpdateImageWithForced(self):
self.mox.StubOutWithMock(autoupdate.Autoupdate,
'GenerateUpdateImageWithCache')
au_mock = self._DummyAutoupdateConstructor()
au_mock._GetLatestImageDir(self.test_board).AndReturn(
os.path.join(self.build_root, self.test_board, self.latest_dir))
au_mock.GenerateUpdateImageWithCache(
os.path.join(self.build_root, self.test_board, self.latest_dir,
'coreos_developer_image.bin'),
static_image_dir=self.static_image_dir).AndReturn('update.gz')
self.mox.ReplayAll()
self.assertTrue(au_mock.GenerateLatestUpdateImage(self.test_board,
'ForcedUpdate',
self.static_image_dir))
self.mox.VerifyAll()
def testHandleUpdatePingForForcedImage(self):
self.mox.StubOutWithMock(autoupdate.Autoupdate,
'GenerateUpdateImageWithCache')
self.mox.StubOutWithMock(autoupdate.Autoupdate, '_StoreMetadataToFile')
au_mock = self._DummyAutoupdateConstructor()
test_data = _TEST_REQUEST % self.test_dict
# Generate a fake payload.
update_gz = os.path.join(self.static_image_dir, autoupdate.UPDATE_FILE)
with open(update_gz, 'w') as fh:
fh.write('')
au_mock.GenerateUpdateImageWithCache(
self.forced_image_path,
static_image_dir=self.static_image_dir).AndReturn(None)
common_util.GetFileSha1(os.path.join(
self.static_image_dir, 'update.gz')).AndReturn(self.sha1)
common_util.GetFileSha256(os.path.join(
self.static_image_dir, 'update.gz')).AndReturn(self.sha256)
common_util.GetFileSize(os.path.join(
self.static_image_dir, 'update.gz')).AndReturn(self.size)
au_mock._StoreMetadataToFile(self.static_image_dir,
mox.IsA(autoupdate.UpdateMetadata))
autoupdate_lib.GetUpdateResponse(
self.sha1, self.sha256, self.size, self.url, False, '3.0',
False).AndReturn(self.payload)
self.mox.ReplayAll()
au_mock.forced_image = self.forced_image_path
self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
self.mox.VerifyAll()
def testHandleUpdatePingForLatestImage(self):
self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GenerateLatestUpdateImage')
self.mox.StubOutWithMock(autoupdate.Autoupdate, '_StoreMetadataToFile')
au_mock = self._DummyAutoupdateConstructor()
test_data = _TEST_REQUEST % self.test_dict
# Generate a fake payload.
update_gz = os.path.join(self.static_image_dir, autoupdate.UPDATE_FILE)
with open(update_gz, 'w') as fh:
fh.write('')
au_mock.GenerateLatestUpdateImage(
self.test_board, 'ForcedUpdate', self.static_image_dir).AndReturn(None)
common_util.GetFileSha1(os.path.join(
self.static_image_dir, 'update.gz')).AndReturn(self.sha1)
common_util.GetFileSha256(os.path.join(
self.static_image_dir, 'update.gz')).AndReturn(self.sha256)
common_util.GetFileSize(os.path.join(
self.static_image_dir, 'update.gz')).AndReturn(self.size)
au_mock._StoreMetadataToFile(self.static_image_dir,
mox.IsA(autoupdate.UpdateMetadata))
autoupdate_lib.GetUpdateResponse(
self.sha1, self.sha256, self.size, self.url, False, '3.0',
False).AndReturn(self.payload)
self.mox.ReplayAll()
self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
curr_host_info = au_mock.host_infos.GetHostInfo('127.0.0.1')
self.assertEqual(curr_host_info.attrs['last_known_version'],
'ForcedUpdate')
self.assertEqual(curr_host_info.attrs['last_event_type'],
self.test_dict['event_type'])
self.assertEqual(curr_host_info.attrs['last_event_status'],
self.test_dict['event_result'])
self.mox.VerifyAll()
def testChangeUrlPort(self):
r = autoupdate._ChangeUrlPort('http://fuzzy:8080/static', 8085)
self.assertEqual(r, 'http://fuzzy:8085/static')
r = autoupdate._ChangeUrlPort('http://fuzzy/static', 8085)
self.assertEqual(r, 'http://fuzzy:8085/static')
r = autoupdate._ChangeUrlPort('ftp://fuzzy/static', 8085)
self.assertEqual(r, 'ftp://fuzzy:8085/static')
r = autoupdate._ChangeUrlPort('ftp://fuzzy', 8085)
self.assertEqual(r, 'ftp://fuzzy:8085')
def testHandleHostInfoPing(self):
au_mock = self._DummyAutoupdateConstructor()
self.assertRaises(AssertionError, au_mock.HandleHostInfoPing, None)
# Setup fake host_infos entry and ensure it comes back to us in one piece.
test_ip = '1.2.3.4'
au_mock.host_infos.GetInitHostInfo(test_ip).attrs = self.test_dict
self.assertEqual(
json.loads(au_mock.HandleHostInfoPing(test_ip)), self.test_dict)
def testHandleSetUpdatePing(self):
au_mock = self._DummyAutoupdateConstructor()
test_ip = '1.2.3.4'
test_label = 'test/old-update'
self.assertRaises(
AssertionError, au_mock.HandleSetUpdatePing, test_ip, None)
self.assertRaises(
AssertionError, au_mock.HandleSetUpdatePing, None, test_label)
self.assertRaises(
AssertionError, au_mock.HandleSetUpdatePing, None, None)
au_mock.HandleSetUpdatePing(test_ip, test_label)
self.assertEqual(
au_mock.host_infos.GetHostInfo(test_ip).attrs['forced_update_label'],
test_label)
def testHandleUpdatePingWithSetUpdate(self):
self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GenerateLatestUpdateImage')
self.mox.StubOutWithMock(autoupdate.Autoupdate, '_StoreMetadataToFile')
au_mock = self._DummyAutoupdateConstructor()
test_data = _TEST_REQUEST % self.test_dict
test_label = 'new_update-test/the-new-update'
new_image_dir = os.path.join(self.static_image_dir, test_label)
new_url = self.url.replace('update.gz', test_label + '/update.gz')
au_mock.GenerateLatestUpdateImage(
self.test_board, 'ForcedUpdate', new_image_dir).AndReturn(None)
# Generate a fake payload.
os.makedirs(new_image_dir)
update_gz = os.path.join(new_image_dir, autoupdate.UPDATE_FILE)
with open(update_gz, 'w') as fh:
fh.write('')
common_util.GetFileSha1(os.path.join(
new_image_dir, 'update.gz')).AndReturn(self.sha1)
common_util.GetFileSha256(os.path.join(
new_image_dir, 'update.gz')).AndReturn(self.sha256)
common_util.GetFileSize(os.path.join(
new_image_dir, 'update.gz')).AndReturn(self.size)
au_mock._StoreMetadataToFile(new_image_dir,
mox.IsA(autoupdate.UpdateMetadata))
autoupdate_lib.GetUpdateResponse(
self.sha1, self.sha256, self.size, new_url, False, '3.0',
False).AndReturn(self.payload)
self.mox.ReplayAll()
au_mock.HandleSetUpdatePing('127.0.0.1', test_label)
self.assertEqual(
au_mock.host_infos.GetHostInfo('127.0.0.1').
attrs['forced_update_label'],
test_label)
self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
self.assertFalse('forced_update_label' in
au_mock.host_infos.GetHostInfo('127.0.0.1').attrs)
def testGetVersionFromDir(self):
au = self._DummyAutoupdateConstructor()
# New-style version number.
self.assertEqual(
au._GetVersionFromDir('/foo/x86-alex/R16-1102.0.2011_09_30_0806-a1'),
'1102.0.2011_09_30_0806')
# Old-style version number.
self.assertEqual(
au._GetVersionFromDir('/foo/x86-alex/0.15.938.2011_08_23_0941-a1'),
'0.15.938.2011_08_23_0941')
def testCanUpdate(self):
au = self._DummyAutoupdateConstructor()
# When both the client and the server have new-style versions, we should
# just compare the tokens directly.
self.assertTrue(
au._CanUpdate('1098.0.2011_09_28_1635', '1098.0.2011_09_30_0806'))
self.assertTrue(
au._CanUpdate('1098.0.2011_09_28_1635', '1100.0.2011_09_26_0000'))
self.assertFalse(
au._CanUpdate('1098.0.2011_09_28_1635', '1098.0.2011_09_26_0000'))
self.assertFalse(
au._CanUpdate('1098.0.2011_09_28_1635', '1096.0.2011_09_30_0000'))
# When the device has an old four-token version number, we should skip the
# first two tokens and compare the rest. If there's a tie, go with the
# server's version.
self.assertTrue(au._CanUpdate('0.16.892.0', '892.0.1'))
self.assertTrue(au._CanUpdate('0.16.892.0', '892.0.0'))
self.assertFalse(au._CanUpdate('0.16.892.0', '890.0.0'))
# Test the case where both the client and the server have old-style
# versions.
self.assertTrue(au._CanUpdate('0.16.892.0', '0.16.892.1'))
self.assertFalse(au._CanUpdate('0.16.892.0', '0.16.892.0'))
def testHandleUpdatePingRemotePayload(self):
self.skipTest("broken, don't care")
self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetRemotePayloadAttrs')
remote_urlbase = 'http://remotehost:6666'
remote_payload_path = 'static/path/to/update.gz'
remote_url = '/'.join([remote_urlbase, remote_payload_path, 'update.gz'])
au_mock = self._DummyAutoupdateConstructor(urlbase=remote_urlbase,
payload_path=remote_payload_path,
remote_payload=True)
test_data = _TEST_REQUEST % self.test_dict
au_mock._GetRemotePayloadAttrs(remote_url).AndReturn(
autoupdate.UpdateMetadata(self.sha1, self.sha256, self.size, False))
autoupdate_lib.GetUpdateResponse(
self.sha1, self.sha256, self.size, remote_url, False,
'3.0', False).AndReturn(self.payload)
self.mox.ReplayAll()
self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
self.mox.VerifyAll()
if __name__ == '__main__':
unittest.main()