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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
// Copyright 2013 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.
//
// A standalone tool for testing MCS connections and the MCS client on their
// own.
#include <cstddef>
#include <cstdio>
#include <string>
#include "base/at_exit.h"
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/threading/thread.h"
#include "base/threading/worker_pool.h"
#include "base/values.h"
#include "google_apis/gcm/base/mcs_message.h"
#include "google_apis/gcm/base/mcs_util.h"
#include "google_apis/gcm/engine/connection_factory_impl.h"
#include "google_apis/gcm/engine/mcs_client.h"
#include "net/base/host_mapping_rules.h"
#include "net/base/net_log_logger.h"
#include "net/cert/cert_verifier.h"
#include "net/dns/host_resolver.h"
#include "net/http/http_auth_handler_factory.h"
#include "net/http/http_network_session.h"
#include "net/http/http_server_properties_impl.h"
#include "net/http/transport_security_state.h"
#include "net/socket/client_socket_factory.h"
#include "net/socket/ssl_client_socket.h"
#include "net/ssl/default_server_bound_cert_store.h"
#include "net/ssl/server_bound_cert_service.h"
#include "net/url_request/url_request_test_util.h"
#if defined(OS_MACOSX)
#include "base/mac/scoped_nsautorelease_pool.h"
#endif
// This is a simple utility that initializes an mcs client and
// prints out any events.
namespace gcm {
namespace {
// The default server to communicate with.
const char kMCSServerHost[] = "mtalk.google.com";
const uint16 kMCSServerPort = 5228;
// Command line switches.
const char kRMQFileName[] = "rmq_file";
const char kAndroidIdSwitch[] = "android_id";
const char kSecretSwitch[] = "secret";
const char kLogFileSwitch[] = "log-file";
const char kIgnoreCertSwitch[] = "ignore-certs";
const char kServerHostSwitch[] = "host";
const char kServerPortSwitch[] = "port";
void MessageReceivedCallback(const MCSMessage& message) {
LOG(INFO) << "Received message with id "
<< GetPersistentId(message.GetProtobuf()) << " and tag "
<< static_cast<int>(message.tag());
if (message.tag() == kDataMessageStanzaTag) {
const mcs_proto::DataMessageStanza& data_message =
reinterpret_cast<const mcs_proto::DataMessageStanza&>(
message.GetProtobuf());
DVLOG(1) << " to: " << data_message.to();
DVLOG(1) << " from: " << data_message.from();
DVLOG(1) << " category: " << data_message.category();
DVLOG(1) << " sent: " << data_message.sent();
for (int i = 0; i < data_message.app_data_size(); ++i) {
DVLOG(1) << " App data " << i << " "
<< data_message.app_data(i).key() << " : "
<< data_message.app_data(i).value();
}
}
}
void MessageSentCallback(const std::string& local_id) {
LOG(INFO) << "Message sent. Status: " << local_id;
}
// Needed to use a real host resolver.
class MyTestURLRequestContext : public net::TestURLRequestContext {
public:
MyTestURLRequestContext() : TestURLRequestContext(true) {
context_storage_.set_host_resolver(
net::HostResolver::CreateDefaultResolver(NULL));
context_storage_.set_transport_security_state(
new net::TransportSecurityState());
Init();
}
virtual ~MyTestURLRequestContext() {}
};
class MyTestURLRequestContextGetter : public net::TestURLRequestContextGetter {
public:
explicit MyTestURLRequestContextGetter(
const scoped_refptr<base::MessageLoopProxy>& io_message_loop_proxy)
: TestURLRequestContextGetter(io_message_loop_proxy) {}
virtual net::TestURLRequestContext* GetURLRequestContext() OVERRIDE {
// Construct |context_| lazily so it gets constructed on the right
// thread (the IO thread).
if (!context_)
context_.reset(new MyTestURLRequestContext());
return context_.get();
}
private:
virtual ~MyTestURLRequestContextGetter() {}
scoped_ptr<MyTestURLRequestContext> context_;
};
// A net log that logs all events by default.
class MyTestNetLog : public net::NetLog {
public:
MyTestNetLog() {
SetBaseLogLevel(LOG_ALL);
}
virtual ~MyTestNetLog() {}
};
// A cert verifier that access all certificates.
class MyTestCertVerifier : public net::CertVerifier {
public:
MyTestCertVerifier() {}
virtual ~MyTestCertVerifier() {}
virtual int Verify(net::X509Certificate* cert,
const std::string& hostname,
int flags,
net::CRLSet* crl_set,
net::CertVerifyResult* verify_result,
const net::CompletionCallback& callback,
RequestHandle* out_req,
const net::BoundNetLog& net_log) OVERRIDE {
return net::OK;
}
virtual void CancelRequest(RequestHandle req) OVERRIDE {
// Do nothing.
}
};
class MCSProbe {
public:
MCSProbe(
const CommandLine& command_line,
scoped_refptr<net::URLRequestContextGetter> url_request_context_getter);
~MCSProbe();
void Start();
uint64 android_id() const { return android_id_; }
uint64 secret() const { return secret_; }
private:
void InitializeNetworkState();
void BuildNetworkSession();
void InitializationCallback(bool success,
uint64 restored_android_id,
uint64 restored_security_token);
CommandLine command_line_;
base::FilePath rmq_path_;
uint64 android_id_;
uint64 secret_;
std::string server_host_;
int server_port_;
// Network state.
scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
MyTestNetLog net_log_;
scoped_ptr<net::NetLogLogger> logger_;
scoped_ptr<base::Value> net_constants_;
scoped_ptr<net::HostResolver> host_resolver_;
scoped_ptr<net::CertVerifier> cert_verifier_;
scoped_ptr<net::ServerBoundCertService> system_server_bound_cert_service_;
scoped_ptr<net::TransportSecurityState> transport_security_state_;
scoped_ptr<net::URLSecurityManager> url_security_manager_;
scoped_ptr<net::HttpAuthHandlerFactory> http_auth_handler_factory_;
scoped_ptr<net::HttpServerPropertiesImpl> http_server_properties_;
scoped_ptr<net::HostMappingRules> host_mapping_rules_;
scoped_refptr<net::HttpNetworkSession> network_session_;
scoped_ptr<net::ProxyService> proxy_service_;
scoped_ptr<MCSClient> mcs_client_;
scoped_ptr<ConnectionFactoryImpl> connection_factory_;
base::Thread file_thread_;
scoped_ptr<base::RunLoop> run_loop_;
};
MCSProbe::MCSProbe(
const CommandLine& command_line,
scoped_refptr<net::URLRequestContextGetter> url_request_context_getter)
: command_line_(command_line),
rmq_path_(base::FilePath(FILE_PATH_LITERAL("gcm_rmq_store"))),
android_id_(0),
secret_(0),
server_port_(0),
url_request_context_getter_(url_request_context_getter),
file_thread_("FileThread") {
if (command_line.HasSwitch(kRMQFileName)) {
rmq_path_ = command_line.GetSwitchValuePath(kRMQFileName);
}
if (command_line.HasSwitch(kAndroidIdSwitch)) {
base::StringToUint64(command_line.GetSwitchValueASCII(kAndroidIdSwitch),
&android_id_);
}
if (command_line.HasSwitch(kSecretSwitch)) {
base::StringToUint64(command_line.GetSwitchValueASCII(kSecretSwitch),
&secret_);
}
server_host_ = kMCSServerHost;
if (command_line.HasSwitch(kServerHostSwitch)) {
server_host_ = command_line.GetSwitchValueASCII(kServerHostSwitch);
}
server_port_ = kMCSServerPort;
if (command_line.HasSwitch(kServerPortSwitch)) {
base::StringToInt(command_line.GetSwitchValueASCII(kServerPortSwitch),
&server_port_);
}
}
MCSProbe::~MCSProbe() {
file_thread_.Stop();
}
void MCSProbe::Start() {
file_thread_.Start();
InitializeNetworkState();
BuildNetworkSession();
connection_factory_.reset(
new ConnectionFactoryImpl(GURL("https://" + net::HostPortPair(
server_host_, server_port_).ToString()),
network_session_,
&net_log_));
mcs_client_.reset(new MCSClient(rmq_path_,
connection_factory_.get(),
file_thread_.message_loop_proxy()));
run_loop_.reset(new base::RunLoop());
mcs_client_->Initialize(base::Bind(&MCSProbe::InitializationCallback,
base::Unretained(this)),
base::Bind(&MessageReceivedCallback),
base::Bind(&MessageSentCallback));
run_loop_->Run();
}
void MCSProbe::InitializeNetworkState() {
FILE* log_file = NULL;
if (command_line_.HasSwitch(kLogFileSwitch)) {
base::FilePath log_path = command_line_.GetSwitchValuePath(kLogFileSwitch);
#if defined(OS_WIN)
log_file = _wfopen(log_path.value().c_str(), L"w");
#elif defined(OS_POSIX)
log_file = fopen(log_path.value().c_str(), "w");
#endif
}
net_constants_.reset(net::NetLogLogger::GetConstants());
if (log_file != NULL) {
logger_.reset(new net::NetLogLogger(log_file, *net_constants_));
logger_->StartObserving(&net_log_);
}
host_resolver_ = net::HostResolver::CreateDefaultResolver(&net_log_);
if (command_line_.HasSwitch(kIgnoreCertSwitch)) {
cert_verifier_.reset(new MyTestCertVerifier());
} else {
cert_verifier_.reset(net::CertVerifier::CreateDefault());
}
system_server_bound_cert_service_.reset(
new net::ServerBoundCertService(
new net::DefaultServerBoundCertStore(NULL),
base::WorkerPool::GetTaskRunner(true)));
transport_security_state_.reset(new net::TransportSecurityState());
url_security_manager_.reset(net::URLSecurityManager::Create(NULL, NULL));
http_auth_handler_factory_.reset(
net::HttpAuthHandlerRegistryFactory::Create(
std::vector<std::string>(1, "basic"),
url_security_manager_.get(),
host_resolver_.get(),
std::string(),
false,
false));
http_server_properties_.reset(new net::HttpServerPropertiesImpl());
host_mapping_rules_.reset(new net::HostMappingRules());
proxy_service_.reset(net::ProxyService::CreateDirectWithNetLog(&net_log_));
}
void MCSProbe::BuildNetworkSession() {
net::HttpNetworkSession::Params session_params;
session_params.host_resolver = host_resolver_.get();
session_params.cert_verifier = cert_verifier_.get();
session_params.server_bound_cert_service =
system_server_bound_cert_service_.get();
session_params.transport_security_state = transport_security_state_.get();
session_params.ssl_config_service = new net::SSLConfigServiceDefaults();
session_params.http_auth_handler_factory = http_auth_handler_factory_.get();
session_params.http_server_properties =
http_server_properties_->GetWeakPtr();
session_params.network_delegate = NULL; // TODO(zea): implement?
session_params.host_mapping_rules = host_mapping_rules_.get();
session_params.ignore_certificate_errors = true;
session_params.http_pipelining_enabled = false;
session_params.testing_fixed_http_port = 0;
session_params.testing_fixed_https_port = 0;
session_params.net_log = &net_log_;
session_params.proxy_service = proxy_service_.get();
network_session_ = new net::HttpNetworkSession(session_params);
}
void MCSProbe::InitializationCallback(bool success,
uint64 restored_android_id,
uint64 restored_security_token) {
LOG(INFO) << "Initialization " << (success ? "success!" : "failure!");
if (restored_android_id && restored_security_token) {
android_id_ = restored_android_id;
secret_ = restored_security_token;
}
if (success)
mcs_client_->Login(android_id_, secret_);
}
int MCSProbeMain(int argc, char* argv[]) {
base::AtExitManager exit_manager;
CommandLine::Init(argc, argv);
logging::LoggingSettings settings;
settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
logging::InitLogging(settings);
base::MessageLoopForIO message_loop;
// For check-in and creating registration ids.
const scoped_refptr<MyTestURLRequestContextGetter> context_getter =
new MyTestURLRequestContextGetter(
base::MessageLoop::current()->message_loop_proxy());
const CommandLine& command_line = *CommandLine::ForCurrentProcess();
MCSProbe mcs_probe(command_line, context_getter);
mcs_probe.Start();
base::RunLoop run_loop;
run_loop.Run();
return 0;
}
} // namespace
} // namespace gcm
int main(int argc, char* argv[]) {
return gcm::MCSProbeMain(argc, argv);
}
|