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

Side by Side Diff: common/ping_test.cc

Issue 624713003: Keep only base/extractor.[cc|h]. (Closed) Base URL: https://chromium.googlesource.com/external/omaha.git@master
Patch Set: Created 6 years, 2 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
« no previous file with comments | « common/ping_event.cc ('k') | common/progress_sampler.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2010 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 // ========================================================================
15
16 #include <string.h>
17 #include "omaha/base/string.h"
18 #include "omaha/base/logging.h"
19 #include "omaha/base/omaha_version.h"
20 #include "omaha/base/utils.h"
21 #include "omaha/common/command_line.h"
22 #include "omaha/common/ping.h"
23 #include "omaha/testing/unit_test.h"
24
25 namespace omaha {
26
27 class PingTest : public testing::Test {
28 };
29
30 TEST_F(PingTest, BuildOmahaPing) {
31 PingEventPtr ping_event1(
32 new PingEvent(PingEvent::EVENT_INSTALL_COMPLETE,
33 PingEvent::EVENT_RESULT_SUCCESS,
34 10,
35 20));
36
37 PingEventPtr ping_event2(
38 new PingEvent(PingEvent::EVENT_INSTALL_COMPLETE,
39 PingEvent::EVENT_RESULT_SUCCESS,
40 30,
41 40));
42
43 CommandLineExtraArgs command_line_extra_args;
44 StringToGuidSafe(_T("{DE06587E-E5AB-4364-A46B-F3AC733007B3}"),
45 &command_line_extra_args.installation_id);
46 command_line_extra_args.brand_code = _T("GGLS");
47 command_line_extra_args.client_id = _T("a client id");
48 command_line_extra_args.language = _T("en");
49
50 // Machine ping.
51 Ping install_ping(true, _T("session"), _T("oneclick"));
52 install_ping.LoadAppDataFromExtraArgs(command_line_extra_args);
53 install_ping.BuildOmahaPing(_T("1.0.0.0"),
54 _T("2.0.0.0"),
55 ping_event1,
56 ping_event2);
57
58 CString expected_ping_request_substring;
59 expected_ping_request_substring.Format(_T("<app appid=\"{430FD4D0-B729-4F61-AA 34-91526481799D}\" version=\"1.0.0.0\" nextversion=\"2.0.0.0\" lang=\"en\" brand =\"GGLS\" client=\"a client id\" iid=\"{DE06587E-E5AB-4364-A46B-F3AC733007B3}\"> <event eventtype=\"2\" eventresult=\"1\" errorcode=\"10\" extracode1=\"20\"/><ev ent eventtype=\"2\" eventresult=\"1\" errorcode=\"30\" extracode1=\"40\"/></app> ")); // NOLINT
60
61 CString actual_ping_request;
62 install_ping.BuildRequestString(&actual_ping_request);
63
64 // The ping_request_string contains some data that depends on the machine
65 // environment, such as operating system version. Look for a partial match in
66 // the string corresponding to the <app> element.
67 EXPECT_NE(-1, actual_ping_request.Find(expected_ping_request_substring));
68 }
69
70 TEST_F(PingTest, BuildAppsPing) {
71 const TCHAR* const kOmahaUserClientStatePath =
72 _T("HKCU\\Software\\") SHORT_COMPANY_NAME _T("\\") PRODUCT_NAME
73 _T("\\ClientState\\") GOOPDATE_APP_ID;
74
75 const CString expected_pv = _T("1.3.23.0");
76 const CString expected_lang = _T("en");
77 const CString expected_brand_code = _T("GGLS");
78 const CString expected_client_id = _T("someclientid");
79 const CString expected_iid =
80 _T("{7C0B6E56-B24B-436b-A960-A6EA201E886F}");
81 const CString expected_experiment_label =
82 _T("some_experiment=a|Fri, 14 Aug 2015 16:13:03 GMT");
83
84 EXPECT_HRESULT_SUCCEEDED(RegKey::SetValue(kOmahaUserClientStatePath,
85 kRegValueProductVersion,
86 expected_pv));
87 EXPECT_HRESULT_SUCCEEDED(RegKey::SetValue(kOmahaUserClientStatePath,
88 kRegValueLanguage,
89 expected_lang));
90 EXPECT_HRESULT_SUCCEEDED(RegKey::SetValue(kOmahaUserClientStatePath,
91 kRegValueBrandCode,
92 expected_brand_code));
93 EXPECT_HRESULT_SUCCEEDED(RegKey::SetValue(kOmahaUserClientStatePath,
94 kRegValueClientId,
95 expected_client_id));
96 EXPECT_HRESULT_SUCCEEDED(RegKey::SetValue(kOmahaUserClientStatePath,
97 kRegValueInstallationId,
98 expected_iid));
99 EXPECT_HRESULT_SUCCEEDED(RegKey::SetValue(kOmahaUserClientStatePath,
100 kRegValueExperimentLabels,
101 expected_experiment_label));
102
103 PingEventPtr ping_event(
104 new PingEvent(PingEvent::EVENT_INSTALL_COMPLETE,
105 PingEvent::EVENT_RESULT_SUCCESS,
106 34,
107 6));
108
109 Ping apps_ping(false, _T("unittest"), _T("InstallSource_Foo"));
110 std::vector<CString> apps;
111 apps.push_back(GOOPDATE_APP_ID);
112 apps_ping.LoadAppDataFromRegistry(apps);
113 apps_ping.BuildAppsPing(ping_event);
114
115 CString expected_ping_request_substring;
116 expected_ping_request_substring.Format(_T("<app appid=\"{430FD4D0-B729-4F61-AA 34-91526481799D}\" version=\"1.3.23.0\" nextversion=\"\" lang=\"en\" brand=\"GGL S\" client=\"someclientid\" experiments=\"some_experiment=a|Fri, 14 Aug 2015 16: 13:03 GMT\" iid=\"{7C0B6E56-B24B-436b-A960-A6EA201E886F}\"><event eventtype=\"2\ " eventresult=\"1\" errorcode=\"34\" extracode1=\"6\"/></app>")); // NOLINT
117
118 CString actual_ping_request;
119 apps_ping.BuildRequestString(&actual_ping_request);
120
121 EXPECT_NE(-1, actual_ping_request.Find(expected_ping_request_substring));
122 }
123
124 TEST_F(PingTest, SendString) {
125 CString request_string = _T("<?xml version=\"1.0\" encoding=\"UTF-8\"?><reques t protocol=\"3.0\" version=\"1.3.23.0\" ismachine=\"1\" sessionid=\"unittest\" i nstallsource=\"oneclick\" testsource=\"dev\" requestid=\"{EC821C33-E4EE-4E75-BC8 5-7E9DFC3652F5}\" periodoverridesec=\"7407360\"><os platform=\"win\" version=\"6 .0\" sp=\"Service Pack 1\"/><app appid=\"{430FD4D0-B729-4F61-AA34-91526481799D}\ " version=\"1.0.0.0\" nextversion=\"2.0.0.0\" lang=\"en\" brand=\"GGLS\" client= \"a client id\" iid=\"{DE06587E-E5AB-4364-A46B-F3AC733007B3}\"><event eventtype= \"10\" eventresult=\"1\" errorcode=\"0\" extracode1=\"0\"/></app></request>"); // NOLINT
126 EXPECT_HRESULT_SUCCEEDED(Ping::SendString(false,
127 HeadersVector(),
128 request_string));
129
130 // 400 Bad Request returned by the server.
131 EXPECT_EQ(0x80042190, Ping::SendString(false, HeadersVector(), _T("")));
132 }
133
134 TEST_F(PingTest, HandlePing) {
135 CString request_string = _T("<?xml version=\"1.0\" encoding=\"UTF-8\"?><reques t protocol=\"3.0\" version=\"1.3.23.0\" ismachine=\"1\" sessionid=\"unittest\" i nstallsource=\"oneclick\" testsource=\"dev\" requestid=\"{EC821C33-E4EE-4E75-BC8 5-7E9DFC3652F5}\" periodoverridesec=\"7407360\"><os platform=\"win\" version=\"6 .0\" sp=\"Service Pack 1\"/><app appid=\"{430FD4D0-B729-4F61-AA34-91526481799D}\ " version=\"1.0.0.0\" nextversion=\"2.0.0.0\" lang=\"en\" brand=\"GGLS\" client= \"a client id\" iid=\"{DE06587E-E5AB-4364-A46B-F3AC733007B3}\"><event eventtype= \"10\" eventresult=\"1\" errorcode=\"0\" extracode1=\"0\"/></app></request>"); // NOLINT
136
137 CStringA request_string_utf8(WideToUtf8(request_string));
138 CStringA ping_string_utf8;
139 WebSafeBase64Escape(request_string_utf8, &ping_string_utf8);
140
141 EXPECT_HRESULT_SUCCEEDED(
142 Ping::HandlePing(false, Utf8ToWideChar(ping_string_utf8,
143 ping_string_utf8.GetLength())));
144
145 // 400 Bad Request returned by the server.
146 EXPECT_EQ(0x80042190, Ping::HandlePing(false, _T("")));
147 }
148
149 TEST_F(PingTest, SendInProcess) {
150 PingEventPtr ping_event(
151 new PingEvent(PingEvent::EVENT_INSTALL_COMPLETE,
152 PingEvent::EVENT_RESULT_SUCCESS,
153 0,
154 0));
155
156 CommandLineExtraArgs command_line_extra_args;
157 StringToGuidSafe(_T("{DE06587E-E5AB-4364-A46B-F3AC733007B3}"),
158 &command_line_extra_args.installation_id);
159 command_line_extra_args.brand_code = _T("GGLS");
160 command_line_extra_args.client_id = _T("a client id");
161 command_line_extra_args.language = _T("en");
162
163 // User ping.
164 Ping install_ping(false, _T("unittest"), _T("oneclick"));
165 install_ping.LoadAppDataFromExtraArgs(command_line_extra_args);
166 install_ping.BuildOmahaPing(_T("1.0.0.0"), _T("2.0.0.0"), ping_event);
167
168 CString request_string;
169 EXPECT_HRESULT_SUCCEEDED(install_ping.BuildRequestString(&request_string));
170 EXPECT_HRESULT_SUCCEEDED(install_ping.SendInProcess(request_string));
171 }
172
173 TEST_F(PingTest, IsPingExpired_PastTime) {
174 const time64 time = GetCurrent100NSTime() - (Ping::kPingExpiry100ns + 1);
175 EXPECT_TRUE(Ping::IsPingExpired(time));
176 }
177
178 TEST_F(PingTest, IsPingExpired_CurrentTime) {
179 const time64 time = GetCurrent100NSTime();
180 EXPECT_FALSE(Ping::IsPingExpired(time));
181 }
182
183 TEST_F(PingTest, IsPingExpired_FutureTime) {
184 const time64 time = GetCurrent100NSTime() + 10;
185 EXPECT_TRUE(Ping::IsPingExpired(time));
186 }
187
188 TEST_F(PingTest, LoadPersistedPings_NoPersistedPings) {
189 Ping::PingsVector pings;
190 EXPECT_EQ(HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND),
191 Ping::LoadPersistedPings(false, &pings));
192 EXPECT_EQ(0, pings.size());
193 }
194
195 TEST_F(PingTest, LoadPersistedPings) {
196 CString ping_reg_path(Ping::GetPingRegPath(false));
197
198 EXPECT_HRESULT_SUCCEEDED(RegKey::SetValue(ping_reg_path,
199 _T("1"),
200 _T("Test Ping String 1")));
201 EXPECT_HRESULT_SUCCEEDED(RegKey::SetValue(ping_reg_path,
202 _T("2"),
203 _T("Test Ping String 2")));
204 EXPECT_HRESULT_SUCCEEDED(RegKey::SetValue(ping_reg_path,
205 _T("3"),
206 _T("Test Ping String 3")));
207
208 Ping::PingsVector pings;
209 EXPECT_HRESULT_SUCCEEDED(Ping::LoadPersistedPings(false, &pings));
210 EXPECT_EQ(3, pings.size());
211
212 EXPECT_EQ(1, pings[0].first);
213 EXPECT_EQ(2, pings[1].first);
214 EXPECT_EQ(3, pings[2].first);
215 EXPECT_STREQ(_T("Test Ping String 1"), pings[0].second);
216 EXPECT_STREQ(_T("Test Ping String 2"), pings[1].second);
217 EXPECT_STREQ(_T("Test Ping String 3"), pings[2].second);
218
219 EXPECT_HRESULT_SUCCEEDED(RegKey::DeleteKey(ping_reg_path));
220 }
221
222 TEST_F(PingTest, PersistPing) {
223 EXPECT_HRESULT_SUCCEEDED(Ping::PersistPing(false, _T("Test Ping String 1")));
224 ::Sleep(15);
225 EXPECT_HRESULT_SUCCEEDED(Ping::PersistPing(false, _T("Test Ping String 2")));
226 ::Sleep(15);
227 EXPECT_HRESULT_SUCCEEDED(Ping::PersistPing(false, _T("Test Ping String 3")));
228
229 Ping::PingsVector pings;
230 EXPECT_HRESULT_SUCCEEDED(Ping::LoadPersistedPings(false, &pings));
231 EXPECT_EQ(3, pings.size());
232
233 EXPECT_FALSE(Ping::IsPingExpired(pings[0].first));
234 EXPECT_FALSE(Ping::IsPingExpired(pings[1].first));
235 EXPECT_FALSE(Ping::IsPingExpired(pings[2].first));
236 EXPECT_STREQ(_T("Test Ping String 1"), pings[0].second);
237 EXPECT_STREQ(_T("Test Ping String 2"), pings[1].second);
238 EXPECT_STREQ(_T("Test Ping String 3"), pings[2].second);
239
240 EXPECT_HRESULT_SUCCEEDED(RegKey::DeleteKey(Ping::GetPingRegPath(false)));
241 }
242
243 TEST_F(PingTest, DeletePersistedPing) {
244 CString ping_reg_path(Ping::GetPingRegPath(false));
245
246 EXPECT_HRESULT_SUCCEEDED(RegKey::SetValue(ping_reg_path,
247 _T("1"),
248 _T("Test Ping String 1")));
249 EXPECT_HRESULT_SUCCEEDED(RegKey::SetValue(ping_reg_path,
250 _T("2"),
251 _T("Test Ping String 2")));
252
253 EXPECT_HRESULT_SUCCEEDED(Ping::DeletePersistedPing(false, 1));
254 EXPECT_HRESULT_SUCCEEDED(Ping::DeletePersistedPing(false, 2));
255
256 EXPECT_FALSE(RegKey::HasKey(ping_reg_path));
257 }
258
259 TEST_F(PingTest, SendPersistedPings) {
260 PingEventPtr ping_event(
261 new PingEvent(PingEvent::EVENT_INSTALL_COMPLETE,
262 PingEvent::EVENT_RESULT_SUCCESS,
263 0,
264 0));
265
266 CommandLineExtraArgs command_line_extra_args;
267 StringToGuidSafe(_T("{DE06587E-E5AB-4364-A46B-F3AC733007B3}"),
268 &command_line_extra_args.installation_id);
269 command_line_extra_args.brand_code = _T("GGLS");
270 command_line_extra_args.client_id = _T("a client id");
271 command_line_extra_args.language = _T("en");
272
273 // User ping.
274 Ping install_ping(false, _T("unittest"), _T("oneclick"));
275 install_ping.LoadAppDataFromExtraArgs(command_line_extra_args);
276 install_ping.BuildOmahaPing(_T("1.0.0.0"), _T("2.0.0.0"), ping_event);
277
278 CString request_string;
279 EXPECT_HRESULT_SUCCEEDED(install_ping.BuildRequestString(&request_string));
280 EXPECT_HRESULT_SUCCEEDED(Ping::PersistPing(false, request_string));
281
282 EXPECT_HRESULT_SUCCEEDED(Ping::SendPersistedPings(false));
283
284 EXPECT_FALSE(RegKey::HasKey(Ping::GetPingRegPath(false)));
285 }
286
287 // The tests below rely on the out-of-process mechanism to send install pings.
288 // Enable the test to debug the sending code.
289 TEST_F(PingTest, DISABLED_SendUsingGoogleUpdate) {
290 PingEventPtr ping_event(
291 new PingEvent(PingEvent::EVENT_INSTALL_COMPLETE,
292 PingEvent::EVENT_RESULT_SUCCESS,
293 0,
294 0));
295
296 CommandLineExtraArgs command_line_extra_args;
297 StringToGuidSafe(_T("{DE06587E-E5AB-4364-A46B-F3AC733007B3}"),
298 &command_line_extra_args.installation_id);
299 command_line_extra_args.brand_code = _T("GGLS");
300 command_line_extra_args.client_id = _T("a client id");
301 command_line_extra_args.language = _T("en");
302
303 // User ping and wait for completion.
304 Ping install_ping(false, _T("unittest"), _T("oneclick"));
305 install_ping.LoadAppDataFromExtraArgs(command_line_extra_args);
306 install_ping.BuildOmahaPing(_T("1.0.0.0"), _T("2.0.0.0"), ping_event);
307
308 const int kWaitForPingProcessToCompleteMs = 60000;
309 CString request_string;
310 EXPECT_HRESULT_SUCCEEDED(install_ping.BuildRequestString(&request_string));
311 EXPECT_HRESULT_SUCCEEDED(install_ping.SendUsingGoogleUpdate(
312 request_string, kWaitForPingProcessToCompleteMs));
313 }
314
315 TEST_F(PingTest, Send_Empty) {
316 CommandLineExtraArgs command_line_extra_args;
317 Ping install_ping(false, _T("unittest"), _T("oneclick"));
318 EXPECT_EQ(S_FALSE, install_ping.Send(false));
319 }
320
321 TEST_F(PingTest, DISABLED_Send) {
322 PingEventPtr ping_event(
323 new PingEvent(PingEvent::EVENT_INSTALL_COMPLETE,
324 PingEvent::EVENT_RESULT_SUCCESS,
325 0,
326 0));
327
328 CommandLineExtraArgs command_line_extra_args;
329 StringToGuidSafe(_T("{DE06587E-E5AB-4364-A46B-F3AC733007B3}"),
330 &command_line_extra_args.installation_id);
331 command_line_extra_args.brand_code = _T("GGLS");
332 command_line_extra_args.client_id = _T("a client id");
333 command_line_extra_args.language = _T("en");
334
335 // User ping and wait for completion.
336 Ping install_ping(false, _T("unittest"), _T("oneclick"));
337 install_ping.LoadAppDataFromExtraArgs(command_line_extra_args);
338 install_ping.BuildOmahaPing(_T("1.0.0.0"), _T("2.0.0.0"), ping_event);
339
340 EXPECT_HRESULT_SUCCEEDED(install_ping.Send(false));
341 }
342
343 TEST_F(PingTest, DISABLED_SendFireAndForget) {
344 PingEventPtr ping_event(
345 new PingEvent(PingEvent::EVENT_INSTALL_COMPLETE,
346 PingEvent::EVENT_RESULT_SUCCESS,
347 0,
348 0));
349
350 CommandLineExtraArgs command_line_extra_args;
351 StringToGuidSafe(_T("{DE06587E-E5AB-4364-A46B-F3AC733007B3}"),
352 &command_line_extra_args.installation_id);
353 command_line_extra_args.brand_code = _T("GGLS");
354 command_line_extra_args.client_id = _T("a client id");
355 command_line_extra_args.language = _T("en");
356
357 // User ping and do not wait for completion.
358 Ping install_ping(false, _T("unittest"), _T("oneclick"));
359 install_ping.LoadAppDataFromExtraArgs(command_line_extra_args);
360 install_ping.BuildOmahaPing(_T("1.0.0.0"), _T("2.0.0.0"), ping_event);
361
362 EXPECT_HRESULT_SUCCEEDED(install_ping.Send(true));
363 }
364
365 } // namespace omaha
366
OLDNEW
« no previous file with comments | « common/ping_event.cc ('k') | common/progress_sampler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698