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
|
// Copyright (c) 2012 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 <string>
#include "base/rand_util.h"
#include "chrome/common/url_constants.h"
#include "chrome_frame/test/mock_ie_event_sink_actions.h"
#include "chrome_frame/test/mock_ie_event_sink_test.h"
#include "components/autofill/browser/webdata/autofill_table.h"
#include "components/webdata/common/web_database.h"
#include "components/webdata/common/webdata_constants.h"
using testing::_;
namespace chrome_frame_test {
class DeleteBrowsingHistoryTest
: public MockIEEventSinkTest,
public testing::Test {
public:
DeleteBrowsingHistoryTest() {}
virtual void SetUp() {
// We will use the OnAccLoad event to monitor page loads, so we ignore
// these.
ie_mock_.ExpectAnyNavigations();
ie_mock2_.ExpectAnyNavigations();
ie_mock3_.ExpectAnyNavigations();
EXPECT_CALL(acc_observer_, OnAccDocLoad(_)).Times(testing::AnyNumber());
// Use a random image_path to ensure that a prior run does not
// interfere with our expectations about caching.
image_path_ = L"/" + RandomChars(32);
topHtml =
"<html><head>"
"<meta http-equiv=\"x-ua-compatible\" content=\"chrome=1\" />"
"</head>"
"<body>"
"<form method=\"POST\" action=\"/form\">"
"<input title=\"username\" type=\"text\" name=\"username\" />"
"<input type=\"submit\" title=\"Submit\" name=\"Submit\" />"
"</form>"
"<img alt=\"Blank image.\" src=\"" + WideToASCII(image_path_) + "\" />"
"This is some text.</body></html>";
}
protected:
std::wstring image_path_;
std::string topHtml;
testing::NiceMock<MockAccEventObserver> acc_observer_;
MockWindowObserver delete_browsing_history_window_observer_mock_;
MockObjectWatcherDelegate ie_process_exit_watcher_mock_;
testing::StrictMock<MockIEEventSink> ie_mock2_;
testing::StrictMock<MockIEEventSink> ie_mock3_;
// Returns a string of |count| lowercase random characters.
static std::wstring RandomChars(int count) {
srand(static_cast<unsigned int>(time(NULL)));
std::wstring str;
for (int i = 0; i < count; ++i)
str += L'a' + base::RandInt(0, 25);
return str;
}
};
namespace {
const wchar_t* kFormFieldName = L"username";
const wchar_t* kFormFieldValue = L"test_username";
const char* kHtmlHttpHeaders =
"HTTP/1.1 200 OK\r\n"
"Connection: close\r\n"
"Content-Type: text/html\r\n";
const char* kFormResultHtml =
"<html><head><meta http-equiv=\"x-ua-compatible\" content=\"chrome=1\" />"
"</head><body>Nice work.</body></html>";
const char* kBlankPngResponse[] = {
"HTTP/1.1 200 OK\r\n"
"Connection: close\r\n"
"Content-Type: image/png\r\n"
"Cache-Control: max-age=3600, must-revalidate\r\n",
"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52"
"\x00\x00\x00\x01\x00\x00\x00\x01\x01\x03\x00\x00\x00\x25\xdb\x56"
"\xca\x00\x00\x00\x03\x50\x4c\x54\x45\x00\x00\x00\xa7\x7a\x3d\xda"
"\x00\x00\x00\x01\x74\x52\x4e\x53\x00\x40\xe6\xd8\x66\x00\x00\x00"
"\x0a\x49\x44\x41\x54\x08\xd7\x63\x60\x00\x00\x00\x02\x00\x01\xe2"
"\x21\xbc\x33\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82"};
const size_t kBlankPngFileLength = 95;
} // anonymous namespace
// Looks up |element_name| in the Chrome form data DB and ensures that the
// results match |matcher|.
ACTION_P2(ExpectFormValuesForElementNameMatch, element_name, matcher) {
base::FilePath root_path;
GetChromeFrameProfilePath(kIexploreProfileName, &root_path);
base::FilePath profile_path(
root_path.Append(L"Default").Append(kWebDataFilename));
autofill::AutofillTable autofill_table("en-US");
WebDatabase web_database;
web_database.AddTable(&autofill_table);
sql::InitStatus init_status = web_database.Init(profile_path);
EXPECT_EQ(sql::INIT_OK, init_status);
if (init_status == sql::INIT_OK) {
std::vector<string16> values;
autofill_table.GetFormValuesForElementName(
element_name, L"", &values, 9999);
EXPECT_THAT(values, matcher);
}
}
// Launch |ie_mock| and navigate it to |url|.
ACTION_P2(LaunchThisIEAndNavigate, ie_mock, url) {
EXPECT_HRESULT_SUCCEEDED(ie_mock->event_sink()->LaunchIEAndNavigate(url,
ie_mock));
}
// Listens for OnAccLoad and OnLoad events for an IE instance and
// sends a single signal once both have been received.
//
// Allows tests to wait for both events to occur irrespective of their relative
// ordering.
class PageLoadHelper {
public:
explicit PageLoadHelper(testing::StrictMock<MockIEEventSink>* ie_mock)
: received_acc_load_(false),
received_on_load_(false),
ie_mock_(ie_mock) {
EXPECT_CALL(*ie_mock_, OnLoad(_, _))
.Times(testing::AnyNumber())
.WillRepeatedly(testing::InvokeWithoutArgs(
this, &PageLoadHelper::HandleOnLoad));
EXPECT_CALL(acc_observer_, OnAccDocLoad(_))
.Times(testing::AnyNumber())
.WillRepeatedly(testing::Invoke(this, &PageLoadHelper::HandleAccLoad));
}
void HandleAccLoad(HWND hwnd) {
ReconcileHwnds(hwnd, &acc_loaded_hwnds_, &on_loaded_hwnds_);
}
void HandleOnLoad() {
HWND hwnd = ie_mock_->event_sink()->GetRendererWindow();
ReconcileHwnds(hwnd, &on_loaded_hwnds_, &acc_loaded_hwnds_);
}
MOCK_METHOD0(OnLoadComplete, void());
private:
void ReconcileHwnds(HWND signaled_hwnd,
std::set<HWND>* signaled_hwnd_set,
std::set<HWND>* other_hwnd_set) {
if (other_hwnd_set->erase(signaled_hwnd) != 0) {
OnLoadComplete();
} else {
signaled_hwnd_set->insert(signaled_hwnd);
}
}
std::set<HWND> acc_loaded_hwnds_;
std::set<HWND> on_loaded_hwnds_;
bool received_acc_load_;
bool received_on_load_;
testing::StrictMock<MockIEEventSink>* ie_mock_;
testing::NiceMock<MockAccEventObserver> acc_observer_;
};
TEST_F(DeleteBrowsingHistoryTest, DISABLED_CFDeleteBrowsingHistory) {
if (GetInstalledIEVersion() < IE_8) {
LOG(ERROR) << "Test does not apply to IE versions < 8.";
return;
}
PageLoadHelper load_helper(&ie_mock_);
PageLoadHelper load_helper2(&ie_mock2_);
PageLoadHelper load_helper3(&ie_mock3_);
delete_browsing_history_window_observer_mock_.WatchWindow(
"Delete Browsing History", "");
// For some reason, this page is occasionally being cached, so we randomize
// its name to ensure that, at least the first time we request it, it is
// retrieved.
std::wstring top_name = RandomChars(32);
std::wstring top_url = server_mock_.Resolve(top_name);
std::wstring top_path = L"/" + top_name;
// Even still, it might not be hit the second or third time, so let's just
// not worry about how often or whether it's called
EXPECT_CALL(server_mock_, Get(_, testing::StrEq(top_path), _))
.Times(testing::AnyNumber())
.WillRepeatedly(SendFast(kHtmlHttpHeaders, topHtml));
testing::InSequence expect_in_sequence_for_scope;
// First launch will hit the server, requesting top.html and then image_path_
EXPECT_CALL(server_mock_, Get(_, testing::StrEq(image_path_), _))
.WillOnce(SendFast(kBlankPngResponse[0],
std::string(kBlankPngResponse[1],
kBlankPngFileLength)));
// top.html contains a form. Fill in the username field and submit, causing
// the value to be stored in Chrome's form data DB.
EXPECT_CALL(load_helper, OnLoadComplete())
.WillOnce(testing::DoAll(
AccLeftClickInRenderer(&ie_mock_, AccObjectMatcher(L"username")),
PostKeyMessagesToRenderer(&ie_mock_, WideToASCII(kFormFieldValue)),
AccLeftClickInRenderer(&ie_mock_, AccObjectMatcher(L"Submit"))));
EXPECT_CALL(server_mock_, Post(_, testing::StrEq(L"/form"), _))
.WillOnce(SendFast(kHtmlHttpHeaders, kFormResultHtml));
// OnLoad of the result page from form submission. Now close the browser.
EXPECT_CALL(load_helper, OnLoadComplete())
.WillOnce(testing::DoAll(
WatchRendererProcess(&ie_process_exit_watcher_mock_, &ie_mock_),
CloseBrowserMock(&ie_mock_)));
EXPECT_CALL(ie_mock_, OnQuit());
// Wait until the process is gone, so that the Chrome databases are unlocked.
// Verify that the submitted username is in the database, then launch a new
// IE instance.
EXPECT_CALL(ie_process_exit_watcher_mock_, OnObjectSignaled(_))
.WillOnce(testing::DoAll(
ExpectFormValuesForElementNameMatch(
kFormFieldName, testing::Contains(kFormFieldValue)),
LaunchThisIEAndNavigate(&ie_mock2_, top_url)));
// Second launch won't load the image due to the cache.
// We do the delete private data twice, each time toggling the state of the
// 'Delete form data' and 'Delete temporary files' options.
// That's because we have no way to know their initial states. Using this,
// trick we are guaranteed to run it exactly once with each option turned on.
// Running it once with the option turned off is harmless.
// Proceed to open up the "Safety" menu for the first time through the loop.
EXPECT_CALL(load_helper2, OnLoadComplete())
.WillOnce(AccDoDefaultActionInBrowser(&ie_mock2_,
AccObjectMatcher(L"Safety")));
// Store the dialog and progress_bar HWNDs for each iteration
// in order to distinguish between the OnClose of each.
HWND dialog[] = {NULL, NULL};
HWND progress_bar[] = {NULL, NULL};
for (int i = 0; i < 2; ++i) {
// Watch for the popup menu, click 'Delete Browsing History...'
EXPECT_CALL(acc_observer_, OnMenuPopup(_))
.WillOnce(
AccLeftClick(AccObjectMatcher(L"Delete Browsing History...*")));
// When it shows up, toggle the options and click "Delete".
EXPECT_CALL(delete_browsing_history_window_observer_mock_, OnWindowOpen(_))
.WillOnce(testing::DoAll(
testing::SaveArg<0>(&dialog[i]),
AccLeftClick(AccObjectMatcher(L"Temporary Internet files")),
AccLeftClick(AccObjectMatcher(L"Form data")),
AccLeftClick(AccObjectMatcher(L"Delete"))));
// The configuration dialog closes.
// This is not reliably ordered with respect to the following OnWindowOpen.
// Specifying 'AnyNumber' of times allows us to disregard it, although we
// can't avoid receiving the call.
EXPECT_CALL(delete_browsing_history_window_observer_mock_,
OnWindowClose(testing::Eq(testing::ByRef(dialog[i]))))
.Times(testing::AnyNumber());
// The progress dialog that pops up has the same caption.
EXPECT_CALL(delete_browsing_history_window_observer_mock_,
OnWindowOpen(_)).WillOnce(testing::SaveArg<0>(&progress_bar[i]));
// Watch for it to go away, then either do the "Delete History" again or
// close the browser.
// In either case, validate the contents of the renderer to ensure that
// we didn't cause Chrome to crash.
if (i == 0) {
EXPECT_CALL(delete_browsing_history_window_observer_mock_,
OnWindowClose(testing::Eq(testing::ByRef(progress_bar[i]))))
.WillOnce(testing::DoAll(
AccExpectInRenderer(&ie_mock2_,
AccObjectMatcher(L"Blank image.")),
AccDoDefaultActionInBrowser(&ie_mock2_,
AccObjectMatcher(L"Safety"))));
} else {
EXPECT_CALL(delete_browsing_history_window_observer_mock_,
OnWindowClose(testing::Eq(testing::ByRef(progress_bar[i]))))
.WillOnce(testing::DoAll(
AccExpectInRenderer(&ie_mock2_,
AccObjectMatcher(L"Blank image.")),
WatchRendererProcess(&ie_process_exit_watcher_mock_,
&ie_mock2_),
CloseBrowserMock(&ie_mock2_)));
}
}
EXPECT_CALL(ie_mock2_, OnQuit());
// When the process is actually exited, and the DB has been released, verify
// that the remembered form data is not in the form data DB.
EXPECT_CALL(ie_process_exit_watcher_mock_, OnObjectSignaled(_))
.WillOnce(testing::DoAll(
ExpectFormValuesForElementNameMatch(
kFormFieldName, testing::Not(testing::Contains(kFormFieldValue))),
LaunchThisIEAndNavigate(&ie_mock3_, top_url)));
// Now that the cache is cleared, final session should load the image from the
// server.
EXPECT_CALL(server_mock_, Get(_, testing::StrEq(image_path_), _))
.WillOnce(
SendFast(kBlankPngResponse[0], std::string(kBlankPngResponse[1],
kBlankPngFileLength)));
EXPECT_CALL(load_helper3, OnLoadComplete())
.WillOnce(CloseBrowserMock(&ie_mock3_));
EXPECT_CALL(ie_mock3_, OnQuit())
.WillOnce(QUIT_LOOP(loop_));
// Start it up. Everything else is triggered as mock actions.
ASSERT_HRESULT_SUCCEEDED(
ie_mock_.event_sink()->LaunchIEAndNavigate(top_url, &ie_mock_));
// 3 navigations + 2 invocations of delete browser history == 5
loop_.RunFor(kChromeFrameLongNavigationTimeout * 5);
}
} // namespace chrome_frame_test
|