michael@0: /* michael@0: * Unit tests for nsLoginManager.js michael@0: */ michael@0: michael@0: michael@0: function run_test() { michael@0: michael@0: try { michael@0: michael@0: var testnum = 0; michael@0: var testdesc = "Setup of nsLoginInfo test-users"; michael@0: var nsLoginInfo = new Components.Constructor( michael@0: "@mozilla.org/login-manager/loginInfo;1", michael@0: Components.interfaces.nsILoginInfo); michael@0: do_check_true(nsLoginInfo != null); michael@0: michael@0: var testuser1 = new nsLoginInfo; michael@0: michael@0: michael@0: /* ========== 1 ========== */ michael@0: var testnum = 1; michael@0: var testdesc = "Initial connection to login manager" michael@0: michael@0: var loginmgr = Cc["@mozilla.org/login-manager;1"]. michael@0: createInstance(Ci.nsILoginManager); michael@0: if (!loginmgr) michael@0: throw "Couldn't create loginmgr instance."; michael@0: michael@0: michael@0: /* ========== 2 ========== */ michael@0: testnum++; michael@0: testdesc = "Force lazy init, check to ensure there is no existing data."; michael@0: loginmgr.removeAllLogins(); michael@0: var hosts = loginmgr.getAllDisabledHosts(); michael@0: hosts.forEach(function(h) loginmgr.setLoginSavingEnabled(h, true)); michael@0: LoginTest.checkStorageData(loginmgr, [], []); michael@0: michael@0: michael@0: /* ========== 3 ========== */ michael@0: testnum++; michael@0: testdesc = "Try adding invalid logins (host / user / pass checks)"; michael@0: michael@0: function tryAddUser(module, aUser, aExpectedError) { michael@0: var err = null; michael@0: try { michael@0: module.addLogin(aUser); michael@0: } catch (e) { michael@0: err = e; michael@0: } michael@0: michael@0: LoginTest.checkExpectedError(aExpectedError, err); michael@0: } michael@0: michael@0: testuser1.init(null, null, "Some Realm", michael@0: "dummydude", "itsasecret", "put_user_here", "put_pw_here"); michael@0: // null hostname michael@0: tryAddUser(loginmgr, testuser1, /null or empty hostname/); michael@0: LoginTest.checkStorageData(loginmgr, [], []); michael@0: michael@0: testuser1.hostname = ""; michael@0: tryAddUser(loginmgr, testuser1, /null or empty hostname/); michael@0: LoginTest.checkStorageData(loginmgr, [], []); michael@0: michael@0: michael@0: testuser1.hostname = "http://dummyhost.mozilla.org"; michael@0: testuser1.username = null; michael@0: tryAddUser(loginmgr, testuser1, /null username/); michael@0: LoginTest.checkStorageData(loginmgr, [], []); michael@0: michael@0: testuser1.username = "dummydude"; michael@0: testuser1.password = null; michael@0: tryAddUser(loginmgr, testuser1, /null or empty password/); michael@0: LoginTest.checkStorageData(loginmgr, [], []); michael@0: michael@0: testuser1.password = ""; michael@0: tryAddUser(loginmgr, testuser1, /null or empty password/); michael@0: LoginTest.checkStorageData(loginmgr, [], []); michael@0: michael@0: michael@0: /* ========== 4 ========== */ michael@0: testnum++; michael@0: testdesc = "Try adding invalid logins (httpRealm / formSubmitURL checks)"; michael@0: michael@0: testuser1.init("http://dummyhost.mozilla.org", null, null, michael@0: "dummydude", "itsasecret", "put_user_here", "put_pw_here"); michael@0: // formSubmitURL == null, httpRealm == null michael@0: tryAddUser(loginmgr, testuser1, /without a httpRealm or formSubmitURL/); michael@0: LoginTest.checkStorageData(loginmgr, [], []); michael@0: michael@0: testuser1.formSubmitURL = ""; michael@0: testuser1.httpRealm = ""; michael@0: tryAddUser(loginmgr, testuser1, /both a httpRealm and formSubmitURL/); michael@0: LoginTest.checkStorageData(loginmgr, [], []); michael@0: michael@0: testuser1.formSubmitURL = "foo"; michael@0: testuser1.httpRealm = "foo"; michael@0: tryAddUser(loginmgr, testuser1, /both a httpRealm and formSubmitURL/); michael@0: LoginTest.checkStorageData(loginmgr, [], []); michael@0: michael@0: michael@0: testuser1.formSubmitURL = ""; michael@0: testuser1.httpRealm = "foo"; michael@0: tryAddUser(loginmgr, testuser1, /both a httpRealm and formSubmitURL/); michael@0: LoginTest.checkStorageData(loginmgr, [], []); michael@0: michael@0: testuser1.formSubmitURL = "foo"; michael@0: testuser1.httpRealm = ""; michael@0: tryAddUser(loginmgr, testuser1, /both a httpRealm and formSubmitURL/); michael@0: LoginTest.checkStorageData(loginmgr, [], []); michael@0: michael@0: michael@0: testuser1.formSubmitURL = null; michael@0: testuser1.httpRealm = ""; michael@0: tryAddUser(loginmgr, testuser1, /without a httpRealm or formSubmitURL/); michael@0: LoginTest.checkStorageData(loginmgr, [], []); michael@0: michael@0: michael@0: /* ========== 5 ========== */ michael@0: testnum++; michael@0: testdesc = "Try adding valid logins (httpRealm / formSubmitURL checks)"; michael@0: michael@0: testuser1.formSubmitURL = null; michael@0: testuser1.httpRealm = "foo"; michael@0: testuser1.hostname ="http://dummyhost1"; michael@0: tryAddUser(loginmgr, testuser1); michael@0: michael@0: testuser1.formSubmitURL = ""; michael@0: testuser1.httpRealm = null; michael@0: testuser1.hostname ="http://dummyhost2"; michael@0: tryAddUser(loginmgr, testuser1); michael@0: michael@0: testuser1.formSubmitURL = "foo"; michael@0: testuser1.httpRealm = null; michael@0: testuser1.hostname ="http://dummyhost3"; michael@0: tryAddUser(loginmgr, testuser1); michael@0: michael@0: michael@0: } catch (e) { michael@0: throw "FAILED in test #" + testnum + " -- " + testdesc + ": " + e; michael@0: } michael@0: };