OLD | NEW |
(Empty) | |
| 1 |
| 2 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 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/memory/scoped_vector.h" |
| 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/run_loop.h" |
| 14 #include "base/strings/stringprintf.h" |
| 15 #include "base/strings/utf_string_conversions.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 class PasswordExporterTest : public testing::Test { |
| 22 public: |
| 23 PasswordExporterTest() : callback_called_(false) {} |
| 24 |
| 25 protected: |
| 26 ScopedVector<autofill::PasswordForm> ConstructTestPasswordForms() { |
| 27 autofill::PasswordForm* password_form_ = 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 ScopedVector<autofill::PasswordForm> password_forms; |
| 33 password_forms.push_back(password_form_); |
| 34 return password_forms.Pass(); |
| 35 } |
| 36 |
| 37 void StartExportAndWaitUntilCompleteThenReadOutput( |
| 38 const base::FilePath::StringType& provided_extension, |
| 39 const base::FilePath::StringType& expected_extension, |
| 40 ScopedVector<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, passwords.Pass(), |
| 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 |
| 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( |
| 101 FILE_PATH_LITERAL(".vsc"), FILE_PATH_LITERAL(".csv"), |
| 102 ConstructTestPasswordForms(), &output)); |
| 103 EXPECT_EQ(kExpectedCSVOutput, output); |
| 104 } |
| 105 |
| 106 } // namespace password_manager |
OLD | NEW |