Sean O'Connor | 14b6a0a | 2010-03-21 06:23:48 | [diff] [blame] | 1 | #!/usr/bin/python |
Gilad Arnold | abb352e | 2012-09-23 08:24:27 | [diff] [blame] | 2 | # |
| 3 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
Sean O'Connor | 14b6a0a | 2010-03-21 06:23:48 | [diff] [blame] | 6 | |
Gilad Arnold | abb352e | 2012-09-23 08:24:27 | [diff] [blame] | 7 | from xml.dom import minidom |
Sean O'Connor | 14b6a0a | 2010-03-21 06:23:48 | [diff] [blame] | 8 | import sys |
| 9 | import threading |
Gilad Arnold | abb352e | 2012-09-23 08:24:27 | [diff] [blame] | 10 | import urllib2 |
| 11 | |
Sean O'Connor | 14b6a0a | 2010-03-21 06:23:48 | [diff] [blame] | 12 | |
| 13 | UPDATE_BLOB="""\ |
| 14 | <?xml version="1.0" encoding="UTF-8"?> |
| 15 | <o:gupdate |
| 16 | xmlns:o="http://www.google.com/update2/request" |
| 17 | version="MementoSoftwareUpdate-0.1.0.0" |
| 18 | protocol="2.0" |
| 19 | machineid="{1B0A13AC-7004-638C-3CA6-EC082E8F5DE9}" |
| 20 | ismachine="0" |
| 21 | userid="{bogus}"> |
| 22 | <o:os version="Memento" |
| 23 | platform="memento" |
| 24 | sp="ForcedUpdate_i686"> |
| 25 | </o:os> |
| 26 | <o:app appid="{87efface-864d-49a5-9bb3-4b050a7c227a}" |
| 27 | version="ForcedUpdate" |
| 28 | lang="en-us" |
| 29 | brand="GGLG" |
| 30 | track="developer-build" |
| 31 | board="x86-generic"> |
| 32 | <o:ping active="0"></o:ping> |
| 33 | <o:updatecheck></o:updatecheck> |
| 34 | </o:app> |
| 35 | </o:gupdate> |
| 36 | """ |
| 37 | |
| 38 | UPDATE_URL = 'http://localhost:8080/update/' |
| 39 | |
| 40 | def do_ping(): |
| 41 | update_ping = urllib2.Request(UPDATE_URL, update_blob) |
| 42 | update_ping.add_header('Content-Type', 'text/xml') |
| 43 | print urllib2.urlopen(update_ping).read() |
| 44 | #TODO assert noupdate |
| 45 | |
| 46 | def do_version_ping(): |
| 47 | url = UPDATE_URL + 'LATEST' |
| 48 | update_ping = urllib2.Request(url, UPDATE_BLOB) |
| 49 | update_ping.add_header('Content-Type', 'text/xml') |
| 50 | response = urllib2.urlopen(update_ping).read() |
| 51 | assert _verify_response(response), 'couldn\'t fetch update file' |
| 52 | print 'Successfully pinged updateserver.' |
| 53 | |
| 54 | def do_badversion_ping(): |
| 55 | url = UPDATE_URL + 'BADVERSION' |
| 56 | update_ping = urllib2.Request(url, UPDATE_BLOB) |
| 57 | update_ping.add_header('Content-Type', 'text/xml') |
| 58 | response = urllib2.urlopen(update_ping).read() |
| 59 | assert ('noupdate' in response) |
| 60 | print 'requesting bogus version returns noupdate.' |
| 61 | |
| 62 | def _verify_download(url, content_length): |
| 63 | # Eventually, verify something about the update. For now, |
| 64 | # sanity-check its size. |
| 65 | f = urllib2.urlopen(url) |
| 66 | data = f.read(1024 * 1024) |
| 67 | length = len(data) |
| 68 | while data: |
| 69 | data = f.read(1024 * 1024) |
| 70 | length += len(data) |
| 71 | assert content_length == length |
| 72 | print 'Got a valid download.' |
| 73 | return True |
| 74 | |
| 75 | def _verify_response(data): |
| 76 | update_dom = minidom.parseString(data) |
| 77 | print data |
| 78 | root = update_dom.firstChild |
| 79 | update_info = root.getElementsByTagName('updatecheck')[0] |
| 80 | update_url = update_info.getAttribute('codebase') |
| 81 | hash = update_info.getAttribute('hash') |
| 82 | head_request = urllib2.Request(update_url) |
| 83 | head_request.get_method = lambda: 'HEAD' |
| 84 | try: |
| 85 | fd = urllib2.urlopen(head_request) |
| 86 | except urllib2.HTTPError, e: |
Sean O'Connor | 1f7fd36 | 2010-04-07 23:34:52 | [diff] [blame] | 87 | # HTTP error |
Sean O'Connor | 14b6a0a | 2010-03-21 06:23:48 | [diff] [blame] | 88 | print 'FAILED: unable to retrieve %s\n\t%s' % (update_url, e) |
Sean O'Connor | 1f7fd36 | 2010-04-07 23:34:52 | [diff] [blame] | 89 | length = 0 |
| 90 | else: |
| 91 | # HTTP succeeded |
| 92 | length = int(fd.headers.getheaders('Content-Length')[0]) |
| 93 | finally: |
| 94 | fd.close() |
| 95 | return (length > 0) |
Sean O'Connor | 14b6a0a | 2010-03-21 06:23:48 | [diff] [blame] | 96 | |
| 97 | def test(num_clients): |
| 98 | # Fake some concurrent requests for each autoupdate operation. |
| 99 | for clients in range(num_clients): |
| 100 | for op in (do_version_ping, do_badversion_ping): |
| 101 | t = threading.Thread(target=op) |
| 102 | t.start() |
| 103 | |
| 104 | if __name__ == '__main__': |
| 105 | if len(sys.argv) > 1: |
| 106 | num_clients = int(sys.argv[1]) |
| 107 | else: |
| 108 | num_clients = 1 |
| 109 | test(num_clients) |