Commit b4801041 authored by Vaclav Brozek's avatar Vaclav Brozek Committed by Commit Bot

Migrate PasswordCSVWriter test off of CSVReader

Currently the PasswordCSVWriter test uses CSVReader to check
serializations of passwords into CSV, the result of the tested code.

However, CSVReader will be soon removed together with its main client,
the PasswordCSVReader.

This CL replaces CSVReader use in PasswordCSVWriter test with the
replacement of PasswordCSVReader: the CSVPasswordSequence. This has an
additional benefit: CSVPasswordSequence both decodes CSV and
constructs a PasswordForm out of it, unlike CSVReader, which only does
the former. Therefore, the test must no longer include implementation
details of PasswordCSVWriter, and can instead just verify that
PasswordCSVWriter is the reverse operation of CSVPasswordSequence.

Bug: 934326
Change-Id: If010b42c7c46dd1ece8f6ef96e85eddbd49d09e5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1920122
Commit-Queue: Vaclav Brozek <vabr@chromium.org>
Reviewed-by: default avatarJan Wilken Dörrie <jdoerrie@chromium.org>
Cr-Commit-Position: refs/heads/master@{#717641}
parent 6fbac7d0
......@@ -596,6 +596,7 @@ source_set("unit_tests") {
deps = [
":affiliation",
":affiliation_unittests",
":csv",
":csv_unittests",
":hash_password_manager",
":password_generator",
......
......@@ -9,25 +9,34 @@
#include "base/strings/utf_string_conversions.h"
#include "components/autofill/core/common/password_form.h"
#include "components/password_manager/core/browser/import/csv_reader.h"
#include "components/password_manager/core/browser/import/csv_password.h"
#include "components/password_manager/core/browser/import/csv_password_sequence.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
using autofill::PasswordForm;
using testing::AllOf;
using testing::Contains;
using testing::ElementsAre;
namespace password_manager {
namespace {
MATCHER_P3(FormHasOriginUsernamePassword, origin, username, password, "") {
return arg.signon_realm == origin && arg.origin == GURL(origin) &&
arg.username_value == base::UTF8ToUTF16(username) &&
arg.password_value == base::UTF8ToUTF16(password);
}
} // namespace
TEST(PasswordCSVWriterTest, SerializePasswords_ZeroPasswords) {
std::vector<std::unique_ptr<PasswordForm>> passwords;
CSVTable table;
ASSERT_TRUE(table.ReadCSV(PasswordCSVWriter::SerializePasswords(passwords)));
CSVPasswordSequence seq(PasswordCSVWriter::SerializePasswords(passwords));
ASSERT_EQ(CSVPassword::Status::kOK, seq.result());
EXPECT_THAT(table.column_names(), AllOf(Contains("url"), Contains("username"),
Contains("password")));
EXPECT_EQ(0u, table.records().size());
EXPECT_EQ(seq.begin(), seq.end());
}
TEST(PasswordCSVWriterTest, SerializePasswords_SinglePassword) {
......@@ -38,18 +47,15 @@ TEST(PasswordCSVWriterTest, SerializePasswords_SinglePassword) {
form.password_value = base::UTF8ToUTF16("Secret");
passwords.push_back(std::make_unique<PasswordForm>(form));
CSVTable table;
ASSERT_TRUE(table.ReadCSV(PasswordCSVWriter::SerializePasswords(passwords)));
EXPECT_THAT(table.column_names(), AllOf(Contains("url"), Contains("username"),
Contains("password")));
EXPECT_EQ(1u, table.records().size());
EXPECT_THAT(table.records()[0],
Contains(std::make_pair("url", "http://example.com/")));
EXPECT_THAT(table.records()[0],
Contains(std::make_pair("username", "Someone")));
EXPECT_THAT(table.records()[0],
Contains(std::make_pair("password", "Secret")));
CSVPasswordSequence seq(PasswordCSVWriter::SerializePasswords(passwords));
ASSERT_EQ(CSVPassword::Status::kOK, seq.result());
std::vector<PasswordForm> pwds;
for (const auto& pwd : seq) {
pwds.push_back(pwd.ParseValid());
}
EXPECT_THAT(pwds, ElementsAre(FormHasOriginUsernamePassword(
"http://example.com/", "Someone", "Secret")));
}
TEST(PasswordCSVWriterTest, SerializePasswords_TwoPasswords) {
......@@ -64,23 +70,17 @@ TEST(PasswordCSVWriterTest, SerializePasswords_TwoPasswords) {
form.password_value = base::UTF8ToUTF16("None");
passwords.push_back(std::make_unique<PasswordForm>(form));
CSVTable table;
ASSERT_TRUE(table.ReadCSV(PasswordCSVWriter::SerializePasswords(passwords)));
EXPECT_THAT(table.column_names(), AllOf(Contains("url"), Contains("username"),
Contains("password")));
EXPECT_EQ(2u, table.records().size());
EXPECT_THAT(table.records()[0],
Contains(std::make_pair("url", "http://example.com/")));
EXPECT_THAT(table.records()[0],
Contains(std::make_pair("username", "Someone")));
EXPECT_THAT(table.records()[0],
Contains(std::make_pair("password", "Secret")));
EXPECT_THAT(table.records()[1],
Contains(std::make_pair("url", "http://other.org/")));
EXPECT_THAT(table.records()[1],
Contains(std::make_pair("username", "Anyone")));
EXPECT_THAT(table.records()[1], Contains(std::make_pair("password", "None")));
CSVPasswordSequence seq(PasswordCSVWriter::SerializePasswords(passwords));
ASSERT_EQ(CSVPassword::Status::kOK, seq.result());
std::vector<PasswordForm> pwds;
for (const auto& pwd : seq) {
pwds.push_back(pwd.ParseValid());
}
EXPECT_THAT(pwds, ElementsAre(FormHasOriginUsernamePassword(
"http://example.com/", "Someone", "Secret"),
FormHasOriginUsernamePassword(
"http://other.org/", "Anyone", "None")));
}
} // namespace password_manager
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment