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/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/files/scoped_temp_dir.h" | |
11 #include "base/memory/ref_counted.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 "base/test/test_simple_task_runner.h" | |
16 #include "base/thread_task_runner_handle.h" | |
17 #include "components/autofill/core/common/password_form.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | |
19 | |
20 namespace password_manager { | |
21 | |
22 namespace { | |
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 const char kTestFileName[] = "test_only.csv"; | |
28 } // namespace | |
29 | |
30 class PasswordImporterTest : public testing::Test { | |
31 public: | |
32 PasswordImporterTest() | |
33 : callback_called_(false), result_(PasswordImporter::SUCCESS) {} | |
34 | |
35 void SetUp() override { ASSERT_TRUE(temp_directory_.CreateUniqueTempDir()); } | |
vabr (Chromium)
2016/03/16 17:48:28
Please add this to the constructor instead. SetUp
xunlu
2016/03/18 21:15:30
Seems that ASSERT_TRUE cannot be used in construct
vabr (Chromium)
2016/03/21 14:01:30
I think it is fine to change ASSERT_TRUE to CHECK
xunlu
2016/03/22 07:17:15
Sounds good. Thanks!
| |
36 | |
37 protected: | |
38 void StartImportAndWaitForCompletion(const base::FilePath& input_file) { | |
39 PasswordImporter::Import(input_file, message_loop_.task_runner(), | |
40 base::Bind(&PasswordImporterTest::OnImportFinished, | |
41 base::Unretained(this))); | |
42 | |
43 base::RunLoop run_loop; | |
44 run_loop.RunUntilIdle(); | |
45 | |
46 ASSERT_TRUE(callback_called_); | |
47 } | |
48 | |
49 void OnImportFinished(PasswordImporter::Result result, | |
50 const std::vector<autofill::PasswordForm>& passwords) { | |
51 callback_called_ = true; | |
52 result_ = result; | |
53 imported_passwords_ = passwords; | |
54 } | |
55 | |
56 const PasswordImporter::Result& result() { return result_; } | |
57 const std::vector<autofill::PasswordForm>& imported_passwords() { | |
58 return imported_passwords_; | |
59 } | |
60 | |
61 // Directory for creating files by this test. | |
62 base::ScopedTempDir temp_directory_; | |
63 | |
64 private: | |
65 base::MessageLoop message_loop_; | |
66 | |
67 bool callback_called_; | |
68 PasswordImporter::Result result_; | |
69 std::vector<autofill::PasswordForm> imported_passwords_; | |
70 | |
71 DISALLOW_COPY_AND_ASSIGN(PasswordImporterTest); | |
72 }; | |
73 | |
74 TEST_F(PasswordImporterTest, CSVImport) { | |
75 const char kTestCSVInput[] = | |
76 "Url,Username,Password\n" | |
77 "http://accounts.google.com/a/LoginAuth,test@gmail.com,test1\n"; | |
78 | |
79 base::FilePath input_path = temp_directory_.path().AppendASCII(kTestFileName); | |
80 ASSERT_TRUE( | |
81 base::WriteFile(input_path, kTestCSVInput, strlen(kTestCSVInput))); | |
82 ASSERT_NO_FATAL_FAILURE(StartImportAndWaitForCompletion(input_path)); | |
83 | |
84 EXPECT_EQ(PasswordImporter::SUCCESS, result()); | |
85 ASSERT_EQ(1u, imported_passwords().size()); | |
86 EXPECT_EQ(GURL(kTestOriginURL), imported_passwords()[0].origin); | |
87 EXPECT_EQ(kTestSignonRealm, imported_passwords()[0].signon_realm); | |
88 EXPECT_EQ(base::ASCIIToUTF16(kTestUsername), | |
89 imported_passwords()[0].username_value); | |
90 EXPECT_EQ(base::ASCIIToUTF16(kTestPassword), | |
91 imported_passwords()[0].password_value); | |
92 } | |
93 | |
94 TEST_F(PasswordImporterTest, ImportIOErrorDueToUnreadableFile) { | |
95 base::FilePath non_existent_input_file(FILE_PATH_LITERAL("nonexistent.csv")); | |
96 ASSERT_NO_FATAL_FAILURE( | |
97 StartImportAndWaitForCompletion(non_existent_input_file)); | |
98 | |
99 EXPECT_EQ(PasswordImporter::IO_ERROR, result()); | |
100 ASSERT_EQ(0u, imported_passwords().size()); | |
101 } | |
102 | |
103 } // namespace password_manager | |
OLD | NEW |