OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 "components/password_manager/core/browser/export/password_exporter.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" |
| 9 #include "base/files/file_util.h" |
| 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/run_loop.h" |
| 12 #include "base/strings/stringprintf.h" |
| 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "components/autofill/core/common/password_form.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 namespace password_manager { |
| 18 |
| 19 class PasswordExporterTest : public testing::Test { |
| 20 public: |
| 21 PasswordExporterTest() {} |
| 22 |
| 23 protected: |
| 24 std::vector<scoped_ptr<autofill::PasswordForm>> ConstructTestPasswordForms() { |
| 25 scoped_ptr<autofill::PasswordForm> password_form_( |
| 26 new autofill::PasswordForm()); |
| 27 password_form_->origin = GURL("http://accounts.google.com/a/LoginAuth"); |
| 28 password_form_->username_value = base::ASCIIToUTF16("test@gmail.com"); |
| 29 password_form_->password_value = base::ASCIIToUTF16("test1"); |
| 30 |
| 31 std::vector<scoped_ptr<autofill::PasswordForm>> password_forms; |
| 32 password_forms.push_back(std::move(password_form_)); |
| 33 return password_forms; |
| 34 } |
| 35 |
| 36 void StartExportAndWaitUntilCompleteThenReadOutput( |
| 37 const base::FilePath::StringType& provided_extension, |
| 38 const base::FilePath::StringType& expected_extension, |
| 39 std::vector<scoped_ptr<autofill::PasswordForm>> passwords, |
| 40 std::string* output) { |
| 41 base::FilePath temporary_dir; |
| 42 ASSERT_TRUE(base::CreateNewTempDirectory(base::FilePath::StringType(), |
| 43 &temporary_dir)); |
| 44 base::FilePath output_file = |
| 45 temporary_dir.AppendASCII("passwords").AddExtension(provided_extension); |
| 46 |
| 47 PasswordExporter::Export(output_file, std::move(passwords), |
| 48 message_loop_.task_runner()); |
| 49 |
| 50 base::RunLoop run_loop; |
| 51 run_loop.RunUntilIdle(); |
| 52 |
| 53 if (provided_extension != expected_extension) { |
| 54 output_file = output_file.ReplaceExtension(expected_extension); |
| 55 } |
| 56 |
| 57 EXPECT_TRUE(base::ReadFileToString(output_file, output)); |
| 58 base::DeleteFile(temporary_dir, true); |
| 59 } |
| 60 |
| 61 private: |
| 62 base::MessageLoop message_loop_; |
| 63 |
| 64 DISALLOW_COPY_AND_ASSIGN(PasswordExporterTest); |
| 65 }; |
| 66 |
| 67 TEST_F(PasswordExporterTest, CSVExport) { |
| 68 #if defined(OS_WIN) |
| 69 const char kLineEnding[] = "\r\n"; |
| 70 #else |
| 71 const char kLineEnding[] = "\n"; |
| 72 #endif |
| 73 std::string kExpectedCSVOutput = base::StringPrintf( |
| 74 "name,url,username,password%s" |
| 75 "accounts.google.com,http://accounts.google.com/a/" |
| 76 "LoginAuth,test@gmail.com,test1%s", |
| 77 kLineEnding, kLineEnding); |
| 78 |
| 79 std::string output; |
| 80 ASSERT_NO_FATAL_FAILURE(StartExportAndWaitUntilCompleteThenReadOutput( |
| 81 FILE_PATH_LITERAL(".csv"), FILE_PATH_LITERAL(".csv"), |
| 82 ConstructTestPasswordForms(), &output)); |
| 83 EXPECT_EQ(kExpectedCSVOutput, output); |
| 84 } |
| 85 |
| 86 } // namespace password_manager |
OLD | NEW |