1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/files/file_path.h"
6#include "base/files/scoped_temp_dir.h"
7#include "base/path_service.h"
8#include "base/strings/utf_string_conversions.h"
9#include "chrome/common/chrome_paths.h"
10#include "chrome/utility/importer/firefox_importer_unittest_utils.h"
11#include "chrome/utility/importer/nss_decryptor.h"
12#include "sql/connection.h"
13#include "testing/gtest/include/gtest/gtest.h"
14
15// TODO(jschuh): Disabled on Win64 build. http://crbug.com/179688
16#if defined(OS_WIN) && defined(ARCH_CPU_X86_64)
17#define MAYBE_NSS(x) DISABLED_##x
18#else
19#define MAYBE_NSS(x) x
20#endif
21
22// The following test requires the use of the NSSDecryptor, on OSX this needs
23// to run in a separate process, so we use a proxy object so we can share the
24// same test between platforms.
25TEST(FirefoxImporterTest, MAYBE_NSS(Firefox3NSS3Decryptor)) {
26  base::FilePath nss_path;
27  ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &nss_path));
28#if defined(OS_MACOSX)
29  nss_path = nss_path.AppendASCII("firefox3_nss_mac");
30#else
31  nss_path = nss_path.AppendASCII("firefox3_nss");
32#endif  // !OS_MACOSX
33  base::FilePath db_path;
34  ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &db_path));
35  db_path = db_path.AppendASCII("firefox3_profile");
36
37  FFUnitTestDecryptorProxy decryptor_proxy;
38  ASSERT_TRUE(decryptor_proxy.Setup(nss_path));
39
40  ASSERT_TRUE(decryptor_proxy.DecryptorInit(nss_path, db_path));
41  EXPECT_EQ(base::ASCIIToUTF16("hello"),
42      decryptor_proxy.Decrypt("MDIEEPgAAAAAAAAAAAAAAAAAAAEwFAYIKoZIhvcNAwcECKa"
43                              "jtRg4qFSHBAhv9luFkXgDJA=="));
44  // Test UTF-16 encoding.
45  EXPECT_EQ(base::WideToUTF16(L"\x4E2D"),
46      decryptor_proxy.Decrypt("MDIEEPgAAAAAAAAAAAAAAAAAAAEwFAYIKoZIhvcNAwcECLW"
47                              "qqiccfQHWBAie74hxnULxlw=="));
48
49  // Test empty string edge case.
50  EXPECT_EQ(base::string16(), decryptor_proxy.Decrypt(std::string()));
51
52  // Test invalid base64.
53  EXPECT_EQ(base::string16(), decryptor_proxy.Decrypt("Not! Valid! Base64!"));
54}
55
56// The following test verifies proper detection of authentication scheme in
57// firefox's signons db. We insert two entries into moz_logins table. The first
58// has httpRealm column filled with non-empty string, therefore resulting
59// PasswordForm should have SCHEME_BASIC in scheme. The second entry has NULL
60// httpRealm, so it should produce a SCHEME_HTML PasswordForm.
61TEST(FirefoxImporterTest, MAYBE_NSS(FirefoxNSSDecryptorDeduceAuthScheme)) {
62  base::ScopedTempDir temp_dir;
63  ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
64  base::FilePath signons_path = temp_dir.path().AppendASCII("signons.sqlite");
65  sql::Connection db_conn;
66
67  ASSERT_TRUE(db_conn.Open(signons_path));
68
69  ASSERT_TRUE(db_conn.Execute(
70      "CREATE TABLE moz_logins (id INTEGER PRIMARY KEY, hostname TEXT NOT "
71      "NULL, httpRealm TEXT, formSubmitURL TEXT, usernameField TEXT NOT NULL,"
72      "passwordField TEXT NOT NULL, encryptedUsername TEXT NOT NULL,"
73      "encryptedPassword TEXT NOT NULL, guid TEXT, encType INTEGER,"
74      "timeCreated INTEGER, timeLastUsed INTEGER, timePasswordChanged "
75      "INTEGER, timesUsed INTEGER)"));
76
77  ASSERT_TRUE(db_conn.Execute(
78      "CREATE TABLE moz_disabledHosts (id INTEGER PRIMARY KEY, hostname TEXT "
79      "UNIQUE ON CONFLICT REPLACE)"));
80
81  ASSERT_TRUE(db_conn.Execute(
82      "INSERT INTO 'moz_logins' VALUES(1,'http://server.com:1234',"
83      "'http_realm',NULL,'','','','','{bfa37106-a4dc-0a47-abb4-dafd507bf2db}',"
84      "1,1401883410959,1401883410959,1401883410959,1)"));
85
86  ASSERT_TRUE(db_conn.Execute(
87      "INSERT INTO 'moz_logins' VALUES(2,'http://server.com:1234',NULL,"
88      "'http://test2.server.com:1234/action','username','password','','',"
89      "'{71ad64fa-b5d4-cf4d-b390-2e4d56fe2aff}',1,1401883939239,"
90      "1401883939239, 1401883939239,1)"));
91
92  db_conn.Close();
93
94  base::FilePath nss_path;
95  ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &nss_path));
96#if defined(OS_MACOSX)
97  nss_path = nss_path.AppendASCII("firefox3_nss_mac");
98#else
99  nss_path = nss_path.AppendASCII("firefox3_nss");
100#endif  // !OS_MACOSX
101  base::FilePath db_path;
102  ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &db_path));
103  db_path = db_path.AppendASCII("firefox3_profile");
104
105  FFUnitTestDecryptorProxy decryptor_proxy;
106  ASSERT_TRUE(decryptor_proxy.Setup(nss_path));
107
108  ASSERT_TRUE(decryptor_proxy.DecryptorInit(nss_path, db_path));
109  std::vector<autofill::PasswordForm> forms =
110      decryptor_proxy.ParseSignons(signons_path);
111
112  ASSERT_EQ(2u, forms.size());
113  EXPECT_EQ(autofill::PasswordForm::SCHEME_BASIC, forms[0].scheme);
114  EXPECT_EQ(autofill::PasswordForm::SCHEME_HTML, forms[1].scheme);
115}
116