Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(30)

Side by Side Diff: components/copresence/rpc/rpc_handler_unittest.cc

Issue 433283002: Adding the Copresence RpcHandler and HttpPost helper. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@directive-handler
Patch Set: Fixes for DEPS review Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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"
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 }
140
141 // CopresenceClientDelegate implementation
142
143 virtual void HandleMessages(
144 const std::string& app_id,
145 const std::string& subscription_id,
146 const std::vector<Message>& messages) OVERRIDE {
147 // app_id is unused for now, pending a server fix.
148 messages_by_subscription_[subscription_id] = messages;
149 }
150
151 virtual net::URLRequestContextGetter* GetRequestContext() const OVERRIDE {
152 return NULL;
153 }
154
155 virtual const std::string GetPlatformVersionString() const OVERRIDE {
156 return "Version String";
157 }
158
159 virtual const std::string GetDeviceId() const OVERRIDE {
160 return stored_device_id_;
161 }
162
163 virtual void SaveDeviceId(const std::string& device_id) OVERRIDE {}
164
165 virtual WhispernetClient* GetWhispernetClient() OVERRIDE {
166 return NULL;
167 }
168
169 protected:
170 // For rpc_handler_.invalid_audio_token_cache_
171 base::MessageLoop message_loop_;
172
173 RpcHandler rpc_handler_;
174
175 std::string rpc_name_;
176 scoped_ptr<MessageLite> request_proto_;
177 bool success_;
178 CopresenceStatus status_;
179 std::map<std::string, std::vector<Message> > messages_by_subscription_;
180
181 std::string stored_device_id_;
182 };
183
184 TEST_F(RpcHandlerTest, Initialize) {
185 // Register with the server (no stored id).
186 SetDeviceId("");
187 rpc_handler_.Initialize(RpcHandler::SuccessCallback());
188 RegisterDeviceRequest* registration =
189 static_cast<RegisterDeviceRequest*>(request_proto_.get());
190 Identity identity = registration->device_identifiers().registrant();
191 EXPECT_EQ(CHROME, identity.type());
192 EXPECT_FALSE(identity.chrome_id().empty());
193
194 // Initialize with a stored id.
195 SetDeviceId("");
196 success_ = false;
197 stored_device_id_ = "Stored Device ID";
198 rpc_handler_.Initialize(base::Bind(&RpcHandlerTest::CaptureSuccess,
199 base::Unretained(this)));
200 EXPECT_TRUE(success_);
201 EXPECT_EQ("Stored Device ID", GetDeviceId());
202 }
203
204 TEST_F(RpcHandlerTest, GetDeviceCapabilities) {
205 // Empty request.
206 rpc_handler_.SendReportRequest(make_scoped_ptr(new ReportRequest));
207 EXPECT_EQ(RpcHandler::kReportRequestRpcName, rpc_name_);
208 const TokenTechnology* token_technology = &GetTokenTechnologyFromReport();
209 EXPECT_EQ(AUDIO_ULTRASOUND_PASSBAND, token_technology->medium());
210 EXPECT_EQ(TRANSMIT, token_technology->instruction_type(0));
211 EXPECT_EQ(RECEIVE, token_technology->instruction_type(1));
212
213 // Request with broadcast only.
214 scoped_ptr<ReportRequest> report(new ReportRequest);
215 AddMessageWithStrategy(report.get(), BROADCAST_ONLY);
216 rpc_handler_.SendReportRequest(report.Pass());
217 token_technology = &GetTokenTechnologyFromReport();
218 EXPECT_EQ(1, token_technology->instruction_type_size());
219 EXPECT_EQ(TRANSMIT, token_technology->instruction_type(0));
220
221 // Request with scan only.
222 report.reset(new ReportRequest);
223 AddSubscriptionWithStrategy(report.get(), SCAN_ONLY);
224 AddSubscriptionWithStrategy(report.get(), SCAN_ONLY);
225 rpc_handler_.SendReportRequest(report.Pass());
226 token_technology = &GetTokenTechnologyFromReport();
227 EXPECT_EQ(1, token_technology->instruction_type_size());
228 EXPECT_EQ(RECEIVE, token_technology->instruction_type(0));
229
230 // Request with both scan and broadcast only (conflict).
231 report.reset(new ReportRequest);
232 AddMessageWithStrategy(report.get(), SCAN_ONLY);
233 AddMessageWithStrategy(report.get(), BROADCAST_ONLY);
234 AddSubscriptionWithStrategy(report.get(), BROADCAST_ONLY);
235 rpc_handler_.SendReportRequest(report.Pass());
236 token_technology = &GetTokenTechnologyFromReport();
237 EXPECT_EQ(TRANSMIT, token_technology->instruction_type(0));
238 EXPECT_EQ(RECEIVE, token_technology->instruction_type(1));
239
240 // Request with broadcast and scan.
241 report.reset(new ReportRequest);
242 AddMessageWithStrategy(report.get(), SCAN_ONLY);
243 AddSubscriptionWithStrategy(report.get(), BROADCAST_AND_SCAN);
244 rpc_handler_.SendReportRequest(report.Pass());
245 token_technology = &GetTokenTechnologyFromReport();
246 EXPECT_EQ(TRANSMIT, token_technology->instruction_type(0));
247 EXPECT_EQ(RECEIVE, token_technology->instruction_type(1));
248 }
249
250 TEST_F(RpcHandlerTest, CreateRequestHeader) {
251 SetDeviceId("CreateRequestHeader Device ID");
252 rpc_handler_.SendReportRequest(make_scoped_ptr(new ReportRequest),
253 "CreateRequestHeader App ID",
254 StatusCallback());
255 EXPECT_EQ(RpcHandler::kReportRequestRpcName, rpc_name_);
256 ReportRequest* report = static_cast<ReportRequest*>(request_proto_.get());
257 EXPECT_TRUE(report->header().has_framework_version());
258 EXPECT_EQ("CreateRequestHeader App ID",
259 report->header().client_version().client());
260 EXPECT_EQ("CreateRequestHeader Device ID",
261 report->header().registered_device_id());
262 }
263
264 TEST_F(RpcHandlerTest, ReportTokens) {
265 std::vector<std::string> test_tokens;
266 test_tokens.push_back("token 1");
267 test_tokens.push_back("token 2");
268 test_tokens.push_back("token 3");
269 AddInvalidToken("token 2");
270
271 rpc_handler_.ReportTokens(AUDIO_ULTRASOUND_PASSBAND, test_tokens);
272 EXPECT_EQ(RpcHandler::kReportRequestRpcName, rpc_name_);
273 ReportRequest* report = static_cast<ReportRequest*>(request_proto_.get());
274 google::protobuf::RepeatedPtrField<TokenObservation> tokens_sent =
275 report->update_signals_request().token_observation();
276 ASSERT_EQ(2, tokens_sent.size());
277 EXPECT_EQ("token 1", tokens_sent.Get(0).token_id());
278 EXPECT_EQ("token 3", tokens_sent.Get(1).token_id());
279 }
280
281 TEST_F(RpcHandlerTest, ReportResponseHandler) {
282 // Fail on HTTP status != 200.
283 ReportResponse empty_response;
284 empty_response.mutable_header()->mutable_status()->set_code(OK);
285 std::string serialized_empty_response;
286 ASSERT_TRUE(empty_response.SerializeToString(&serialized_empty_response));
287 status_ = SUCCESS;
288 InvokeReportResponseHandler(net::HTTP_BAD_REQUEST, serialized_empty_response);
289 EXPECT_EQ(FAIL, status_);
290
291 std::vector<std::string> subscription_1(1, "Subscription 1");
292 std::vector<std::string> subscription_2(1, "Subscription 2");
293 std::vector<std::string> both_subscriptions;
294 both_subscriptions.push_back("Subscription 1");
295 both_subscriptions.push_back("Subscription 2");
296
297 ReportResponse test_response;
298 test_response.mutable_header()->mutable_status()->set_code(OK);
299 UpdateSignalsResponse* update_response =
300 test_response.mutable_update_signals_response();
301 update_response->set_status(util::error::OK);
302 Token* invalid_token = update_response->add_token();
303 invalid_token->set_id("bad token");
304 invalid_token->set_status(INVALID);
305 CreateSubscribedMessage(
306 subscription_1, "Message A", update_response->add_message());
307 CreateSubscribedMessage(
308 subscription_2, "Message B", update_response->add_message());
309 CreateSubscribedMessage(
310 both_subscriptions, "Message C", update_response->add_message());
311 update_response->add_directive()->set_subscription_id("Subscription 1");
312 update_response->add_directive()->set_subscription_id("Subscription 2");
313
314 messages_by_subscription_.clear();
315 FakeDirectiveHandler* directive_handler = InstallFakeDirectiveHandler();
316 std::string serialized_proto;
317 ASSERT_TRUE(test_response.SerializeToString(&serialized_proto));
318 status_ = FAIL;
319 InvokeReportResponseHandler(net::HTTP_OK, serialized_proto);
320
321 EXPECT_EQ(SUCCESS, status_);
322 EXPECT_TRUE(TokenIsInvalid("bad token"));
323 ASSERT_EQ(2U, messages_by_subscription_.size());
324 ASSERT_EQ(2U, messages_by_subscription_["Subscription 1"].size());
325 ASSERT_EQ(2U, messages_by_subscription_["Subscription 2"].size());
326 EXPECT_EQ("Message A",
327 messages_by_subscription_["Subscription 1"][0].payload());
328 EXPECT_EQ("Message B",
329 messages_by_subscription_["Subscription 2"][0].payload());
330 EXPECT_EQ("Message C",
331 messages_by_subscription_["Subscription 1"][1].payload());
332 EXPECT_EQ("Message C",
333 messages_by_subscription_["Subscription 2"][1].payload());
334
335 ASSERT_EQ(2U, directive_handler->added_directives().size());
336 EXPECT_EQ("Subscription 1",
337 directive_handler->added_directives()[0].subscription_id());
338 EXPECT_EQ("Subscription 2",
339 directive_handler->added_directives()[1].subscription_id());
340 }
341
342 } // namespace copresence
OLDNEW
« components/copresence/rpc/http_post.cc ('K') | « components/copresence/rpc/rpc_handler.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698