Chromium Code Reviews| 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/files/file_path.h" | |
| 6 #include "base/files/file_util.h" | |
| 7 #include "base/memory/ptr_util.h" | |
| 8 #include "base/message_loop/message_loop.h" | |
| 9 #include "components/sync/base/cancelation_signal.h" | |
| 10 #include "components/sync/engine_impl/loopback_server/loopback_connection_manage r.h" | |
| 11 #include "components/sync/engine_impl/syncer_proto_util.h" | |
| 12 #include "components/sync/protocol/sync.pb.h" | |
| 13 #include "components/sync/protocol/sync_enums.pb.h" | |
| 14 #include "components/sync/syncable/directory.h" | |
| 15 #include "components/sync/test/engine/test_directory_setter_upper.h" | |
| 16 #include "testing/gtest/include/gtest/gtest.h" | |
| 17 | |
| 18 using sync_pb::ClientToServerMessage; | |
| 19 | |
| 20 namespace syncer { | |
| 21 | |
| 22 class LoopbackServerTest : public testing::Test { | |
| 23 public: | |
| 24 void SetUp() override { | |
| 25 dir_maker_.SetUp(); | |
| 26 base::CreateTemporaryFile(&persistent_file_); | |
| 27 lcm_ = | |
| 28 base::MakeUnique<LoopbackConnectionManager>(&signal_, persistent_file_); | |
| 29 } | |
| 30 | |
| 31 void TearDown() override { dir_maker_.TearDown(); } | |
| 32 | |
| 33 syncable::Directory* directory() { return dir_maker_.directory(); } | |
| 34 | |
| 35 // Helper functions to call GetProtocolErrorFromResponse. Allows not adding | |
| 36 // individual tests as friends to SyncerProtoUtil. | |
| 37 static SyncProtocolError CallGetProtocolErrorFromResponse( | |
|
pavely
2016/10/06 23:48:45
You don't use this function right now, Do you have
pastarmovj
2016/10/13 14:13:40
I was planning to but for not I don't need it so r
| |
| 38 const sync_pb::ClientToServerResponse& response, | |
| 39 syncable::Directory* directory) { | |
| 40 return SyncerProtoUtil::GetProtocolErrorFromResponse(response, directory); | |
| 41 } | |
| 42 | |
| 43 static bool CallPostAndProcessHeaders( | |
| 44 ServerConnectionManager* scm, | |
| 45 SyncCycle* cycle, | |
| 46 const sync_pb::ClientToServerMessage& msg, | |
| 47 sync_pb::ClientToServerResponse* response) { | |
| 48 return SyncerProtoUtil::PostAndProcessHeaders(scm, cycle, msg, response); | |
| 49 } | |
| 50 | |
| 51 protected: | |
| 52 base::MessageLoop message_loop_; | |
| 53 TestDirectorySetterUpper dir_maker_; | |
|
pavely
2016/10/06 23:48:45
Why do you need directory and message loop here?
pastarmovj
2016/10/13 14:13:40
Leftovers :) Removed.
| |
| 54 CancelationSignal signal_; | |
| 55 base::FilePath persistent_file_; | |
| 56 std::unique_ptr<LoopbackConnectionManager> lcm_; | |
| 57 }; | |
| 58 | |
| 59 TEST_F(LoopbackServerTest, WrongBirthday) { | |
| 60 ClientToServerMessage msg; | |
| 61 SyncerProtoUtil::SetProtocolVersion(&msg); | |
| 62 msg.set_share("required"); | |
| 63 msg.set_store_birthday("not_your_birthday"); | |
| 64 msg.set_message_contents(ClientToServerMessage::GET_UPDATES); | |
| 65 msg.mutable_get_updates()->add_from_progress_marker()->set_data_type_id( | |
| 66 sync_pb::EntitySpecifics::kBookmarkFieldNumber); | |
| 67 sync_pb::ClientToServerResponse response; | |
| 68 | |
| 69 EXPECT_TRUE(CallPostAndProcessHeaders(lcm_.get(), NULL, msg, &response)); | |
| 70 EXPECT_EQ(sync_pb::SyncEnums::NOT_MY_BIRTHDAY, response.error_code()); | |
| 71 } | |
| 72 | |
| 73 TEST_F(LoopbackServerTest, GetUpdateCommand) { | |
| 74 ClientToServerMessage msg; | |
| 75 SyncerProtoUtil::SetProtocolVersion(&msg); | |
| 76 msg.set_share("required"); | |
| 77 msg.set_message_contents(ClientToServerMessage::GET_UPDATES); | |
| 78 msg.mutable_get_updates()->add_from_progress_marker()->set_data_type_id( | |
| 79 sync_pb::EntitySpecifics::kBookmarkFieldNumber); | |
| 80 sync_pb::ClientToServerResponse response; | |
| 81 | |
| 82 EXPECT_TRUE(CallPostAndProcessHeaders(lcm_.get(), NULL, msg, &response)); | |
| 83 EXPECT_EQ(sync_pb::SyncEnums::SUCCESS, response.error_code()); | |
| 84 ASSERT_TRUE(response.has_get_updates()); | |
| 85 // Expect to see the three top-level folders in this update already. | |
| 86 EXPECT_EQ(3, response.get_updates().entries_size()); | |
| 87 } | |
| 88 | |
| 89 TEST_F(LoopbackServerTest, ClearServerDataCommand) { | |
| 90 ClientToServerMessage msg; | |
| 91 SyncerProtoUtil::SetProtocolVersion(&msg); | |
| 92 msg.set_share("required"); | |
| 93 msg.set_message_contents(ClientToServerMessage::CLEAR_SERVER_DATA); | |
| 94 sync_pb::ClientToServerResponse response; | |
| 95 | |
| 96 EXPECT_TRUE(CallPostAndProcessHeaders(lcm_.get(), NULL, msg, &response)); | |
| 97 EXPECT_EQ(sync_pb::SyncEnums::SUCCESS, response.error_code()); | |
| 98 EXPECT_TRUE(response.has_clear_server_data()); | |
| 99 } | |
| 100 | |
| 101 TEST_F(LoopbackServerTest, CommitCommand) { | |
| 102 ClientToServerMessage msg; | |
| 103 SyncerProtoUtil::SetProtocolVersion(&msg); | |
| 104 msg.set_share("required"); | |
| 105 msg.set_message_contents(ClientToServerMessage::COMMIT); | |
| 106 msg.set_invalidator_client_id("client_id"); | |
| 107 auto* commit = msg.mutable_commit(); | |
| 108 commit->set_cache_guid("cache_guid"); | |
| 109 auto* entry = commit->add_entries(); | |
| 110 // Not quite well formed but enough to fool the server. | |
| 111 entry->set_parent_id_string("bookmark_bar"); | |
| 112 entry->set_id_string("id_string"); | |
| 113 entry->set_version(0); | |
| 114 entry->set_name("google"); | |
| 115 entry->mutable_specifics()->mutable_bookmark()->set_url("http://google.de"); | |
| 116 | |
| 117 sync_pb::ClientToServerResponse response; | |
| 118 | |
| 119 EXPECT_TRUE(CallPostAndProcessHeaders(lcm_.get(), NULL, msg, &response)); | |
| 120 EXPECT_EQ(sync_pb::SyncEnums::SUCCESS, response.error_code()); | |
| 121 EXPECT_TRUE(response.has_commit()); | |
| 122 } | |
| 123 | |
| 124 TEST_F(LoopbackServerTest, LoadSavedState) { | |
| 125 ClientToServerMessage commit_msg; | |
| 126 SyncerProtoUtil::SetProtocolVersion(&commit_msg); | |
| 127 commit_msg.set_share("required"); | |
| 128 commit_msg.set_message_contents(ClientToServerMessage::COMMIT); | |
| 129 commit_msg.set_invalidator_client_id("client_id"); | |
| 130 auto* commit = commit_msg.mutable_commit(); | |
| 131 commit->set_cache_guid("cache_guid"); | |
| 132 auto* entry = commit->add_entries(); | |
| 133 // Not quite well formed but enough to fool the server. | |
| 134 entry->set_parent_id_string("bookmark_bar"); | |
| 135 entry->set_id_string("id_string"); | |
| 136 entry->set_version(0); | |
| 137 entry->set_name("google"); | |
| 138 entry->mutable_specifics()->mutable_bookmark()->set_url("http://google.de"); | |
| 139 | |
| 140 sync_pb::ClientToServerResponse response; | |
| 141 | |
| 142 EXPECT_TRUE( | |
| 143 CallPostAndProcessHeaders(lcm_.get(), NULL, commit_msg, &response)); | |
| 144 EXPECT_EQ(sync_pb::SyncEnums::SUCCESS, response.error_code()); | |
| 145 EXPECT_TRUE(response.has_commit()); | |
| 146 | |
| 147 CancelationSignal signal; | |
| 148 LoopbackConnectionManager second_user(&signal, persistent_file_); | |
| 149 | |
| 150 ClientToServerMessage get_updates_msg; | |
| 151 SyncerProtoUtil::SetProtocolVersion(&get_updates_msg); | |
| 152 get_updates_msg.set_share("required"); | |
| 153 get_updates_msg.set_message_contents(ClientToServerMessage::GET_UPDATES); | |
| 154 get_updates_msg.mutable_get_updates() | |
| 155 ->add_from_progress_marker() | |
| 156 ->set_data_type_id(sync_pb::EntitySpecifics::kBookmarkFieldNumber); | |
| 157 | |
| 158 EXPECT_TRUE( | |
| 159 CallPostAndProcessHeaders(lcm_.get(), NULL, get_updates_msg, &response)); | |
|
pavely
2016/10/06 23:48:45
I think you meant second_user.
pastarmovj
2016/10/13 14:13:40
Done.
| |
| 160 EXPECT_EQ(sync_pb::SyncEnums::SUCCESS, response.error_code()); | |
| 161 ASSERT_TRUE(response.has_get_updates()); | |
|
pavely
2016/10/06 23:48:45
Sometimes you use ASSERT instead of EXPECT. Is it
pastarmovj
2016/10/13 14:13:40
Yes. The EXPECT below will crash if this is false.
| |
| 162 // Expect to see the three top-level folders and the newly added bookmark! | |
| 163 EXPECT_EQ(4, response.get_updates().entries_size()); | |
| 164 } | |
| 165 | |
| 166 TEST_F(LoopbackServerTest, CommitCommandUpdate) { | |
| 167 ClientToServerMessage commit_msg_1; | |
| 168 SyncerProtoUtil::SetProtocolVersion(&commit_msg_1); | |
| 169 commit_msg_1.set_share("required"); | |
| 170 commit_msg_1.set_message_contents(ClientToServerMessage::COMMIT); | |
| 171 commit_msg_1.set_invalidator_client_id("client_id"); | |
| 172 auto* commit = commit_msg_1.mutable_commit(); | |
| 173 commit->set_cache_guid("cache_guid"); | |
| 174 auto* entry = commit->add_entries(); | |
| 175 // Not quite well formed but enough to fool the server. | |
|
pavely
2016/10/06 23:48:45
This block of code is repeated in every test that
pastarmovj
2016/10/13 14:13:40
Done.
| |
| 176 entry->set_parent_id_string("bookmark_bar"); | |
| 177 entry->set_id_string("id_string"); | |
| 178 entry->set_version(0); | |
| 179 entry->set_name("google"); | |
| 180 entry->mutable_specifics()->mutable_bookmark()->set_url("http://google.de"); | |
| 181 | |
| 182 sync_pb::ClientToServerResponse response; | |
| 183 | |
| 184 EXPECT_TRUE( | |
| 185 CallPostAndProcessHeaders(lcm_.get(), NULL, commit_msg_1, &response)); | |
| 186 EXPECT_EQ(sync_pb::SyncEnums::SUCCESS, response.error_code()); | |
| 187 EXPECT_TRUE(response.has_commit()); | |
| 188 | |
| 189 ClientToServerMessage get_updates_msg; | |
| 190 SyncerProtoUtil::SetProtocolVersion(&get_updates_msg); | |
| 191 get_updates_msg.set_share("required"); | |
| 192 get_updates_msg.set_message_contents(ClientToServerMessage::GET_UPDATES); | |
| 193 get_updates_msg.mutable_get_updates() | |
| 194 ->add_from_progress_marker() | |
| 195 ->set_data_type_id(sync_pb::EntitySpecifics::kBookmarkFieldNumber); | |
| 196 | |
| 197 EXPECT_TRUE( | |
| 198 CallPostAndProcessHeaders(lcm_.get(), NULL, get_updates_msg, &response)); | |
| 199 EXPECT_EQ(sync_pb::SyncEnums::SUCCESS, response.error_code()); | |
| 200 ASSERT_TRUE(response.has_get_updates()); | |
| 201 // Expect to see the three top-level folders and the newly added bookmark! | |
| 202 EXPECT_EQ(4, response.get_updates().entries_size()); | |
| 203 | |
| 204 ClientToServerMessage commit_msg_2; | |
| 205 SyncerProtoUtil::SetProtocolVersion(&commit_msg_2); | |
| 206 commit_msg_2.set_share("required"); | |
| 207 commit_msg_2.set_message_contents(ClientToServerMessage::COMMIT); | |
| 208 commit_msg_2.set_invalidator_client_id("client_id"); | |
| 209 auto* commit2 = commit_msg_2.mutable_commit(); | |
| 210 commit2->set_cache_guid("cache_guid"); | |
| 211 auto* entry2 = commit->add_entries(); | |
| 212 // Move and change the bookmark | |
| 213 entry2->set_parent_id_string("other_bookmarks"); | |
| 214 entry2->set_id_string("id_string"); | |
| 215 entry2->set_version(1); | |
| 216 entry2->set_name("google"); | |
| 217 entry2->mutable_specifics()->mutable_bookmark()->set_url("http://google.bg"); | |
| 218 | |
| 219 EXPECT_TRUE( | |
| 220 CallPostAndProcessHeaders(lcm_.get(), NULL, commit_msg_2, &response)); | |
| 221 EXPECT_EQ(sync_pb::SyncEnums::SUCCESS, response.error_code()); | |
| 222 EXPECT_TRUE(response.has_commit()); | |
| 223 | |
| 224 EXPECT_TRUE( | |
| 225 CallPostAndProcessHeaders(lcm_.get(), NULL, get_updates_msg, &response)); | |
| 226 EXPECT_EQ(sync_pb::SyncEnums::SUCCESS, response.error_code()); | |
| 227 ASSERT_TRUE(response.has_get_updates()); | |
| 228 // Expect to see no fifth bookmark! | |
| 229 EXPECT_EQ(4, response.get_updates().entries_size()); | |
| 230 } | |
| 231 | |
| 232 } // namespace syncer | |
| OLD | NEW |