Commit 771dd2a0 authored by Mohamed Amir Yosef's avatar Mohamed Amir Yosef Committed by Commit Bot

[Sync::USS] Implement PasswordSyncBridge::GetData()

Bug: 902349
Change-Id: I14ca95a063835d72ca48d9c49c4a45f816e5fb55
Reviewed-on: https://chromium-review.googlesource.com/c/1424881
Commit-Queue: Mohamed Amir Yosef <mamir@chromium.org>
Reviewed-by: default avatarMohamed Amir Yosef <mamir@chromium.org>
Reviewed-by: default avatarMikel Astiz <mastiz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#626097}
parent 12445b71
......@@ -12,6 +12,7 @@
#include "components/password_manager/core/browser/password_store_sync.h"
#include "components/sync/model/metadata_change_list.h"
#include "components/sync/model/model_type_change_processor.h"
#include "components/sync/model/mutable_data_batch.h"
#include "components/sync/model_impl/in_memory_metadata_change_list.h"
#include "components/sync/model_impl/sync_metadata_store_change_list.h"
#include "net/base/escape.h"
......@@ -280,7 +281,24 @@ base::Optional<syncer::ModelError> PasswordSyncBridge::ApplySyncChanges(
void PasswordSyncBridge::GetData(StorageKeyList storage_keys,
DataCallback callback) {
NOTIMPLEMENTED();
// This method is called only when there are uncommitted changes on startup.
// There are more efficient implementations, but since this method is rarely
// called, simplicity is preferred over efficiency.
PrimaryKeyToFormMap key_to_form_map;
if (!password_store_sync_->ReadAllLogins(&key_to_form_map)) {
change_processor()->ReportError(
{FROM_HERE, "Failed to load entries from table."});
return;
}
auto batch = std::make_unique<syncer::MutableDataBatch>();
for (const std::string& storage_key : storage_keys) {
int primary_key = ParsePrimaryKey(storage_key);
if (key_to_form_map.count(primary_key) != 0) {
batch->Put(storage_key, CreateEntityData(*key_to_form_map[primary_key]));
}
}
std::move(callback).Run(std::move(batch));
}
void PasswordSyncBridge::GetAllDataForDebugging(DataCallback callback) {
......
......@@ -8,8 +8,11 @@
#include <string>
#include <utility>
#include "base/bind.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/bind_test_util.h"
#include "components/password_manager/core/browser/password_store_sync.h"
#include "components/sync/model/data_batch.h"
#include "components/sync/model/entity_change.h"
#include "components/sync/model/metadata_batch.h"
#include "components/sync/model/mock_model_type_change_processor.h"
......@@ -25,6 +28,7 @@ namespace {
using testing::_;
using testing::Eq;
using testing::Invoke;
using testing::NotNull;
using testing::Return;
constexpr char kSignonRealm1[] = "abc";
......@@ -238,6 +242,24 @@ class PasswordSyncBridgeTest : public testing::Test {
~PasswordSyncBridgeTest() override {}
base::Optional<sync_pb::PasswordSpecifics> GetDataFromBridge(
const std::string& storage_key) {
std::unique_ptr<syncer::DataBatch> batch;
bridge_.GetData({storage_key},
base::BindLambdaForTesting(
[&](std::unique_ptr<syncer::DataBatch> in_batch) {
batch = std::move(in_batch);
}));
EXPECT_THAT(batch, NotNull());
if (!batch || !batch->HasNext()) {
return base::nullopt;
}
const syncer::KeyAndData& data_pair = batch->Next();
EXPECT_THAT(data_pair.first, Eq(storage_key));
EXPECT_FALSE(batch->HasNext());
return data_pair.second->specifics.password();
}
FakeDatabase* fake_db() { return &fake_db_; }
PasswordSyncBridge* bridge() { return &bridge_; }
......@@ -422,4 +444,36 @@ TEST_F(PasswordSyncBridgeTest, ShouldApplyRemoteDeletion) {
EXPECT_FALSE(error);
}
TEST_F(PasswordSyncBridgeTest, ShouldGetDataForStorageKey) {
const int kPrimaryKey1 = 1000;
const int kPrimaryKey2 = 1001;
const std::string kPrimaryKeyStr1 = "1000";
const std::string kPrimaryKeyStr2 = "1001";
autofill::PasswordForm form1 = MakePasswordForm(kSignonRealm1);
autofill::PasswordForm form2 = MakePasswordForm(kSignonRealm2);
fake_db()->AddLoginForPrimaryKey(kPrimaryKey1, form1);
fake_db()->AddLoginForPrimaryKey(kPrimaryKey2, form2);
base::Optional<sync_pb::PasswordSpecifics> optional_specifics =
GetDataFromBridge(/*storage_key=*/kPrimaryKeyStr1);
ASSERT_TRUE(optional_specifics.has_value());
EXPECT_EQ(
kSignonRealm1,
optional_specifics.value().client_only_encrypted_data().signon_realm());
optional_specifics = GetDataFromBridge(/*storage_key=*/kPrimaryKeyStr2);
ASSERT_TRUE(optional_specifics.has_value());
EXPECT_EQ(kSignonRealm2,
optional_specifics->client_only_encrypted_data().signon_realm());
}
TEST_F(PasswordSyncBridgeTest, ShouldNotGetDataForNonExistingStorageKey) {
const std::string kPrimaryKeyStr = "1";
base::Optional<sync_pb::PasswordSpecifics> optional_specifics =
GetDataFromBridge(/*storage_key=*/kPrimaryKeyStr);
EXPECT_FALSE(optional_specifics.has_value());
}
} // 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