| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012 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 "base/format_macros.h" |
| 6 #include "base/location.h" |
| 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/stringprintf.h" |
| 9 #include "sync/engine/apply_control_data_updates.h" |
| 10 #include "sync/engine/syncer.h" |
| 11 #include "sync/engine/syncer_util.h" |
| 12 #include "sync/internal_api/public/test/test_entry_factory.h" |
| 13 #include "sync/protocol/nigori_specifics.pb.h" |
| 14 #include "sync/syncable/mutable_entry.h" |
| 15 #include "sync/syncable/nigori_util.h" |
| 16 #include "sync/syncable/read_transaction.h" |
| 17 #include "sync/syncable/syncable_util.h" |
| 18 #include "sync/syncable/write_transaction.h" |
| 19 #include "sync/test/engine/fake_model_worker.h" |
| 20 #include "sync/test/engine/syncer_command_test.h" |
| 21 #include "sync/test/engine/test_id_factory.h" |
| 22 #include "sync/test/fake_sync_encryption_handler.h" |
| 23 #include "sync/util/cryptographer.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" |
| 25 |
| 26 namespace syncer { |
| 27 |
| 28 using syncable::MutableEntry; |
| 29 using syncable::UNITTEST; |
| 30 using syncable::Id; |
| 31 |
| 32 class ApplyControlDataUpdatesTest : public SyncerCommandTest { |
| 33 public: |
| 34 protected: |
| 35 ApplyControlDataUpdatesTest() {} |
| 36 virtual ~ApplyControlDataUpdatesTest() {} |
| 37 |
| 38 virtual void SetUp() { |
| 39 workers()->clear(); |
| 40 mutable_routing_info()->clear(); |
| 41 workers()->push_back(make_scoped_refptr(new FakeModelWorker(GROUP_UI))); |
| 42 workers()->push_back( |
| 43 make_scoped_refptr(new FakeModelWorker(GROUP_PASSWORD))); |
| 44 (*mutable_routing_info())[BOOKMARKS] = GROUP_UI; |
| 45 (*mutable_routing_info())[PASSWORDS] = GROUP_PASSWORD; |
| 46 (*mutable_routing_info())[NIGORI] = GROUP_PASSIVE; |
| 47 SyncerCommandTest::SetUp(); |
| 48 entry_factory_.reset(new TestEntryFactory(directory())); |
| 49 |
| 50 syncable::ReadTransaction trans(FROM_HERE, directory()); |
| 51 } |
| 52 |
| 53 TestIdFactory id_factory_; |
| 54 scoped_ptr<TestEntryFactory> entry_factory_; |
| 55 private: |
| 56 DISALLOW_COPY_AND_ASSIGN(ApplyControlDataUpdatesTest); |
| 57 }; |
| 58 |
| 59 TEST_F(ApplyControlDataUpdatesTest, NigoriUpdate) { |
| 60 // Storing the cryptographer separately is bad, but for this test we |
| 61 // know it's safe. |
| 62 Cryptographer* cryptographer; |
| 63 ModelTypeSet encrypted_types; |
| 64 encrypted_types.Put(PASSWORDS); |
| 65 |
| 66 // We start with initial_sync_ended == false. This is wrong, since would have |
| 67 // no nigori node if that were the case. Howerver, it makes it easier to |
| 68 // verify that ApplyControlDataUpdates sets initial_sync_ended correctly. |
| 69 EXPECT_FALSE(directory()->initial_sync_ended_types().Has(NIGORI)); |
| 70 |
| 71 { |
| 72 syncable::ReadTransaction trans(FROM_HERE, directory()); |
| 73 cryptographer = directory()->GetCryptographer(&trans); |
| 74 EXPECT_TRUE(directory()->GetNigoriHandler()->GetEncryptedTypes(&trans) |
| 75 .Equals(encrypted_types)); |
| 76 } |
| 77 |
| 78 // Nigori node updates should update the Cryptographer. |
| 79 Cryptographer other_cryptographer(cryptographer->encryptor()); |
| 80 KeyParams params = {"localhost", "dummy", "foobar"}; |
| 81 other_cryptographer.AddKey(params); |
| 82 |
| 83 sync_pb::EntitySpecifics specifics; |
| 84 sync_pb::NigoriSpecifics* nigori = specifics.mutable_nigori(); |
| 85 other_cryptographer.GetKeys(nigori->mutable_encrypted()); |
| 86 nigori->set_encrypt_everything(true); |
| 87 entry_factory_->CreateUnappliedNewItem( |
| 88 ModelTypeToRootTag(NIGORI), specifics, true); |
| 89 EXPECT_FALSE(cryptographer->has_pending_keys()); |
| 90 |
| 91 ApplyControlDataUpdates(directory()); |
| 92 |
| 93 EXPECT_FALSE(cryptographer->is_ready()); |
| 94 EXPECT_TRUE(cryptographer->has_pending_keys()); |
| 95 { |
| 96 syncable::ReadTransaction trans(FROM_HERE, directory()); |
| 97 EXPECT_TRUE(directory()->GetNigoriHandler()->GetEncryptedTypes(&trans) |
| 98 .Equals(ModelTypeSet::All())); |
| 99 EXPECT_TRUE(directory()->initial_sync_ended_types().Has(NIGORI)); |
| 100 } |
| 101 } |
| 102 |
| 103 TEST_F(ApplyControlDataUpdatesTest, NigoriUpdateForDisabledTypes) { |
| 104 // Storing the cryptographer separately is bad, but for this test we |
| 105 // know it's safe. |
| 106 Cryptographer* cryptographer; |
| 107 ModelTypeSet encrypted_types; |
| 108 encrypted_types.Put(PASSWORDS); |
| 109 |
| 110 // We start with initial_sync_ended == false. This is wrong, since would have |
| 111 // no nigori node if that were the case. Howerver, it makes it easier to |
| 112 // verify that ApplyControlDataUpdates sets initial_sync_ended correctly. |
| 113 EXPECT_FALSE(directory()->initial_sync_ended_types().Has(NIGORI)); |
| 114 |
| 115 { |
| 116 syncable::ReadTransaction trans(FROM_HERE, directory()); |
| 117 cryptographer = directory()->GetCryptographer(&trans); |
| 118 EXPECT_TRUE(directory()->GetNigoriHandler()->GetEncryptedTypes(&trans) |
| 119 .Equals(encrypted_types)); |
| 120 } |
| 121 |
| 122 // Nigori node updates should update the Cryptographer. |
| 123 Cryptographer other_cryptographer(cryptographer->encryptor()); |
| 124 KeyParams params = {"localhost", "dummy", "foobar"}; |
| 125 other_cryptographer.AddKey(params); |
| 126 |
| 127 sync_pb::EntitySpecifics specifics; |
| 128 sync_pb::NigoriSpecifics* nigori = specifics.mutable_nigori(); |
| 129 other_cryptographer.GetKeys(nigori->mutable_encrypted()); |
| 130 nigori->set_encrypt_everything(true); |
| 131 entry_factory_->CreateUnappliedNewItem( |
| 132 ModelTypeToRootTag(NIGORI), specifics, true); |
| 133 EXPECT_FALSE(cryptographer->has_pending_keys()); |
| 134 |
| 135 ApplyControlDataUpdates(directory()); |
| 136 |
| 137 EXPECT_FALSE(cryptographer->is_ready()); |
| 138 EXPECT_TRUE(cryptographer->has_pending_keys()); |
| 139 { |
| 140 syncable::ReadTransaction trans(FROM_HERE, directory()); |
| 141 EXPECT_TRUE(directory()->GetNigoriHandler()->GetEncryptedTypes(&trans) |
| 142 .Equals(ModelTypeSet::All())); |
| 143 EXPECT_TRUE(directory()->initial_sync_ended_types().Has(NIGORI)); |
| 144 } |
| 145 } |
| 146 |
| 147 // Create some local unsynced and unencrypted data. Apply a nigori update that |
| 148 // turns on encryption for the unsynced data. Ensure we properly encrypt the |
| 149 // data as part of the nigori update. Apply another nigori update with no |
| 150 // changes. Ensure we ignore already-encrypted unsynced data and that nothing |
| 151 // breaks. |
| 152 TEST_F(ApplyControlDataUpdatesTest, EncryptUnsyncedChanges) { |
| 153 // Storing the cryptographer separately is bad, but for this test we |
| 154 // know it's safe. |
| 155 Cryptographer* cryptographer; |
| 156 ModelTypeSet encrypted_types; |
| 157 encrypted_types.Put(PASSWORDS); |
| 158 { |
| 159 syncable::ReadTransaction trans(FROM_HERE, directory()); |
| 160 cryptographer = directory()->GetCryptographer(&trans); |
| 161 EXPECT_TRUE(directory()->GetNigoriHandler()->GetEncryptedTypes(&trans) |
| 162 .Equals(encrypted_types)); |
| 163 |
| 164 // With default encrypted_types, this should be true. |
| 165 EXPECT_TRUE(VerifyUnsyncedChangesAreEncrypted(&trans, encrypted_types)); |
| 166 |
| 167 Syncer::UnsyncedMetaHandles handles; |
| 168 GetUnsyncedEntries(&trans, &handles); |
| 169 EXPECT_TRUE(handles.empty()); |
| 170 } |
| 171 |
| 172 // Create unsynced bookmarks without encryption. |
| 173 // First item is a folder |
| 174 Id folder_id = id_factory_.NewLocalId(); |
| 175 entry_factory_->CreateUnsyncedItem(folder_id, id_factory_.root(), "folder", |
| 176 true, BOOKMARKS, NULL); |
| 177 // Next five items are children of the folder |
| 178 size_t i; |
| 179 size_t batch_s = 5; |
| 180 for (i = 0; i < batch_s; ++i) { |
| 181 entry_factory_->CreateUnsyncedItem(id_factory_.NewLocalId(), folder_id, |
| 182 base::StringPrintf("Item %"PRIuS"", i), |
| 183 false, BOOKMARKS, NULL); |
| 184 } |
| 185 // Next five items are children of the root. |
| 186 for (; i < 2*batch_s; ++i) { |
| 187 entry_factory_->CreateUnsyncedItem( |
| 188 id_factory_.NewLocalId(), id_factory_.root(), |
| 189 base::StringPrintf("Item %"PRIuS"", i), false, |
| 190 BOOKMARKS, NULL); |
| 191 } |
| 192 |
| 193 KeyParams params = {"localhost", "dummy", "foobar"}; |
| 194 cryptographer->AddKey(params); |
| 195 sync_pb::EntitySpecifics specifics; |
| 196 sync_pb::NigoriSpecifics* nigori = specifics.mutable_nigori(); |
| 197 cryptographer->GetKeys(nigori->mutable_encrypted()); |
| 198 nigori->set_encrypt_everything(true); |
| 199 encrypted_types.Put(BOOKMARKS); |
| 200 entry_factory_->CreateUnappliedNewItem( |
| 201 ModelTypeToRootTag(NIGORI), specifics, true); |
| 202 EXPECT_FALSE(cryptographer->has_pending_keys()); |
| 203 EXPECT_TRUE(cryptographer->is_ready()); |
| 204 |
| 205 { |
| 206 // Ensure we have unsynced nodes that aren't properly encrypted. |
| 207 syncable::ReadTransaction trans(FROM_HERE, directory()); |
| 208 EXPECT_FALSE(VerifyUnsyncedChangesAreEncrypted(&trans, encrypted_types)); |
| 209 |
| 210 Syncer::UnsyncedMetaHandles handles; |
| 211 GetUnsyncedEntries(&trans, &handles); |
| 212 EXPECT_EQ(2*batch_s+1, handles.size()); |
| 213 } |
| 214 |
| 215 ApplyControlDataUpdates(directory()); |
| 216 |
| 217 EXPECT_FALSE(cryptographer->has_pending_keys()); |
| 218 EXPECT_TRUE(cryptographer->is_ready()); |
| 219 { |
| 220 syncable::ReadTransaction trans(FROM_HERE, directory()); |
| 221 |
| 222 // If ProcessUnsyncedChangesForEncryption worked, all our unsynced changes |
| 223 // should be encrypted now. |
| 224 EXPECT_TRUE(directory()->GetNigoriHandler()->GetEncryptedTypes(&trans) |
| 225 .Equals(ModelTypeSet::All())); |
| 226 EXPECT_TRUE(VerifyUnsyncedChangesAreEncrypted(&trans, encrypted_types)); |
| 227 |
| 228 Syncer::UnsyncedMetaHandles handles; |
| 229 GetUnsyncedEntries(&trans, &handles); |
| 230 EXPECT_EQ(2*batch_s+1, handles.size()); |
| 231 } |
| 232 |
| 233 // Simulate another nigori update that doesn't change anything. |
| 234 { |
| 235 syncable::WriteTransaction trans(FROM_HERE, UNITTEST, directory()); |
| 236 MutableEntry entry(&trans, syncable::GET_BY_SERVER_TAG, |
| 237 ModelTypeToRootTag(NIGORI)); |
| 238 ASSERT_TRUE(entry.good()); |
| 239 entry.Put(syncable::SERVER_VERSION, entry_factory_->GetNextRevision()); |
| 240 entry.Put(syncable::IS_UNAPPLIED_UPDATE, true); |
| 241 } |
| 242 |
| 243 ApplyControlDataUpdates(directory()); |
| 244 |
| 245 EXPECT_FALSE(cryptographer->has_pending_keys()); |
| 246 EXPECT_TRUE(cryptographer->is_ready()); |
| 247 { |
| 248 syncable::ReadTransaction trans(FROM_HERE, directory()); |
| 249 |
| 250 // All our changes should still be encrypted. |
| 251 EXPECT_TRUE(directory()->GetNigoriHandler()->GetEncryptedTypes(&trans) |
| 252 .Equals(ModelTypeSet::All())); |
| 253 EXPECT_TRUE(VerifyUnsyncedChangesAreEncrypted(&trans, encrypted_types)); |
| 254 |
| 255 Syncer::UnsyncedMetaHandles handles; |
| 256 GetUnsyncedEntries(&trans, &handles); |
| 257 EXPECT_EQ(2*batch_s+1, handles.size()); |
| 258 } |
| 259 } |
| 260 |
| 261 TEST_F(ApplyControlDataUpdatesTest, CannotEncryptUnsyncedChanges) { |
| 262 // Storing the cryptographer separately is bad, but for this test we |
| 263 // know it's safe. |
| 264 Cryptographer* cryptographer; |
| 265 ModelTypeSet encrypted_types; |
| 266 encrypted_types.Put(PASSWORDS); |
| 267 { |
| 268 syncable::ReadTransaction trans(FROM_HERE, directory()); |
| 269 cryptographer = directory()->GetCryptographer(&trans); |
| 270 EXPECT_TRUE(directory()->GetNigoriHandler()->GetEncryptedTypes(&trans) |
| 271 .Equals(encrypted_types)); |
| 272 |
| 273 // With default encrypted_types, this should be true. |
| 274 EXPECT_TRUE(VerifyUnsyncedChangesAreEncrypted(&trans, encrypted_types)); |
| 275 |
| 276 Syncer::UnsyncedMetaHandles handles; |
| 277 GetUnsyncedEntries(&trans, &handles); |
| 278 EXPECT_TRUE(handles.empty()); |
| 279 } |
| 280 |
| 281 // Create unsynced bookmarks without encryption. |
| 282 // First item is a folder |
| 283 Id folder_id = id_factory_.NewLocalId(); |
| 284 entry_factory_->CreateUnsyncedItem( |
| 285 folder_id, id_factory_.root(), "folder", true, |
| 286 BOOKMARKS, NULL); |
| 287 // Next five items are children of the folder |
| 288 size_t i; |
| 289 size_t batch_s = 5; |
| 290 for (i = 0; i < batch_s; ++i) { |
| 291 entry_factory_->CreateUnsyncedItem(id_factory_.NewLocalId(), folder_id, |
| 292 base::StringPrintf("Item %"PRIuS"", i), |
| 293 false, BOOKMARKS, NULL); |
| 294 } |
| 295 // Next five items are children of the root. |
| 296 for (; i < 2*batch_s; ++i) { |
| 297 entry_factory_->CreateUnsyncedItem( |
| 298 id_factory_.NewLocalId(), id_factory_.root(), |
| 299 base::StringPrintf("Item %"PRIuS"", i), false, |
| 300 BOOKMARKS, NULL); |
| 301 } |
| 302 |
| 303 // We encrypt with new keys, triggering the local cryptographer to be unready |
| 304 // and unable to decrypt data (once updated). |
| 305 Cryptographer other_cryptographer(cryptographer->encryptor()); |
| 306 KeyParams params = {"localhost", "dummy", "foobar"}; |
| 307 other_cryptographer.AddKey(params); |
| 308 sync_pb::EntitySpecifics specifics; |
| 309 sync_pb::NigoriSpecifics* nigori = specifics.mutable_nigori(); |
| 310 other_cryptographer.GetKeys(nigori->mutable_encrypted()); |
| 311 nigori->set_encrypt_everything(true); |
| 312 encrypted_types.Put(BOOKMARKS); |
| 313 entry_factory_->CreateUnappliedNewItem( |
| 314 ModelTypeToRootTag(NIGORI), specifics, true); |
| 315 EXPECT_FALSE(cryptographer->has_pending_keys()); |
| 316 |
| 317 { |
| 318 // Ensure we have unsynced nodes that aren't properly encrypted. |
| 319 syncable::ReadTransaction trans(FROM_HERE, directory()); |
| 320 EXPECT_FALSE(VerifyUnsyncedChangesAreEncrypted(&trans, encrypted_types)); |
| 321 Syncer::UnsyncedMetaHandles handles; |
| 322 GetUnsyncedEntries(&trans, &handles); |
| 323 EXPECT_EQ(2*batch_s+1, handles.size()); |
| 324 } |
| 325 |
| 326 ApplyControlDataUpdates(directory()); |
| 327 |
| 328 EXPECT_FALSE(cryptographer->is_ready()); |
| 329 EXPECT_TRUE(cryptographer->has_pending_keys()); |
| 330 { |
| 331 syncable::ReadTransaction trans(FROM_HERE, directory()); |
| 332 |
| 333 // Since we have pending keys, we would have failed to encrypt, but the |
| 334 // cryptographer should be updated. |
| 335 EXPECT_FALSE(VerifyUnsyncedChangesAreEncrypted(&trans, encrypted_types)); |
| 336 EXPECT_TRUE(directory()->GetNigoriHandler()->GetEncryptedTypes(&trans) |
| 337 .Equals(ModelTypeSet::All())); |
| 338 EXPECT_FALSE(cryptographer->is_ready()); |
| 339 EXPECT_TRUE(cryptographer->has_pending_keys()); |
| 340 |
| 341 Syncer::UnsyncedMetaHandles handles; |
| 342 GetUnsyncedEntries(&trans, &handles); |
| 343 EXPECT_EQ(2*batch_s+1, handles.size()); |
| 344 } |
| 345 } |
| 346 |
| 347 } // namespace syncer |
| OLD | NEW |