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

Side by Side Diff: components/password_manager/core/browser/export/password_exporter_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
vabr (Chromium) 2016/03/10 10:53:32 nit: remove the blank line
xunlu 2016/03/16 07:23:59 Done.
2 // 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.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5
6 #include "components/password_manager/core/browser/export/password_exporter.h"
7
8 #include "base/bind.h"
9 #include "base/bind_helpers.h"
10 #include "base/files/file_util.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/run_loop.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "components/autofill/core/common/password_form.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 namespace password_manager {
19
20 class PasswordExporterTest : public testing::Test {
21 public:
22 PasswordExporterTest() : callback_called_(false) {}
23
24 protected:
25 std::vector<scoped_ptr<autofill::PasswordForm>> ConstructTestPasswordForms() {
26 scoped_ptr<autofill::PasswordForm> password_form_(
27 new autofill::PasswordForm());
28 password_form_->origin = GURL("http://accounts.google.com/a/LoginAuth");
29 password_form_->username_value = base::ASCIIToUTF16("test@gmail.com");
30 password_form_->password_value = base::ASCIIToUTF16("test1");
31
32 std::vector<scoped_ptr<autofill::PasswordForm>> password_forms;
33 password_forms.push_back(std::move(password_form_));
34 return password_forms;
35 }
36
37 void StartExportAndWaitUntilCompleteThenReadOutput(
38 const base::FilePath::StringType& provided_extension,
39 const base::FilePath::StringType& expected_extension,
40 std::vector<scoped_ptr<autofill::PasswordForm>> passwords,
41 std::string* output) {
42 base::FilePath temporary_dir;
43 ASSERT_TRUE(base::CreateNewTempDirectory(base::FilePath::StringType(),
44 &temporary_dir));
45 base::FilePath output_file =
46 temporary_dir.AppendASCII("passwords").AddExtension(provided_extension);
47
48 PasswordExporter::Export(output_file, std::move(passwords),
49 message_loop_.task_runner(),
50 base::Bind(&PasswordExporterTest::OnExportFinished,
51 base::Unretained(this)));
52
53 base::RunLoop run_loop;
54 run_loop.RunUntilIdle();
55
56 if (provided_extension != expected_extension) {
57 output_file = output_file.ReplaceExtension(expected_extension);
58 }
59
60 EXPECT_TRUE(callback_called_);
61 EXPECT_TRUE(base::ReadFileToString(output_file, output));
62 base::DeleteFile(temporary_dir, true);
63 }
64
65 void OnExportFinished() { callback_called_ = true; }
66
67 private:
68 base::MessageLoop message_loop_;
69 bool callback_called_;
70
71 DISALLOW_COPY_AND_ASSIGN(PasswordExporterTest);
72 };
73
74 TEST_F(PasswordExporterTest, CSVExport) {
75 #if defined(OS_WIN)
76 const char kLineEnding[] = "\r\n";
77 #else
78 const char kLineEnding[] = "\n";
79 #endif
80 std::string kExpectedCSVOutput = base::StringPrintf(
81 "name,url,username,password%s"
82 "accounts.google.com,http://accounts.google.com/a/"
83 "LoginAuth,test@gmail.com,test1%s",
84 kLineEnding, kLineEnding);
85
86 std::string output;
87 ASSERT_NO_FATAL_FAILURE(StartExportAndWaitUntilCompleteThenReadOutput(
88 FILE_PATH_LITERAL(".csv"), FILE_PATH_LITERAL(".csv"),
89 ConstructTestPasswordForms(), &output));
90 EXPECT_EQ(kExpectedCSVOutput, output);
91
92 // Verify that exporter use ".csv" extension when no extension is provided
vabr (Chromium) 2016/03/10 10:53:32 nit: Add a full-stop (.) at the end of the sentenc
xunlu 2016/03/16 07:23:59 Done.
93 ASSERT_NO_FATAL_FAILURE(StartExportAndWaitUntilCompleteThenReadOutput(
94 FILE_PATH_LITERAL(""), FILE_PATH_LITERAL(".csv"),
95 ConstructTestPasswordForms(), &output));
96 EXPECT_EQ(kExpectedCSVOutput, output);
97
98 // Verify that exporter use ".csv" extension when unknown extension is
99 // provided
100 ASSERT_NO_FATAL_FAILURE(StartExportAndWaitUntilCompleteThenReadOutput(
vabr (Chromium) 2016/03/10 10:53:32 (As noted above, this behaviour seems rather risky
xunlu 2016/03/16 07:23:59 See my comments above.
101 FILE_PATH_LITERAL(".vsc"), FILE_PATH_LITERAL(".csv"),
102 ConstructTestPasswordForms(), &output));
103 EXPECT_EQ(kExpectedCSVOutput, output);
104 }
105
106 } // namespace password_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698