Bug 1084233 - Part 4: Add marionette test for unlocking puk. r=hsinyi
authorEdgar Chen <echen@mozilla.com>
Mon, 27 Oct 2014 16:14:46 +0800
changeset 217527 1ce89b6f89893700dc0f15ab7c91fd5a9877ad19
parent 217526 d638e47fe6380956933fc2340fb29d13a592729a
child 217528 9bf115cb09f7ec27148ea8c9f9dfcf811331fa67
push id12413
push userechen@mozilla.com
push dateWed, 26 Nov 2014 10:51:25 +0000
treeherderb2g-inbound@1ce89b6f8989 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewershsinyi
bugs1084233
milestone36.0a1
Bug 1084233 - Part 4: Add marionette test for unlocking puk. r=hsinyi
dom/icc/tests/marionette/head.js
dom/icc/tests/marionette/manifest.ini
dom/icc/tests/marionette/test_icc_card_lock_unlock_puk.js
--- a/dom/icc/tests/marionette/head.js
+++ b/dom/icc/tests/marionette/head.js
@@ -2,16 +2,18 @@
  * http://creativecommons.org/publicdomain/zero/1.0/ */
 
 const {Cc: Cc, Ci: Ci, Cr: Cr, Cu: Cu} = SpecialPowers;
 
 const PREF_KEY_RIL_DEBUGGING_ENABLED = "ril.debugging.enabled";
 
 // The pin code hard coded in emulator is "0000".
 const DEFAULT_PIN = "0000";
+// The puk code hard coded in emulator is "12345678".
+const DEFAULT_PUK = "12345678";
 
 // Emulate Promise.jsm semantics.
 Promise.defer = function() { return new Deferred(); }
 function Deferred() {
   this.promise = new Promise(function(resolve, reject) {
     this.resolve = resolve;
     this.reject = reject;
   }.bind(this));
--- a/dom/icc/tests/marionette/manifest.ini
+++ b/dom/icc/tests/marionette/manifest.ini
@@ -4,16 +4,17 @@ browser = false
 qemu = true
 
 [test_stk_proactive_command.js]
 [test_icc_contact.js]
 [test_icc_card_lock_get_retry_count.js]
 [test_icc_card_lock_change_pin.js]
 [test_icc_card_lock_enable_pin.js]
 [test_icc_card_lock_unlock_pin.js]
+[test_icc_card_lock_unlock_puk.js]
 [test_icc_card_state.js]
 [test_icc_info.js]
 [test_stk_refresh.js]
 [test_stk_poll_off.js]
 [test_stk_setup_event_list.js]
 [test_stk_setup_call.js]
 [test_stk_send_ss.js]
 [test_stk_send_ussd.js]
new file mode 100644
--- /dev/null
+++ b/dom/icc/tests/marionette/test_icc_card_lock_unlock_puk.js
@@ -0,0 +1,97 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+MARIONETTE_TIMEOUT = 60000;
+MARIONETTE_HEAD_JS = "head.js";
+
+function passingWrongPinAndWait(aIcc) {
+  return aIcc.getCardLockRetryCount("pin").then((aResult) => {
+    let promises = [];
+    let retryCount = aResult.retryCount;
+
+    ok(true, "pin retryCount is " + retryCount);
+
+    promises.push(waitForTargetEvent(aIcc, "cardstatechange", function() {
+      return aIcc.cardState === "pukRequired";
+    }));
+
+    for (let i = 0; i < retryCount; i++) {
+      promises.push(aIcc.unlockCardLock({ lockType: "pin", pin: "1111" })
+        .then(() => {
+          ok(false, "unlocking pin should not success");
+        }, function reject(aRetryCount, aError) {
+          // check the request error.
+          is(aError.name, "IncorrectPassword", "error.name");
+          is(aError.retryCount, aRetryCount, "error.retryCount");
+        }.bind(null, retryCount - i - 1)));
+    }
+
+    return Promise.all(promises);
+  });
+}
+
+function testUnlockPuk(aIcc, aPuk, aNewPin, aErrorName, aRetryCount) {
+  log("testUnlockPuk with puk=" + aPuk + " and newPin=" + aNewPin);
+
+  return aIcc.unlockCardLock({ lockType: "puk", puk: aPuk, newPin: aNewPin })
+    .then((aResult) => {
+      if (aErrorName) {
+        ok(false, "unlocking puk should not success");
+      }
+    }, (aError) => {
+      if (!aErrorName) {
+        ok(false, "unlocking puk should not fail");
+        return;
+      }
+
+      // Check the request error.
+      is(aError.name, aErrorName, "error.name");
+      is(aError.retryCount, aRetryCount, "error.retryCount");
+    });
+}
+
+function testUnlockPukAndWait(aIcc, aPuk, aNewPin, aCardState) {
+  log("testUnlockPuk with puk=" + aPuk + "/newPin=" + aNewPin +
+      ", and wait card state changes to '" + aCardState + "'");
+
+  let promises = [];
+
+  promises.push(waitForTargetEvent(aIcc, "cardstatechange", function() {
+    return aIcc.cardState === aCardState;
+  }));
+  promises.push(testUnlockPuk(aIcc, aPuk, aNewPin));
+
+  return Promise.all(promises);
+}
+
+// Start tests
+startTestCommon(function() {
+  let icc = getMozIcc();
+  let retryCount;
+
+  // Enable pin lock.
+  return setPinLockEnabled(icc, true)
+    // Reset card state to "pinRequired" by restarting radio
+    .then(() => restartRadioAndWait("pinRequired"))
+    .then(() => { icc = getMozIcc() })
+    // Switch cardState to "pukRequired" by entering wrong pin code.
+    .then(() => passingWrongPinAndWait(icc))
+
+    // Get current PUK-lock retry count.
+    .then(() => icc.getCardLockRetryCount("puk"))
+    .then((aResult) => {
+      retryCount = aResult.retryCount;
+      ok(true, "puk retryCount is " + retryCount);
+    })
+
+    // Test unlock PUK code fail.
+    // The retry count should be decreased by 1.
+    .then(() => testUnlockPuk(icc, "11111111", DEFAULT_PIN, "IncorrectPassword",
+                              retryCount - 1))
+
+    // Test unlock PUK code success.
+    .then(() => testUnlockPukAndWait(icc, DEFAULT_PUK, DEFAULT_PIN, "ready"))
+
+    // Restore pin state.
+    .then(() => setPinLockEnabled(icc, false));
+});