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 | |
| 3 // that can be 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" | |
| 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 virtual void Initialize( | |
| 58 const AudioRecorder::DecodeSamplesCallback& decode_cb, | |
| 59 const AudioDirectiveList::EncodeTokenCallback& encode_cb) OVERRIDE {} | |
| 60 | |
| 61 virtual void AddDirective(const Directive& directive) OVERRIDE { | |
| 62 directives.push_back(directive); | |
| 63 } | |
| 64 | |
| 65 virtual void RemoveDirectives(const std::string& op_id) OVERRIDE {} | |
|
Daniel Erat
2014/08/06 21:35:18
should this scan through directives_? seems a bit
Charlie
2014/08/06 22:36:23
RemoveDirectives isn't implemented yet in the real
| |
| 66 | |
| 67 const std::vector<Directive>& GetDirectives() { | |
|
Daniel Erat
2014/08/06 21:35:18
nit: make this be a simple const accessor and move
Charlie
2014/08/06 22:36:23
Done.
| |
| 68 return directives; | |
| 69 } | |
| 70 | |
| 71 private: | |
| 72 std::vector<Directive> directives; | |
|
Daniel Erat
2014/08/06 21:35:18
s/directives/directives_/
also, DISALLOW_COPY_AND
Charlie
2014/08/06 22:36:23
Done.
| |
| 73 }; | |
| 74 | |
| 75 } // namespace | |
| 76 | |
| 77 class RpcHandlerTest : public testing::Test, public CopresenceClientDelegate { | |
| 78 public: | |
| 79 RpcHandlerTest() : rpc_handler_(this), status_(NONE) { | |
| 80 rpc_handler_.server_post_callback_ = | |
| 81 base::Bind(&RpcHandlerTest::CaptureHttpPost, base::Unretained(this)); | |
| 82 rpc_handler_.device_id_ = "Device ID"; | |
| 83 } | |
| 84 | |
| 85 void CaptureHttpPost(net::URLRequestContextGetter* url_context_getter, | |
| 86 const std::string& rpc_name, | |
| 87 scoped_ptr<MessageLite> request_proto, | |
| 88 const HttpPost::ResponseCallback& response_callback) { | |
| 89 rpc_name_ = rpc_name; | |
| 90 request_proto_ = request_proto.Pass(); | |
| 91 } | |
| 92 | |
| 93 void CaptureSuccess(bool success) { | |
| 94 success_ = success; | |
| 95 } | |
| 96 | |
| 97 void CaptureStatus(CopresenceStatus status) { | |
| 98 status_ = status; | |
| 99 } | |
| 100 | |
| 101 const TokenTechnology& GetTokenTechnologyFromReport() { | |
| 102 ReportRequest* report = static_cast<ReportRequest*>(request_proto_.get()); | |
| 103 return report->update_signals_request().state().capabilities() | |
| 104 .token_technology(0); | |
| 105 } | |
| 106 | |
| 107 void SetDeviceId(const std::string& device_id) { | |
| 108 rpc_handler_.device_id_ = device_id; | |
| 109 } | |
| 110 | |
| 111 const std::string& GetDeviceId() { | |
| 112 return rpc_handler_.device_id_; | |
| 113 } | |
| 114 | |
| 115 void AddInvalidToken(const std::string& token) { | |
| 116 rpc_handler_.invalid_audio_token_cache_.Add(token, true); | |
| 117 } | |
| 118 | |
| 119 bool TokenIsInvalid(const std::string& token) { | |
| 120 return rpc_handler_.invalid_audio_token_cache_.HasKey(token); | |
| 121 } | |
| 122 | |
| 123 FakeDirectiveHandler* InstallFakeDirectiveHandler() { | |
| 124 FakeDirectiveHandler* handler = new FakeDirectiveHandler; | |
| 125 rpc_handler_.directive_handler_.reset(handler); | |
| 126 return handler; | |
| 127 } | |
| 128 | |
| 129 void InvokeReportResponseHandler(int status_code, | |
| 130 const std::string& response) { | |
| 131 rpc_handler_.ReportResponseHandler( | |
| 132 base::Bind(&RpcHandlerTest::CaptureStatus, base::Unretained(this)), | |
| 133 status_code, | |
| 134 response); | |
| 135 } | |
| 136 | |
| 137 // CopresenceClientDelegate implementation | |
| 138 | |
| 139 virtual void HandleMessages( | |
| 140 const std::string& app_id, | |
| 141 const std::string& subscription_id, | |
| 142 const std::vector<Message>& messages) OVERRIDE { | |
| 143 // app_id is unused for now, pending a server fix. | |
| 144 messages_by_subscription_[subscription_id] = messages; | |
| 145 } | |
| 146 | |
| 147 virtual net::URLRequestContextGetter* GetRequestContext() const OVERRIDE { | |
| 148 return NULL; | |
| 149 } | |
| 150 | |
| 151 virtual const std::string GetPlatformVersionString() const OVERRIDE { | |
| 152 return "Version String"; | |
| 153 } | |
| 154 | |
| 155 virtual const std::string GetDeviceId() const OVERRIDE { | |
| 156 return stored_device_id_; | |
| 157 } | |
| 158 | |
| 159 virtual void SaveDeviceId(const std::string& device_id) OVERRIDE {} | |
| 160 | |
| 161 virtual WhispernetClient* GetWhispernetClient() OVERRIDE { | |
| 162 return NULL; | |
| 163 } | |
| 164 | |
| 165 protected: | |
| 166 // For rpc_handler_.invalid_audio_token_cache_ | |
| 167 base::MessageLoop message_loop_; | |
| 168 | |
| 169 RpcHandler rpc_handler_; | |
| 170 | |
| 171 std::string rpc_name_; | |
| 172 scoped_ptr<MessageLite> request_proto_; | |
| 173 bool success_; | |
| 174 CopresenceStatus status_; | |
| 175 std::map<std::string, std::vector<Message> > messages_by_subscription_; | |
| 176 | |
| 177 std::string stored_device_id_; | |
| 178 }; | |
| 179 | |
| 180 TEST_F(RpcHandlerTest, Initialize) { | |
| 181 // Register with the server (no stored id). | |
| 182 SetDeviceId(""); | |
| 183 rpc_handler_.Initialize(RpcHandler::SuccessCallback()); | |
| 184 RegisterDeviceRequest* registration = | |
| 185 static_cast<RegisterDeviceRequest*>(request_proto_.get()); | |
| 186 Identity identity = registration->device_identifiers().registrant(); | |
| 187 EXPECT_EQ(CHROME, identity.type()); | |
| 188 EXPECT_FALSE(identity.chrome_id().empty()); | |
| 189 | |
| 190 // Initialize with a stored id. | |
| 191 SetDeviceId(""); | |
| 192 success_ = false; | |
| 193 stored_device_id_ = "Stored Device ID"; | |
| 194 rpc_handler_.Initialize(base::Bind(&RpcHandlerTest::CaptureSuccess, | |
| 195 base::Unretained(this))); | |
| 196 EXPECT_TRUE(success_); | |
| 197 EXPECT_EQ("Stored Device ID", GetDeviceId()); | |
| 198 } | |
| 199 | |
| 200 TEST_F(RpcHandlerTest, GetDeviceCapabilities) { | |
| 201 // Empty request. | |
| 202 rpc_handler_.SendReportRequest(make_scoped_ptr(new ReportRequest)); | |
| 203 EXPECT_EQ(RpcHandler::kReportRequestRpcName, rpc_name_); | |
| 204 const TokenTechnology* token_technology = &GetTokenTechnologyFromReport(); | |
| 205 EXPECT_EQ(AUDIO_ULTRASOUND_PASSBAND, token_technology->medium()); | |
| 206 EXPECT_EQ(TRANSMIT, token_technology->instruction_type(0)); | |
| 207 EXPECT_EQ(RECEIVE, token_technology->instruction_type(1)); | |
| 208 | |
| 209 // Request with broadcast only. | |
| 210 scoped_ptr<ReportRequest> report(new ReportRequest); | |
| 211 AddMessageWithStrategy(report.get(), BROADCAST_ONLY); | |
| 212 rpc_handler_.SendReportRequest(report.Pass()); | |
| 213 token_technology = &GetTokenTechnologyFromReport(); | |
| 214 EXPECT_EQ(1, token_technology->instruction_type_size()); | |
| 215 EXPECT_EQ(TRANSMIT, token_technology->instruction_type(0)); | |
| 216 | |
| 217 // Request with scan only. | |
| 218 report.reset(new ReportRequest); | |
| 219 AddSubscriptionWithStrategy(report.get(), SCAN_ONLY); | |
| 220 AddSubscriptionWithStrategy(report.get(), SCAN_ONLY); | |
| 221 rpc_handler_.SendReportRequest(report.Pass()); | |
| 222 token_technology = &GetTokenTechnologyFromReport(); | |
| 223 EXPECT_EQ(1, token_technology->instruction_type_size()); | |
| 224 EXPECT_EQ(RECEIVE, token_technology->instruction_type(0)); | |
| 225 | |
| 226 // Request with both scan and broadcast only (conflict). | |
| 227 report.reset(new ReportRequest); | |
| 228 AddMessageWithStrategy(report.get(), SCAN_ONLY); | |
| 229 AddMessageWithStrategy(report.get(), BROADCAST_ONLY); | |
| 230 AddSubscriptionWithStrategy(report.get(), BROADCAST_ONLY); | |
| 231 rpc_handler_.SendReportRequest(report.Pass()); | |
| 232 token_technology = &GetTokenTechnologyFromReport(); | |
| 233 EXPECT_EQ(TRANSMIT, token_technology->instruction_type(0)); | |
| 234 EXPECT_EQ(RECEIVE, token_technology->instruction_type(1)); | |
| 235 | |
| 236 // Request with broadcast and scan. | |
| 237 report.reset(new ReportRequest); | |
| 238 AddMessageWithStrategy(report.get(), SCAN_ONLY); | |
| 239 AddSubscriptionWithStrategy(report.get(), BROADCAST_AND_SCAN); | |
| 240 rpc_handler_.SendReportRequest(report.Pass()); | |
| 241 token_technology = &GetTokenTechnologyFromReport(); | |
| 242 EXPECT_EQ(TRANSMIT, token_technology->instruction_type(0)); | |
| 243 EXPECT_EQ(RECEIVE, token_technology->instruction_type(1)); | |
| 244 } | |
| 245 | |
| 246 TEST_F(RpcHandlerTest, CreateRequestHeader) { | |
| 247 SetDeviceId("CreateRequestHeader Device ID"); | |
| 248 rpc_handler_.SendReportRequest(make_scoped_ptr(new ReportRequest), | |
| 249 "CreateRequestHeader App ID", | |
| 250 StatusCallback()); | |
| 251 EXPECT_EQ(RpcHandler::kReportRequestRpcName, rpc_name_); | |
| 252 ReportRequest* report = static_cast<ReportRequest*>(request_proto_.get()); | |
| 253 EXPECT_TRUE(report->header().has_framework_version()); | |
| 254 EXPECT_EQ("CreateRequestHeader App ID", | |
| 255 report->header().client_version().client()); | |
| 256 EXPECT_EQ("CreateRequestHeader Device ID", | |
| 257 report->header().registered_device_id()); | |
| 258 } | |
| 259 | |
| 260 TEST_F(RpcHandlerTest, ReportTokens) { | |
| 261 std::vector<std::string> test_tokens; | |
| 262 test_tokens.push_back("token 1"); | |
| 263 test_tokens.push_back("token 2"); | |
| 264 test_tokens.push_back("token 3"); | |
| 265 AddInvalidToken("token 2"); | |
| 266 | |
| 267 rpc_handler_.ReportTokens(AUDIO_ULTRASOUND_PASSBAND, test_tokens); | |
| 268 EXPECT_EQ(RpcHandler::kReportRequestRpcName, rpc_name_); | |
| 269 ReportRequest* report = static_cast<ReportRequest*>(request_proto_.get()); | |
| 270 google::protobuf::RepeatedPtrField<TokenObservation> tokens_sent = | |
| 271 report->update_signals_request().token_observation(); | |
| 272 ASSERT_EQ(2, tokens_sent.size()); | |
| 273 EXPECT_EQ("token 1", tokens_sent.Get(0).token_id()); | |
| 274 EXPECT_EQ("token 3", tokens_sent.Get(1).token_id()); | |
| 275 } | |
| 276 | |
| 277 TEST_F(RpcHandlerTest, ReportResponseHandler) { | |
| 278 // Fail on HTTP status != 200. | |
| 279 ReportResponse empty_response; | |
| 280 empty_response.mutable_header()->mutable_status()->set_code(OK); | |
| 281 std::string serialized_empty_response; | |
| 282 ASSERT_TRUE(empty_response.SerializeToString(&serialized_empty_response)); | |
| 283 InvokeReportResponseHandler(net::HTTP_BAD_REQUEST, serialized_empty_response); | |
| 284 EXPECT_EQ(FAIL, status_); | |
| 285 | |
| 286 std::vector<std::string> subscription_1(1, "Subscription 1"); | |
| 287 std::vector<std::string> subscription_2(1, "Subscription 2"); | |
| 288 std::vector<std::string> both_subscriptions; | |
| 289 both_subscriptions.push_back("Subscription 1"); | |
| 290 both_subscriptions.push_back("Subscription 2"); | |
| 291 | |
| 292 ReportResponse test_response; | |
| 293 test_response.mutable_header()->mutable_status()->set_code(OK); | |
| 294 UpdateSignalsResponse* update_response = | |
| 295 test_response.mutable_update_signals_response(); | |
| 296 update_response->set_status(util::error::OK); | |
| 297 Token* invalid_token = update_response->add_token(); | |
| 298 invalid_token->set_id("bad token"); | |
| 299 invalid_token->set_status(INVALID); | |
| 300 CreateSubscribedMessage( | |
| 301 subscription_1, "Message A", update_response->add_message()); | |
| 302 CreateSubscribedMessage( | |
| 303 subscription_2, "Message B", update_response->add_message()); | |
| 304 CreateSubscribedMessage( | |
| 305 both_subscriptions, "Message C", update_response->add_message()); | |
| 306 update_response->add_directive()->set_subscription_id("Subscription 1"); | |
| 307 update_response->add_directive()->set_subscription_id("Subscription 2"); | |
| 308 | |
| 309 messages_by_subscription_.clear(); | |
| 310 FakeDirectiveHandler* directive_handler = InstallFakeDirectiveHandler(); | |
| 311 std::string serialized_proto; | |
| 312 ASSERT_TRUE(test_response.SerializeToString(&serialized_proto)); | |
| 313 InvokeReportResponseHandler(net::HTTP_OK, serialized_proto); | |
| 314 | |
| 315 EXPECT_EQ(SUCCESS, status_); | |
| 316 EXPECT_TRUE(TokenIsInvalid("bad token")); | |
| 317 ASSERT_EQ(2U, messages_by_subscription_.size()); | |
| 318 ASSERT_EQ(2U, messages_by_subscription_["Subscription 1"].size()); | |
| 319 ASSERT_EQ(2U, messages_by_subscription_["Subscription 2"].size()); | |
| 320 EXPECT_EQ("Message A", | |
| 321 messages_by_subscription_["Subscription 1"][0].payload()); | |
| 322 EXPECT_EQ("Message B", | |
| 323 messages_by_subscription_["Subscription 2"][0].payload()); | |
| 324 EXPECT_EQ("Message C", | |
| 325 messages_by_subscription_["Subscription 1"][1].payload()); | |
| 326 EXPECT_EQ("Message C", | |
| 327 messages_by_subscription_["Subscription 2"][1].payload()); | |
| 328 | |
| 329 ASSERT_EQ(2U, directive_handler->GetDirectives().size()); | |
| 330 EXPECT_EQ("Subscription 1", | |
| 331 directive_handler->GetDirectives()[0].subscription_id()); | |
| 332 EXPECT_EQ("Subscription 2", | |
| 333 directive_handler->GetDirectives()[1].subscription_id()); | |
| 334 } | |
| 335 | |
| 336 } // namespace copresence | |
| OLD | NEW |