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