1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/copresence/rpc/rpc_handler.h"
#include <map>
#include <string>
#include <vector>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/memory/scoped_vector.h"
#include "base/message_loop/message_loop.h"
#include "components/audio_modem/public/modem.h"
#include "components/audio_modem/test/stub_whispernet_client.h"
#include "components/copresence/copresence_state_impl.h"
#include "components/copresence/handlers/directive_handler.h"
#include "components/copresence/proto/data.pb.h"
#include "components/copresence/proto/enums.pb.h"
#include "components/copresence/proto/rpcs.pb.h"
#include "components/copresence/test/fake_directive_handler.h"
#include "net/http/http_status_code.h"
#include "testing/gmock/include/gmock/gmock.h"
using google::protobuf::MessageLite;
using google::protobuf::RepeatedPtrField;
using testing::ElementsAre;
using testing::Property;
using testing::SizeIs;
using audio_modem::AudioToken;
using audio_modem::WhispernetClient;
namespace copresence {
namespace {
const char kChromeVersion[] = "Chrome Version String";
void IgnoreMessages(
const RepeatedPtrField<SubscribedMessage>& /* messages */) {}
} // namespace
class RpcHandlerTest : public testing::Test, public CopresenceDelegate {
public:
RpcHandlerTest()
: whispernet_client_(new audio_modem::StubWhispernetClient),
// TODO(ckehoe): Use a FakeCopresenceState here
// and test that it gets called correctly.
state_(new CopresenceStateImpl),
rpc_handler_(this,
state_.get(),
&directive_handler_,
nullptr,
base::Bind(&IgnoreMessages),
base::Bind(&RpcHandlerTest::CaptureHttpPost,
base::Unretained(this))),
status_(SUCCESS) {}
// CopresenceDelegate implementation
void HandleMessages(const std::string& /* app_id */,
const std::string& subscription_id,
const std::vector<Message>& messages) override {
NOTREACHED();
}
void HandleStatusUpdate(CopresenceStatus /* status */) override {
NOTREACHED();
}
net::URLRequestContextGetter* GetRequestContext() const override {
return nullptr;
}
const std::string GetPlatformVersionString() const override {
return kChromeVersion;
}
const std::string GetAPIKey(const std::string& app_id) const override {
return app_id + " API Key";
}
WhispernetClient* GetWhispernetClient() override {
return whispernet_client_.get();
}
// TODO(ckehoe): Add GCM tests.
gcm::GCMDriver* GetGCMDriver() override {
return nullptr;
}
const std::string GetDeviceId(bool authenticated) override {
return device_id_by_auth_state_[authenticated];
}
void SaveDeviceId(bool authenticated, const std::string& device_id) override {
device_id_by_auth_state_[authenticated] = device_id;
}
protected:
// Send test input to RpcHandler
void RegisterDevice(bool authenticated) {
rpc_handler_.RegisterDevice(authenticated);
}
void SendRegisterResponse(bool authenticated,
const std::string& device_id) {
RegisterDeviceResponse response;
response.set_registered_device_id(device_id);
response.mutable_header()->mutable_status()->set_code(OK);
std::string serialized_response;
response.SerializeToString(&serialized_response);
rpc_handler_.RegisterResponseHandler(
authenticated, false, nullptr, net::HTTP_OK, serialized_response);
}
void SendReport(scoped_ptr<ReportRequest> request,
const std::string& app_id,
const std::string& auth_token) {
rpc_handler_.SendReportRequest(
request.Pass(), app_id, auth_token, StatusCallback());
}
void SendReportResponse(int status_code,
scoped_ptr<ReportResponse> response) {
response->mutable_header()->mutable_status()->set_code(OK);
std::string serialized_response;
response->SerializeToString(&serialized_response);
rpc_handler_.ReportResponseHandler(
base::Bind(&RpcHandlerTest::CaptureStatus, base::Unretained(this)),
nullptr,
status_code,
serialized_response);
}
// Read and modify RpcHandler state
void SetAuthToken(const std::string& auth_token) {
rpc_handler_.auth_token_ = auth_token;
}
const ScopedVector<RpcHandler::PendingRequest>& request_queue() const {
return rpc_handler_.pending_requests_queue_;
}
void AddInvalidToken(const std::string& token) {
rpc_handler_.invalid_audio_token_cache_.Add(token, true);
}
bool TokenIsInvalid(const std::string& token) {
return rpc_handler_.invalid_audio_token_cache_.HasKey(token);
}
// For rpc_handler_.invalid_audio_token_cache_
base::MessageLoop message_loop_;
scoped_ptr<WhispernetClient> whispernet_client_;
FakeDirectiveHandler directive_handler_;
scoped_ptr<CopresenceStateImpl> state_;
RpcHandler rpc_handler_;
std::map<bool, std::string> device_id_by_auth_state_;
CopresenceStatus status_;
std::string rpc_name_;
std::string api_key_;
std::string auth_token_;
ScopedVector<MessageLite> request_protos_;
private:
void CaptureHttpPost(
net::URLRequestContextGetter* url_context_getter,
const std::string& rpc_name,
const std::string& api_key,
const std::string& auth_token,
scoped_ptr<MessageLite> request_proto,
const RpcHandler::PostCleanupCallback& response_callback) {
rpc_name_ = rpc_name;
api_key_ = api_key;
auth_token_ = auth_token;
request_protos_.push_back(request_proto.release());
}
void CaptureStatus(CopresenceStatus status) {
status_ = status;
}
};
TEST_F(RpcHandlerTest, RegisterDevice) {
RegisterDevice(false);
EXPECT_THAT(request_protos_, SizeIs(1));
const RegisterDeviceRequest* registration =
static_cast<RegisterDeviceRequest*>(request_protos_[0]);
EXPECT_EQ(CHROME, registration->device_identifiers().registrant().type());
SetAuthToken("Register auth");
RegisterDevice(true);
EXPECT_THAT(request_protos_, SizeIs(2));
registration = static_cast<RegisterDeviceRequest*>(request_protos_[1]);
EXPECT_FALSE(registration->has_device_identifiers());
}
TEST_F(RpcHandlerTest, RequestQueuing) {
// Send a report.
ReportRequest* report = new ReportRequest;
report->mutable_manage_messages_request()->add_id_to_unpublish("unpublish");
SendReport(make_scoped_ptr(report), "Q App ID", "Q Auth Token");
EXPECT_THAT(request_queue(), SizeIs(1));
EXPECT_TRUE(request_queue()[0]->authenticated);
// Check for registration request.
EXPECT_THAT(request_protos_, SizeIs(1));
const RegisterDeviceRequest* registration =
static_cast<RegisterDeviceRequest*>(request_protos_[0]);
EXPECT_FALSE(registration->device_identifiers().has_registrant());
EXPECT_EQ("Q Auth Token", auth_token_);
// Send a second report.
report = new ReportRequest;
report->mutable_manage_subscriptions_request()->add_id_to_unsubscribe(
"unsubscribe");
SendReport(make_scoped_ptr(report), "Q App ID", "Q Auth Token");
EXPECT_THAT(request_protos_, SizeIs(1));
EXPECT_THAT(request_queue(), SizeIs(2));
EXPECT_TRUE(request_queue()[1]->authenticated);
// Send an anonymous report.
report = new ReportRequest;
report->mutable_update_signals_request()->add_token_observation()
->set_token_id("Q Audio Token");
SendReport(make_scoped_ptr(report), "Q App ID", "");
EXPECT_THAT(request_queue(), SizeIs(3));
EXPECT_FALSE(request_queue()[2]->authenticated);
// Check for another registration request.
EXPECT_THAT(request_protos_, SizeIs(2));
registration = static_cast<RegisterDeviceRequest*>(request_protos_[1]);
EXPECT_TRUE(registration->device_identifiers().has_registrant());
EXPECT_EQ("", auth_token_);
// Respond to the first registration.
SendRegisterResponse(true, "Q Auth Device ID");
EXPECT_EQ("Q Auth Device ID", device_id_by_auth_state_[true]);
// Check that queued reports are sent.
EXPECT_THAT(request_protos_, SizeIs(4));
EXPECT_THAT(request_queue(), SizeIs(1));
EXPECT_THAT(directive_handler_.removed_directives(),
ElementsAre("unpublish", "unsubscribe"));
report = static_cast<ReportRequest*>(request_protos_[2]);
EXPECT_EQ("unpublish", report->manage_messages_request().id_to_unpublish(0));
report = static_cast<ReportRequest*>(request_protos_[3]);
EXPECT_EQ("unsubscribe",
report->manage_subscriptions_request().id_to_unsubscribe(0));
// Respond to the second registration.
SendRegisterResponse(false, "Q Anonymous Device ID");
EXPECT_EQ("Q Anonymous Device ID", device_id_by_auth_state_[false]);
// Check for last report.
EXPECT_THAT(request_protos_, SizeIs(5));
EXPECT_TRUE(request_queue().empty());
report = static_cast<ReportRequest*>(request_protos_[4]);
EXPECT_EQ("Q Audio Token",
report->update_signals_request().token_observation(0).token_id());
}
TEST_F(RpcHandlerTest, CreateRequestHeader) {
device_id_by_auth_state_[true] = "CreateRequestHeader Device ID";
SendReport(make_scoped_ptr(new ReportRequest),
"CreateRequestHeader App",
"CreateRequestHeader Auth Token");
EXPECT_EQ(RpcHandler::kReportRequestRpcName, rpc_name_);
EXPECT_EQ("CreateRequestHeader App API Key", api_key_);
EXPECT_EQ("CreateRequestHeader Auth Token", auth_token_);
const ReportRequest* report = static_cast<ReportRequest*>(request_protos_[0]);
EXPECT_EQ(kChromeVersion,
report->header().framework_version().version_name());
EXPECT_EQ("CreateRequestHeader App",
report->header().client_version().client());
EXPECT_EQ("CreateRequestHeader Device ID",
report->header().registered_device_id());
EXPECT_EQ(CHROME_PLATFORM_TYPE,
report->header().device_fingerprint().type());
}
TEST_F(RpcHandlerTest, ReportTokens) {
std::vector<AudioToken> test_tokens;
test_tokens.push_back(AudioToken("token 1", false));
test_tokens.push_back(AudioToken("token 2", false));
test_tokens.push_back(AudioToken("token 3", true));
AddInvalidToken("token 2");
device_id_by_auth_state_[false] = "ReportTokens Anonymous Device";
device_id_by_auth_state_[true] = "ReportTokens Auth Device";
SetAuthToken("ReportTokens Auth");
rpc_handler_.ReportTokens(test_tokens);
EXPECT_EQ(RpcHandler::kReportRequestRpcName, rpc_name_);
EXPECT_EQ(" API Key", api_key_);
EXPECT_THAT(request_protos_, SizeIs(2));
const ReportRequest* report = static_cast<ReportRequest*>(request_protos_[0]);
RepeatedPtrField<TokenObservation> tokens_sent =
report->update_signals_request().token_observation();
EXPECT_THAT(tokens_sent, ElementsAre(
Property(&TokenObservation::token_id, "token 1"),
Property(&TokenObservation::token_id, "token 3"),
Property(&TokenObservation::token_id, "current audible"),
Property(&TokenObservation::token_id, "current inaudible")));
}
TEST_F(RpcHandlerTest, ReportResponseHandler) {
// Fail on HTTP status != 200.
scoped_ptr<ReportResponse> response(new ReportResponse);
status_ = SUCCESS;
SendReportResponse(net::HTTP_BAD_REQUEST, response.Pass());
EXPECT_EQ(FAIL, status_);
// Construct a test ReportResponse.
response.reset(new ReportResponse);
response->mutable_header()->mutable_status()->set_code(OK);
UpdateSignalsResponse* update_response =
response->mutable_update_signals_response();
update_response->set_status(util::error::OK);
Token* invalid_token = update_response->add_token();
invalid_token->set_id("bad token");
invalid_token->set_status(INVALID);
update_response->add_directive()->set_subscription_id("Subscription 1");
update_response->add_directive()->set_subscription_id("Subscription 2");
// Check processing.
status_ = FAIL;
SendReportResponse(net::HTTP_OK, response.Pass());
EXPECT_EQ(SUCCESS, status_);
EXPECT_TRUE(TokenIsInvalid("bad token"));
EXPECT_THAT(directive_handler_.added_directives(),
ElementsAre("Subscription 1", "Subscription 2"));
}
} // namespace copresence
|