Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: chrome/browser/extensions/api/mdns/mdns_api_unittest.cc

Issue 1040773002: Limit number of service instances passed to onServiceList listeners. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Service instance limit and unit test against HEAD of master Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/api/mdns/mdns_api.h" 5 #include "chrome/browser/extensions/api/mdns/mdns_api.h"
6 #include "chrome/browser/extensions/extension_service.h" 6 #include "chrome/browser/extensions/extension_service.h"
7 #include "chrome/browser/extensions/extension_service_test_base.h" 7 #include "chrome/browser/extensions/extension_service_test_base.h"
8 #include "chrome/browser/extensions/test_extension_system.h" 8 #include "chrome/browser/extensions/test_extension_system.h"
9 #include "chrome/common/extensions/api/mdns.h" 9 #include "chrome/common/extensions/api/mdns.h"
10 #include "content/public/browser/browser_context.h" 10 #include "content/public/browser/browser_context.h"
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 // Set up browser_context(). 56 // Set up browser_context().
57 InitializeEmptyExtensionService(); 57 InitializeEmptyExtensionService();
58 58
59 // A custom TestingFactoryFunction is required for an MDnsAPI to actually be 59 // A custom TestingFactoryFunction is required for an MDnsAPI to actually be
60 // constructed. 60 // constructed.
61 MDnsAPI::GetFactoryInstance()->SetTestingFactory( 61 MDnsAPI::GetFactoryInstance()->SetTestingFactory(
62 browser_context(), 62 browser_context(),
63 MDnsAPITestingFactoryFunction); 63 MDnsAPITestingFactoryFunction);
64 64
65 // Create an event router and associate it with the context. 65 // Create an event router and associate it with the context.
66 extensions::EventRouter* event_router = new extensions::EventRouter( 66 extensions::EventRouter* event_router = CreateEventRouter_();
67 browser_context(),
68 ExtensionPrefsFactory::GetInstance()->GetForBrowserContext(
69 browser_context()));
70 static_cast<TestExtensionSystem*>( 67 static_cast<TestExtensionSystem*>(
71 ExtensionSystem::Get(browser_context()))->SetEventRouter( 68 ExtensionSystem::Get(browser_context()))->SetEventRouter(
72 scoped_ptr<extensions::EventRouter>(event_router)); 69 scoped_ptr<extensions::EventRouter>(event_router));
73 70
74 // Do some sanity checking 71 // Do some sanity checking
75 ASSERT_EQ(event_router, EventRouter::Get(browser_context())); 72 ASSERT_EQ(event_router, EventRouter::Get(browser_context()));
76 ASSERT_TRUE(MDnsAPI::Get(browser_context())); // constructs MDnsAPI 73 ASSERT_TRUE(MDnsAPI::Get(browser_context())); // constructs MDnsAPI
77 74
78 registry_ = new MockDnsSdRegistry(MDnsAPI::Get(browser_context())); 75 registry_ = new MockDnsSdRegistry(MDnsAPI::Get(browser_context()));
79 EXPECT_CALL(*dns_sd_registry(), 76 EXPECT_CALL(*dns_sd_registry(),
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 manifest, 123 manifest,
127 Extension::NO_FLAGS, 124 Extension::NO_FLAGS,
128 extension_id, 125 extension_id,
129 &error); 126 &error);
130 } 127 }
131 128
132 content::RenderProcessHost* render_process_host() const { 129 content::RenderProcessHost* render_process_host() const {
133 return render_process_host_.get(); 130 return render_process_host_.get();
134 } 131 }
135 132
133 protected:
134 virtual extensions::EventRouter* CreateEventRouter_() {
135 return new extensions::EventRouter(
136 browser_context(),
137 ExtensionPrefsFactory::GetInstance()->GetForBrowserContext(
138 browser_context()));
139 }
140
136 private: 141 private:
137 // The registry is owned by MDnsAPI, but MDnsAPI does not have an accessor 142 // The registry is owned by MDnsAPI, but MDnsAPI does not have an accessor
138 // for it, so use a private member. 143 // for it, so use a private member.
139 MockDnsSdRegistry* registry_; 144 MockDnsSdRegistry* registry_;
140 145
141 scoped_ptr<content::RenderProcessHost> render_process_host_; 146 scoped_ptr<content::RenderProcessHost> render_process_host_;
142 147
143 }; 148 };
144 149
145 TEST_F(MDnsAPITest, ExtensionRespectsWhitelist) { 150 TEST_F(MDnsAPITest, ExtensionRespectsWhitelist) {
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 EventRouter::Get(browser_context())->AddFilteredEventListener( 210 EventRouter::Get(browser_context())->AddFilteredEventListener(
206 api::mdns::OnServiceList::kEventName, render_process_host(), ext_id, 211 api::mdns::OnServiceList::kEventName, render_process_host(), ext_id,
207 filter, false); 212 filter, false);
208 213
209 EXPECT_CALL(*dns_sd_registry(), UnregisterDnsSdListener("_trex._tcp.local")); 214 EXPECT_CALL(*dns_sd_registry(), UnregisterDnsSdListener("_trex._tcp.local"));
210 EventRouter::Get(browser_context())->RemoveFilteredEventListener( 215 EventRouter::Get(browser_context())->RemoveFilteredEventListener(
211 api::mdns::OnServiceList::kEventName, render_process_host(), ext_id, 216 api::mdns::OnServiceList::kEventName, render_process_host(), ext_id,
212 filter, false); 217 filter, false);
213 } 218 }
214 219
220 class MockEventRouter : public EventRouter {
221 public:
222 explicit MockEventRouter(content::BrowserContext* browser_context,
223 ExtensionPrefs* extension_prefs) :
224 EventRouter(browser_context, extension_prefs) {}
225 virtual ~MockEventRouter() {}
226
227 virtual void BroadcastEvent(scoped_ptr<Event> event) {
228 BroadcastEventPtr(event.get());
229 }
230 MOCK_METHOD1(BroadcastEventPtr, void(Event* event));
231 };
232
233 class MDnsAPIMaxServicesTest : public MDnsAPITest {
234 public:
235 MockEventRouter* event_router() {
236 return static_cast<MockEventRouter*>(EventRouter::Get(browser_context()));
237 }
238 protected:
239 virtual extensions::EventRouter* CreateEventRouter_() override {
240 return new MockEventRouter(
241 browser_context(),
242 ExtensionPrefsFactory::GetInstance()->GetForBrowserContext(
243 browser_context()));
244 }
245 };
246
247 class EventServiceListSizeMatcher :
248 public testing::MatcherInterface<const Event&> {
249 public:
250 explicit EventServiceListSizeMatcher(const testing::Matcher<size_t>& m)
251 : service_count_matcher_(m) {}
252
253 virtual bool MatchAndExplain(const Event& e,
254 testing::MatchResultListener* listener) const {
255 if (e.event_args.get() == nullptr) {
256 *listener << "event.event_arg is null when it shouldn't be";
257 return false;
258 }
259 if (e.event_args->GetSize() != 1) {
260 *listener << "event.event_arg.GetSize() should be 1 but is "
261 << e.event_args->GetSize();
262 return false;
263 }
264 const base::ListValue* services = nullptr;
265 {
266 const base::Value* out;
267 e.event_args->Get(0, &out);
268 services = static_cast<const base::ListValue*>(out);
269 }
270 if (services == nullptr) {
271 *listener << "event's service list argument is not a ListValue";
272 return false;
273 }
274 *listener << "number of services is "
275 << services->GetSize()
276 return service_count_matcher_.MatchAndExplain(services->GetSize(),
mark a. foltz 2015/03/30 17:57:22 It seems like all you really care about is whether
Red Daly 2015/03/31 19:23:50 I think SizeIs() only works with stl containers, n
277 listener);
278 }
279
280 virtual void DescribeTo(::std::ostream* os) const {
281 *os << "is an onServiceList event where the number of services ";
282 service_count_matcher_.DescribeTo(os);
283 }
284
285 virtual void DescribeNegationTo(::std::ostream* os) const {
286 *os << "isn't an onServiceList event where the number of services ";
287 service_count_matcher_.DescribeTo(os);
288 }
289 private:
290 testing::Matcher<size_t> service_count_matcher_;
291 };
292
293 inline testing::Matcher<const Event&> EventServiceListSize(
294 const testing::Matcher<size_t>& m) {
295 return MakeMatcher(new EventServiceListSizeMatcher(m));
296 }
297
298 TEST_F(MDnsAPIMaxServicesTest, OnServiceListDoesNotExceedLimit) {
299 // Dispatch an mDNS event with more service instances than
300 // kMaxServicesPerOnServiceListEvent, and ensure that the list is truncated by
301 // inspecting the argument to MockEventRouter's BroadcastEvent method.
302 DnsSdRegistry::DnsSdServiceList services;
303 for (int i=0; i < 80; ++i) {
not at google - send to devlin 2015/03/30 17:08:50 when it's a shared constant, use that rather than
Red Daly 2015/03/31 19:23:50 Done, except it is a new function rather than a sh
304 services.push_back(DnsSdService());
305 }
306 EXPECT_CALL(
307 *event_router(),
308 BroadcastEventPtr(
309 testing::Pointee(EventServiceListSize(testing::Eq(size_t(64))))))
310 .Times(1);
not at google - send to devlin 2015/03/30 17:08:50 I dunno - a mock here seems pretty complex and ove
Red Daly 2015/03/31 19:23:50 The MDnsAPI object does not hold onto the event it
311 dns_sd_registry()->DispatchMDnsEvent("_testing._tcp.local", services);
312 }
313
215 } // empty namespace 314 } // empty namespace
216 315
217 } // namespace extensions 316 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698