Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(60)

Side by Side Diff: components/password_manager/core/browser/import/password_importer_unittest.cc

Issue 1193143003: Enable import/export of passwords into/from Password Manager (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Removed import-complete dialog. Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
vabr (Chromium) 2016/03/10 10:53:32 2016
xunlu 2016/03/16 07:23:59 Done.
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 "components/password_manager/core/browser/import/password_importer.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/files/file_util.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/run_loop.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/test/test_simple_task_runner.h"
15 #include "base/thread_task_runner_handle.h"
16 #include "components/autofill/core/common/password_form.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 namespace password_manager {
20
21 namespace {
22
23 const char kTestOriginURL[] = "http://accounts.google.com/a/LoginAuth";
24 const char kTestSignonRealm[] = "http://accounts.google.com/";
25 const char kTestUsername[] = "test@gmail.com";
26 const char kTestPassword[] = "test1";
27
28 // Creates a test input file in a temporary directory with the specified
vabr (Chromium) 2016/03/10 10:53:32 Consider using a ScopedTempDir to keep your testin
xunlu 2016/03/16 07:23:59 Done.
29 // contents and extension; and cleans it up when the object is destroyed.
30 class ScopedInputFile {
31 public:
32 ScopedInputFile() {}
33 ~ScopedInputFile() { base::DeleteFile(input_file_path_.DirName(), true); }
34
35 void Create(const base::FilePath::CharType* extension,
36 base::StringPiece contents) {
37 base::FilePath temporary_dir;
38 ASSERT_TRUE(base::CreateNewTempDirectory(base::FilePath::StringType(),
39 &temporary_dir));
40 input_file_path_ =
41 temporary_dir.AppendASCII("passwords").AddExtension(extension);
42 ASSERT_TRUE(
43 base::WriteFile(input_file_path_, contents.data(), contents.size()));
44 }
45
46 base::FilePath path() { return input_file_path_; }
47
48 private:
49 base::FilePath input_file_path_;
50
51 DISALLOW_COPY_AND_ASSIGN(ScopedInputFile);
52 };
53
54 } // namespace
55
56 class PasswordImporterTest : public testing::Test {
57 public:
58 PasswordImporterTest()
59 : callback_called_(false), result_(PasswordImporter::SUCCESS) {}
60
61 protected:
62 void StartImportAndWaitForCompletion(const base::FilePath& input_file) {
63 PasswordImporter::Import(input_file, message_loop_.task_runner(),
64 base::Bind(&PasswordImporterTest::OnImportFinished,
65 base::Unretained(this)));
66
67 base::RunLoop run_loop;
68 run_loop.RunUntilIdle();
69
70 ASSERT_TRUE(callback_called_);
71 }
72
73 void OnImportFinished(PasswordImporter::Result result,
74 const std::vector<autofill::PasswordForm>& passwords) {
75 callback_called_ = true;
76 result_ = result;
77 imported_passwords_ = passwords;
78 }
79
80 const PasswordImporter::Result& result() { return result_; }
81 const std::vector<autofill::PasswordForm>& imported_passwords() {
82 return imported_passwords_;
83 }
84
85 private:
86 base::MessageLoop message_loop_;
87
88 bool callback_called_;
89 PasswordImporter::Result result_;
90 std::vector<autofill::PasswordForm> imported_passwords_;
91
92 DISALLOW_COPY_AND_ASSIGN(PasswordImporterTest);
93 };
94
95 TEST_F(PasswordImporterTest, CSVImport) {
96 const char kTestCSVInput[] =
97 "Url,Username,Password\n"
98 "http://accounts.google.com/a/LoginAuth,test@gmail.com,test1\n";
99
100 ScopedInputFile input_file;
101 ASSERT_NO_FATAL_FAILURE(
102 input_file.Create(FILE_PATH_LITERAL(".csv"), kTestCSVInput));
103
104 ASSERT_NO_FATAL_FAILURE(StartImportAndWaitForCompletion(input_file.path()));
105
106 EXPECT_EQ(PasswordImporter::SUCCESS, result());
107 ASSERT_EQ(1u, imported_passwords().size());
108 EXPECT_EQ(GURL(kTestOriginURL), imported_passwords()[0].origin);
109 EXPECT_EQ(kTestSignonRealm, imported_passwords()[0].signon_realm);
110 EXPECT_EQ(base::ASCIIToUTF16(kTestUsername),
111 imported_passwords()[0].username_value);
112 EXPECT_EQ(base::ASCIIToUTF16(kTestPassword),
113 imported_passwords()[0].password_value);
114 }
115
116 TEST_F(PasswordImporterTest, ImportIOErrorDueToUnreadableFile) {
117 base::FilePath non_existent_input_file(FILE_PATH_LITERAL("nonexistent.csv"));
118 ASSERT_NO_FATAL_FAILURE(
119 StartImportAndWaitForCompletion(non_existent_input_file));
120
121 EXPECT_EQ(PasswordImporter::IO_ERROR, result());
122 ASSERT_EQ(0u, imported_passwords().size());
123 }
124
125 } // namespace password_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698