Bug 507339 - Test more files in the complete / partial mar files and check file permissions. r=blassey, r=me

--HG--
rename : toolkit/mozapps/update/test/unit/head_update.js => toolkit/mozapps/update/test/unit/head_update.js.in
rename : toolkit/mozapps/update/test/unit/test_0040_general.js.in => toolkit/mozapps/update/test/unit/test_0040_general.js
This commit is contained in:
Robert Strong 2009-08-07 13:19:58 -07:00
parent a5472e8be1
commit 289f1720d4
8 changed files with 1583 additions and 269 deletions

View File

@ -99,8 +99,8 @@ else
bug473417dir = test_bug473417
endif
libs:: unit/test_0040_general.js.in
$(PYTHON) $(MOZILLA_DIR)/config/Preprocessor.py -Fsubstitution $(DEFINES) $^ > $(TESTROOT)/unit/test_0040_general.js
libs:: unit/head_update.js.in
$(PYTHON) $(MOZILLA_DIR)/config/Preprocessor.py -Fsubstitution $(DEFINES) $(ACDEFINES) $^ > $(TESTROOT)/unit/head_update.js
check::
$(RM) -rf $(DEPTH)/_tests/updater/ && $(NSINSTALL) -D $(DEPTH)/_tests/updater/$(bug473417dir)/

View File

@ -0,0 +1,897 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Application Update Service.
*
* The Initial Developer of the Original Code is
* Robert Strong <robert.bugzilla@gmail.com>.
*
* Portions created by the Initial Developer are Copyright (C) 2008
* the Mozilla Foundation <http://www.mozilla.org/>. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK *****
*/
// const Cc, Ci, and Cr are defined in netwerk/test/httpserver/httpd.js so we
// need to define unique ones.
const AUS_Cc = Components.classes;
const AUS_Ci = Components.interfaces;
const AUS_Cr = Components.results;
const NS_APP_PROFILE_DIR_STARTUP = "ProfDS";
const NS_APP_USER_PROFILE_50_DIR = "ProfD";
const NS_GRE_DIR = "GreD";
const NS_XPCOM_CURRENT_PROCESS_DIR = "XCurProcD"
const XRE_UPDATE_ROOT_DIR = "UpdRootD";
const PREF_APP_UPDATE_URL_OVERRIDE = "app.update.url.override";
const PREF_APP_UPDATE_SHOW_INSTALLED_UI = "app.update.showInstalledUI";
const URI_UPDATES_PROPERTIES = "chrome://mozapps/locale/update/updates.properties";
const gUpdateBundle = AUS_Cc["@mozilla.org/intl/stringbundle;1"].
getService(AUS_Ci.nsIStringBundleService).
createBundle(URI_UPDATES_PROPERTIES);
const STATE_NONE = "null";
const STATE_DOWNLOADING = "downloading";
const STATE_PENDING = "pending";
const STATE_APPLYING = "applying";
const STATE_SUCCEEDED = "succeeded";
const STATE_DOWNLOAD_FAILED = "download-failed";
const STATE_FAILED = "failed";
const FILE_UPDATES_DB = "updates.xml";
const FILE_UPDATE_ACTIVE = "active-update.xml";
const MODE_RDONLY = 0x01;
const MODE_WRONLY = 0x02;
const MODE_CREATE = 0x08;
const MODE_APPEND = 0x10;
const MODE_TRUNCATE = 0x20;
const PERMS_FILE = 0644;
const PERMS_DIRECTORY = 0755;
const URL_HOST = "http://localhost:4444/"
const DIR_DATA = "data"
var gDirSvc = AUS_Cc["@mozilla.org/file/directory_service;1"].
getService(AUS_Ci.nsIProperties);
var gAUS;
var gUpdateChecker;
var gUpdateManager;
var gTestserver;
var gXHR;
var gXHRCallback;
var gCheckFunc;
var gResponseBody;
var gResponseStatusCode = 200;
var gRequestURL;
var gUpdateCount;
var gUpdates;
var gStatusCode;
var gStatusText;
function getPrefBranch() {
return AUS_Cc["@mozilla.org/preferences;1"].getService(AUS_Ci.nsIPrefBranch);
}
/**
* Nulls out the most commonly used global vars used by tests as appropriate.
* This was moved here from the tail file due to check-interactive executing
* the tail file prior to _execute_test(); (bug 384339). It hasn't been moved
* back since it is easier to comment out the call to cleanUp when needed.
*/
function cleanUp() {
// Always call app update's observe method passing xpcom-shutdown to test that
// the shutdown of app update runs without throwing or leaking. The observer
// method is used directly instead of calling notifyObservers so components
// outside of the scope of this test don't assert and thereby cause app update
// tests to fail.
if (gAUS)
gAUS.observe(null, "xpcom-shutdown", "");
removeUpdateDirsAndFiles();
gDirSvc.unregisterProvider(gDirProvider);
if (gXHR) {
gXHRCallback = null;
gXHR.responseXML = null;
// null out the event handlers to prevent a mFreeCount leak of 1
gXHR.onerror = null;
gXHR.onload = null;
gXHR.onprogress = null;
gXHR = null;
}
gUpdateManager = null;
gUpdateChecker = null;
gAUS = null;
gTestserver = null;
}
/**
* Sets the most commonly used preferences used by tests
*/
function setDefaultPrefs() {
var pb = getPrefBranch();
// Don't display UI for a successful installation. Some apps may not set this
// pref to false like Firefox does.
pb.setBoolPref(PREF_APP_UPDATE_SHOW_INSTALLED_UI, false);
// Enable Update logging
pb.setBoolPref("app.update.log.all", true);
// Lessens the noise in the logs when using Update Service logging
pb.setBoolPref("app.update.enabled", false);
pb.setBoolPref("extensions.blocklist.enabled", false);
pb.setBoolPref("extensions.update.enabled", false);
pb.setBoolPref("browser.search.update", false);
pb.setBoolPref("browser.microsummary.updateGenerators", false);
}
/**
* Initializes nsIApplicationUpdateService and the most commonly used global
* vars used by tests.
*/
function startAUS() {
createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1.0", "2.0");
setDefaultPrefs();
gAUS = AUS_Cc["@mozilla.org/updates/update-service;1"].
getService(AUS_Ci.nsIApplicationUpdateService).
QueryInterface(AUS_Ci.nsIObserver);
var os = AUS_Cc["@mozilla.org/observer-service;1"].
getService(AUS_Ci.nsIObserverService);
os.notifyObservers(null, "profile-after-change", null);
os.notifyObservers(null, "final-ui-startup", null);
}
/* Initializes nsIUpdateChecker */
function startUpdateChecker() {
gUpdateChecker = AUS_Cc["@mozilla.org/updates/update-checker;1"].
createInstance(AUS_Ci.nsIUpdateChecker);
}
/* Initializes nsIUpdateManager */
function startUpdateManager() {
gUpdateManager = AUS_Cc["@mozilla.org/updates/update-manager;1"].
getService(AUS_Ci.nsIUpdateManager);
}
/**
* Constructs a string representing a remote update xml file.
* @param aUpdates
* The string representing the update elements.
* @returns The string representing a remote update xml file.
*/
function getRemoteUpdatesXMLString(aUpdates) {
return "<?xml version=\"1.0\"?>\n" +
"<updates>\n" +
aUpdates +
"</updates>\n";
}
/**
* Constructs a string representing an update element for a remote update xml
* file.
* See getUpdateString
* @returns The string representing an update element for an update xml file.
*/
function getRemoteUpdateString(aPatches, aName, aType, aVersion,
aPlatformVersion, aExtensionVersion, aBuildID,
aLicenseURL, aDetailsURL) {
return getUpdateString(aName, aType, aVersion, aPlatformVersion,
aExtensionVersion, aBuildID, aLicenseURL,
aDetailsURL) + ">\n" +
aPatches +
" </update>\n";
}
/**
* Constructs a string representing a patch element for a remote update xml
* file
* See getPatchString
* @returns The string representing a patch element for a remote update xml
* file.
*/
function getRemotePatchString(aType, aURL, aHashFunction, aHashValue, aSize) {
return getPatchString(aType, aURL, aHashFunction, aHashValue, aSize) +
"/>\n";
}
/**
* Constructs a string representing a local update xml file.
* @param aUpdates
* The string representing the update elements.
* @returns The string representing a local update xml file.
*/
function getLocalUpdatesXMLString(aUpdates) {
if (!aUpdates || aUpdates == "")
return "<updates xmlns=\"http://www.mozilla.org/2005/app-update\"/>"
return ("<updates xmlns=\"http://www.mozilla.org/2005/app-update\">" +
aUpdates +
"</updates>").replace(/>\s+\n*</g,'><');
}
/**
* Constructs a string representing an update element for a local update xml
* file.
* See getUpdateString
* @param aServiceURL
* The update's xml url.
* If null this will default to 'http://dummyservice/'.
* @param aIsCompleteUpdate
* The string 'true' if this update was a complete update or the string
* 'false' if this update was a partial update.
* If null this will default to 'true'.
* @param aChannel
* The update channel name.
* If null this will default to 'bogus_channel'.
* @param aForegroundDownload
* The string 'true' if this update was manually downloaded or the
* string 'false' if this update was automatically downloaded.
* If null this will default to 'true'.
* @returns The string representing an update element for an update xml file.
*/
function getLocalUpdateString(aPatches, aName, aType, aVersion, aPlatformVersion,
aExtensionVersion, aBuildID, aLicenseURL,
aDetailsURL, aServiceURL, aInstallDate, aStatusText,
aIsCompleteUpdate, aChannel, aForegroundDownload) {
var serviceURL = aServiceURL ? aServiceURL : "http://dummyservice/";
var installDate = aInstallDate ? aInstallDate : "1238441400314";
var statusText = aStatusText ? aStatusText : "Install Pending";
var isCompleteUpdate =
typeof(aIsCompleteUpdate) == "string" ? aIsCompleteUpdate : "true";
var channel = aChannel ? aChannel : "bogus_channel";
var foregroundDownload =
typeof(aForegroundDownload) == "string" ? aForegroundDownload : "true";
return getUpdateString(aName, aType, aVersion, aPlatformVersion,
aExtensionVersion, aBuildID, aLicenseURL,
aDetailsURL) + " " +
"serviceURL=\"" + serviceURL + "\" " +
"installDate=\"" + installDate + "\" " +
"statusText=\"" + statusText + "\" " +
"isCompleteUpdate=\"" + isCompleteUpdate + "\" " +
"channel=\"" + channel + "\" " +
"foregroundDownload=\"" + foregroundDownload + "\">" +
aPatches +
" </update>";
}
/**
* Constructs a string representing a patch element for a local update xml file.
* See getPatchString
* @param aSelected
* Whether this patch is selected represented or not. The string 'true'
* denotes selected and the string 'false' denotes not selected.
* If null this will default to the string 'true'.
* @param aState
* The patch's state.
* If null this will default to STATE_SUCCEEDED (e.g. 'succeeded').
* @returns The string representing a patch element for a local update xml file.
*/
function getLocalPatchString(aType, aURL, aHashFunction, aHashValue, aSize,
aSelected, aState) {
var selected = typeof(aSelected) == "string" ? aSelected : "true";
var state = aState ? aState : STATE_SUCCEEDED;
return getPatchString(aType, aURL, aHashFunction, aHashValue, aSize) + " " +
"selected=\"" + selected + "\" " +
"state=\"" + state + "\"/>\n";
}
/**
* Constructs a string representing an update element for a remote update xml
* file.
* @param aPatches
* The string representing the update's patches.
* @param aName
* The update's name.
* If null this will default to 'XPCShell App Update Test'.
* @param aType
* The update's type which should be major or minor.
* If null this will default to 'major'.
* @param aVersion
* The update's app version.
* If null this will default to '4.0'.
* @param aPlatformVersion
* The update's platform version.
* If null this will default to '4.0'.
* @param aExtensionVersion
* The update's extension version.
* If null this will default to '4.0'.
* @param aBuildID
* The update's build id.
* If null this will default to '20080811053724'.
* @param aLicenseURL
* The update's license url.
* If null this will default to 'http://dummylicense/'.
* @param aDetailsURL
* The update's details url.
* If null this will default to 'http://dummydetails/'.
* @returns The string representing an update element for an update xml file.
*/
function getUpdateString(aName, aType, aVersion, aPlatformVersion,
aExtensionVersion, aBuildID, aLicenseURL, aDetailsURL) {
var name = aName ? aName : "XPCShell App Update Test";
var type = aType ? aType : "major";
var version = aVersion ? aVersion : "4.0";
var platformVersion = aPlatformVersion ? aPlatformVersion : "4.0";
var extensionVersion = aExtensionVersion ? aExtensionVersion : "4.0";
var buildID = aBuildID ? aBuildID : "20080811053724";
var licenseURL = aLicenseURL ? aLicenseURL : "http://dummylicense/";
var detailsURL = aDetailsURL ? aDetailsURL : "http://dummydetails/";
return " <update name=\"" + name + "\" " +
"type=\"" + type + "\" " +
"version=\"" + version + "\" " +
"platformVersion=\"" + platformVersion + "\" " +
"extensionVersion=\"" + extensionVersion + "\" " +
"buildID=\"" + buildID + "\" " +
"licenseURL=\"" + licenseURL + "\" " +
"detailsURL=\"" + detailsURL + "\"";
}
/**
* Constructs a string representing a patch element for an update xml file.
* @param aType
* The patch's type which should be complete or partial.
* If null this will default to 'complete'.
* @param aURL
* The patch's url to the mar file.
* If null this will default to 'http://localhost:4444/data/empty.mar'.
* @param aHashFunction
* The patch's hash function used to verify the mar file.
* If null this will default to 'MD5'.
* @param aHashValue
* The patch's hash value used to verify the mar file.
* If null this will default to '6232cd43a1c77e30191c53a329a3f99d'
* which is the MD5 hash value for the empty.mar.
* @param aSize
* The patch's file size for the mar file.
* If null this will default to '775' which is the file size for the
* empty.mar.
* @returns The string representing a patch element for an update xml file.
*/
function getPatchString(aType, aURL, aHashFunction, aHashValue, aSize) {
var type = aType ? aType : "complete";
var url = aURL ? aURL : URL_HOST + DIR_DATA + "/empty.mar";
var hashFunction = aHashFunction ? aHashFunction : "MD5";
var hashValue = aHashValue ? aHashValue : "6232cd43a1c77e30191c53a329a3f99d";
var size = aSize ? aSize : "775";
return " <patch type=\"" + type + "\" " +
"URL=\"" + url + "\" " +
"hashFunction=\"" + hashFunction + "\" " +
"hashValue=\"" + hashValue + "\" " +
"size=\"" + size + "\"";
}
/**
* Writes the updates specified to either the active-update.xml or the
* updates.xml.
* @param updates
* The updates represented as a string to write to the XML file.
* @param isActiveUpdate
* If true this will write to the active-update.xml otherwise it will
* write to the updates.xml file.
*/
function writeUpdatesToXMLFile(aContent, aIsActiveUpdate) {
var file = getCurrentProcessDir();
file.append(aIsActiveUpdate ? FILE_UPDATE_ACTIVE : FILE_UPDATES_DB);
writeFile(file, aContent);
}
/**
* Writes the current update operation/state to a file in the patch
* directory, indicating to the patching system that operations need
* to be performed.
* @param aStatus
* The status value to write.
*/
function writeStatusFile(aStatus) {
var file = getCurrentProcessDir();
file.append("updates");
file.append("0");
file.append("update.status");
aStatus += "\n";
writeFile(file, aStatus);
}
/**
* Writes text to a file. This will replace existing text if the file exists
* and create the file if it doesn't exist.
* @param aFile
* The file to write to. Will be created if it doesn't exist.
* @param aText
* The text to write to the file. If there is existing text it will be
* replaced.
*/
function writeFile(aFile, aText) {
var fos = AUS_Cc["@mozilla.org/network/file-output-stream;1"].
createInstance(AUS_Ci.nsIFileOutputStream);
if (!aFile.exists())
aFile.create(AUS_Ci.nsILocalFile.NORMAL_FILE_TYPE, PERMS_FILE);
fos.init(aFile, MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE, PERMS_FILE, 0);
fos.write(aText, aText.length);
fos.close();
}
/**
* Reads text from a file and returns the string.
* @param aFile
* The file to read from.
* @returns The string of text read from the file.
*/
function readFile(aFile) {
var fis = AUS_Cc["@mozilla.org/network/file-input-stream;1"].
createInstance(AUS_Ci.nsIFileInputStream);
if (!aFile.exists())
return null;
fis.init(aFile, MODE_RDONLY, PERMS_FILE, 0);
var sis = AUS_Cc["@mozilla.org/scriptableinputstream;1"].
createInstance(AUS_Ci.nsIScriptableInputStream);
sis.init(fis);
var text = sis.read(sis.available());
sis.close();
return text;
}
/**
* Reads the binary contents of a file and returns is as a string.
* @param aFile
* The file to read from.
* @returns The contents of the file as a string.
*/
function readFileBytes(aFile) {
var fis = AUS_Cc["@mozilla.org/network/file-input-stream;1"].
createInstance(AUS_Ci.nsIFileInputStream);
fis.init(aFile, -1, -1, false);
var bis = AUS_Cc["@mozilla.org/binaryinputstream;1"].
createInstance(AUS_Ci.nsIBinaryInputStream);
bis.setInputStream(fis);
var data = [];
var count = fis.available();
while (count > 0) {
var bytes = bis.readByteArray(Math.min(65535, count));
data.push(String.fromCharCode.apply(null, bytes));
count -= bytes.length;
if (bytes.length == 0)
do_throw("Nothing read from input stream!");
}
data.join('');
fis.close();
return data.toString();
}
/* Custom path handler for the http server */
function pathHandler(metadata, response) {
response.setHeader("Content-Type", "text/xml", false);
response.setStatusLine(metadata.httpVersion, gResponseStatusCode, "OK");
response.bodyOutputStream.write(gResponseBody, gResponseBody.length);
}
/* Returns human readable status text from the updates.properties bundle */
function getStatusText(aErrCode) {
return getString("check_error-" + aErrCode);
}
/* Returns a string from the updates.properties bundle */
function getString(aName) {
try {
return gUpdateBundle.GetStringFromName(aName);
}
catch (e) {
}
return null;
}
/**
* Launches the updater binary to apply a mar file using the current working
* directory for the location to apply the mar.
* @param aUpdatesDir
* The directory to copy the update mar, binary, etc.
* @param aUpdater
* The updater binary binary to copy to aUpdatesDir.
* @returns The exit value returned from the updater binary.
*/
function runUpdate(aUpdatesDir, aUpdater) {
// Copy the updater binary to the update directory so the updater.ini is not
// in the same directory as it is. This prevents the PostUpdate executable
// which is defined in the updater.ini from launching and the updater ui from
// displaying.
aUpdater.copyTo(aUpdatesDir, aUpdater.leafName);
var updateBin = aUpdatesDir.clone();
updateBin.append(aUpdater.leafName);
if (updateBin.leafName == "updater.app") {
updateBin.append("Contents");
updateBin.append("MacOS");
updateBin.append("updater");
if (!updateBin.exists())
do_throw("Unable to find the updater executable!");
}
var updatesDirPath = aUpdatesDir.path;
if (/ /.test(updatesDirPath))
updatesDirPath = '"' + updatesDirPath + '"';
var cwdPath = do_get_file("/", true).path;
if (/ /.test(cwdPath))
cwdPath = '"' + cwdPath + '"';
var process = AUS_Cc["@mozilla.org/process/util;1"].
createInstance(AUS_Ci.nsIProcess);
process.init(updateBin);
var args = [updatesDirPath, 0, cwdPath];
process.run(true, args, args.length);
return process.exitValue;
}
/**
* Sets up the bare bones XMLHttpRequest implementation below.
*
* @param callback
* The callback function that will call the nsIDomEventListener's
* handleEvent method.
*
* Example of the callback function
*
* function callHandleEvent() {
* gXHR.status = gExpectedStatus;
* var e = { target: gXHR };
* gXHR.onload.handleEvent(e);
* }
*/
function overrideXHR(callback) {
gXHRCallback = callback;
gXHR = new xhr();
var registrar = Components.manager.QueryInterface(AUS_Ci.nsIComponentRegistrar);
registrar.registerFactory(gXHR.classID, gXHR.classDescription,
gXHR.contractID, gXHR);
}
/**
* Bare bones XMLHttpRequest implementation for testing onprogress, onerror,
* and onload nsIDomEventListener handleEvent.
*/
function xhr() {
}
xhr.prototype = {
overrideMimeType: function(mimetype) { },
setRequestHeader: function(header, value) { },
status: null,
channel: { set notificationCallbacks(val) { } },
_url: null,
_method: null,
open: function (method, url) {
gXHR.channel.originalURI = AUS_Cc["@mozilla.org/network/io-service;1"].
getService(AUS_Ci.nsIIOService).
newURI(url, null, null);
gXHR._method = method; gXHR._url = url;
},
responseXML: null,
responseText: null,
send: function(body) {
do_timeout(0, "gXHRCallback()"); // Use a timeout so the XHR completes
},
_onprogress: null,
set onprogress(val) { gXHR._onprogress = val; },
get onprogress() { return gXHR._onprogress; },
_onerror: null,
set onerror(val) { gXHR._onerror = val; },
get onerror() { return gXHR._onerror; },
_onload: null,
set onload(val) { gXHR._onload = val; },
get onload() { return gXHR._onload; },
flags: AUS_Ci.nsIClassInfo.SINGLETON,
implementationLanguage: AUS_Ci.nsIProgrammingLanguage.JAVASCRIPT,
getHelperForLanguage: function(language) null,
getInterfaces: function(count) {
var interfaces = [AUS_Ci.nsIXMLHttpRequest, AUS_Ci.nsIJSXMLHttpRequest,
AUS_Ci.nsIXMLHttpRequestEventTarget];
count.value = interfaces.length;
return interfaces;
},
classDescription: "XMLHttpRequest",
contractID: "@mozilla.org/xmlextras/xmlhttprequest;1",
classID: Components.ID("{c9b37f43-4278-4304-a5e0-600991ab08cb}"),
createInstance: function (outer, aIID) {
if (outer == null)
return gXHR.QueryInterface(aIID);
throw AUS_Cr.NS_ERROR_NO_AGGREGATION;
},
QueryInterface: function(aIID) {
if (aIID.equals(AUS_Ci.nsIXMLHttpRequest) ||
aIID.equals(AUS_Ci.nsIJSXMLHttpRequest) ||
aIID.equals(AUS_Ci.nsIXMLHttpRequestEventTarget) ||
aIID.equals(AUS_Ci.nsIClassInfo) ||
aIID.equals(AUS_Ci.nsISupports))
return gXHR;
throw AUS_Cr.NS_ERROR_NO_INTERFACE;
}
};
/* Update check listener */
const updateCheckListener = {
onProgress: function(request, position, totalSize) {
},
onCheckComplete: function(request, updates, updateCount) {
gRequestURL = request.channel.originalURI.spec;
gUpdateCount = updateCount;
gUpdates = updates;
dump("onError: url = " + gRequestURL + ", " +
"request.status = " + request.status + ", " +
"update.statusText = " + request.statusText + ", " +
"updateCount = " + updateCount + "\n");
// Use a timeout to allow the XHR to complete
do_timeout(0, "gCheckFunc()");
},
onError: function(request, update) {
gRequestURL = request.channel.originalURI.spec;
gStatusCode = request.status;
gStatusText = update.statusText;
dump("onError: url = " + gRequestURL + ", " +
"request.status = " + gStatusCode + ", " +
"update.statusText = " + gStatusText + "\n");
// Use a timeout to allow the XHR to complete
do_timeout(0, "gCheckFunc()");
},
QueryInterface: function(aIID) {
if (!aIID.equals(AUS_Ci.nsIUpdateCheckListener) &&
!aIID.equals(AUS_Ci.nsISupports))
throw AUS_Cr.NS_ERROR_NO_INTERFACE;
return this;
}
};
/**
* Removes the updates directory, updates.xml file, and active-update.xml file
* if they exist. This prevents some tests from failing due to files being left
* behind when the tests are interrupted.
*/
function removeUpdateDirsAndFiles() {
var appDir = getCurrentProcessDir();
var file = appDir.clone();
file.append("active-update.xml");
try {
if (file.exists())
file.remove(false);
}
catch (e) {
dump("Unable to remove file\npath: " + file.path +
"\nException: " + e + "\n");
}
file = appDir.clone();
file.append("updates.xml");
try {
if (file.exists())
file.remove(false);
}
catch (e) {
dump("Unable to remove file\npath: " + file.path +
"\nException: " + e + "\n");
}
file = appDir.clone();
file.append("updates");
file.append("last-update.log");
try {
if (file.exists())
file.remove(false);
}
catch (e) {
dump("Unable to remove file\npath: " + file.path +
"\nException: " + e + "\n");
}
// This fails sporadically on Mac OS X so wrap it in a try catch
var updatesDir = appDir.clone();
updatesDir.append("updates");
try {
removeDirRecursive(updatesDir);
}
catch (e) {
dump("Unable to remove directory\npath: " + updatesDir.path +
"\nException: " + e + "\n");
}
}
/**
* Deletes a directory and its children. First it tries nsIFile::Remove(true).
* If that fails it will fall back to recursing, setting the appropriate
* permissions, and deleting the current entry.
* @param dir
* A nsIFile for the directory to be deleted
*/
function removeDirRecursive(aDir) {
if (!aDir.exists())
return;
try {
aDir.remove(true);
return;
}
catch (e) {
}
var dirEntries = aDir.directoryEntries;
while (dirEntries.hasMoreElements()) {
var entry = dirEntries.getNext().QueryInterface(AUS_Ci.nsIFile);
if (entry.isDirectory()) {
removeDirRecursive(entry);
}
else {
entry.permissions = PERMS_FILE;
entry.remove(false);
}
}
aDir.permissions = PERMS_DIRECTORY;
aDir.remove(true);
}
/**
* Helper for starting the http server used by the tests
* @param aRelativeDirName
* The directory name to register relative to
* toolkit/mozapps/update/test/unit/
*/
function start_httpserver(aRelativeDirName) {
var dir = do_get_file(aRelativeDirName);
if (!dir.exists())
do_throw("The directory used by nsHttpServer does not exist! path: " +
dir.path + "\n");
if (!dir.isDirectory())
do_throw("A file instead of a directory was specified for nsHttpServer " +
"registerDirectory! path: " + dir.path + "\n");
do_load_httpd_js();
gTestserver = new nsHttpServer();
gTestserver.registerDirectory("/data/", dir);
gTestserver.start(4444);
}
/* Helper for stopping the http server used by the tests */
function stop_httpserver(callback) {
do_check_true(!!callback);
gTestserver.stop(callback);
}
/**
* Creates an nsIXULAppInfo
* @param id
* The ID of the test application
* @param name
* A name for the test application
* @param version
* The version of the application
* @param platformVersion
* The gecko version of the application
*/
function createAppInfo(id, name, version, platformVersion) {
const XULAPPINFO_CONTRACTID = "@mozilla.org/xre/app-info;1";
const XULAPPINFO_CID = Components.ID("{c763b610-9d49-455a-bbd2-ede71682a1ac}");
var XULAppInfo = {
vendor: "Mozilla",
name: name,
ID: id,
version: version,
appBuildID: "2007010101",
platformVersion: platformVersion,
platformBuildID: "2007010101",
inSafeMode: false,
logConsoleErrors: true,
OS: "XPCShell",
XPCOMABI: "noarch-spidermonkey",
QueryInterface: function QueryInterface(iid) {
if (iid.equals(AUS_Ci.nsIXULAppInfo) ||
iid.equals(AUS_Ci.nsIXULRuntime) ||
iid.equals(AUS_Ci.nsISupports))
return this;
throw AUS_Cr.NS_ERROR_NO_INTERFACE;
}
};
var XULAppInfoFactory = {
createInstance: function (outer, iid) {
if (outer == null)
return XULAppInfo.QueryInterface(iid);
throw AUS_Cr.NS_ERROR_NO_AGGREGATION;
}
};
var registrar = Components.manager.QueryInterface(AUS_Ci.nsIComponentRegistrar);
registrar.registerFactory(XULAPPINFO_CID, "XULAppInfo",
XULAPPINFO_CONTRACTID, XULAppInfoFactory);
}
/**
* Returns the Gecko Runtime Engine directory. This is used to locate the the
* updater binary (Windows and Linux) or updater package (Mac OS X). For
* XULRunner applications this is different than the currently running process
* directory.
*/
function getGREDir() {
return gDirSvc.get(NS_GRE_DIR, AUS_Ci.nsIFile);
}
/**
* Returns the directory for the currently running process. This is used to
* clean up after the tests and to locate the active-update.xml and updates.xml
* files.
*/
function getCurrentProcessDir() {
return gDirSvc.get(NS_XPCOM_CURRENT_PROCESS_DIR, AUS_Ci.nsIFile);
}
// On Vista XRE_UPDATE_ROOT_DIR can be a directory other than the one in the
// application directory. This will reroute it back to the one in the
// application directory.
var gDirProvider = {
getFile: function(prop, persistent) {
persistent.value = true;
if (prop == XRE_UPDATE_ROOT_DIR)
return getCurrentProcessDir();
return null;
},
QueryInterface: function(iid) {
if (iid.equals(AUS_Ci.nsIDirectoryServiceProvider) ||
iid.equals(AUS_Ci.nsISupports))
return this;
throw AUS_Cr.NS_ERROR_NO_INTERFACE;
}
};
gDirSvc.QueryInterface(AUS_Ci.nsIDirectoryService).registerProvider(gDirProvider);
const INSTALL_LOCALE = "@AB_CD@";
// These are placed at the end so they don't mess up line numbering
#ifdef XP_WIN
const IS_WIN = true;
#else
const IS_WIN = false;
#endif
#ifdef XP_OS2
const IS_OS2 = true;
#else
const IS_OS2 = false;
#endif
#ifdef XP_MACOSX
const IS_MACOSX = true;
#else
const IS_MACOSX = false;
#endif

View File

@ -0,0 +1,316 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Application Update Service.
*
* The Initial Developer of the Original Code is
* Robert Strong <robert.bugzilla@gmail.com>.
*
* Portions created by the Initial Developer are Copyright (C) 2008
* the Mozilla Foundation <http://www.mozilla.org/>. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK *****
*/
/* General URL Construction Tests */
const URL_PREFIX = URL_HOST + DIR_DATA + "/";
const PREF_APP_UPDATE_CHANNEL = "app.update.channel";
const PREF_PARTNER_BRANCH = "app.partner.";
const PREF_APP_DISTRIBUTION = "distribution.id";
const PREF_APP_DISTRIBUTION_VERSION = "distribution.version";
var gAppInfo;
function run_test() {
do_test_pending();
removeUpdateDirsAndFiles();
// The mock XMLHttpRequest is MUCH faster
overrideXHR(callHandleEvent);
startAUS();
startUpdateChecker();
gAppInfo = AUS_Cc["@mozilla.org/xre/app-info;1"].
getService(AUS_Ci.nsIXULAppInfo).
QueryInterface(AUS_Ci.nsIXULRuntime);
do_timeout(0, "run_test_pt1()");
}
function end_test() {
do_test_finished();
cleanUp();
}
// Callback function used by the custom XMLHttpRequest implementation to
// call the nsIDOMEventListener's handleEvent method for onload.
function callHandleEvent() {
var e = { target: gXHR };
gXHR.onload.handleEvent(e);
}
// Helper function for parsing the result from the contructed url
function getResult(url) {
return url.substr(URL_PREFIX.length).split("/")[0];
}
// url constructed with %PRODUCT%
function run_test_pt1() {
gCheckFunc = check_test_pt1;
var url = URL_PREFIX + "%PRODUCT%/";
dump("Testing: url constructed with %PRODUCT% - " + url + "\n");
getPrefBranch().setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, url);
gUpdateChecker.checkForUpdates(updateCheckListener, true);
}
function check_test_pt1() {
do_check_eq(getResult(gRequestURL), gAppInfo.name);
run_test_pt2();
}
// url constructed with %VERSION%
function run_test_pt2() {
gCheckFunc = check_test_pt2;
var url = URL_PREFIX + "%VERSION%/";
dump("Testing: url constructed with %VERSION% - " + url + "\n");
getPrefBranch().setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, url);
gUpdateChecker.checkForUpdates(updateCheckListener, true);
}
function check_test_pt2() {
do_check_eq(getResult(gRequestURL), gAppInfo.version);
run_test_pt3();
}
// url constructed with %BUILD_ID%
function run_test_pt3() {
gCheckFunc = check_test_pt3;
var url = URL_PREFIX + "%BUILD_ID%/";
dump("Testing: url constructed with %BUILD_ID% - " + url + "\n");
getPrefBranch().setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, url);
gUpdateChecker.checkForUpdates(updateCheckListener, true);
}
function check_test_pt3() {
do_check_eq(getResult(gRequestURL), gAppInfo.appBuildID);
run_test_pt4();
}
// url constructed with %BUILD_TARGET%
// XXX TODO - it might be nice if we tested the actual ABI
function run_test_pt4() {
gCheckFunc = check_test_pt4;
var url = URL_PREFIX + "%BUILD_TARGET%/";
dump("Testing: url constructed with %BUILD_TARGET% - " + url + "\n");
getPrefBranch().setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, url);
gUpdateChecker.checkForUpdates(updateCheckListener, true);
}
function check_test_pt4() {
try {
abi = gAppInfo.XPCOMABI;
}
catch (e) {
do_throw("nsIXULAppInfo:XPCOMABI not defined\n");
}
if (IS_MACOSX) {
// Mac universal build should report a different ABI than either macppc
// or mactel. This is necessary since nsUpdateService.js will set the ABI to
// Universal-gcc3 for Mac universal builds.
var macutils = AUS_Cc["@mozilla.org/xpcom/mac-utils;1"].
getService(AUS_Ci.nsIMacUtils);
if (macutils.isUniversalBinary)
abi = "Universal-gcc3";
}
do_check_eq(getResult(gRequestURL), gAppInfo.OS + "_" + abi);
run_test_pt5();
}
// url constructed with %LOCALE%
// Bug 488936 added the update.locale file that stores the update locale
function run_test_pt5() {
gCheckFunc = check_test_pt5;
var url = URL_PREFIX + "%LOCALE%/";
dump("Testing: url constructed with %LOCALE% - " + url + "\n");
getPrefBranch().setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, url);
try {
gUpdateChecker.checkForUpdates(updateCheckListener, true);
}
catch (e) {
dump("***\n*** The following error is most likely due to a missing " +
"update.locale file\n***\n");
do_throw(e);
}
}
function check_test_pt5() {
do_check_eq(getResult(gRequestURL), INSTALL_LOCALE);
run_test_pt6();
}
// url constructed with %CHANNEL%
function run_test_pt6() {
gCheckFunc = check_test_pt6;
var url = URL_PREFIX + "%CHANNEL%/";
dump("Testing: url constructed with %CHANNEL% - " + url + "\n");
var pb = getPrefBranch();
pb.setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, url);
var defaults = pb.QueryInterface(AUS_Ci.nsIPrefService).getDefaultBranch(null);
defaults.setCharPref(PREF_APP_UPDATE_CHANNEL, "bogus_channel");
gUpdateChecker.checkForUpdates(updateCheckListener, true);
}
function check_test_pt6() {
do_check_eq(getResult(gRequestURL), "bogus_channel");
run_test_pt7();
}
// url constructed with %CHANNEL% with distribution partners
function run_test_pt7() {
gCheckFunc = check_test_pt7;
var url = URL_PREFIX + "%CHANNEL%/";
dump("Testing: url constructed with %CHANNEL% - " + url + "\n");
var pb = getPrefBranch();
pb.setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, url);
var defaults = pb.QueryInterface(AUS_Ci.nsIPrefService).getDefaultBranch(null);
defaults.setCharPref(PREF_PARTNER_BRANCH + "bogus_partner1", "bogus_partner1");
defaults.setCharPref(PREF_PARTNER_BRANCH + "bogus_partner2", "bogus_partner2");
gUpdateChecker.checkForUpdates(updateCheckListener, true);
}
function check_test_pt7() {
do_check_eq(getResult(gRequestURL), "bogus_channel-cck-bogus_partner1-bogus_partner2");
run_test_pt8();
}
// url constructed with %PLATFORM_VERSION%
function run_test_pt8() {
gCheckFunc = check_test_pt8;
var url = URL_PREFIX + "%PLATFORM_VERSION%/";
dump("Testing: url constructed with %PLATFORM_VERSION% - " + url + "\n");
getPrefBranch().setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, url);
gUpdateChecker.checkForUpdates(updateCheckListener, true);
}
function check_test_pt8() {
do_check_eq(getResult(gRequestURL), gAppInfo.platformVersion);
run_test_pt9();
}
// url constructed with %OS_VERSION%
function run_test_pt9() {
gCheckFunc = check_test_pt9;
var url = URL_PREFIX + "%OS_VERSION%/";
dump("Testing: url constructed with %OS_VERSION% - " + url + "\n");
getPrefBranch().setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, url);
gUpdateChecker.checkForUpdates(updateCheckListener, true);
}
function check_test_pt9() {
var osVersion;
var sysInfo = AUS_Cc["@mozilla.org/system-info;1"].
getService(AUS_Ci.nsIPropertyBag2);
osVersion = sysInfo.getProperty("name") + " " + sysInfo.getProperty("version");
if (osVersion) {
try {
osVersion += " (" + sysInfo.getProperty("secondaryLibrary") + ")";
}
catch (e) {
// Not all platforms have a secondary widget library, so an error is
// nothing to worry about.
}
osVersion = encodeURIComponent(osVersion);
}
do_check_eq(getResult(gRequestURL), osVersion);
run_test_pt10();
}
// url constructed with %DISTRIBUTION%
function run_test_pt10() {
gCheckFunc = check_test_pt10;
var url = URL_PREFIX + "%DISTRIBUTION%/";
dump("Testing: url constructed with %DISTRIBUTION% - " + url + "\n");
var pb = getPrefBranch();
pb.setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, url);
var defaults = pb.QueryInterface(AUS_Ci.nsIPrefService).getDefaultBranch(null);
defaults.setCharPref(PREF_APP_DISTRIBUTION, "bogus_distro");
gUpdateChecker.checkForUpdates(updateCheckListener, true);
}
function check_test_pt10() {
do_check_eq(getResult(gRequestURL), "bogus_distro");
run_test_pt11();
}
// url constructed with %DISTRIBUTION_VERSION%
function run_test_pt11() {
gCheckFunc = check_test_pt11;
var url = URL_PREFIX + "%DISTRIBUTION_VERSION%/";
dump("Testing: url constructed with %DISTRIBUTION_VERSION% - " + url + "\n");
var pb = getPrefBranch();
pb.setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, url);
var defaults = pb.QueryInterface(AUS_Ci.nsIPrefService).getDefaultBranch(null);
defaults.setCharPref(PREF_APP_DISTRIBUTION_VERSION, "bogus_distro_version");
gUpdateChecker.checkForUpdates(updateCheckListener, true);
}
function check_test_pt11() {
do_check_eq(getResult(gRequestURL), "bogus_distro_version");
run_test_pt12();
}
// url constructed that doesn't have a parameter - bug 454357
function run_test_pt12() {
gCheckFunc = check_test_pt12;
var url = URL_PREFIX;
dump("Testing: url constructed that doesn't have a parameter - " + url + "\n");
getPrefBranch().setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, url);
gUpdateChecker.checkForUpdates(updateCheckListener, true);
}
function check_test_pt12() {
do_check_eq(getResult(gRequestURL), "?force=1");
run_test_pt13();
}
// url constructed that has a parameter - bug 454357
function run_test_pt13() {
gCheckFunc = check_test_pt13;
var url = URL_PREFIX + "?bogus=param";
dump("Testing: url constructed that has a parameter - " + url + "\n");
getPrefBranch().setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, url);
gUpdateChecker.checkForUpdates(updateCheckListener, true);
}
function check_test_pt13() {
do_check_eq(getResult(gRequestURL), "?bogus=param&force=1");
end_test();
}

View File

@ -38,15 +38,63 @@
/* General Complete MAR File Patch Apply Test */
var gTestFiles = [
{
fileName : "1_exe1.exe",
destinationDir : "mar_test/1/",
originalContents : null,
compareContents : null,
originalFile : "data/aus-0111_general_ref_image.png",
compareFile : "data/aus-0110_general_ref_image.png",
originalPerms : 0777,
comparePerms : 0755
}, {
fileName : "1_1_image1.png",
destinationDir : "mar_test/1/1_1/",
originalContents : null,
compareContents : null,
originalFile : null,
compareFile : "data/aus-0110_general_ref_image.png",
originalPerms : 0776,
comparePerms : 0644
}, {
fileName : "1_1_text1",
destinationDir : "mar_test/1/1_1/",
originalContents : "ToBeReplacedWithToBeModified\n",
compareContents : "ToBeModified\n",
originalFile : null,
compareFile : null,
originalPerms : 0775,
comparePerms : 0644
}, {
fileName : "1_1_text2",
destinationDir : "mar_test/1/1_1/",
originalContents : "ToBeReplacedWithToBeDeleted\n",
compareContents : "ToBeDeleted\n",
originalFile : null,
compareFile : null,
originalPerms : 0677,
comparePerms : 0644
}, {
fileName : "2_1_text1",
destinationDir : "mar_test/2/2_1/",
originalContents : "ToBeReplacedWithToBeDeleted\n",
compareContents : "ToBeDeleted\n",
originalFile : null,
compareFile : null,
originalPerms : 0767,
comparePerms : 0644
}];
function run_test() {
var testFile;
// The directory the updates will be applied to is the current working
// directory and not dist/bin.
var testDir = do_get_file("mar_test", true);
// The mar files were created with all files in a subdirectory named
// mar_test... clear it out of the way if it exists and then create it.
try {
if (testDir.exists())
testDir.remove(true);
removeDirRecursive(testDir);
}
catch (e) {
dump("Unable to remove directory\npath: " + testDir.path +
@ -54,13 +102,36 @@ function run_test() {
}
dump("Testing: successful removal of the directory used to apply the mar file\n");
do_check_false(testDir.exists());
testDir = do_get_file("mar_test/1/1_1/", true);
testDir.create(AUS_Ci.nsIFile.DIRECTORY_TYPE, PERMS_DIRECTORY);
// Create an empty test file to test the complete mar's ability to replace an
// existing file.
var testFile = do_get_file("mar_test/1/1_1/1_1_text1", true);
testFile.create(AUS_Ci.nsIFile.NORMAL_FILE_TYPE, PERMS_FILE);
// Create the files to test the complete mar's ability to replace files.
for (var i = 0; i < gTestFiles.length; i++) {
var f = gTestFiles[i];
if (f.originalFile || f.originalContents) {
testDir = do_get_file(f.destinationDir, true);
if (!testDir.exists())
testDir.create(AUS_Ci.nsIFile.DIRECTORY_TYPE, PERMS_DIRECTORY);
if (f.originalFile) {
testFile = do_get_file(f.originalFile);
testFile.copyTo(testDir, f.fileName);
testFile = do_get_file(f.destinationDir + f.fileName);
}
else {
testFile = do_get_file(f.destinationDir + f.fileName, true);
writeFile(testFile, f.originalContents);
}
// Skip these tests on Windows (includes WinCE) and OS/2 since their
// implementaions of chmod doesn't really set permissions.
if (!IS_WIN && !IS_OS2 && f.originalPerms) {
testFile.permissions = f.originalPerms;
// Store the actual permissions on the file for reference later after
// setting the permissions.
if (!f.comparePerms)
f.comparePerms = testFile.permissions;
}
}
}
var binDir = getGREDir();
@ -80,101 +151,68 @@ function run_test() {
}
// Use a directory outside of dist/bin to lessen the garbage in dist/bin
var updatesSubDir = do_get_file("0110_complete_mar", true);
var updatesDir = do_get_file("0110_complete_mar", true);
try {
// Mac OS X intermittently fails when removing the dir where the updater
// binary was launched.
if (updatesSubDir.exists())
updatesSubDir.remove(true);
removeDirRecursive(updatesDir);
}
catch (e) {
dump("Unable to remove directory\npath: " + updatesSubDir.path +
dump("Unable to remove directory\npath: " + updatesDir.path +
"\nException: " + e + "\n");
}
var mar = do_get_file("data/aus-0110_general.mar");
mar.copyTo(updatesSubDir, "update.mar");
mar.copyTo(updatesDir, "update.mar");
// apply the complete mar and check the innards of the files
var exitValue = runUpdate(updatesSubDir, updater);
var exitValue = runUpdate(updatesDir, updater);
dump("Testing: updater binary process exitValue for success when applying " +
"a complete mar\n");
do_check_eq(exitValue, 0);
dump("Testing: update.status should be set to STATE_SUCCEEDED\n");
testFile = updatesSubDir.clone();
testFile = updatesDir.clone();
testFile.append("update.status");
do_check_eq(readFile(testFile).split("\n")[0], STATE_SUCCEEDED);
dump("Testing: contents of files added by a complete mar\n");
do_check_eq(getFileBytes(do_get_file("mar_test/1/1_1/1_1_text1", true)),
"ToBeModified\n");
do_check_eq(getFileBytes(do_get_file("mar_test/1/1_1/1_1_text2", true)),
"ToBeDeleted\n");
for (i = 0; i < gTestFiles.length; i++) {
f = gTestFiles[i];
testFile = do_get_file(f.destinationDir + f.fileName, true);
dump("Testing: " + testFile.path + "\n");
if (f.compareFile || f.compareContents) {
do_check_true(testFile.exists());
var refImage = do_get_file("data/aus-0110_general_ref_image.png");
var srcImage = do_get_file("mar_test/1/1_1/1_1_image1.png", true);
do_check_eq(getFileBytes(srcImage), getFileBytes(refImage));
// Skip these tests on Windows (includes WinCE) and OS/2 since their
// implementaions of chmod doesn't really set permissions.
if (!IS_WIN && !IS_OS2 && f.comparePerms) {
// Check if the permssions as set in the complete mar file are correct.
if (f.originalPerms)
dump("original permissions: " + f.originalPerms.toString(8) + "\n");
dump("compare permissions : " + f.comparePerms.toString(8) + "\n");
dump("updated permissions : " + testFile.permissions.toString(8) + "\n");
do_check_eq(testFile.permissions & 0xfff, f.comparePerms & 0xfff);
}
do_check_eq(getFileBytes(do_get_file("mar_test/2/2_1/2_1_text1", true)),
"ToBeDeleted\n");
try {
// Mac OS X intermittently fails when removing the dir where the updater
// binary was launched.
if (updatesSubDir.exists())
updatesSubDir.remove(true);
}
catch (e) {
dump("Unable to remove directory\npath: " + updatesSubDir.path +
"\nException: " + e + "\n");
if (f.compareFile) {
do_check_eq(readFileBytes(testFile),
readFileBytes(do_get_file(f.compareFile)));
if (f.originalFile) {
// Verify that readFileBytes returned the entire contents by checking
// the contents against the original file.
do_check_neq(readFileBytes(testFile),
readFileBytes(do_get_file(f.originalFile)));
}
}
else {
do_check_eq(readFileBytes(testFile), f.compareContents);
}
}
else {
do_check_false(testFile.exists());
}
}
cleanUp();
}
// Launches the updater binary to apply a mar file
function runUpdate(aUpdatesSubDir, aUpdater) {
// Copy the updater binary to the update directory so the updater.ini is not
// in the same directory as it is. This prevents ui from displaying and the
// PostUpdate executable which is defined in the updater.ini from launching.
aUpdater.copyTo(aUpdatesSubDir, aUpdater.leafName);
var updateBin = aUpdatesSubDir.clone();
updateBin.append(aUpdater.leafName);
if (updateBin.leafName == "updater.app") {
updateBin.append("Contents");
updateBin.append("MacOS");
updateBin.append("updater");
if (!updateBin.exists())
do_throw("Unable to find the updater executable!");
}
var updatesSubDirPath = aUpdatesSubDir.path;
if (/ /.test(updatesSubDirPath))
updatesSubDirPath = '"' + updatesSubDirPath + '"';
var cwdPath = do_get_file("/", true).path;
if (/ /.test(cwdPath))
cwdPath = '"' + cwdPath + '"';
var process = AUS_Cc["@mozilla.org/process/util;1"].
createInstance(AUS_Ci.nsIProcess);
process.init(updateBin);
var args = [updatesSubDirPath, 0, cwdPath];
process.run(true, args, args.length);
return process.exitValue;
}
// Returns the binary contents of a file
function getFileBytes(aFile) {
var fis = AUS_Cc["@mozilla.org/network/file-input-stream;1"].
createInstance(AUS_Ci.nsIFileInputStream);
fis.init(aFile, -1, -1, false);
var bis = AUS_Cc["@mozilla.org/binaryinputstream;1"].
createInstance(AUS_Ci.nsIBinaryInputStream);
bis.setInputStream(fis);
var data = bis.readBytes(bis.available());
bis.close();
fis.close();
return data;
}

View File

@ -38,15 +38,81 @@
/* General Partial MAR File Patch Apply Test */
var gTestFiles = [
{
fileName : "1_exe1.exe",
destinationDir : "mar_test/1/",
originalContents : null,
compareContents : null,
originalFile : "data/aus-0110_general_ref_image.png",
compareFile : "data/aus-0111_general_ref_image.png",
originalPerms : 0755,
comparePerms : null
}, {
fileName : "1_1_image1.png",
destinationDir : "mar_test/1/1_1/",
originalContents : null,
compareContents : null,
originalFile : "data/aus-0110_general_ref_image.png",
compareFile : "data/aus-0111_general_ref_image.png",
originalPerms : 0644,
comparePerms : null
}, {
fileName : "1_1_text1",
destinationDir : "mar_test/1/1_1/",
originalContents : "ToBeModified\n",
compareContents : "Modified\n",
originalFile : null,
compareFile : null,
originalPerms : 0644,
comparePerms : null
}, {
fileName : "1_1_text2",
destinationDir : "mar_test/1/1_1/",
originalContents : "ToBeDeleted\n",
compareContents : null,
originalFile : null,
compareFile : null,
originalPerms : null,
comparePerms : null
}, {
fileName : "1_1_text3",
destinationDir : "mar_test/1/1_1/",
originalContents : null,
compareContents : "Added\n",
originalFile : null,
compareFile : null,
originalPerms : null,
comparePerms : 0644
}, {
fileName : "2_1_text1",
destinationDir : "mar_test/2/2_1/",
originalContents : "ToBeDeleted\n",
compareContents : null,
originalFile : null,
compareFile : null,
originalPerms : null,
comparePerms : null
}, {
fileName : "3_1_text1",
destinationDir : "mar_test/3/3_1/",
originalContents : null,
compareContents : "Added\n",
originalFile : null,
compareFile : null,
originalPerms : null,
comparePerms : 0644
}];
function run_test() {
var testFile;
// The directory the updates will be applied to is the current working
// directory and not dist/bin.
var testDir = do_get_file("mar_test", true);
// The mar files were created with all files in a subdirectory named
// mar_test... clear it out of the way if it exists and then create it.
try {
if (testDir.exists())
testDir.remove(true);
removeDirRecursive(testDir);
}
catch (e) {
dump("Unable to remove directory\npath: " + testDir.path +
@ -54,22 +120,37 @@ function run_test() {
}
dump("Testing: successful removal of the directory used to apply the mar file\n");
do_check_false(testDir.exists());
testDir = do_get_file("mar_test/1/1_1/", true);
testDir.create(AUS_Ci.nsIFile.DIRECTORY_TYPE, PERMS_DIRECTORY);
// Create the files to test the partial mar's ability to modify and delete
// files.
var testFile = do_get_file("mar_test/1/1_1/1_1_text1", true);
writeFile(testFile, "ToBeModified\n");
for (var i = 0; i < gTestFiles.length; i++) {
var f = gTestFiles[i];
if (f.originalFile || f.originalContents) {
testDir = do_get_file(f.destinationDir, true);
if (!testDir.exists())
testDir.create(AUS_Ci.nsIFile.DIRECTORY_TYPE, PERMS_DIRECTORY);
testFile = do_get_file("mar_test/1/1_1/1_1_text2", true);
writeFile(testFile, "ToBeDeleted\n");
if (f.originalFile) {
testFile = do_get_file(f.originalFile);
testFile.copyTo(testDir, f.fileName);
testFile = do_get_file(f.destinationDir + f.fileName);
}
else {
testFile = do_get_file(f.destinationDir + f.fileName, true);
writeFile(testFile, f.originalContents);
}
testFile = do_get_file("data/aus-0110_general_ref_image.png");
testFile.copyTo(testDir, "1_1_image1.png");
testFile = do_get_file("mar_test/2/2_1/2_1_text1", true);
writeFile(testFile, "ToBeDeleted\n");
// Skip these tests on Windows (includes WinCE) and OS/2 since their
// implementaions of chmod doesn't really set permissions.
if (!IS_WIN && !IS_OS2 && f.originalPerms) {
testFile.permissions = f.originalPerms;
// Store the actual permissions on the file for reference later after
// setting the permissions.
if (!f.comparePerms)
f.comparePerms = testFile.permissions;
}
}
}
var binDir = getGREDir();
@ -89,110 +170,74 @@ function run_test() {
}
// Use a directory outside of dist/bin to lessen the garbage in dist/bin
var updatesSubDir = do_get_file("0111_complete_mar", true);
var updatesDir = do_get_file("0111_complete_mar", true);
try {
// Mac OS X intermittently fails when removing the dir where the updater
// binary was launched.
if (updatesSubDir.exists())
updatesSubDir.remove(true);
removeDirRecursive(updatesDir);
}
catch (e) {
dump("Unable to remove directory\npath: " + updatesSubDir.path +
dump("Unable to remove directory\npath: " + updatesDir.path +
"\nException: " + e + "\n");
}
updatesDir.create(AUS_Ci.nsIFile.DIRECTORY_TYPE, PERMS_DIRECTORY);
var mar = do_get_file("data/aus-0111_general.mar");
mar.copyTo(updatesSubDir, "update.mar");
mar.copyTo(updatesDir, "update.mar");
// apply the partial mar and check the innards of the files
var exitValue = runUpdate(updatesSubDir, updater);
var exitValue = runUpdate(updatesDir, updater);
dump("Testing: updater binary process exitValue for success when applying " +
"a partial mar\n");
do_check_eq(exitValue, 0);
dump("Testing: update.status should be set to STATE_SUCCEEDED\n");
testFile = updatesSubDir.clone();
testFile = updatesDir.clone();
testFile.append("update.status");
do_check_eq(readFile(testFile).split("\n")[0], STATE_SUCCEEDED);
dump("Testing: removal of a file and contents of added / modified files by " +
"a partial mar\n");
do_check_eq(getFileBytes(do_get_file("mar_test/1/1_1/1_1_text1", true)),
"Modified\n");
do_check_false(do_get_file("mar_test/1/1_1/1_1_text2", true).exists());
do_check_eq(getFileBytes(do_get_file("mar_test/1/1_1/1_1_text3", true)),
"Added\n");
dump("Testing: removal of files and contents of added / modified files by " +
"a partial mar including retention of file permissions\n");
for (i = 0; i < gTestFiles.length; i++) {
f = gTestFiles[i];
testFile = do_get_file(f.destinationDir + f.fileName, true);
dump("Testing: " + testFile.path + "\n");
if (f.compareFile || f.compareContents) {
do_check_true(testFile.exists());
var refImage = do_get_file("data/aus-0111_general_ref_image.png");
var srcImage = do_get_file("mar_test/1/1_1/1_1_image1.png", true);
do_check_eq(getFileBytes(srcImage), getFileBytes(refImage));
// Skip these tests on Windows (includes WinCE) and OS/2 since their
// implementaions of chmod doesn't really set permissions.
if (!IS_WIN && !IS_OS2 && f.comparePerms) {
// Check the original permssions are retained on the file.
if (f.originalPerms)
dump("original permissions: " + f.originalPerms.toString(8) + "\n");
dump("compare permissions : " + f.comparePerms.toString(8) + "\n");
dump("updated permissions : " + testFile.permissions.toString(8) + "\n");
do_check_eq(testFile.permissions & 0xfff, f.comparePerms & 0xfff);
}
dump("Testing: removal of a file by a partial mar\n");
do_check_false(do_get_file("mar_test/2/2_1/2_1_text1", true).exists());
do_check_eq(getFileBytes(do_get_file("mar_test/3/3_1/3_1_text1", true)),
"Added\n");
if (f.compareFile) {
do_check_eq(readFileBytes(testFile),
readFileBytes(do_get_file(f.compareFile)));
if (f.originalFile) {
// Verify that readFileBytes returned the entire contents by checking
// the contents against the original file.
do_check_neq(readFileBytes(testFile),
readFileBytes(do_get_file(f.originalFile)));
}
}
else {
do_check_eq(readFileBytes(testFile), f.compareContents);
}
}
else {
do_check_false(testFile.exists());
}
}
dump("Testing: directory still exists after removal of the last file in " +
"the directory (bug 386760)\n");
do_check_true(do_get_file("mar_test/2/2_1/", true).exists());
try {
// Mac OS X intermittently fails when removing the dir where the updater
// binary was launched.
if (updatesSubDir.exists())
updatesSubDir.remove(true);
}
catch (e) {
dump("Unable to remove directory\npath: " + updatesSubDir.path +
"\nException: " + e + "\n");
}
cleanUp();
}
// Launches the updater binary to apply a mar file
function runUpdate(aUpdatesSubDir, aUpdater) {
// Copy the updater binary to the update directory so the updater.ini is not
// in the same directory as it is. This prevents ui from displaying and the
// PostUpdate executable which is defined in the updater.ini from launching.
aUpdater.copyTo(aUpdatesSubDir, aUpdater.leafName);
var updateBin = aUpdatesSubDir.clone();
updateBin.append(aUpdater.leafName);
if (updateBin.leafName == "updater.app") {
updateBin.append("Contents");
updateBin.append("MacOS");
updateBin.append("updater");
if (!updateBin.exists())
do_throw("Unable to find the updater executable!");
}
var updatesSubDirPath = aUpdatesSubDir.path;
if (/ /.test(updatesSubDirPath))
updatesSubDirPath = '"' + updatesSubDirPath + '"';
var cwdPath = do_get_file("/", true).path;
if (/ /.test(cwdPath))
cwdPath = '"' + cwdPath + '"';
var process = AUS_Cc["@mozilla.org/process/util;1"].
createInstance(AUS_Ci.nsIProcess);
process.init(updateBin);
var args = [updatesSubDirPath, 0, cwdPath];
process.run(true, args, args.length);
return process.exitValue;
}
// Returns the binary contents of a file
function getFileBytes(aFile) {
var fis = AUS_Cc["@mozilla.org/network/file-input-stream;1"].
createInstance(AUS_Ci.nsIFileInputStream);
fis.init(aFile, -1, -1, false);
var bis = AUS_Cc["@mozilla.org/binaryinputstream;1"].
createInstance(AUS_Ci.nsIBinaryInputStream);
bis.setInputStream(fis);
var data = bis.readBytes(bis.available());
bis.close();
fis.close();
return data;
}

View File

@ -38,15 +38,63 @@
/* General Partial MAR File Patch Apply Failure Test */
var gTestFiles = [
{
fileName : "1_exe1.exe",
destinationDir : "mar_test/1/",
originalContents : null,
compareContents : null,
originalFile : "data/aus-0111_general_ref_image.png",
compareFile : "data/aus-0111_general_ref_image.png",
originalPerms : 0755,
comparePerms : null
}, {
fileName : "1_1_image1.png",
destinationDir : "mar_test/1/1_1/",
originalContents : null,
compareContents : null,
originalFile : "data/aus-0110_general_ref_image.png",
compareFile : "data/aus-0110_general_ref_image.png",
originalPerms : 0644,
comparePerms : null
}, {
fileName : "1_1_text1",
destinationDir : "mar_test/1/1_1/",
originalContents : "ShouldNotBeDeleted\n",
compareContents : "ShouldNotBeDeleted\n",
originalFile : null,
compareFile : null,
originalPerms : 0644,
comparePerms : null
}, {
fileName : "1_1_text2",
destinationDir : "mar_test/1/1_1/",
originalContents : "ShouldNotBeDeleted\n",
compareContents : "ShouldNotBeDeleted\n",
originalFile : null,
compareFile : null,
originalPerms : 0644,
comparePerms : null
}, {
fileName : "2_1_text1",
destinationDir : "mar_test/2/2_1/",
originalContents : "ShouldNotBeDeleted\n",
compareContents : "ShouldNotBeDeleted\n",
originalFile : null,
compareFile : null,
originalPerms : 0644,
comparePerms : null
}];
function run_test() {
var testFile;
// The directory the updates will be applied to is the current working
// directory and not dist/bin.
var testDir = do_get_file("mar_test", true);
// The mar files were created with all files in a subdirectory named
// mar_test... clear it out of the way if it exists and then create it.
try {
if (testDir.exists())
testDir.remove(true);
removeDirRecursive(testDir);
}
catch (e) {
dump("Unable to remove directory\npath: " + testDir.path +
@ -54,22 +102,37 @@ function run_test() {
}
dump("Testing: successful removal of the directory used to apply the mar file\n");
do_check_false(testDir.exists());
testDir = do_get_file("mar_test/1/1_1/", true);
testDir.create(AUS_Ci.nsIFile.DIRECTORY_TYPE, PERMS_DIRECTORY);
// Create the files to test the partial mar's ability to modify and delete
// files.
var testFile = do_get_file("mar_test/1/1_1/1_1_text1", true);
writeFile(testFile, "ShouldNotBeModified\n");
// Create the files to test the partial mar's ability to rollback to the
// original files.
for (var i = 0; i < gTestFiles.length; i++) {
var f = gTestFiles[i];
if (f.originalFile || f.originalContents) {
testDir = do_get_file(f.destinationDir, true);
if (!testDir.exists())
testDir.create(AUS_Ci.nsIFile.DIRECTORY_TYPE, PERMS_DIRECTORY);
testFile = do_get_file("mar_test/1/1_1/1_1_text2", true);
writeFile(testFile, "ShouldNotBeDeleted\n");
if (f.originalFile) {
testFile = do_get_file(f.originalFile);
testFile.copyTo(testDir, f.fileName);
testFile = do_get_file(f.destinationDir + f.fileName);
}
else {
testFile = do_get_file(f.destinationDir + f.fileName, true);
writeFile(testFile, f.originalContents);
}
testFile = do_get_file("data/aus-0111_general_ref_image.png");
testFile.copyTo(testDir, "1_1_image1.png");
testFile = do_get_file("mar_test/2/2_1/2_1_text1", true);
writeFile(testFile, "ShouldNotBeDeleted\n");
// Skip these tests on Windows (includes WinCE) and OS/2 since their
// implementaions of chmod doesn't really set permissions.
if (!IS_WIN && !IS_OS2 && f.originalPerms) {
testFile.permissions = f.originalPerms;
// Store the actual permissions on the file for reference later after
// setting the permissions.
if (!f.comparePerms)
f.comparePerms = testFile.permissions;
}
}
}
var binDir = getGREDir();
@ -89,111 +152,66 @@ function run_test() {
}
// Use a directory outside of dist/bin to lessen the garbage in dist/bin
var updatesSubDir = do_get_file("0112_complete_mar", true);
var updatesDir = do_get_file("0112_complete_mar", true);
try {
// Mac OS X intermittently fails when removing the dir where the updater
// binary was launched.
if (updatesSubDir.exists())
updatesSubDir.remove(true);
removeDirRecursive(updatesDir);
}
catch (e) {
dump("Unable to remove directory\npath: " + updatesSubDir.path +
dump("Unable to remove directory\npath: " + updatesDir.path +
"\nException: " + e + "\n");
}
updatesDir.create(AUS_Ci.nsIFile.DIRECTORY_TYPE, PERMS_DIRECTORY);
var mar = do_get_file("data/aus-0111_general.mar");
mar.copyTo(updatesSubDir, "update.mar");
mar.copyTo(updatesDir, "update.mar");
// apply the partial mar and check the innards of the files
var exitValue = runUpdate(updatesSubDir, updater);
var exitValue = runUpdate(updatesDir, updater);
dump("Testing: updater binary process exitValue for success when applying " +
"a partial mar\n");
do_check_eq(exitValue, 0);
dump("Testing: update.status should be set to STATE_FAILED\n");
testFile = updatesSubDir.clone();
testFile = updatesDir.clone();
testFile.append("update.status");
// The update status format for a failure is failed: # where # is the error
// code for the failure.
do_check_eq(readFile(testFile).split(": ")[0], STATE_FAILED);
dump("Testing: files should not be modified or deleted when an update " +
"fails\n");
do_check_eq(getFileBytes(do_get_file("mar_test/1/1_1/1_1_text1", true)),
"ShouldNotBeModified\n");
do_check_true(do_get_file("mar_test/1/1_1/1_1_text2", true).exists());
do_check_eq(getFileBytes(do_get_file("mar_test/1/1_1/1_1_text2", true)),
"ShouldNotBeDeleted\n");
"fails including retention of file permissions\n");
for (i = 0; i < gTestFiles.length; i++) {
f = gTestFiles[i];
testFile = do_get_file(f.destinationDir + f.fileName, true);
dump("Testing: " + testFile.path + "\n");
if (f.compareFile || f.compareContents) {
do_check_true(testFile.exists());
var refImage = do_get_file("data/aus-0111_general_ref_image.png");
var srcImage = do_get_file("mar_test/1/1_1/1_1_image1.png", true);
do_check_eq(getFileBytes(srcImage), getFileBytes(refImage));
// Skip these tests on Windows (includes WinCE) and OS/2 since their
// implementaions of chmod doesn't really set permissions.
if (!IS_WIN && !IS_OS2 && f.comparePerms) {
// Check the original permssions are retained on the file.
if (f.originalPerms)
dump("original permissions: " + f.originalPerms.toString(8) + "\n");
dump("compare permissions : " + f.comparePerms.toString(8) + "\n");
dump("updated permissions : " + testFile.permissions.toString(8) + "\n");
do_check_eq(testFile.permissions & 0xfff, f.comparePerms & 0xfff);
}
dump("Testing: removal of a file by a partial mar\n");
do_check_true(do_get_file("mar_test/2/2_1/2_1_text1", true).exists());
do_check_eq(getFileBytes(do_get_file("mar_test/1/1_1/1_1_text2", true)),
"ShouldNotBeDeleted\n");
do_check_false(do_get_file("mar_test/3/3_1/3_1_text1", true).exists());
do_check_eq(getFileBytes(do_get_file("mar_test/1/1_1/1_1_text2", true)),
"ShouldNotBeDeleted\n");
try {
// Mac OS X intermittently fails when removing the dir where the updater
// binary was launched.
if (updatesSubDir.exists())
updatesSubDir.remove(true);
}
catch (e) {
dump("Unable to remove directory\npath: " + updatesSubDir.path +
"\nException: " + e + "\n");
if (f.compareFile) {
do_check_eq(readFileBytes(testFile),
readFileBytes(do_get_file(f.compareFile)));
}
else {
do_check_eq(readFileBytes(testFile), f.compareContents);
}
}
else {
do_check_false(testFile.exists());
}
}
cleanUp();
}
// Launches the updater binary to apply a mar file
function runUpdate(aUpdatesSubDir, aUpdater) {
// Copy the updater binary to the update directory so the updater.ini is not
// in the same directory as it is. This prevents ui from displaying and the
// PostUpdate executable which is defined in the updater.ini from launching.
aUpdater.copyTo(aUpdatesSubDir, aUpdater.leafName);
var updateBin = aUpdatesSubDir.clone();
updateBin.append(aUpdater.leafName);
if (updateBin.leafName == "updater.app") {
updateBin.append("Contents");
updateBin.append("MacOS");
updateBin.append("updater");
if (!updateBin.exists())
do_throw("Unable to find the updater executable!");
}
var updatesSubDirPath = aUpdatesSubDir.path;
if (/ /.test(updatesSubDirPath))
updatesSubDirPath = '"' + updatesSubDirPath + '"';
var cwdPath = do_get_file("/", true).path;
if (/ /.test(cwdPath))
cwdPath = '"' + cwdPath + '"';
var process = AUS_Cc["@mozilla.org/process/util;1"].
createInstance(AUS_Ci.nsIProcess);
process.init(updateBin);
var args = [updatesSubDirPath, 0, cwdPath];
process.run(true, args, args.length);
return process.exitValue;
}
// Returns the binary contents of a file
function getFileBytes(aFile) {
var fis = AUS_Cc["@mozilla.org/network/file-input-stream;1"].
createInstance(AUS_Ci.nsIFileInputStream);
fis.init(aFile, -1, -1, false);
var bis = AUS_Cc["@mozilla.org/binaryinputstream;1"].
createInstance(AUS_Ci.nsIBinaryInputStream);
bis.setInputStream(fis);
var data = bis.readBytes(bis.available());
bis.close();
fis.close();
return data;
}