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/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 ScopedVector<autofill::PasswordForm> ConstructTestPasswordForms() { |
| 26 autofill::PasswordForm* password_form_ = 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 ScopedVector<autofill::PasswordForm> password_forms; |
| 32 password_forms.push_back(password_form_); |
| 33 return password_forms.Pass(); |
| 34 } |
| 35 |
| 36 void StartExportAndWaitUntilCompleteThenReadOutput( |
| 37 const base::FilePath::StringType& extension, |
| 38 ScopedVector<autofill::PasswordForm> passwords, |
| 39 std::string* output) { |
| 40 base::FilePath temporary_dir; |
| 41 ASSERT_TRUE(base::CreateNewTempDirectory(base::FilePath::StringType(), |
| 42 &temporary_dir)); |
| 43 base::FilePath output_file = |
| 44 temporary_dir.AppendASCII("passwords").AddExtension(extension); |
| 45 |
| 46 PasswordExporter::Export(output_file, passwords.Pass(), |
| 47 message_loop_.task_runner(), |
| 48 base::Bind(&PasswordExporterTest::OnExportFinished, |
| 49 base::Unretained(this))); |
| 50 |
| 51 base::RunLoop run_loop; |
| 52 run_loop.RunUntilIdle(); |
| 53 |
| 54 EXPECT_TRUE(callback_called_); |
| 55 EXPECT_TRUE(base::ReadFileToString(output_file, output)); |
| 56 base::DeleteFile(temporary_dir, true); |
| 57 } |
| 58 |
| 59 void OnExportFinished() { callback_called_ = true; } |
| 60 |
| 61 private: |
| 62 base::MessageLoop message_loop_; |
| 63 bool callback_called_; |
| 64 |
| 65 DISALLOW_COPY_AND_ASSIGN(PasswordExporterTest); |
| 66 }; |
| 67 |
| 68 TEST_F(PasswordExporterTest, CSVExport) { |
| 69 const char kExpectedCSVOutput[] = |
| 70 "name,url,username,password\n" |
| 71 "accounts.google.com,http://accounts.google.com/a/" |
| 72 "LoginAuth,test@gmail.com,test1\n"; |
| 73 |
| 74 std::string output; |
| 75 ASSERT_NO_FATAL_FAILURE(StartExportAndWaitUntilCompleteThenReadOutput( |
| 76 FILE_PATH_LITERAL(".csv"), ConstructTestPasswordForms(), &output)); |
| 77 EXPECT_EQ(kExpectedCSVOutput, output); |
| 78 } |
| 79 |
| 80 TEST_F(PasswordExporterTest, JSONExport) { |
| 81 const char kExpectedJSONOutput[] = |
| 82 "[{" |
| 83 "\"password\":\"test1\"," |
| 84 "\"url\":\"http://accounts.google.com/a/LoginAuth\"," |
| 85 "\"username\":\"test@gmail.com\"" |
| 86 "}]"; |
| 87 |
| 88 std::string output; |
| 89 ASSERT_NO_FATAL_FAILURE(StartExportAndWaitUntilCompleteThenReadOutput( |
| 90 FILE_PATH_LITERAL(".json"), ConstructTestPasswordForms(), &output)); |
| 91 EXPECT_EQ(kExpectedJSONOutput, output); |
| 92 } |
| 93 |
| 94 } // namespace password_manager |
OLD | NEW |