Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 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 <map> | |
| 6 #include <string> | |
| 7 #include <vector> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/bind_helpers.h" | |
| 11 #include "base/message_loop/message_loop.h" | |
| 12 #include "components/copresence/handlers/directive_handler.h" | |
| 13 #include "components/copresence/proto/data.pb.h" | |
| 14 #include "components/copresence/proto/enums.pb.h" | |
| 15 #include "components/copresence/proto/rpcs.pb.h" | |
| 16 #include "components/copresence/rpc/rpc_handler.h" | |
|
willchan no longer on Chromium
2014/08/08 18:01:20
This should go first, as per style guide.
Charlie
2014/08/08 21:57:53
I wondered about that. Fixed.
| |
| 17 #include "net/http/http_status_code.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | |
| 19 | |
| 20 using google::protobuf::MessageLite; | |
| 21 | |
| 22 namespace copresence { | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 void AddMessageWithStrategy(ReportRequest* report, | |
| 27 BroadcastScanConfiguration strategy) { | |
| 28 report->mutable_manage_messages_request()->add_message_to_publish() | |
| 29 ->mutable_token_exchange_strategy()->set_broadcast_scan_configuration( | |
| 30 strategy); | |
| 31 } | |
| 32 | |
| 33 void AddSubscriptionWithStrategy(ReportRequest* report, | |
| 34 BroadcastScanConfiguration strategy) { | |
| 35 report->mutable_manage_subscriptions_request()->add_subscription() | |
| 36 ->mutable_token_exchange_strategy()->set_broadcast_scan_configuration( | |
| 37 strategy); | |
| 38 } | |
| 39 | |
| 40 void CreateSubscribedMessage(const std::vector<std::string>& subscription_ids, | |
| 41 const std::string& message_string, | |
| 42 SubscribedMessage* message_proto) { | |
| 43 message_proto->mutable_published_message()->set_payload(message_string); | |
| 44 for (std::vector<std::string>::const_iterator subscription_id = | |
| 45 subscription_ids.begin(); | |
| 46 subscription_id != subscription_ids.end(); | |
| 47 ++subscription_id) { | |
| 48 message_proto->add_subscription_id(*subscription_id); | |
| 49 } | |
| 50 } | |
| 51 | |
| 52 class FakeDirectiveHandler : public DirectiveHandler { | |
| 53 public: | |
| 54 FakeDirectiveHandler() {} | |
| 55 virtual ~FakeDirectiveHandler() {} | |
| 56 | |
| 57 const std::vector<Directive>& added_directives() const { | |
| 58 return added_directives_; | |
| 59 } | |
| 60 | |
| 61 virtual void Initialize( | |
| 62 const AudioRecorder::DecodeSamplesCallback& decode_cb, | |
| 63 const AudioDirectiveList::EncodeTokenCallback& encode_cb) OVERRIDE {} | |
| 64 | |
| 65 virtual void AddDirective(const Directive& directive) OVERRIDE { | |
| 66 added_directives_.push_back(directive); | |
| 67 } | |
| 68 | |
| 69 virtual void RemoveDirectives(const std::string& op_id) OVERRIDE { | |
| 70 // TODO(ckehoe): Add a parallel implementation when prod has one. | |
| 71 } | |
| 72 | |
| 73 private: | |
| 74 std::vector<Directive> added_directives_; | |
| 75 | |
| 76 DISALLOW_COPY_AND_ASSIGN(FakeDirectiveHandler); | |
| 77 }; | |
| 78 | |
| 79 } // namespace | |
| 80 | |
| 81 class RpcHandlerTest : public testing::Test, public CopresenceClientDelegate { | |
| 82 public: | |
| 83 RpcHandlerTest() : rpc_handler_(this), status_(SUCCESS) { | |
| 84 rpc_handler_.server_post_callback_ = | |
| 85 base::Bind(&RpcHandlerTest::CaptureHttpPost, base::Unretained(this)); | |
| 86 rpc_handler_.device_id_ = "Device ID"; | |
| 87 } | |
| 88 | |
| 89 void CaptureHttpPost(net::URLRequestContextGetter* url_context_getter, | |
| 90 const std::string& rpc_name, | |
| 91 scoped_ptr<MessageLite> request_proto, | |
| 92 const HttpPost::ResponseCallback& response_callback) { | |
| 93 rpc_name_ = rpc_name; | |
| 94 request_proto_ = request_proto.Pass(); | |
| 95 } | |
| 96 | |
| 97 void CaptureSuccess(bool success) { | |
| 98 success_ = success; | |
| 99 } | |
| 100 | |
| 101 void CaptureStatus(CopresenceStatus status) { | |
| 102 status_ = status; | |
| 103 } | |
| 104 | |
| 105 const TokenTechnology& GetTokenTechnologyFromReport() { | |
| 106 ReportRequest* report = static_cast<ReportRequest*>(request_proto_.get()); | |
| 107 return report->update_signals_request().state().capabilities() | |
| 108 .token_technology(0); | |
| 109 } | |
| 110 | |
| 111 void SetDeviceId(const std::string& device_id) { | |
| 112 rpc_handler_.device_id_ = device_id; | |
| 113 } | |
| 114 | |
| 115 const std::string& GetDeviceId() { | |
| 116 return rpc_handler_.device_id_; | |
| 117 } | |
| 118 | |
| 119 void AddInvalidToken(const std::string& token) { | |
| 120 rpc_handler_.invalid_audio_token_cache_.Add(token, true); | |
| 121 } | |
| 122 | |
| 123 bool TokenIsInvalid(const std::string& token) { | |
| 124 return rpc_handler_.invalid_audio_token_cache_.HasKey(token); | |
| 125 } | |
| 126 | |
| 127 FakeDirectiveHandler* InstallFakeDirectiveHandler() { | |
| 128 FakeDirectiveHandler* handler = new FakeDirectiveHandler; | |
| 129 rpc_handler_.directive_handler_.reset(handler); | |
| 130 return handler; | |
| 131 } | |
| 132 | |
| 133 void InvokeReportResponseHandler(int status_code, | |
| 134 const std::string& response) { | |
| 135 rpc_handler_.ReportResponseHandler( | |
| 136 base::Bind(&RpcHandlerTest::CaptureStatus, base::Unretained(this)), | |
| 137 status_code, | |
| 138 response, | |
| 139 NULL); | |
| 140 } | |
| 141 | |
| 142 // CopresenceClientDelegate implementation | |
| 143 | |
| 144 virtual void HandleMessages( | |
| 145 const std::string& app_id, | |
| 146 const std::string& subscription_id, | |
| 147 const std::vector<Message>& messages) OVERRIDE { | |
| 148 // app_id is unused for now, pending a server fix. | |
| 149 messages_by_subscription_[subscription_id] = messages; | |
| 150 } | |
| 151 | |
| 152 virtual net::URLRequestContextGetter* GetRequestContext() const OVERRIDE { | |
| 153 return NULL; | |
| 154 } | |
| 155 | |
| 156 virtual const std::string GetPlatformVersionString() const OVERRIDE { | |
| 157 return "Version String"; | |
| 158 } | |
| 159 | |
| 160 virtual const std::string GetDeviceId() const OVERRIDE { | |
| 161 return stored_device_id_; | |
| 162 } | |
| 163 | |
| 164 virtual void SaveDeviceId(const std::string& device_id) OVERRIDE {} | |
| 165 | |
| 166 virtual WhispernetClient* GetWhispernetClient() OVERRIDE { | |
| 167 return NULL; | |
| 168 } | |
| 169 | |
| 170 protected: | |
| 171 // For rpc_handler_.invalid_audio_token_cache_ | |
| 172 base::MessageLoop message_loop_; | |
| 173 | |
| 174 RpcHandler rpc_handler_; | |
| 175 | |
| 176 std::string rpc_name_; | |
| 177 scoped_ptr<MessageLite> request_proto_; | |
| 178 bool success_; | |
| 179 CopresenceStatus status_; | |
| 180 std::map<std::string, std::vector<Message> > messages_by_subscription_; | |
| 181 | |
| 182 std::string stored_device_id_; | |
| 183 }; | |
| 184 | |
| 185 TEST_F(RpcHandlerTest, Initialize) { | |
| 186 // Register with the server (no stored id). | |
| 187 SetDeviceId(""); | |
| 188 rpc_handler_.Initialize(RpcHandler::SuccessCallback()); | |
| 189 RegisterDeviceRequest* registration = | |
| 190 static_cast<RegisterDeviceRequest*>(request_proto_.get()); | |
| 191 Identity identity = registration->device_identifiers().registrant(); | |
| 192 EXPECT_EQ(CHROME, identity.type()); | |
| 193 EXPECT_FALSE(identity.chrome_id().empty()); | |
| 194 | |
| 195 // Initialize with a stored id. | |
| 196 SetDeviceId(""); | |
| 197 success_ = false; | |
| 198 stored_device_id_ = "Stored Device ID"; | |
| 199 rpc_handler_.Initialize(base::Bind(&RpcHandlerTest::CaptureSuccess, | |
| 200 base::Unretained(this))); | |
| 201 EXPECT_TRUE(success_); | |
| 202 EXPECT_EQ("Stored Device ID", GetDeviceId()); | |
| 203 } | |
| 204 | |
| 205 TEST_F(RpcHandlerTest, GetDeviceCapabilities) { | |
| 206 // Empty request. | |
| 207 rpc_handler_.SendReportRequest(make_scoped_ptr(new ReportRequest)); | |
| 208 EXPECT_EQ(RpcHandler::kReportRequestRpcName, rpc_name_); | |
| 209 const TokenTechnology* token_technology = &GetTokenTechnologyFromReport(); | |
| 210 EXPECT_EQ(AUDIO_ULTRASOUND_PASSBAND, token_technology->medium()); | |
| 211 EXPECT_EQ(TRANSMIT, token_technology->instruction_type(0)); | |
| 212 EXPECT_EQ(RECEIVE, token_technology->instruction_type(1)); | |
| 213 | |
| 214 // Request with broadcast only. | |
| 215 scoped_ptr<ReportRequest> report(new ReportRequest); | |
| 216 AddMessageWithStrategy(report.get(), BROADCAST_ONLY); | |
| 217 rpc_handler_.SendReportRequest(report.Pass()); | |
| 218 token_technology = &GetTokenTechnologyFromReport(); | |
| 219 EXPECT_EQ(1, token_technology->instruction_type_size()); | |
| 220 EXPECT_EQ(TRANSMIT, token_technology->instruction_type(0)); | |
| 221 | |
| 222 // Request with scan only. | |
| 223 report.reset(new ReportRequest); | |
| 224 AddSubscriptionWithStrategy(report.get(), SCAN_ONLY); | |
| 225 AddSubscriptionWithStrategy(report.get(), SCAN_ONLY); | |
| 226 rpc_handler_.SendReportRequest(report.Pass()); | |
| 227 token_technology = &GetTokenTechnologyFromReport(); | |
| 228 EXPECT_EQ(1, token_technology->instruction_type_size()); | |
| 229 EXPECT_EQ(RECEIVE, token_technology->instruction_type(0)); | |
| 230 | |
| 231 // Request with both scan and broadcast only (conflict). | |
| 232 report.reset(new ReportRequest); | |
| 233 AddMessageWithStrategy(report.get(), SCAN_ONLY); | |
| 234 AddMessageWithStrategy(report.get(), BROADCAST_ONLY); | |
| 235 AddSubscriptionWithStrategy(report.get(), BROADCAST_ONLY); | |
| 236 rpc_handler_.SendReportRequest(report.Pass()); | |
| 237 token_technology = &GetTokenTechnologyFromReport(); | |
| 238 EXPECT_EQ(TRANSMIT, token_technology->instruction_type(0)); | |
| 239 EXPECT_EQ(RECEIVE, token_technology->instruction_type(1)); | |
| 240 | |
| 241 // Request with broadcast and scan. | |
| 242 report.reset(new ReportRequest); | |
| 243 AddMessageWithStrategy(report.get(), SCAN_ONLY); | |
| 244 AddSubscriptionWithStrategy(report.get(), BROADCAST_AND_SCAN); | |
| 245 rpc_handler_.SendReportRequest(report.Pass()); | |
| 246 token_technology = &GetTokenTechnologyFromReport(); | |
| 247 EXPECT_EQ(TRANSMIT, token_technology->instruction_type(0)); | |
| 248 EXPECT_EQ(RECEIVE, token_technology->instruction_type(1)); | |
| 249 } | |
| 250 | |
| 251 TEST_F(RpcHandlerTest, CreateRequestHeader) { | |
| 252 SetDeviceId("CreateRequestHeader Device ID"); | |
| 253 rpc_handler_.SendReportRequest(make_scoped_ptr(new ReportRequest), | |
| 254 "CreateRequestHeader App ID", | |
| 255 StatusCallback()); | |
| 256 EXPECT_EQ(RpcHandler::kReportRequestRpcName, rpc_name_); | |
| 257 ReportRequest* report = static_cast<ReportRequest*>(request_proto_.get()); | |
| 258 EXPECT_TRUE(report->header().has_framework_version()); | |
| 259 EXPECT_EQ("CreateRequestHeader App ID", | |
| 260 report->header().client_version().client()); | |
| 261 EXPECT_EQ("CreateRequestHeader Device ID", | |
| 262 report->header().registered_device_id()); | |
| 263 } | |
| 264 | |
| 265 TEST_F(RpcHandlerTest, ReportTokens) { | |
| 266 std::vector<std::string> test_tokens; | |
| 267 test_tokens.push_back("token 1"); | |
| 268 test_tokens.push_back("token 2"); | |
| 269 test_tokens.push_back("token 3"); | |
| 270 AddInvalidToken("token 2"); | |
| 271 | |
| 272 rpc_handler_.ReportTokens(AUDIO_ULTRASOUND_PASSBAND, test_tokens); | |
| 273 EXPECT_EQ(RpcHandler::kReportRequestRpcName, rpc_name_); | |
| 274 ReportRequest* report = static_cast<ReportRequest*>(request_proto_.get()); | |
| 275 google::protobuf::RepeatedPtrField<TokenObservation> tokens_sent = | |
| 276 report->update_signals_request().token_observation(); | |
| 277 ASSERT_EQ(2, tokens_sent.size()); | |
| 278 EXPECT_EQ("token 1", tokens_sent.Get(0).token_id()); | |
| 279 EXPECT_EQ("token 3", tokens_sent.Get(1).token_id()); | |
| 280 } | |
| 281 | |
| 282 TEST_F(RpcHandlerTest, ReportResponseHandler) { | |
| 283 // Fail on HTTP status != 200. | |
| 284 ReportResponse empty_response; | |
| 285 empty_response.mutable_header()->mutable_status()->set_code(OK); | |
| 286 std::string serialized_empty_response; | |
| 287 ASSERT_TRUE(empty_response.SerializeToString(&serialized_empty_response)); | |
| 288 status_ = SUCCESS; | |
| 289 InvokeReportResponseHandler(net::HTTP_BAD_REQUEST, serialized_empty_response); | |
| 290 EXPECT_EQ(FAIL, status_); | |
| 291 | |
| 292 std::vector<std::string> subscription_1(1, "Subscription 1"); | |
| 293 std::vector<std::string> subscription_2(1, "Subscription 2"); | |
| 294 std::vector<std::string> both_subscriptions; | |
| 295 both_subscriptions.push_back("Subscription 1"); | |
| 296 both_subscriptions.push_back("Subscription 2"); | |
| 297 | |
| 298 ReportResponse test_response; | |
| 299 test_response.mutable_header()->mutable_status()->set_code(OK); | |
| 300 UpdateSignalsResponse* update_response = | |
| 301 test_response.mutable_update_signals_response(); | |
| 302 update_response->set_status(util::error::OK); | |
| 303 Token* invalid_token = update_response->add_token(); | |
| 304 invalid_token->set_id("bad token"); | |
| 305 invalid_token->set_status(INVALID); | |
| 306 CreateSubscribedMessage( | |
| 307 subscription_1, "Message A", update_response->add_message()); | |
| 308 CreateSubscribedMessage( | |
| 309 subscription_2, "Message B", update_response->add_message()); | |
| 310 CreateSubscribedMessage( | |
| 311 both_subscriptions, "Message C", update_response->add_message()); | |
| 312 update_response->add_directive()->set_subscription_id("Subscription 1"); | |
| 313 update_response->add_directive()->set_subscription_id("Subscription 2"); | |
| 314 | |
| 315 messages_by_subscription_.clear(); | |
| 316 FakeDirectiveHandler* directive_handler = InstallFakeDirectiveHandler(); | |
| 317 std::string serialized_proto; | |
| 318 ASSERT_TRUE(test_response.SerializeToString(&serialized_proto)); | |
| 319 status_ = FAIL; | |
| 320 InvokeReportResponseHandler(net::HTTP_OK, serialized_proto); | |
| 321 | |
| 322 EXPECT_EQ(SUCCESS, status_); | |
| 323 EXPECT_TRUE(TokenIsInvalid("bad token")); | |
| 324 ASSERT_EQ(2U, messages_by_subscription_.size()); | |
| 325 ASSERT_EQ(2U, messages_by_subscription_["Subscription 1"].size()); | |
| 326 ASSERT_EQ(2U, messages_by_subscription_["Subscription 2"].size()); | |
| 327 EXPECT_EQ("Message A", | |
| 328 messages_by_subscription_["Subscription 1"][0].payload()); | |
| 329 EXPECT_EQ("Message B", | |
| 330 messages_by_subscription_["Subscription 2"][0].payload()); | |
| 331 EXPECT_EQ("Message C", | |
| 332 messages_by_subscription_["Subscription 1"][1].payload()); | |
| 333 EXPECT_EQ("Message C", | |
| 334 messages_by_subscription_["Subscription 2"][1].payload()); | |
| 335 | |
| 336 ASSERT_EQ(2U, directive_handler->added_directives().size()); | |
| 337 EXPECT_EQ("Subscription 1", | |
| 338 directive_handler->added_directives()[0].subscription_id()); | |
| 339 EXPECT_EQ("Subscription 2", | |
| 340 directive_handler->added_directives()[1].subscription_id()); | |
| 341 } | |
| 342 | |
| 343 } // namespace copresence | |
| OLD | NEW |