Bug 1337314 - Part 2: Encrypt card number while normallizing field. r=lchang draft
authorsteveck-chung <schung@mozilla.com>
Mon, 17 Jul 2017 17:14:54 +0800
changeset 609701 a514815149da53bd6cc56ff6f658fb2432df92b3
parent 609700 14b169dfe2156107dfed0c2ec93ad8365056c8c4
child 637632 7e8e665dd009e372796d012b555fa9283874367d
push id68645
push userbmo:schung@mozilla.com
push dateMon, 17 Jul 2017 09:15:27 +0000
reviewerslchang
bugs1337314
milestone56.0a1
Bug 1337314 - Part 2: Encrypt card number while normallizing field. r=lchang MozReview-Commit-ID: 9HSpLzJMnoE
browser/extensions/formautofill/ProfileStorage.jsm
browser/extensions/formautofill/test/unit/test_creditCardRecords.js
--- a/browser/extensions/formautofill/ProfileStorage.jsm
+++ b/browser/extensions/formautofill/ProfileStorage.jsm
@@ -727,18 +727,17 @@ class CreditCards extends AutofillRecord
     if (creditCard["cc-number"]) {
       let ccNumber = creditCard["cc-number"].replace(/\s/g, "");
       delete creditCard["cc-number"];
 
       if (!/^\d+$/.test(ccNumber)) {
         throw new Error("Credit card number contains invalid characters.");
       }
 
-      // TODO: Encrypt cc-number here (bug 1337314).
-      // e.g. creditCard["cc-number-encrypted"] = Encrypt(creditCard["cc-number"]);
+      creditCard["cc-number-encrypted"] = FormAutofillUtils.encrypt(ccNumber);
 
       if (ccNumber.length > 4) {
         creditCard["cc-number-masked"] = "*".repeat(ccNumber.length - 4) + ccNumber.substr(-4);
       } else {
         creditCard["cc-number-masked"] = ccNumber;
       }
     }
 
--- a/browser/extensions/formautofill/test/unit/test_creditCardRecords.js
+++ b/browser/extensions/formautofill/test/unit/test_creditCardRecords.js
@@ -69,18 +69,17 @@ let prepareTestCreditCards = async funct
 };
 
 let reCCNumber = /^(\*+)(.{4})$/;
 
 let do_check_credit_card_matches = (creditCardWithMeta, creditCard) => {
   for (let key in creditCard) {
     if (key == "cc-number") {
       do_check_eq(creditCardWithMeta["cc-number"], undefined);
-
-      // check "cc-number-encrypted" after encryption lands (bug 1337314).
+      do_check_neq(creditCardWithMeta["cc-number-encrypted"], creditCard["cc-number"]);
 
       let matches = reCCNumber.exec(creditCardWithMeta["cc-number-masked"]);
       do_check_neq(matches, null);
       do_check_eq(creditCardWithMeta["cc-number-masked"].length, creditCard["cc-number"].length);
       do_check_eq(creditCard["cc-number"].endsWith(matches[2]), true);
     } else {
       do_check_eq(creditCardWithMeta[key], creditCard[key]);
     }
@@ -159,22 +158,16 @@ add_task(async function test_getByFilter
 
   let profileStorage = new ProfileStorage(path);
   await profileStorage.initialize();
 
   let filter = {info: {fieldName: "cc-name"}, searchString: "Tim"};
   let creditCards = profileStorage.creditCards.getByFilter(filter);
   do_check_eq(creditCards.length, 1);
   do_check_credit_card_matches(creditCards[0], TEST_CREDIT_CARD_2);
-
-  // TODO: Uncomment this after decryption lands (bug 1337314).
-  // filter = {info: {fieldName: "cc-number"}, searchString: "11"};
-  // creditCards = profileStorage.creditCards.getByFilter(filter);
-  // do_check_eq(creditCards.length, 1);
-  // do_check_credit_card_matches(creditCards[0], TEST_CREDIT_CARD_2);
 });
 
 add_task(async function test_add() {
   let path = getTempFile(TEST_STORE_FILE_NAME).path;
   await prepareTestCreditCards(path);
 
   let profileStorage = new ProfileStorage(path);
   await profileStorage.initialize();
@@ -252,18 +245,16 @@ add_task(async function test_validate() 
   do_check_eq(creditCards[0]["cc-exp-month"], undefined);
   do_check_eq(creditCards[0]["cc-exp-year"], undefined);
 
   do_check_eq(creditCards[1]["cc-exp-month"], TEST_CREDIT_CARD_WITH_2_DIGITS_YEAR["cc-exp-month"]);
   do_check_eq(creditCards[1]["cc-exp-year"],
     parseInt(TEST_CREDIT_CARD_WITH_2_DIGITS_YEAR["cc-exp-year"], 10) + 2000);
 
   do_check_eq(creditCards[2]["cc-number-masked"].length, 16);
-  // TODO: Check the decrypted numbers should not contain spaces after
-  //       decryption lands (bug 1337314).
 
   Assert.throws(() => profileStorage.creditCards.add(TEST_CREDIT_CARD_WITH_INVALID_NUMBERS),
     /Credit card number contains invalid characters\./);
 });
 
 add_task(async function test_notifyUsed() {
   let path = getTempFile(TEST_STORE_FILE_NAME).path;
   await prepareTestCreditCards(path);