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