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
|
// Copyright 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 "chrome/browser/extensions/test_blacklist.h"
#include <set>
#include "base/bind.h"
#include "base/run_loop.h"
#include "base/stl_util.h"
#include "base/thread_task_runner_handle.h"
#include "chrome/browser/extensions/blacklist.h"
#include "chrome/browser/extensions/blacklist_state_fetcher.h"
#include "chrome/browser/extensions/fake_safe_browsing_database_manager.h"
namespace extensions {
namespace {
void Assign(BlacklistState *out, BlacklistState in) {
*out = in;
}
} // namespace
BlacklistStateFetcherMock::BlacklistStateFetcherMock() : request_count_(0) {}
BlacklistStateFetcherMock::~BlacklistStateFetcherMock() {}
void BlacklistStateFetcherMock::Request(const std::string& id,
const RequestCallback& callback) {
++request_count_;
BlacklistState result = NOT_BLACKLISTED;
if (ContainsKey(states_, id))
result = states_[id];
base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
base::Bind(callback, result));
}
void BlacklistStateFetcherMock::SetState(const std::string& id,
BlacklistState state) {
states_[id] = state;
}
void BlacklistStateFetcherMock::Clear() {
states_.clear();
}
TestBlacklist::TestBlacklist()
: blacklist_(NULL),
blacklist_db_(new FakeSafeBrowsingDatabaseManager(true)),
scoped_blacklist_db_(blacklist_db_) {
}
TestBlacklist::TestBlacklist(Blacklist* blacklist)
: blacklist_(NULL),
blacklist_db_(new FakeSafeBrowsingDatabaseManager(true)),
scoped_blacklist_db_(blacklist_db_) {
Attach(blacklist);
}
TestBlacklist::~TestBlacklist() {
Detach();
}
void TestBlacklist::Attach(Blacklist* blacklist) {
if (blacklist_)
Detach();
blacklist_ = blacklist;
blacklist_->SetBlacklistStateFetcherForTest(&state_fetcher_mock_);
}
void TestBlacklist::Detach() {
blacklist_->ResetBlacklistStateFetcherForTest();
}
void TestBlacklist::SetBlacklistState(const std::string& extension_id,
BlacklistState state,
bool notify) {
state_fetcher_mock_.SetState(extension_id, state);
switch (state) {
case NOT_BLACKLISTED:
blacklist_db_->RemoveUnsafe(extension_id);
break;
case BLACKLISTED_MALWARE:
case BLACKLISTED_SECURITY_VULNERABILITY:
case BLACKLISTED_CWS_POLICY_VIOLATION:
case BLACKLISTED_POTENTIALLY_UNWANTED:
blacklist_db_->AddUnsafe(extension_id);
break;
default:
break;
}
if (notify)
blacklist_db_->NotifyUpdate();
}
void TestBlacklist::Clear(bool notify) {
state_fetcher_mock_.Clear();
blacklist_db_->ClearUnsafe();
if (notify)
blacklist_db_->NotifyUpdate();
}
BlacklistState TestBlacklist::GetBlacklistState(
const std::string& extension_id) {
BlacklistState blacklist_state;
blacklist_->IsBlacklisted(extension_id,
base::Bind(&Assign, &blacklist_state));
base::RunLoop().RunUntilIdle();
return blacklist_state;
}
void TestBlacklist::DisableSafeBrowsing() {
blacklist_db_->Disable();
}
void TestBlacklist::EnableSafeBrowsing() {
blacklist_db_->Enable();
}
void TestBlacklist::NotifyUpdate() {
blacklist_db_->NotifyUpdate();
}
} // namespace extensions
|