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
|
// Copyright (c) 2009 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 <atlbase.h>
#include <atlcom.h>
#include "base/scoped_bstr_win.h"
#include "base/scoped_comptr_win.h"
#include "base/string_util.h"
#include "base/utf_string_conversions.h"
#include "chrome_frame/http_negotiate.h"
#include "chrome_frame/html_utils.h"
#include "chrome_frame/test/chrome_frame_test_utils.h"
#include "chrome_frame/utils.h"
#include "gtest/gtest.h"
#include "gmock/gmock.h"
class HttpNegotiateTest : public testing::Test {
protected:
HttpNegotiateTest() {
}
};
class TestHttpNegotiate
: public CComObjectRootEx<CComMultiThreadModel>,
public IHttpNegotiate {
public:
TestHttpNegotiate()
: beginning_transaction_ret_(S_OK), additional_headers_(NULL) {
}
BEGIN_COM_MAP(TestHttpNegotiate)
COM_INTERFACE_ENTRY(IHttpNegotiate)
END_COM_MAP()
STDMETHOD(BeginningTransaction)(LPCWSTR url, LPCWSTR headers, // NOLINT
DWORD reserved, // NOLINT
LPWSTR* additional_headers) { // NOLINT
if (additional_headers_) {
int len = lstrlenW(additional_headers_);
len++;
*additional_headers = reinterpret_cast<wchar_t*>(
::CoTaskMemAlloc(len * sizeof(wchar_t)));
lstrcpyW(*additional_headers, additional_headers_);
}
return beginning_transaction_ret_;
}
STDMETHOD(OnResponse)(DWORD response_code, LPCWSTR response_header,
LPCWSTR request_header,
LPWSTR* additional_request_headers) {
return S_OK;
}
HRESULT beginning_transaction_ret_;
const wchar_t* additional_headers_;
};
TEST_F(HttpNegotiateTest, BeginningTransaction) {
static const int kBeginningTransactionIndex = 3;
CComObjectStackEx<TestHttpNegotiate> test_http;
IHttpNegotiate_BeginningTransaction_Fn original =
reinterpret_cast<IHttpNegotiate_BeginningTransaction_Fn>(
(*reinterpret_cast<void***>(
static_cast<IHttpNegotiate*>(
&test_http)))[kBeginningTransactionIndex]);
std::wstring cf_ua(
ASCIIToWide(http_utils::GetDefaultUserAgentHeaderWithCFTag()));
std::wstring cf_tag(
ASCIIToWide(http_utils::GetChromeFrameUserAgent()));
EXPECT_NE(std::wstring::npos, cf_ua.find(cf_tag));
struct TestCase {
const std::wstring original_headers_;
const std::wstring delegate_additional_;
const std::wstring expected_additional_;
HRESULT delegate_return_value_;
} test_cases[] = {
{ L"Accept: */*\r\n",
L"",
cf_ua + L"\r\n",
S_OK },
{ L"Accept: */*\r\n",
L"",
L"",
E_OUTOFMEMORY },
{ L"",
L"Accept: */*\r\n",
L"Accept: */*\r\n" + cf_ua + L"\r\n",
S_OK },
{ L"User-Agent: Bingo/1.0\r\n",
L"",
L"User-Agent: Bingo/1.0 " + cf_tag + L"\r\n",
S_OK },
{ L"User-Agent: NotMe/1.0\r\n",
L"User-Agent: MeMeMe/1.0\r\n",
L"User-Agent: MeMeMe/1.0 " + cf_tag + L"\r\n",
S_OK },
{ L"",
L"User-Agent: MeMeMe/1.0\r\n",
L"User-Agent: MeMeMe/1.0 " + cf_tag + L"\r\n",
S_OK },
};
for (int i = 0; i < arraysize(test_cases); ++i) {
TestCase& test = test_cases[i];
wchar_t* additional = NULL;
test_http.beginning_transaction_ret_ = test.delegate_return_value_;
test_http.additional_headers_ = test.delegate_additional_.c_str();
HttpNegotiatePatch::BeginningTransaction(original, &test_http,
L"http://www.google.com", test.original_headers_.c_str(), 0,
&additional);
EXPECT_TRUE(additional != NULL);
if (additional) {
// Check against the expected additional headers.
EXPECT_EQ(test.expected_additional_, std::wstring(additional));
::CoTaskMemFree(additional);
}
}
}
class TestInternetProtocolSink
: public CComObjectRootEx<CComMultiThreadModel>,
public IInternetProtocolSink {
public:
TestInternetProtocolSink() : status_(0) {
// Create an instance of IE to fullfill the requirements of being able
// to detect whether a sub-frame or top-frame is being loaded (see
// IsSubFrameRequest) and to be able to mark an IBrowserService
// implementation as a target for CF navigation.
HRESULT hr = browser_.CreateInstance(CLSID_InternetExplorer);
CHECK(SUCCEEDED(hr));
if (SUCCEEDED(hr)) {
browser_->Navigate(ScopedBstr(L"about:blank"), NULL, NULL, NULL, NULL);
}
}
~TestInternetProtocolSink() {
if (browser_)
browser_->Quit();
}
BEGIN_COM_MAP(TestInternetProtocolSink)
COM_INTERFACE_ENTRY(IInternetProtocolSink)
COM_INTERFACE_ENTRY_AGGREGATE(IID_IServiceProvider, browser_)
END_COM_MAP()
// IInternetProtocolSink.
STDMETHOD(Switch)(PROTOCOLDATA* data) {
NOTREACHED();
return S_OK;
}
STDMETHOD(ReportProgress)(ULONG status, LPCWSTR text) {
status_ = status;
status_text_ = text ? text : L"";
return S_OK;
}
STDMETHOD(ReportData)(DWORD bscf, ULONG progress, ULONG progress_max) {
NOTREACHED();
return S_OK;
}
STDMETHOD(ReportResult)(HRESULT hr, DWORD err, LPCWSTR result) {
NOTREACHED();
return S_OK;
}
ULONG last_status() const {
return status_;
}
const std::wstring& last_status_text() const {
return status_text_;
}
protected:
ULONG status_;
std::wstring status_text_;
ScopedComPtr<IWebBrowser2> browser_;
};
TEST_F(HttpNegotiateTest, ReportProgress) {
if (chrome_frame_test::GetInstalledIEVersion() == IE_6) {
DLOG(INFO) << "Not running test for IE6";
return;
}
static const int kReportProgressIndex = 4;
CComObjectStackEx<TestInternetProtocolSink> test_sink;
IInternetProtocolSink_ReportProgress_Fn original =
reinterpret_cast<IInternetProtocolSink_ReportProgress_Fn>(
(*reinterpret_cast<void***>(
static_cast<IInternetProtocolSink*>(
&test_sink)))[kReportProgressIndex]);
struct TestCase {
bool mark_browser_;
const ULONG status_;
const std::wstring status_text_;
const std::wstring expected_status_text_;
} test_cases[] = {
// Cases where we could switch the mime type.
{ true,
BINDSTATUS_MIMETYPEAVAILABLE,
L"text/html",
kChromeMimeType },
{ false,
BINDSTATUS_MIMETYPEAVAILABLE,
L"text/html",
L"text/html" },
{ true,
BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE,
L"text/html",
kChromeMimeType },
{ false,
BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE,
L"text/html",
L"text/html" },
{ true,
LOCAL_BINDSTATUS_SERVER_MIMETYPEAVAILABLE,
L"text/html",
kChromeMimeType },
{ false,
LOCAL_BINDSTATUS_SERVER_MIMETYPEAVAILABLE,
L"text/html",
L"text/html" },
// Cases where we mark the browser but can't switch due to mime type.
{ true,
BINDSTATUS_MIMETYPEAVAILABLE,
L"application/pdf",
L"application/pdf" },
{ true,
BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE,
L"application/pdf",
L"application/pdf" },
{ true,
LOCAL_BINDSTATUS_SERVER_MIMETYPEAVAILABLE,
L"application/pdf",
L"application/pdf" },
// Cases where we should do nothing.
{ false,
BINDSTATUS_CONNECTING,
L"foo",
L"foo" },
{ false,
BINDSTATUS_UPLOADINGDATA,
L"bar",
L"bar" },
};
ScopedComPtr<IBrowserService> browser;
EXPECT_HRESULT_SUCCEEDED(GetBrowserServiceFromProtocolSink(&test_sink,
browser.Receive()));
for (int i = 0; i < arraysize(test_cases); ++i) {
TestCase& test = test_cases[i];
if (test.mark_browser_) {
MarkBrowserOnThreadForCFNavigation(browser);
}
HRESULT hr = HttpNegotiatePatch::ReportProgress(original, &test_sink,
test.status_, test.status_text_.c_str());
EXPECT_EQ(S_OK, hr);
// The TLS flag should always be cleared.
EXPECT_FALSE(CheckForCFNavigation(browser, false));
EXPECT_EQ(test.expected_status_text_, test_sink.last_status_text());
EXPECT_EQ(test.status_, test_sink.last_status());
}
}
using testing::AllOf;
using testing::ContainsRegex;
using testing::HasSubstr;
TEST(AppendUserAgent, Append) {
EXPECT_THAT(AppendCFUserAgentString(NULL, NULL),
testing::ContainsRegex("User-Agent:.+chromeframe.+\r\n"));
// Check Http headers are reasonably parsed.
EXPECT_THAT(AppendCFUserAgentString(L"Bad User-Agent: Age Tuners;\r\n", NULL),
AllOf(ContainsRegex("User-Agent:.+chromeframe.+\r\n"),
testing::Not(testing::HasSubstr("Age Tuners"))));
// Honor headers User-Agent, if additional headers does not specify one.
EXPECT_THAT(AppendCFUserAgentString(L"User-Agent: A Tense Rug;\r\n", NULL),
ContainsRegex("User-Agent: A Tense Rug; chromeframe.+\r\n"));
// Honor additional headers User-Agent.
EXPECT_THAT(AppendCFUserAgentString(L"User-Agent: Near Guest;\r\n",
L"User-Agent: Rat see Gun;\r\n"),
ContainsRegex("User-Agent: Rat see Gun; chromeframe.+\r\n"));
// Check additional headers are preserved.
EXPECT_THAT(AppendCFUserAgentString(NULL,
L"Authorization: A Zoo That I Ruin\r\n"
L"User-Agent: Get a Nurse;\r\n"
L"Accept-Language: Cleanup a Cat Egg\r\n"),
AllOf(ContainsRegex("User-Agent: Get a Nurse; chromeframe.+\r\n"),
HasSubstr("Authorization: A Zoo That I Ruin\r\n"),
HasSubstr("Accept-Language: Cleanup a Cat Egg\r\n")));
}
|