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
|
// Copyright (c) 2010 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.
//
// IE browser helper object implementation.
#include "ceee/ie/plugin/bho/cookie_accountant.h"
#include <wininet.h>
#include "ceee/ie/testing/mock_broker_and_friends.h"
#include "ceee/testing/utils/mock_static.h"
#include "ceee/testing/utils/test_utils.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
namespace {
using testing::_;
using testing::MockCookieEventsFunnel;
using testing::Return;
bool StringsNullOrEqual(wchar_t* a, wchar_t* b) {
if (a == NULL && b == NULL)
return true;
if (a != NULL && b != NULL && wcscmp(a, b) == 0)
return true;
return false;
}
MATCHER_P(CookiesEqual, cookie, "") {
return
StringsNullOrEqual(arg.name, cookie->name) &&
StringsNullOrEqual(arg.value, cookie->value) &&
StringsNullOrEqual(arg.domain, cookie->domain) &&
arg.host_only == cookie->host_only &&
StringsNullOrEqual(arg.path, cookie->path) &&
arg.secure == cookie->secure &&
arg.http_only == cookie->http_only &&
arg.session == cookie->session &&
arg.expiration_date == cookie->expiration_date;
}
// Mock WinInet functions.
MOCK_STATIC_CLASS_BEGIN(MockWinInet)
MOCK_STATIC_INIT_BEGIN(MockWinInet)
MOCK_STATIC_INIT(InternetSetCookieExA);
MOCK_STATIC_INIT(InternetSetCookieExW);
MOCK_STATIC_INIT_END()
MOCK_STATIC5(DWORD, CALLBACK, InternetSetCookieExA, LPCSTR, LPCSTR, LPCSTR,
DWORD, DWORD_PTR);
MOCK_STATIC5(DWORD, CALLBACK, InternetSetCookieExW, LPCWSTR, LPCWSTR,
LPCWSTR, DWORD, DWORD_PTR);
MOCK_STATIC_CLASS_END(MockWinInet)
class MockCookieAccountant : public CookieAccountant {
public:
MOCK_METHOD3(RecordCookie,
void(const std::string&, const std::string&,
const base::Time&));
void CallRecordCookie(const std::string& url,
const std::string& cookie_data,
const base::Time& current_time) {
CookieAccountant::RecordCookie(url, cookie_data, current_time);
}
virtual CookieEventsFunnel& cookie_events_funnel() {
return mock_cookie_events_funnel_;
}
static void set_singleton_instance(CookieAccountant* instance) {
singleton_instance_ = instance;
}
MockCookieEventsFunnel mock_cookie_events_funnel_;
};
class CookieAccountantTest : public testing::Test {
};
TEST_F(CookieAccountantTest, SetCookiePatchFiresCookieEvent) {
MockWinInet mock_wininet;
MockCookieAccountant cookie_accountant;
MockCookieAccountant::set_singleton_instance(&cookie_accountant);
EXPECT_CALL(mock_wininet, InternetSetCookieExA(_, _, _, _, _)).
WillOnce(Return(5));
EXPECT_CALL(cookie_accountant, RecordCookie("foo.com", "foo=bar", _));
EXPECT_EQ(5, CookieAccountant::InternetSetCookieExAPatch(
"foo.com", NULL, "foo=bar", 0, NULL));
EXPECT_CALL(mock_wininet, InternetSetCookieExW(_, _, _, _, _)).
WillOnce(Return(6));
EXPECT_CALL(cookie_accountant, RecordCookie("foo.com", "foo=bar", _));
EXPECT_EQ(6, CookieAccountant::InternetSetCookieExWPatch(
L"foo.com", NULL, L"foo=bar", 0, NULL));
}
TEST_F(CookieAccountantTest, RecordCookie) {
testing::LogDisabler no_dchecks;
MockCookieAccountant cookie_accountant;
cookie_api::CookieInfo expected_cookie;
expected_cookie.name = ::SysAllocString(L"FOO");
expected_cookie.value = ::SysAllocString(L"bar");
expected_cookie.host_only = TRUE;
expected_cookie.http_only = TRUE;
expected_cookie.expiration_date = 1278201600;
EXPECT_CALL(cookie_accountant.mock_cookie_events_funnel_,
OnChanged(false, CookiesEqual(&expected_cookie)));
cookie_accountant.CallRecordCookie(
"http://www.google.com",
"FOO=bar; httponly; expires=Sun, 4 Jul 2010 00:00:00 UTC",
base::Time::Now());
cookie_api::CookieInfo expected_cookie2;
expected_cookie2.name = ::SysAllocString(L"");
expected_cookie2.value = ::SysAllocString(L"helloworld");
expected_cookie2.domain = ::SysAllocString(L"omg.com");
expected_cookie2.path = ::SysAllocString(L"/leaping/lizards");
expected_cookie2.secure = TRUE;
expected_cookie2.session = TRUE;
EXPECT_CALL(cookie_accountant.mock_cookie_events_funnel_,
OnChanged(false, CookiesEqual(&expected_cookie2)));
cookie_accountant.CallRecordCookie(
"http://www.omg.com",
"helloworld; path=/leaping/lizards; secure; domain=omg.com",
base::Time::Now());
}
TEST_F(CookieAccountantTest, RecordHttpResponseCookies) {
testing::LogDisabler no_dchecks;
MockCookieAccountant cookie_accountant;
EXPECT_CALL(cookie_accountant, RecordCookie("", " foo=bar", _));
EXPECT_CALL(cookie_accountant, RecordCookie("", "HELLO=world235", _));
cookie_accountant.RecordHttpResponseCookies(
"HTTP/1.1 200 OK\nSet-Cookie: foo=bar\nCookie: not_a=cookie\n"
"Set-Cookie:HELLO=world235", base::Time::Now());
}
} // namespace
|