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
|
// 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.
#include "chrome/browser/chromeos/external_cookie_handler.h"
#include <set>
#include <vector>
#include "base/basictypes.h"
#include "base/time.h"
#include "googleurl/src/gurl.h"
#include "net/base/cookie_options.h"
#include "net/base/cookie_store.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chromeos {
typedef testing::Test ExternalCookieHandlerTest;
static const std::string cookie1 = "coookie1\n";
static const std::string cookie2 = "coookie2\n";
static const std::string cookie3 = "coookie3";
class MockCookieStore : public net::CookieStore {
public:
MockCookieStore() : expected_url_(ExternalCookieHandler::kGoogleAccountsUrl) {
cookies_.insert(cookie1);
cookies_.insert(cookie2);
cookies_.insert(cookie3);
}
virtual ~MockCookieStore() {}
virtual bool SetCookieWithOptions(const GURL& url,
const std::string& cookie_line,
const net::CookieOptions& options) {
EXPECT_FALSE(options.exclude_httponly());
EXPECT_EQ(expected_url_, url);
std::set<std::string>::iterator it;
it = cookies_.find(cookie_line);
bool has_cookie = cookies_.end() != it;
if (has_cookie)
cookies_.erase(it);
return has_cookie;
}
virtual std::string GetCookiesWithOptions(const GURL& url,
const net::CookieOptions& options) {
EXPECT_TRUE(false);
return std::string();
}
virtual void DeleteCookie(const GURL& url,
const std::string& cookie_name) {
EXPECT_TRUE(false);
}
virtual net::CookieMonster* GetCookieMonster() { return NULL; }
private:
std::set<std::string> cookies_;
const GURL expected_url_;
DISALLOW_COPY_AND_ASSIGN(MockCookieStore);
};
TEST_F(ExternalCookieHandlerTest, MockCookieStoreSanityTest) {
GURL url(ExternalCookieHandler::kGoogleAccountsUrl);
// Need to use a scoped_refptr here because net::CookieStore extends
// base::RefCountedThreadSafe<> in base/ref_counted.h.
scoped_refptr<MockCookieStore> cookie_store(new MockCookieStore);
net::CookieOptions options;
options.set_include_httponly();
EXPECT_TRUE(cookie_store->SetCookieWithOptions(url, cookie1, options));
EXPECT_TRUE(cookie_store->SetCookieWithOptions(url, cookie2, options));
EXPECT_TRUE(cookie_store->SetCookieWithOptions(url, cookie3, options));
EXPECT_FALSE(cookie_store->SetCookieWithOptions(url, cookie1, options));
EXPECT_FALSE(cookie_store->SetCookieWithOptions(url, cookie2, options));
EXPECT_FALSE(cookie_store->SetCookieWithOptions(url, cookie3, options));
}
class MockReader : public PipeReader {
public:
explicit MockReader(const std::vector<std::string>& cookies)
: data_(cookies) {
}
std::string Read(const uint32 bytes_to_read) {
std::string to_return;
if (!data_.empty()) {
to_return = data_.back();
data_.pop_back();
}
return to_return;
}
private:
std::vector<std::string> data_;
};
TEST_F(ExternalCookieHandlerTest, SuccessfulReadTest) {
GURL url(ExternalCookieHandler::kGoogleAccountsUrl);
scoped_refptr<MockCookieStore> cookie_store(new MockCookieStore);
std::vector<std::string> cookies;
cookies.push_back(cookie3);
cookies.push_back(cookie2);
cookies.push_back(cookie1);
MockReader *reader = new MockReader(cookies);
ExternalCookieHandler handler(reader); // takes ownership.
EXPECT_TRUE(handler.HandleCookies(cookie_store.get()));
}
TEST_F(ExternalCookieHandlerTest, SuccessfulSlowReadTest) {
GURL url(ExternalCookieHandler::kGoogleAccountsUrl);
scoped_refptr<MockCookieStore> cookie_store(new MockCookieStore);
std::vector<std::string> cookies;
cookies.push_back(cookie3);
cookies.push_back(cookie2.substr(2));
cookies.push_back(cookie2.substr(0, 2));
cookies.push_back(cookie1);
MockReader *reader = new MockReader(cookies);
ExternalCookieHandler handler(reader); // takes ownership.
EXPECT_TRUE(handler.HandleCookies(cookie_store.get()));
}
} // namespace chromeos
|