1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/invalidation/impl/p2p_invalidator.h"
#include <stddef.h>
#include <cstddef>
#include <memory>
#include "base/memory/ptr_util.h"
#include "components/invalidation/impl/fake_invalidation_handler.h"
#include "components/invalidation/impl/invalidator_test_template.h"
#include "components/invalidation/impl/notifier_reason_util.h"
#include "components/invalidation/public/invalidator_state.h"
#include "jingle/notifier/listener/fake_push_client.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace syncer {
namespace {
class P2PInvalidatorTestDelegate {
public:
P2PInvalidatorTestDelegate() : fake_push_client_(NULL) {}
~P2PInvalidatorTestDelegate() {
DestroyInvalidator();
}
void CreateInvalidator(
const std::string& invalidator_client_id,
const std::string& initial_state,
const base::WeakPtr<InvalidationStateTracker>&
invalidation_state_tracker) {
DCHECK(!fake_push_client_);
DCHECK(!invalidator_.get());
fake_push_client_ = new notifier::FakePushClient();
invalidator_.reset(new P2PInvalidator(base::WrapUnique(fake_push_client_),
invalidator_client_id,
NOTIFY_OTHERS));
}
P2PInvalidator* GetInvalidator() {
return invalidator_.get();
}
notifier::FakePushClient* GetPushClient() {
return fake_push_client_;
}
void DestroyInvalidator() {
invalidator_.reset();
fake_push_client_ = NULL;
}
void WaitForInvalidator() {
// Do Nothing.
}
void TriggerOnInvalidatorStateChange(InvalidatorState state) {
if (state == INVALIDATIONS_ENABLED) {
fake_push_client_->EnableNotifications();
} else {
fake_push_client_->DisableNotifications(ToNotifierReasonForTest(state));
}
}
void TriggerOnIncomingInvalidation(
const ObjectIdInvalidationMap& invalidation_map) {
const P2PNotificationData notification_data(
std::string(), NOTIFY_ALL, invalidation_map);
notifier::Notification notification;
notification.channel = kSyncP2PNotificationChannel;
notification.data = notification_data.ToString();
fake_push_client_->SimulateIncomingNotification(notification);
}
private:
// Owned by |invalidator_|.
notifier::FakePushClient* fake_push_client_;
std::unique_ptr<P2PInvalidator> invalidator_;
};
class P2PInvalidatorTest : public testing::Test {
protected:
P2PInvalidatorTest()
: next_sent_notification_to_reflect_(0) {
default_enabled_ids_.insert(invalidation::ObjectId(10, "A"));
default_enabled_ids_.insert(invalidation::ObjectId(10, "B"));
delegate_.CreateInvalidator("sender",
"fake_state",
base::WeakPtr<InvalidationStateTracker>());
delegate_.GetInvalidator()->RegisterHandler(&fake_handler_);
}
~P2PInvalidatorTest() override {
delegate_.GetInvalidator()->UnregisterHandler(&fake_handler_);
}
ObjectIdInvalidationMap MakeInvalidationMap(ObjectIdSet ids) {
return ObjectIdInvalidationMap::InvalidateAll(ids);
}
// Simulate receiving all the notifications we sent out since last
// time this was called.
void ReflectSentNotifications() {
const std::vector<notifier::Notification>& sent_notifications =
delegate_.GetPushClient()->sent_notifications();
for(size_t i = next_sent_notification_to_reflect_;
i < sent_notifications.size(); ++i) {
delegate_.GetInvalidator()->OnIncomingNotification(sent_notifications[i]);
}
next_sent_notification_to_reflect_ = sent_notifications.size();
}
ObjectIdSet default_enabled_ids_;
FakeInvalidationHandler fake_handler_;
P2PInvalidatorTestDelegate delegate_;
private:
size_t next_sent_notification_to_reflect_;
};
// Make sure the P2PNotificationTarget <-> string conversions work.
TEST_F(P2PInvalidatorTest, P2PNotificationTarget) {
for (int i = FIRST_NOTIFICATION_TARGET;
i <= LAST_NOTIFICATION_TARGET; ++i) {
P2PNotificationTarget target = static_cast<P2PNotificationTarget>(i);
const std::string& target_str = P2PNotificationTargetToString(target);
EXPECT_FALSE(target_str.empty());
EXPECT_EQ(target, P2PNotificationTargetFromString(target_str));
}
EXPECT_EQ(NOTIFY_SELF, P2PNotificationTargetFromString("unknown"));
}
// Make sure notification targeting works correctly.
TEST_F(P2PInvalidatorTest, P2PNotificationDataIsTargeted) {
{
const P2PNotificationData notification_data(
"sender", NOTIFY_SELF, ObjectIdInvalidationMap());
EXPECT_TRUE(notification_data.IsTargeted("sender"));
EXPECT_FALSE(notification_data.IsTargeted("other1"));
EXPECT_FALSE(notification_data.IsTargeted("other2"));
}
{
const P2PNotificationData notification_data(
"sender", NOTIFY_OTHERS, ObjectIdInvalidationMap());
EXPECT_FALSE(notification_data.IsTargeted("sender"));
EXPECT_TRUE(notification_data.IsTargeted("other1"));
EXPECT_TRUE(notification_data.IsTargeted("other2"));
}
{
const P2PNotificationData notification_data(
"sender", NOTIFY_ALL, ObjectIdInvalidationMap());
EXPECT_TRUE(notification_data.IsTargeted("sender"));
EXPECT_TRUE(notification_data.IsTargeted("other1"));
EXPECT_TRUE(notification_data.IsTargeted("other2"));
}
}
// Make sure the P2PNotificationData <-> string conversions work for a
// default-constructed P2PNotificationData.
TEST_F(P2PInvalidatorTest, P2PNotificationDataDefault) {
const P2PNotificationData notification_data;
EXPECT_TRUE(notification_data.IsTargeted(std::string()));
EXPECT_FALSE(notification_data.IsTargeted("other1"));
EXPECT_FALSE(notification_data.IsTargeted("other2"));
EXPECT_TRUE(notification_data.GetIdInvalidationMap().Empty());
const std::string& notification_data_str = notification_data.ToString();
EXPECT_EQ(
"{\"invalidations\":[],\"notificationType\":\"notifySelf\","
"\"senderId\":\"\"}", notification_data_str);
P2PNotificationData notification_data_parsed;
EXPECT_TRUE(notification_data_parsed.ResetFromString(notification_data_str));
EXPECT_TRUE(notification_data.Equals(notification_data_parsed));
}
// Make sure the P2PNotificationData <-> string conversions work for a
// non-default-constructed P2PNotificationData.
TEST_F(P2PInvalidatorTest, P2PNotificationDataNonDefault) {
ObjectIdInvalidationMap invalidation_map =
ObjectIdInvalidationMap::InvalidateAll(default_enabled_ids_);
const P2PNotificationData notification_data("sender",
NOTIFY_ALL,
invalidation_map);
EXPECT_TRUE(notification_data.IsTargeted("sender"));
EXPECT_TRUE(notification_data.IsTargeted("other1"));
EXPECT_TRUE(notification_data.IsTargeted("other2"));
EXPECT_EQ(invalidation_map, notification_data.GetIdInvalidationMap());
const std::string& notification_data_str = notification_data.ToString();
EXPECT_EQ(
"{\"invalidations\":["
"{\"isUnknownVersion\":true,"
"\"objectId\":{\"name\":\"A\",\"source\":10}},"
"{\"isUnknownVersion\":true,"
"\"objectId\":{\"name\":\"B\",\"source\":10}}"
"],\"notificationType\":\"notifyAll\","
"\"senderId\":\"sender\"}",
notification_data_str);
P2PNotificationData notification_data_parsed;
EXPECT_TRUE(notification_data_parsed.ResetFromString(notification_data_str));
EXPECT_TRUE(notification_data.Equals(notification_data_parsed));
}
// Set up the P2PInvalidator, simulate a successful connection, and send
// a notification with the default target (NOTIFY_OTHERS). The
// observer should receive only a notification from the call to
// UpdateEnabledTypes().
TEST_F(P2PInvalidatorTest, NotificationsBasic) {
P2PInvalidator* const invalidator = delegate_.GetInvalidator();
notifier::FakePushClient* const push_client = delegate_.GetPushClient();
invalidator->UpdateRegisteredIds(&fake_handler_, default_enabled_ids_);
const char kEmail[] = "foo@bar.com";
const char kToken[] = "token";
invalidator->UpdateCredentials(kEmail, kToken);
{
notifier::Subscription expected_subscription;
expected_subscription.channel = kSyncP2PNotificationChannel;
expected_subscription.from = kEmail;
EXPECT_TRUE(notifier::SubscriptionListsEqual(
push_client->subscriptions(),
notifier::SubscriptionList(1, expected_subscription)));
}
EXPECT_EQ(kEmail, push_client->email());
EXPECT_EQ(kToken, push_client->token());
ReflectSentNotifications();
push_client->EnableNotifications();
EXPECT_EQ(INVALIDATIONS_ENABLED, fake_handler_.GetInvalidatorState());
ReflectSentNotifications();
EXPECT_EQ(1, fake_handler_.GetInvalidationCount());
EXPECT_THAT(MakeInvalidationMap(default_enabled_ids_),
Eq(fake_handler_.GetLastInvalidationMap()));
// Sent with target NOTIFY_OTHERS so should not be propagated to
// |fake_handler_|.
invalidator->SendInvalidation(default_enabled_ids_);
ReflectSentNotifications();
EXPECT_EQ(1, fake_handler_.GetInvalidationCount());
}
// Set up the P2PInvalidator and send out notifications with various
// target settings. The notifications received by the observer should
// be consistent with the target settings.
TEST_F(P2PInvalidatorTest, SendNotificationData) {
ObjectIdSet enabled_ids;
ObjectIdSet changed_ids;
ObjectIdSet expected_ids;
enabled_ids.insert(invalidation::ObjectId(20, "A"));
enabled_ids.insert(invalidation::ObjectId(20, "B"));
enabled_ids.insert(invalidation::ObjectId(20, "C"));
changed_ids.insert(invalidation::ObjectId(20, "A"));
changed_ids.insert(invalidation::ObjectId(20, "Z"));
expected_ids.insert(invalidation::ObjectId(20, "A"));
const ObjectIdInvalidationMap& invalidation_map =
MakeInvalidationMap(changed_ids);
P2PInvalidator* const invalidator = delegate_.GetInvalidator();
notifier::FakePushClient* const push_client = delegate_.GetPushClient();
invalidator->UpdateRegisteredIds(&fake_handler_, enabled_ids);
invalidator->UpdateCredentials("foo@bar.com", "fake_token");
ReflectSentNotifications();
push_client->EnableNotifications();
EXPECT_EQ(INVALIDATIONS_ENABLED, fake_handler_.GetInvalidatorState());
ReflectSentNotifications();
EXPECT_EQ(1, fake_handler_.GetInvalidationCount());
EXPECT_EQ(enabled_ids, fake_handler_.GetLastInvalidationMap().GetObjectIds());
// Should be dropped.
invalidator->SendNotificationDataForTest(P2PNotificationData());
ReflectSentNotifications();
EXPECT_EQ(1, fake_handler_.GetInvalidationCount());
// Should be propagated.
invalidator->SendNotificationDataForTest(
P2PNotificationData("sender", NOTIFY_SELF, invalidation_map));
ReflectSentNotifications();
EXPECT_EQ(2, fake_handler_.GetInvalidationCount());
EXPECT_EQ(expected_ids,
fake_handler_.GetLastInvalidationMap().GetObjectIds());
// Should be dropped.
invalidator->SendNotificationDataForTest(
P2PNotificationData("sender2", NOTIFY_SELF, invalidation_map));
ReflectSentNotifications();
EXPECT_EQ(2, fake_handler_.GetInvalidationCount());
// Should be dropped.
invalidator->SendNotificationDataForTest(
P2PNotificationData("sender", NOTIFY_SELF, ObjectIdInvalidationMap()));
ReflectSentNotifications();
EXPECT_EQ(2, fake_handler_.GetInvalidationCount());
// Should be dropped.
invalidator->SendNotificationDataForTest(
P2PNotificationData("sender", NOTIFY_OTHERS, invalidation_map));
ReflectSentNotifications();
EXPECT_EQ(2, fake_handler_.GetInvalidationCount());
// Should be propagated.
invalidator->SendNotificationDataForTest(
P2PNotificationData("sender2", NOTIFY_OTHERS, invalidation_map));
ReflectSentNotifications();
EXPECT_EQ(3, fake_handler_.GetInvalidationCount());
EXPECT_EQ(expected_ids,
fake_handler_.GetLastInvalidationMap().GetObjectIds());
// Should be dropped.
invalidator->SendNotificationDataForTest(
P2PNotificationData("sender2", NOTIFY_OTHERS, ObjectIdInvalidationMap()));
ReflectSentNotifications();
EXPECT_EQ(3, fake_handler_.GetInvalidationCount());
// Should be propagated.
invalidator->SendNotificationDataForTest(
P2PNotificationData("sender", NOTIFY_ALL, invalidation_map));
ReflectSentNotifications();
EXPECT_EQ(4, fake_handler_.GetInvalidationCount());
EXPECT_EQ(expected_ids,
fake_handler_.GetLastInvalidationMap().GetObjectIds());
// Should be propagated.
invalidator->SendNotificationDataForTest(
P2PNotificationData("sender2", NOTIFY_ALL, invalidation_map));
ReflectSentNotifications();
EXPECT_EQ(5, fake_handler_.GetInvalidationCount());
EXPECT_EQ(expected_ids,
fake_handler_.GetLastInvalidationMap().GetObjectIds());
// Should be dropped.
invalidator->SendNotificationDataForTest(
P2PNotificationData("sender2", NOTIFY_ALL, ObjectIdInvalidationMap()));
ReflectSentNotifications();
EXPECT_EQ(5, fake_handler_.GetInvalidationCount());
}
INSTANTIATE_TYPED_TEST_CASE_P(
P2PInvalidatorTest, InvalidatorTest,
P2PInvalidatorTestDelegate);
} // namespace
} // namespace syncer
|