1
|
|
|
# coding: utf8 |
2
|
|
|
|
3
|
|
|
""" |
4
|
|
|
This software is licensed under the Apache 2 license, quoted below. |
5
|
|
|
|
6
|
|
|
Copyright 2014 Crystalnix Limited |
7
|
|
|
|
8
|
|
|
Licensed under the Apache License, Version 2.0 (the "License"); you may not |
9
|
|
|
use this file except in compliance with the License. You may obtain a copy of |
10
|
|
|
the License at |
11
|
|
|
|
12
|
|
|
http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
|
14
|
|
|
Unless required by applicable law or agreed to in writing, software |
15
|
|
|
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
16
|
|
|
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
17
|
|
|
License for the specific language governing permissions and limitations under |
18
|
|
|
the License. |
19
|
|
|
""" |
20
|
|
|
|
21
|
|
|
from datetime import datetime, timedelta |
22
|
|
|
|
23
|
|
|
from django.test import TestCase |
24
|
|
|
from django.test.client import Client |
25
|
|
|
from django.core.urlresolvers import reverse |
26
|
|
|
from django.core.files.uploadedfile import SimpleUploadedFile |
27
|
|
|
from django.db.models import signals |
28
|
|
|
from django.core.files.storage import DefaultStorage |
29
|
|
|
|
30
|
|
|
from xmlunittest import XmlTestMixin |
31
|
|
|
from freezegun import freeze_time |
32
|
|
|
from mock import patch |
33
|
|
|
from bitmapist import DayEvents |
34
|
|
|
import factory |
35
|
|
|
|
36
|
|
|
from omaha.tests import fixtures, OverloadTestStorageMixin |
37
|
|
|
from omaha.tests.utils import temporary_media_root |
38
|
|
|
|
39
|
|
|
from omaha.factories import ApplicationFactory, ChannelFactory, PlatformFactory, VersionFactory |
40
|
|
|
from omaha.models import Action, Request, EVENT_DICT_CHOICES, Data, NAME_DATA_DICT_CHOICES, Version |
41
|
|
|
from omaha.utils import redis, get_id |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
class UpdateViewTest(OverloadTestStorageMixin, TestCase, XmlTestMixin): |
45
|
|
|
model = Version |
46
|
|
|
|
47
|
|
|
def setUp(self): |
48
|
|
|
self.client = Client() |
49
|
|
|
redis.flushdb() |
50
|
|
|
super(UpdateViewTest, self).setUp() |
51
|
|
|
|
52
|
|
|
def tearDown(self): |
53
|
|
|
redis.flushdb() |
54
|
|
|
super(UpdateViewTest, self).tearDown() |
55
|
|
|
|
56
|
|
|
@freeze_time('2014-01-01 15:41:48') # 56508 sec |
57
|
|
|
def test_updatecheck_negative(self): |
58
|
|
|
response = self.client.post(reverse('update'), |
59
|
|
|
fixtures.request_update_check, content_type='text/xml') |
60
|
|
|
|
61
|
|
|
self.assertEqual(response.status_code, 200) |
62
|
|
|
|
63
|
|
|
self.assertXmlDocument(response.content) |
64
|
|
|
self.assertXmlEquivalentOutputs(response.content, |
65
|
|
|
fixtures.response_update_check_negative) |
66
|
|
|
|
67
|
|
|
@freeze_time('2014-01-01 15:41:48') # 56508 sec |
68
|
|
|
@temporary_media_root(MEDIA_URL='http://cache.pack.google.com/edgedl/chrome/install/782.112/') |
69
|
|
|
@patch('omaha.models.version_upload_to', lambda o, f: f) |
70
|
|
|
def test_updatecheck_positive(self): |
71
|
|
|
app = ApplicationFactory.create(id='{D0AB2EBC-931B-4013-9FEB-C9C4C2225C8C}', name='chrome') |
72
|
|
|
platform = PlatformFactory.create(name='win') |
73
|
|
|
channel = ChannelFactory.create(name='stable') |
74
|
|
|
obj = VersionFactory.create( |
75
|
|
|
app=app, |
76
|
|
|
platform=platform, |
77
|
|
|
channel=channel, |
78
|
|
|
version='13.0.782.112', |
79
|
|
|
file=SimpleUploadedFile('./chrome_installer.exe', b'_' * 23963192), |
80
|
|
|
file_size=23963192) |
81
|
|
|
obj.file_hash = 'VXriGUVI0TNqfLlU02vBel4Q3Zo=' |
82
|
|
|
obj.save() |
83
|
|
|
|
84
|
|
|
Action.objects.create( |
85
|
|
|
version=obj, |
86
|
|
|
arguments='--do-not-launch-chrome', |
87
|
|
|
event=EVENT_DICT_CHOICES['install'], |
88
|
|
|
run='chrome_installer.exe' |
89
|
|
|
) |
90
|
|
|
|
91
|
|
|
Action.objects.create( |
92
|
|
|
version=obj, |
93
|
|
|
event=EVENT_DICT_CHOICES['postinstall'], |
94
|
|
|
other=dict( |
95
|
|
|
version='13.0.782.112', |
96
|
|
|
onsuccess='exitsilentlyonlaunchcmd', |
97
|
|
|
) |
98
|
|
|
) |
99
|
|
|
|
100
|
|
|
response = self.client.post(reverse('update'), |
101
|
|
|
fixtures.request_update_check, content_type='text/xml') |
102
|
|
|
|
103
|
|
|
self.assertEqual(response.status_code, 200) |
104
|
|
|
|
105
|
|
|
self.assertXmlDocument(response.content) |
106
|
|
|
self.assertXmlEquivalentOutputs(response.content, |
107
|
|
|
fixtures.response_update_check_positive) |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
@freeze_time('2014-01-01 15:41:48') # 56508 sec |
111
|
|
|
@temporary_media_root(MEDIA_URL='http://cache.pack.google.com/edgedl/chrome/install/782.112/') |
112
|
|
|
@patch('omaha.models.version_upload_to', lambda o, f: f) |
113
|
|
|
def test_updatecheck_positive_critical(self): |
114
|
|
|
app = ApplicationFactory.create(id='{D0AB2EBC-931B-4013-9FEB-C9C4C2225C8C}', name='chrome') |
115
|
|
|
platform = PlatformFactory.create(name='win') |
116
|
|
|
channel = ChannelFactory.create(name='stable') |
117
|
|
|
first_version = VersionFactory.create( |
118
|
|
|
app=app, |
119
|
|
|
platform=platform, |
120
|
|
|
channel=channel, |
121
|
|
|
version='13.0.782.110', |
122
|
|
|
file=SimpleUploadedFile('./chrome_installer_first.exe', b'_' * 23963192), |
123
|
|
|
file_size=23963192) |
124
|
|
|
first_version.file_hash = 'VXriGUVI0TNqfLlU02vBel4Q3Zo=' |
125
|
|
|
first_version.save() |
126
|
|
|
|
127
|
|
|
critical_version = VersionFactory.create( |
128
|
|
|
is_critical=True, |
129
|
|
|
app=app, |
130
|
|
|
platform=platform, |
131
|
|
|
channel=channel, |
132
|
|
|
version='13.0.782.111', |
133
|
|
|
file=SimpleUploadedFile('./chrome_installer_critical.exe', b'_' * 23963192), |
134
|
|
|
file_size=23963192) |
135
|
|
|
critical_version.file_hash = 'VXriGUVI0TNqfLlU02vBel4Q3Zo=' |
136
|
|
|
critical_version.save() |
137
|
|
|
|
138
|
|
|
last_version = VersionFactory.create( |
139
|
|
|
app=app, |
140
|
|
|
platform=platform, |
141
|
|
|
channel=channel, |
142
|
|
|
version='13.0.782.112', |
143
|
|
|
file=SimpleUploadedFile('./chrome_installer.exe', b'_' * 23963192), |
144
|
|
|
file_size=23963192) |
145
|
|
|
last_version.file_hash = 'VXriGUVI0TNqfLlU02vBel4Q3Zo=' |
146
|
|
|
last_version.save() |
147
|
|
|
|
148
|
|
|
response = self.client.post(reverse('update'), |
149
|
|
|
fixtures.request_update_check, content_type='text/xml') |
150
|
|
|
|
151
|
|
|
self.assertEqual(response.status_code, 200) |
152
|
|
|
|
153
|
|
|
self.assertXmlDocument(response.content) |
154
|
|
|
self.assertXmlEquivalentOutputs(response.content, |
155
|
|
|
fixtures.response_update_check_postitive_critical) |
156
|
|
|
|
157
|
|
|
|
158
|
|
|
@freeze_time('2014-01-01 15:41:48') # 56508 sec |
159
|
|
|
@temporary_media_root(MEDIA_URL='http://cache.pack.google.com/edgedl/chrome/install/782.112/') |
160
|
|
|
@patch('omaha.models.version_upload_to', lambda o, f: f) |
161
|
|
|
def test_updatecheck_positive_critical_on_other_channel(self): |
162
|
|
|
app = ApplicationFactory.create(id='{D0AB2EBC-931B-4013-9FEB-C9C4C2225C8C}', name='chrome') |
163
|
|
|
platform = PlatformFactory.create(name='win') |
164
|
|
|
channel = ChannelFactory.create(name='stable') |
165
|
|
|
channel2 = ChannelFactory.create(name='alpha') |
166
|
|
|
first_version = VersionFactory.create( |
167
|
|
|
app=app, |
168
|
|
|
platform=platform, |
169
|
|
|
channel=channel, |
170
|
|
|
version='13.0.782.110', |
171
|
|
|
file=SimpleUploadedFile('./chrome_installer_first.exe', b'_' * 23963192), |
172
|
|
|
file_size=23963192) |
173
|
|
|
first_version.file_hash = 'VXriGUVI0TNqfLlU02vBel4Q3Zo=' |
174
|
|
|
first_version.save() |
175
|
|
|
|
176
|
|
|
critical_version = VersionFactory.create( |
177
|
|
|
is_critical=True, |
178
|
|
|
app=app, |
179
|
|
|
platform=platform, |
180
|
|
|
channel=channel2, |
181
|
|
|
version='13.0.782.111', |
182
|
|
|
file=SimpleUploadedFile('./chrome_installer_critical.exe', b'_' * 23963192), |
183
|
|
|
file_size=23963192) |
184
|
|
|
critical_version.file_hash = 'VXriGUVI0TNqfLlU02vBel4Q3Zo=' |
185
|
|
|
critical_version.save() |
186
|
|
|
|
187
|
|
|
last_version = VersionFactory.create( |
188
|
|
|
app=app, |
189
|
|
|
platform=platform, |
190
|
|
|
channel=channel, |
191
|
|
|
version='13.0.782.112', |
192
|
|
|
file=SimpleUploadedFile('./chrome_installer.exe', b'_' * 23963192), |
193
|
|
|
file_size=23963192) |
194
|
|
|
last_version.file_hash = 'VXriGUVI0TNqfLlU02vBel4Q3Zo=' |
195
|
|
|
last_version.save() |
196
|
|
|
|
197
|
|
|
Action.objects.create( |
198
|
|
|
version=last_version, |
199
|
|
|
arguments='--do-not-launch-chrome', |
200
|
|
|
event=EVENT_DICT_CHOICES['install'], |
201
|
|
|
run='chrome_installer.exe' |
202
|
|
|
) |
203
|
|
|
|
204
|
|
|
Action.objects.create( |
205
|
|
|
version=last_version, |
206
|
|
|
event=EVENT_DICT_CHOICES['postinstall'], |
207
|
|
|
other=dict( |
208
|
|
|
version='13.0.782.112', |
209
|
|
|
onsuccess='exitsilentlyonlaunchcmd', |
210
|
|
|
) |
211
|
|
|
) |
212
|
|
|
|
213
|
|
|
response = self.client.post(reverse('update'), |
214
|
|
|
fixtures.request_update_check, content_type='text/xml') |
215
|
|
|
|
216
|
|
|
self.assertEqual(response.status_code, 200) |
217
|
|
|
|
218
|
|
|
self.assertXmlDocument(response.content) |
219
|
|
|
self.assertXmlEquivalentOutputs(response.content, |
220
|
|
|
fixtures.response_update_check_positive) |
221
|
|
|
|
222
|
|
|
|
223
|
|
|
|
224
|
|
|
@temporary_media_root(MEDIA_URL='http://cache.pack.google.com/edgedl/chrome/install/782.112/') |
225
|
|
|
@patch('omaha.models.version_upload_to', lambda o, f: f) |
226
|
|
|
def test_userid_counting(self): |
227
|
|
|
now = datetime.utcnow() |
228
|
|
|
userid = '{D0BBD725-742D-44ae-8D46-0231E881D58E}' |
229
|
|
|
user_id = get_id(userid) |
230
|
|
|
appid1 = '{430FD4D0-B729-4F61-AA34-91526481799D}' |
231
|
|
|
appid2 = '{D0AB2EBC-931B-4013-9FEB-C9C4C2225C8C}' |
232
|
|
|
install_date = datetime(year=2014, month=1, day=1, hour=15, minute=41, second=48) |
233
|
|
|
update_date = install_date + timedelta(days=31) |
234
|
|
|
|
235
|
|
|
request_events = DayEvents('request', install_date.year, install_date.month, install_date.day) |
236
|
|
|
app1_install_events = DayEvents('new_install:%s' % appid1, install_date.year, install_date.month, install_date.day) |
237
|
|
|
app2_install_events = DayEvents('new_install:%s' % appid2, install_date.year, install_date.month, install_date.day) |
238
|
|
|
app1_update_events = DayEvents('request:%s' % appid1, update_date.year, update_date.month, update_date.day) |
239
|
|
|
app2_update_events = DayEvents('request:%s' % appid2, update_date.year, update_date.month, update_date.day) |
240
|
|
|
|
241
|
|
|
self.assertEqual(len(request_events), 0) |
242
|
|
|
self.assertEqual(len(app1_install_events), 0) |
243
|
|
|
self.assertEqual(len(app2_install_events), 0) |
244
|
|
|
|
245
|
|
|
app = ApplicationFactory.create(id='{D0AB2EBC-931B-4013-9FEB-C9C4C2225C8C}', name='chrome') |
246
|
|
|
platform = PlatformFactory.create(name='win') |
247
|
|
|
channel = ChannelFactory.create(name='stable') |
248
|
|
|
obj = VersionFactory.create( |
249
|
|
|
app=app, |
250
|
|
|
platform=platform, |
251
|
|
|
channel=channel, |
252
|
|
|
version='13.0.782.112', |
253
|
|
|
file=SimpleUploadedFile('./chrome_installer.exe', b'_' * 23963192)) |
254
|
|
|
obj.file_hash = 'VXriGUVI0TNqfLlU02vBel4Q3Zo=' |
255
|
|
|
obj.save() |
256
|
|
|
|
257
|
|
|
Action.objects.create( |
258
|
|
|
version=obj, |
259
|
|
|
arguments='--do-not-launch-chrome', |
260
|
|
|
event=EVENT_DICT_CHOICES['install'], |
261
|
|
|
run='chrome_installer.exe' |
262
|
|
|
) |
263
|
|
|
|
264
|
|
|
Action.objects.create( |
265
|
|
|
version=obj, |
266
|
|
|
event=EVENT_DICT_CHOICES['postinstall'], |
267
|
|
|
other=dict( |
268
|
|
|
version='13.0.782.112', |
269
|
|
|
onsuccess='exitsilentlyonlaunchcmd', |
270
|
|
|
) |
271
|
|
|
) |
272
|
|
|
|
273
|
|
|
with freeze_time(install_date): # 56508 sec |
274
|
|
|
self.client.post(reverse('update'), |
275
|
|
|
fixtures.request_update_check, content_type='text/xml') |
276
|
|
|
self.client.post(reverse('update'), |
277
|
|
|
fixtures.request_event_install_success, content_type='text/xml') |
278
|
|
|
|
279
|
|
|
self.assertEqual(len(request_events), 1) |
280
|
|
|
self.assertEqual(len(app1_install_events), 0) |
281
|
|
|
self.assertEqual(len(app2_install_events), 1) |
282
|
|
|
self.assertEqual(len(app1_update_events), 0) |
283
|
|
|
self.assertEqual(len(app2_update_events), 0) |
284
|
|
|
self.assertTrue(user_id in request_events) |
285
|
|
|
self.assertFalse(user_id in app1_install_events) |
286
|
|
|
self.assertTrue(user_id in app2_install_events) |
287
|
|
|
|
288
|
|
|
with freeze_time(update_date): |
289
|
|
|
self.client.post(reverse('update'), |
290
|
|
|
fixtures.request_update_check, content_type='text/xml') |
291
|
|
|
|
292
|
|
|
self.assertEqual(len(app1_update_events), 0) |
293
|
|
|
self.assertEqual(len(app2_update_events), 1) |
294
|
|
|
self.assertFalse(user_id in app1_update_events) |
295
|
|
|
self.assertTrue(user_id in app2_update_events) |
296
|
|
|
|
297
|
|
|
@freeze_time('2014-01-01 15:45:54') # 56754 sec |
298
|
|
|
def test_event(self): |
299
|
|
|
response = self.client.post(reverse('update'), |
300
|
|
|
fixtures.request_event, |
301
|
|
|
REMOTE_ADDR="8.8.8.8", |
302
|
|
|
content_type='text/xml') |
303
|
|
|
|
304
|
|
|
self.assertEqual(response.status_code, 200) |
305
|
|
|
|
306
|
|
|
self.assertXmlDocument(response.content) |
307
|
|
|
self.assertXmlEquivalentOutputs(response.content, |
308
|
|
|
fixtures.response_event) |
309
|
|
|
|
310
|
|
|
request = Request.objects.get() |
311
|
|
|
self.assertEqual(request.ip, '8.8.8.8') |
312
|
|
|
|
313
|
|
|
@freeze_time('2014-01-01 15:45:54') # 56754 sec |
314
|
|
|
@temporary_media_root(MEDIA_URL='http://cache.pack.google.com/edgedl/chrome/install/782.112/') |
315
|
|
|
@patch('omaha.models.version_upload_to', lambda o, f: f) |
316
|
|
|
def test_data(self): |
317
|
|
|
app = ApplicationFactory.create(id='{430FD4D0-B729-4F61-AA34-91526481799D}', name='chrome') |
318
|
|
|
platform = PlatformFactory.create(name='win') |
319
|
|
|
channel = ChannelFactory.create(name='stable') |
320
|
|
|
obj = VersionFactory.create( |
321
|
|
|
app=app, |
322
|
|
|
platform=platform, |
323
|
|
|
channel=channel, |
324
|
|
|
version='13.0.782.112', |
325
|
|
|
file=SimpleUploadedFile('./chrome_installer.exe', b'_' * 23963192), |
326
|
|
|
file_size=23963192) |
327
|
|
|
obj.file_hash = 'VXriGUVI0TNqfLlU02vBel4Q3Zo=' |
328
|
|
|
obj.save() |
329
|
|
|
|
330
|
|
|
Data.objects.create( |
331
|
|
|
app=app, |
332
|
|
|
name=NAME_DATA_DICT_CHOICES['install'], |
333
|
|
|
index='verboselogging', |
334
|
|
|
value='app-specific values here') |
335
|
|
|
|
336
|
|
|
Data.objects.create( |
337
|
|
|
app=app, |
338
|
|
|
name=NAME_DATA_DICT_CHOICES['untrusted']) |
339
|
|
|
|
340
|
|
|
response = self.client.post(reverse('update'), |
341
|
|
|
fixtures.request_data, content_type='text/xml') |
342
|
|
|
|
343
|
|
|
self.assertEqual(response.status_code, 200) |
344
|
|
|
|
345
|
|
|
self.assertXmlDocument(response.content) |
346
|
|
|
self.assertXmlEquivalentOutputs(response.content, |
347
|
|
|
fixtures.response_data) |
348
|
|
|
|
349
|
|
|
def test_bad_request(self): |
350
|
|
|
response = self.client.post(reverse('update')) |
351
|
|
|
|
352
|
|
|
msg = b"""<?xml version="1.0" encoding="utf-8"?> |
353
|
|
|
<data> |
354
|
|
|
<message> |
355
|
|
|
Bad Request |
356
|
|
|
</message> |
357
|
|
|
</data>""" |
358
|
|
|
self.assertEqual(response.status_code, 400) |
359
|
|
|
self.assertEqual(response.content, msg) |
360
|
|
|
|