1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is the Application Update Service.
16 * The Initial Developer of the Original Code is
17 * Robert Strong <robert.bugzilla@gmail.com>.
19 * Portions created by the Initial Developer are Copyright (C) 2008
20 * the Mozilla Foundation <http://www.mozilla.org/>. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK *****
39 /* General MAR File Download Tests */
41 const DIR_DATA = "data"
42 const URL_PREFIX = "http://localhost:4444/" + DIR_DATA + "/";
44 const PREF_APP_UPDATE_URL_OVERRIDE = "app.update.url.override";
56 start_httpserver(DIR_DATA);
57 do_timeout(0, "run_test_pt1()");
65 // Helper function for testing mar downloads that have the correct size
66 // specified in the update xml.
67 function run_test_helper_pt1(aUpdateXML, aMsg, aResult, aNextRunFunc) {
71 gCheckFunc = check_test_helper_pt1_1;
72 gNextRunFunc = aNextRunFunc;
73 gExpectedResult = aResult;
74 var url = URL_PREFIX + aUpdateXML;
75 dump("Testing: " + aMsg + " - " + url + "\n");
76 gPrefs.setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, url);
77 gUpdateChecker.checkForUpdates(updateCheckListener, true);
80 function check_test_helper_pt1_1() {
81 do_check_eq(gUpdateCount, 1);
82 gCheckFunc = check_test_helper_pt1_2;
83 var bestUpdate = gAUS.selectUpdate(gUpdates, gUpdateCount);
84 var state = gAUS.downloadUpdate(bestUpdate, false);
85 if (state == "null" || state == "failed")
86 do_throw("nsIApplicationUpdateService:downloadUpdate returned " + state);
87 gAUS.addDownloadListener(downloadListener);
90 function check_test_helper_pt1_2() {
91 do_check_eq(gStatus, gExpectedResult);
92 gAUS.removeDownloadListener(downloadListener);
96 // mar download with a valid MD5 hash
97 function run_test_pt1() {
98 run_test_helper_pt1("aus-0030_general-1.xml",
99 "mar download with a valid MD5 hash",
100 AUS_Cr.NS_OK, run_test_pt2);
103 // mar download with an invalid MD5 hash
104 function run_test_pt2() {
105 run_test_helper_pt1("aus-0030_general-2.xml",
106 "mar download with an invalid MD5 hash",
107 AUS_Cr.NS_ERROR_UNEXPECTED, run_test_pt3);
110 // mar download with a valid SHA1 hash
111 function run_test_pt3() {
112 run_test_helper_pt1("aus-0030_general-3.xml",
113 "mar download with a valid SHA1 hash",
114 AUS_Cr.NS_OK, run_test_pt4);
117 // mar download with an invalid SHA1 hash
118 function run_test_pt4() {
119 run_test_helper_pt1("aus-0030_general-4.xml",
120 "mar download with an invalid SHA1 hash",
121 AUS_Cr.NS_ERROR_UNEXPECTED, run_test_pt5);
124 // mar download with a valid SHA256 hash
125 function run_test_pt5() {
126 run_test_helper_pt1("aus-0030_general-5.xml",
127 "mar download with a valid SHA256 hash",
128 AUS_Cr.NS_OK, run_test_pt6);
131 // mar download with an invalid SHA256 hash
132 function run_test_pt6() {
133 run_test_helper_pt1("aus-0030_general-6.xml",
134 "mar download with an invalid SHA256 hash",
135 AUS_Cr.NS_ERROR_UNEXPECTED, run_test_pt7);
138 // mar download with a valid SHA384 hash
139 function run_test_pt7() {
140 run_test_helper_pt1("aus-0030_general-7.xml",
141 "mar download with a valid SHA384 hash",
142 AUS_Cr.NS_OK, run_test_pt8);
145 // mar download with an invalid SHA384 hash
146 function run_test_pt8() {
147 run_test_helper_pt1("aus-0030_general-8.xml",
148 "mar download with an invalid SHA384 hash",
149 AUS_Cr.NS_ERROR_UNEXPECTED, run_test_pt9);
152 // mar download with a valid SHA512 hash
153 function run_test_pt9() {
154 run_test_helper_pt1("aus-0030_general-9.xml",
155 "mar download with a valid SHA512 hash",
156 AUS_Cr.NS_OK, run_test_pt10);
159 // mar download with an invalid SHA384 hash
160 function run_test_pt10() {
161 run_test_helper_pt1("aus-0030_general-10.xml",
162 "mar download with an invalid SHA512 hash",
163 AUS_Cr.NS_ERROR_UNEXPECTED, run_test_pt11);
166 // mar download with the mar not found
167 function run_test_pt11() {
168 run_test_helper_pt1("aus-0030_general-11.xml",
169 "mar download with the mar not found",
170 AUS_Cr.NS_ERROR_UNEXPECTED, end_test);
173 // Update check listener
174 const updateCheckListener = {
175 onProgress: function(request, position, totalSize) {
178 onCheckComplete: function(request, updates, updateCount) {
179 gUpdateCount = updateCount;
181 dump("onCheckComplete url = " + request.channel.originalURI.spec + "\n\n");
182 // Use a timeout to allow the XHR to complete
183 do_timeout(0, "gCheckFunc()");
186 onError: function(request, update) {
187 dump("onError url = " + request.channel.originalURI.spec + "\n\n");
188 // Use a timeout to allow the XHR to complete
189 do_timeout(0, "gCheckFunc()");
192 QueryInterface: function(aIID) {
193 if (!aIID.equals(AUS_Ci.nsIUpdateCheckListener) &&
194 !aIID.equals(AUS_Ci.nsISupports))
195 throw AUS_Cr.NS_ERROR_NO_INTERFACE;
200 /* Update download listener - nsIRequestObserver */
201 const downloadListener = {
202 onStartRequest: function(request, context) {
205 onProgress: function(request, context, progress, maxProgress) {
208 onStatus: function(request, context, status, statusText) {
211 onStopRequest: function(request, context, status) {
213 // Use a timeout to allow the request to complete
214 do_timeout(0, "gCheckFunc()");
217 QueryInterface: function(iid) {
218 if (!iid.equals(AUS_Ci.nsIRequestObserver) &&
219 !iid.equals(AUS_Ci.nsIProgressEventSink) &&
220 !iid.equals(AUS_Ci.nsISupports))
221 throw AUS_Cr.NS_ERROR_NO_INTERFACE;