/*global do_load_module: false, do_get_file: false, do_get_cwd: false, testing: false, test: false, Assert: false, resetting: false, JSUnit: false, do_test_pending: false, do_test_finished: false */
/*global dump: false */
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

"use strict";

do_load_module("file://" + do_get_cwd().path + "/testHelper.js");
/* global setupTestAccounts: false, withTestGpgHome: false, withEnigmail: false, component: false */

testing("autocrypt.jsm"); /*global EnigmailAutocrypt: false */
component("enigmail/keyRing.jsm"); /*global EnigmailKeyRing: false */
component("enigmail/stdlib.jsm"); /* global EnigmailStdlib: false */

const pubkey1 =
  `mQENBFdGIzkBCADKys5q0rYiTr/FYdoupmNAJ0o20XWuFp/V58qsnQAMcAY2pCB/ydx9Y7
A80QjZPuVcE5QdROfvvkMXAA47ZxZrH79Kaqj11DS5XOgtLVtITGtWvrYqIFujxP42ICWB
h7LXUwrHfi93FX74ucXoWo/PZndbo+JBxc0ZsrHUdu24grTDuqLZQ8mRCx5U4tf+zEVIU6
kXubFzq8aPSnjfEg6MhXxSRictjIBKM0Ez2QwZmh1vAEmvn0kr0VaJJ7xVRgIH1CgNh/WW
tbr0lrblKCkFkTFnQfslWvSEko+LqvwgBSKyKg8VtWbYftnBkn8FPbP5Brp3wYgBc/c7mr
LROqAFABEBAAG0ImRldi10aWdlciA8ZGV2LXRpZ2VyQHRlc3Qubm90cmVhbD6JATcEEwEI
ACEFAldGIzkCGwMFCwkIBwIGFQgJCgsCBBYCAwECHgECF4AACgkQhDnhcEaXfEb3/gf/V0
da3gXN5TNOsWZKj/fI2FhQBglJ2vlEamnppwtnWZGktdFZ1h6ymzQ9PY3IidbKctqs/QQW
KtIBVh5k02fvUe99nsFZmINcLeajdu7IqvKxtFBuEwZAA1Bw9dhM3JRQM8z+l+CtbFh6dV
ufU7q5vVEXciCkhdn172QYTMAXNYE4Tfh7eaEAOdRyFcwiAGZ826pOp7Al52frK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WFzGc4xHEo+UP/02AIUR0349zGk9a4D5Jv0
07y+d0tWKPL0V2btaq9xQzoM51CtuT0ZoqTO8A0uFmEhCkw++reQcWOz1Nn+MarPjjJwMj
hTPS/H1qiwTXmuwx92xLL0pajloq7oWYwlxsgVGCMDYE0TOMN8p/Vc+eoJaWZh8yO1xJGD
P98RHbZQWwYN6qLzE4y/ECTHxqi9UKc68qHNVH9ZgtTXnmgLAkEvXzRV1UOEEttJ6rrgPr
TubjsIG+ckZK5mlivy+UW6XN0WBE0oetKjT8/Cb1dQYiX/8MJkGcIUFRurU7gtGW3ncSTd
r6WRXaQtfnRn9JG1aSXNYB/xZWzCBdykZp+tLuu4A3LVeOzn064hqf3rz2N7b8dWMk5WL5
LIUhXYoYc7232RkNSiiKndeJNryvTowFt9anuMj4pFgGveClQc9+QGyMVdTe6G5kOJkKG8
ydHKFEFObtsTLaut4lHTtxn+06QO/LKtQTXqNEyOyfYhbyX7xDbCbu4/MA23MzTs1hhwgI
y4+UejU/Yeny6VkBodA3bFyEYKWPoMDDgfdlZbzjv3qAN4Qq+ollo8K3gJgH0QONrUaRY8
4/hil05T4EnUZiXdzPWvhMv5lEK+pTMlO8FbOG31+aB8rxCg+wp1ovyC/fp5XjZaLHcyPA
WAXKLBn4tb400iHp7byO85tF/H0OOI1K`;

setupTestAccounts("strike.devtest@gmail.com", "0x781617319CE311C4");

/* global Sqlite */

test(function prepareDb() {
  // Drop autocrypt_keydata table (if it exists)
  do_test_pending();
  Sqlite.openConnection({
    path: "enigmail.sqlite",
    sharedMemoryCache: false
  }).
  then(connection => {
    connection.execute("drop table autocrypt_keydata;").then(ok => {
      connection.close();
      dump("dropped table\n");
      do_test_finished();
    }).catch(err => {
      do_test_finished();
    });
  });
  JSUnit.waitForAsyncTest(); // wait until that's done before starting the next test
});


// testing: extractMessageId
test(function processHeader() {

  const hdr0 = "type=1; addr=dev-tiger@test.notreal; keydata=" + pubkey1;
  const hdr1 = "type=1; addr=dev-tiger@test.notreal; keydata=" + pubkey2;

  let d0 = new Date();
  d0.setTime(Date.now() - 5 * 86400 * 1000); // 5 days ago
  const sentDate = d0.toUTCString();

  let d1 = new Date();
  d1.setTime(Date.now() - 4 * 86400 * 1000); // 4 days ago

  const updateDate = d1.toUTCString();

  do_test_pending();
  EnigmailAutocrypt.processAutocryptHeader("dev-tiger@test.notreal", [hdr0], sentDate).
  then(result => {
    Assert.equal(0, result);

    return EnigmailAutocrypt.getOpenPGPKeyForEmail(["dev-tiger@test.notreal"]);
  }).then(result => {
    Assert.equal(1, result.length);
    Assert.equal(sentDate, result[0].lastAutocrypt.toUTCString());
    Assert.equal("8C140834F2D683E9A016D3098439E17046977C46", result[0].fpr);
    Assert.equal(pubkey1.replace(/[\r\n ]/g, ""), result[0].keyData);

    return EnigmailAutocrypt.processAutocryptHeader("dev-tiger@test.notreal", [hdr1], updateDate);
  }).then(result => {
    Assert.equal(0, result);

    return EnigmailAutocrypt.getOpenPGPKeyForEmail(["dev-tiger@test.notreal"]);
  }).then(result => {
    Assert.equal(1, result.length);
    Assert.equal(updateDate, result[0].lastAutocrypt.toUTCString());
    Assert.equal("65537E212DC19025AD38EDB2781617319CE311C4", result[0].fpr);
    Assert.equal(pubkey2.replace(/[\r\n ]/g, ""), result[0].keyData);

    // this should not change anything, update in the past
    return EnigmailAutocrypt.processAutocryptHeader("dev-tiger@test.notreal", [hdr0], sentDate);
  }).then(result => {
    Assert.equal(0, result);

    return EnigmailAutocrypt.getOpenPGPKeyForEmail(["dev-tiger@test.notreal"]);
  }).then(result => {
    Assert.equal(1, result.length);
    Assert.equal(updateDate, result[0].lastAutocrypt.toUTCString());
    Assert.equal("65537E212DC19025AD38EDB2781617319CE311C4", result[0].fpr);
    Assert.equal(pubkey2.replace(/[\r\n ]/g, ""), result[0].keyData);

    do_test_finished();
  }).
  catch(err => {
    Assert.equal(err, 1);
    do_test_finished();
  });

});

test(withTestGpgHome(withEnigmail(function shouldGetKeyFunctions() {
  const publicKey = do_get_file("resources/dev-strike.asc", false);
  const secretKey = do_get_file("resources/dev-strike.sec", false);
  EnigmailKeyRing.importKeyFromFile(publicKey, {}, {});
  EnigmailKeyRing.importKeyFromFile(secretKey, {}, {});

  let inspector = Cc["@mozilla.org/jsinspector;1"].createInstance(Ci.nsIJSInspector);

  let id = EnigmailStdlib.getIdentityForEmail("strike.devtest@gmail.com").identity;
  Assert.equal(id.email, "strike.devtest@gmail.com");

  EnigmailAutocrypt.createSetupMessage(id).then(res => {
    Assert.ok(res);
    Assert.equal(res.passwd.length, 44);
    Assert.ok(res.msg.length > 9500);
    Assert.equal(res.msg.substr(0, 100), "To: strike.devtest@gmail.com\r\nFrom: strike.devtest@gmail.com\r\nAutocrypt-Setup-Message: v1\r\nSubject: ");

    return EnigmailAutocrypt.handleBackupMessage(res.passwd, res.msg, "strike.devtest@gmail.com");
  }).then(res => {
    Assert.ok(res);
    Assert.equal(res.fpr, "65537E212DC19025AD38EDB2781617319CE311C4");

    inspector.exitNestedEventLoop();
  }).catch(err => {
    Assert.equal(err, 0);
    inspector.exitNestedEventLoop();
  });
  inspector.enterNestedEventLoop(0);

})));
