1 // Copyright 2018 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "osp/impl/mdns_responder_service.h"
6
7 #include <cstdint>
8 #include <iostream>
9 #include <memory>
10 #include <utility>
11
12 #include "gmock/gmock.h"
13 #include "gtest/gtest.h"
14 #include "osp/impl/service_listener_impl.h"
15 #include "osp/impl/testing/fake_mdns_platform_service.h"
16 #include "osp/impl/testing/fake_mdns_responder_adapter.h"
17 #include "platform/test/fake_task_runner.h"
18
19 namespace openscreen {
20 namespace osp {
21
22 // Child of the MdnsResponderService for testing purposes. Only difference
23 // betweeen this and the base class is that methods on this class are executed
24 // synchronously, rather than pushed to the task runner for later execution.
25 class TestingMdnsResponderService final : public MdnsResponderService {
26 public:
TestingMdnsResponderService(FakeTaskRunner * task_runner,const std::string & service_name,const std::string & service_protocol,std::unique_ptr<MdnsResponderAdapterFactory> mdns_responder_factory,std::unique_ptr<MdnsPlatformService> platform_service)27 TestingMdnsResponderService(
28 FakeTaskRunner* task_runner,
29 const std::string& service_name,
30 const std::string& service_protocol,
31 std::unique_ptr<MdnsResponderAdapterFactory> mdns_responder_factory,
32 std::unique_ptr<MdnsPlatformService> platform_service)
33 : MdnsResponderService(&FakeClock::now,
34 task_runner,
35 service_name,
36 service_protocol,
37 std::move(mdns_responder_factory),
38 std::move(platform_service)) {}
39 ~TestingMdnsResponderService() = default;
40
41 // Override the default ServiceListenerImpl and ServicePublisherImpl
42 // implementations. These call the internal implementations of each of the
43 // methods provided, meaning that the end result of the call is the same, but
44 // without pushing to the task runner and waiting for it to be pulled off
45 // again.
46 // ServiceListenerImpl::Delegate overrides.
StartListener()47 void StartListener() override { StartListenerInternal(); }
StartAndSuspendListener()48 void StartAndSuspendListener() override { StartAndSuspendListenerInternal(); }
StopListener()49 void StopListener() override { StopListenerInternal(); }
SuspendListener()50 void SuspendListener() override { SuspendListenerInternal(); }
ResumeListener()51 void ResumeListener() override { ResumeListenerInternal(); }
SearchNow(ServiceListener::State from)52 void SearchNow(ServiceListener::State from) override {
53 SearchNowInternal(from);
54 }
55
56 // ServicePublisherImpl::Delegate overrides.
StartPublisher()57 void StartPublisher() override { StartPublisherInternal(); }
StartAndSuspendPublisher()58 void StartAndSuspendPublisher() override {
59 StartAndSuspendPublisherInternal();
60 }
StopPublisher()61 void StopPublisher() override { StopPublisherInternal(); }
SuspendPublisher()62 void SuspendPublisher() override { SuspendPublisherInternal(); }
ResumePublisher()63 void ResumePublisher() override { ResumePublisherInternal(); }
64
65 // Handles new events as OnRead does, but without the need of a TaskRunner.
HandleNewEvents()66 void HandleNewEvents() {
67 if (!mdns_responder_) {
68 return;
69 }
70
71 mdns_responder_->RunTasks();
72 HandleMdnsEvents();
73 }
74 };
75
76 class FakeMdnsResponderAdapterFactory final
77 : public MdnsResponderAdapterFactory,
78 public FakeMdnsResponderAdapter::LifetimeObserver {
79 public:
80 ~FakeMdnsResponderAdapterFactory() override = default;
81
Create()82 std::unique_ptr<MdnsResponderAdapter> Create() override {
83 auto mdns = std::make_unique<FakeMdnsResponderAdapter>();
84 mdns->SetLifetimeObserver(this);
85 last_mdns_responder_ = mdns.get();
86 ++instances_;
87 return mdns;
88 }
89
OnDestroyed()90 void OnDestroyed() override {
91 last_running_ = last_mdns_responder_->running();
92 last_registered_services_size_ =
93 last_mdns_responder_->registered_services().size();
94 last_mdns_responder_ = nullptr;
95 }
96
last_mdns_responder()97 FakeMdnsResponderAdapter* last_mdns_responder() {
98 return last_mdns_responder_;
99 }
100
instances() const101 int32_t instances() const { return instances_; }
last_running() const102 bool last_running() const { return last_running_; }
last_registered_services_size() const103 size_t last_registered_services_size() const {
104 return last_registered_services_size_;
105 }
106
107 private:
108 FakeMdnsResponderAdapter* last_mdns_responder_ = nullptr;
109 int32_t instances_ = 0;
110 bool last_running_ = false;
111 size_t last_registered_services_size_ = 0;
112 };
113
114 namespace {
115
116 using ::testing::_;
117
118 constexpr char kTestServiceInstance[] = "turtle";
119 constexpr char kTestServiceName[] = "_foo";
120 constexpr char kTestServiceProtocol[] = "_udp";
121 constexpr char kTestHostname[] = "hostname";
122 constexpr uint16_t kTestPort = 12345;
123
124 // Wrapper around the above class. In MdnsResponderServiceTest, we need to both
125 // pass a unique_ptr to the created MdnsResponderService and to maintain a
126 // local pointer as well. Doing this with the same object causes a race
127 // condition, where ~FakeMdnsResponderAdapter() calls observer_->OnDestroyed()
128 // after the object is already deleted, resulting in a seg fault. This is to
129 // prevent that race condition.
130 class WrapperMdnsResponderAdapterFactory final
131 : public MdnsResponderAdapterFactory,
132 public FakeMdnsResponderAdapter::LifetimeObserver {
133 public:
WrapperMdnsResponderAdapterFactory(FakeMdnsResponderAdapterFactory * ptr)134 explicit WrapperMdnsResponderAdapterFactory(
135 FakeMdnsResponderAdapterFactory* ptr)
136 : other_(ptr) {}
137
Create()138 std::unique_ptr<MdnsResponderAdapter> Create() override {
139 return other_->Create();
140 }
141
OnDestroyed()142 void OnDestroyed() override { other_->OnDestroyed(); }
143
144 private:
145 FakeMdnsResponderAdapterFactory* other_;
146 };
147
148 class MockServiceListenerObserver final : public ServiceListener::Observer {
149 public:
150 ~MockServiceListenerObserver() override = default;
151
152 MOCK_METHOD0(OnStarted, void());
153 MOCK_METHOD0(OnStopped, void());
154 MOCK_METHOD0(OnSuspended, void());
155 MOCK_METHOD0(OnSearching, void());
156
157 MOCK_METHOD1(OnReceiverAdded, void(const ServiceInfo&));
158 MOCK_METHOD1(OnReceiverChanged, void(const ServiceInfo&));
159 MOCK_METHOD1(OnReceiverRemoved, void(const ServiceInfo&));
160 MOCK_METHOD0(OnAllReceiversRemoved, void());
161
162 MOCK_METHOD1(OnError, void(ServiceListenerError));
163 MOCK_METHOD1(OnMetrics, void(ServiceListener::Metrics));
164 };
165
166 class MockServicePublisherObserver final : public ServicePublisher::Observer {
167 public:
168 ~MockServicePublisherObserver() override = default;
169
170 MOCK_METHOD0(OnStarted, void());
171 MOCK_METHOD0(OnStopped, void());
172 MOCK_METHOD0(OnSuspended, void());
173 MOCK_METHOD1(OnError, void(ServicePublisherError));
174 MOCK_METHOD1(OnMetrics, void(ServicePublisher::Metrics));
175 };
176
177 UdpSocket* const kDefaultSocket =
178 reinterpret_cast<UdpSocket*>(static_cast<uintptr_t>(16));
179 UdpSocket* const kSecondSocket =
180 reinterpret_cast<UdpSocket*>(static_cast<uintptr_t>(24));
181
182 class MdnsResponderServiceTest : public ::testing::Test {
183 protected:
SetUp()184 void SetUp() override {
185 mdns_responder_factory_ =
186 std::make_unique<FakeMdnsResponderAdapterFactory>();
187 auto wrapper_factory = std::make_unique<WrapperMdnsResponderAdapterFactory>(
188 mdns_responder_factory_.get());
189 clock_ = std::make_unique<FakeClock>(Clock::now());
190 task_runner_ = std::make_unique<FakeTaskRunner>(clock_.get());
191 auto platform_service = std::make_unique<FakeMdnsPlatformService>();
192 fake_platform_service_ = platform_service.get();
193 fake_platform_service_->set_interfaces(bound_interfaces_);
194 mdns_service_ = std::make_unique<TestingMdnsResponderService>(
195 task_runner_.get(), kTestServiceName, kTestServiceProtocol,
196 std::move(wrapper_factory), std::move(platform_service));
197 service_listener_ =
198 std::make_unique<ServiceListenerImpl>(mdns_service_.get());
199 service_listener_->AddObserver(&observer_);
200
201 mdns_service_->SetServiceConfig(kTestHostname, kTestServiceInstance,
202 kTestPort, {}, {{"model", "shifty"}});
203 service_publisher_ = std::make_unique<ServicePublisherImpl>(
204 &publisher_observer_, mdns_service_.get());
205 }
206
207 std::unique_ptr<FakeClock> clock_;
208 std::unique_ptr<FakeTaskRunner> task_runner_;
209 MockServiceListenerObserver observer_;
210 FakeMdnsPlatformService* fake_platform_service_;
211 std::unique_ptr<FakeMdnsResponderAdapterFactory> mdns_responder_factory_;
212 std::unique_ptr<TestingMdnsResponderService> mdns_service_;
213 std::unique_ptr<ServiceListenerImpl> service_listener_;
214 MockServicePublisherObserver publisher_observer_;
215 std::unique_ptr<ServicePublisherImpl> service_publisher_;
216 const uint8_t default_mac_[6] = {0, 11, 22, 33, 44, 55};
217 const uint8_t second_mac_[6] = {55, 33, 22, 33, 44, 77};
218 const IPSubnet default_subnet_{IPAddress{192, 168, 3, 2}, 24};
219 const IPSubnet second_subnet_{IPAddress{10, 0, 0, 3}, 24};
220 std::vector<MdnsPlatformService::BoundInterface> bound_interfaces_{
221 MdnsPlatformService::BoundInterface{
222 InterfaceInfo{1,
223 default_mac_,
224 "eth0",
225 InterfaceInfo::Type::kEthernet,
226 {default_subnet_}},
227 default_subnet_, kDefaultSocket},
228 MdnsPlatformService::BoundInterface{
229 InterfaceInfo{2,
230 second_mac_,
231 "eth1",
232 InterfaceInfo::Type::kEthernet,
233 {second_subnet_}},
234 second_subnet_, kSecondSocket},
235 };
236 };
237
238 } // namespace
239
TEST_F(MdnsResponderServiceTest,BasicServiceStates)240 TEST_F(MdnsResponderServiceTest, BasicServiceStates) {
241 EXPECT_CALL(observer_, OnStarted());
242 service_listener_->Start();
243
244 auto* mdns_responder = mdns_responder_factory_->last_mdns_responder();
245 ASSERT_TRUE(mdns_responder);
246 ASSERT_TRUE(mdns_responder->running());
247
248 AddEventsForNewService(mdns_responder, kTestServiceInstance, kTestServiceName,
249 kTestServiceProtocol, "gigliorononomicon", kTestPort,
250 {"model=shifty", "id=asdf"}, IPAddress{192, 168, 3, 7},
251 kDefaultSocket);
252
253 std::string service_id;
254 EXPECT_CALL(observer_, OnReceiverAdded(_))
255 .WillOnce(::testing::Invoke([&service_id](const ServiceInfo& info) {
256 service_id = info.service_id;
257 EXPECT_EQ(kTestServiceInstance, info.friendly_name);
258 EXPECT_EQ((IPEndpoint{{192, 168, 3, 7}, kTestPort}), info.v4_endpoint);
259 EXPECT_FALSE(info.v6_endpoint.address);
260 }));
261 mdns_service_->HandleNewEvents();
262
263 mdns_responder->AddAEvent(MakeAEvent(
264 "gigliorononomicon", IPAddress{192, 168, 3, 8}, kDefaultSocket));
265
266 EXPECT_CALL(observer_, OnReceiverChanged(_))
267 .WillOnce(::testing::Invoke([&service_id](const ServiceInfo& info) {
268 EXPECT_EQ(service_id, info.service_id);
269 EXPECT_EQ(kTestServiceInstance, info.friendly_name);
270 EXPECT_EQ((IPEndpoint{{192, 168, 3, 8}, kTestPort}), info.v4_endpoint);
271 EXPECT_FALSE(info.v6_endpoint.address);
272 }));
273 mdns_service_->HandleNewEvents();
274
275 auto ptr_remove = MakePtrEvent(kTestServiceInstance, kTestServiceName,
276 kTestServiceProtocol, kDefaultSocket);
277 ptr_remove.header.response_type = QueryEventHeader::Type::kRemoved;
278 mdns_responder->AddPtrEvent(std::move(ptr_remove));
279
280 EXPECT_CALL(observer_, OnReceiverRemoved(_))
281 .WillOnce(::testing::Invoke([&service_id](const ServiceInfo& info) {
282 EXPECT_EQ(service_id, info.service_id);
283 }));
284 mdns_service_->HandleNewEvents();
285 }
286
TEST_F(MdnsResponderServiceTest,NetworkNetworkInterfaceIndex)287 TEST_F(MdnsResponderServiceTest, NetworkNetworkInterfaceIndex) {
288 constexpr uint8_t mac[6] = {12, 34, 56, 78, 90};
289 const IPSubnet subnet{IPAddress{10, 0, 0, 2}, 24};
290 bound_interfaces_.emplace_back(
291 InterfaceInfo{2, mac, "wlan0", InterfaceInfo::Type::kWifi, {subnet}},
292 subnet, kSecondSocket);
293 fake_platform_service_->set_interfaces(bound_interfaces_);
294 EXPECT_CALL(observer_, OnStarted());
295 service_listener_->Start();
296
297 auto* mdns_responder = mdns_responder_factory_->last_mdns_responder();
298 ASSERT_TRUE(mdns_responder);
299 ASSERT_TRUE(mdns_responder->running());
300
301 AddEventsForNewService(mdns_responder, kTestServiceInstance, kTestServiceName,
302 kTestServiceProtocol, "gigliorononomicon", kTestPort,
303 {"model=shifty", "id=asdf"}, IPAddress{192, 168, 3, 7},
304 kSecondSocket);
305
306 EXPECT_CALL(observer_, OnReceiverAdded(_))
307 .WillOnce(::testing::Invoke([](const ServiceInfo& info) {
308 EXPECT_EQ(2, info.network_interface_index);
309 }));
310 mdns_service_->HandleNewEvents();
311 }
312
TEST_F(MdnsResponderServiceTest,SimultaneousFieldChanges)313 TEST_F(MdnsResponderServiceTest, SimultaneousFieldChanges) {
314 EXPECT_CALL(observer_, OnStarted());
315 service_listener_->Start();
316
317 auto* mdns_responder = mdns_responder_factory_->last_mdns_responder();
318 ASSERT_TRUE(mdns_responder);
319 ASSERT_TRUE(mdns_responder->running());
320
321 AddEventsForNewService(mdns_responder, kTestServiceInstance, kTestServiceName,
322 kTestServiceProtocol, "gigliorononomicon", kTestPort,
323 {"model=shifty", "id=asdf"}, IPAddress{192, 168, 3, 7},
324 kDefaultSocket);
325
326 EXPECT_CALL(observer_, OnReceiverAdded(_));
327 mdns_service_->HandleNewEvents();
328
329 mdns_responder->AddSrvEvent(
330 MakeSrvEvent(kTestServiceInstance, kTestServiceName, kTestServiceProtocol,
331 "gigliorononomicon", 54321, kDefaultSocket));
332 auto a_remove = MakeAEvent("gigliorononomicon", IPAddress{192, 168, 3, 7},
333 kDefaultSocket);
334 a_remove.header.response_type = QueryEventHeader::Type::kRemoved;
335 mdns_responder->AddAEvent(std::move(a_remove));
336 mdns_responder->AddAEvent(MakeAEvent(
337 "gigliorononomicon", IPAddress{192, 168, 3, 8}, kDefaultSocket));
338
339 EXPECT_CALL(observer_, OnReceiverChanged(_))
340 .WillOnce(::testing::Invoke([](const ServiceInfo& info) {
341 EXPECT_EQ((IPAddress{192, 168, 3, 8}), info.v4_endpoint.address);
342 EXPECT_EQ(54321, info.v4_endpoint.port);
343 EXPECT_FALSE(info.v6_endpoint.address);
344 }));
345 mdns_service_->HandleNewEvents();
346 }
347
TEST_F(MdnsResponderServiceTest,SimultaneousHostAndAddressChange)348 TEST_F(MdnsResponderServiceTest, SimultaneousHostAndAddressChange) {
349 EXPECT_CALL(observer_, OnStarted());
350 service_listener_->Start();
351
352 auto* mdns_responder = mdns_responder_factory_->last_mdns_responder();
353 ASSERT_TRUE(mdns_responder);
354 ASSERT_TRUE(mdns_responder->running());
355
356 AddEventsForNewService(mdns_responder, kTestServiceInstance, kTestServiceName,
357 kTestServiceProtocol, "gigliorononomicon", kTestPort,
358 {"model=shifty", "id=asdf"}, IPAddress{192, 168, 3, 7},
359 kDefaultSocket);
360
361 EXPECT_CALL(observer_, OnReceiverAdded(_));
362 mdns_service_->HandleNewEvents();
363
364 auto srv_remove =
365 MakeSrvEvent(kTestServiceInstance, kTestServiceName, kTestServiceProtocol,
366 "gigliorononomicon", kTestPort, kDefaultSocket);
367 srv_remove.header.response_type = QueryEventHeader::Type::kRemoved;
368 mdns_responder->AddSrvEvent(std::move(srv_remove));
369 mdns_responder->AddSrvEvent(
370 MakeSrvEvent(kTestServiceInstance, kTestServiceName, kTestServiceProtocol,
371 "alpha", kTestPort, kDefaultSocket));
372 mdns_responder->AddAEvent(MakeAEvent(
373 "gigliorononomicon", IPAddress{192, 168, 3, 8}, kDefaultSocket));
374 mdns_responder->AddAEvent(
375 MakeAEvent("alpha", IPAddress{192, 168, 3, 10}, kDefaultSocket));
376
377 EXPECT_CALL(observer_, OnReceiverChanged(_))
378 .WillOnce(::testing::Invoke([](const ServiceInfo& info) {
379 EXPECT_EQ((IPAddress{192, 168, 3, 10}), info.v4_endpoint.address);
380 EXPECT_FALSE(info.v6_endpoint.address);
381 }));
382 mdns_service_->HandleNewEvents();
383 }
384
TEST_F(MdnsResponderServiceTest,ListenerStateTransitions)385 TEST_F(MdnsResponderServiceTest, ListenerStateTransitions) {
386 EXPECT_CALL(observer_, OnStarted());
387 service_listener_->Start();
388
389 auto* mdns_responder = mdns_responder_factory_->last_mdns_responder();
390 ASSERT_TRUE(mdns_responder);
391 ASSERT_TRUE(mdns_responder->running());
392
393 EXPECT_CALL(observer_, OnSuspended());
394 service_listener_->Suspend();
395 ASSERT_EQ(mdns_responder, mdns_responder_factory_->last_mdns_responder());
396 EXPECT_FALSE(mdns_responder->running());
397
398 EXPECT_CALL(observer_, OnStarted());
399 service_listener_->Resume();
400 ASSERT_EQ(mdns_responder, mdns_responder_factory_->last_mdns_responder());
401 EXPECT_TRUE(mdns_responder->running());
402
403 EXPECT_CALL(observer_, OnStopped());
404 service_listener_->Stop();
405 ASSERT_FALSE(mdns_responder_factory_->last_mdns_responder());
406
407 EXPECT_CALL(observer_, OnSuspended());
408 auto instances = mdns_responder_factory_->instances();
409 service_listener_->StartAndSuspend();
410 EXPECT_EQ(instances + 1, mdns_responder_factory_->instances());
411 mdns_responder = mdns_responder_factory_->last_mdns_responder();
412 EXPECT_FALSE(mdns_responder->running());
413
414 EXPECT_CALL(observer_, OnStopped());
415 service_listener_->Stop();
416 ASSERT_FALSE(mdns_responder_factory_->last_mdns_responder());
417 }
418
TEST_F(MdnsResponderServiceTest,BasicServicePublish)419 TEST_F(MdnsResponderServiceTest, BasicServicePublish) {
420 EXPECT_CALL(publisher_observer_, OnStarted());
421 service_publisher_->Start();
422
423 auto* mdns_responder = mdns_responder_factory_->last_mdns_responder();
424 ASSERT_TRUE(mdns_responder);
425 ASSERT_TRUE(mdns_responder->running());
426
427 const auto& services = mdns_responder->registered_services();
428 ASSERT_EQ(1u, services.size());
429 EXPECT_EQ(kTestServiceInstance, services[0].service_instance);
430 EXPECT_EQ(kTestServiceName, services[0].service_name);
431 EXPECT_EQ(kTestServiceProtocol, services[0].service_protocol);
432 auto host_labels = services[0].target_host.GetLabels();
433 ASSERT_EQ(2u, host_labels.size());
434 EXPECT_EQ(kTestHostname, host_labels[0]);
435 EXPECT_EQ("local", host_labels[1]);
436 EXPECT_EQ(kTestPort, services[0].target_port);
437
438 EXPECT_CALL(publisher_observer_, OnStopped());
439 service_publisher_->Stop();
440
441 EXPECT_FALSE(mdns_responder_factory_->last_mdns_responder());
442 EXPECT_EQ(0u, mdns_responder_factory_->last_registered_services_size());
443 }
444
TEST_F(MdnsResponderServiceTest,PublisherStateTransitions)445 TEST_F(MdnsResponderServiceTest, PublisherStateTransitions) {
446 EXPECT_CALL(publisher_observer_, OnStarted());
447 service_publisher_->Start();
448
449 auto* mdns_responder = mdns_responder_factory_->last_mdns_responder();
450 ASSERT_TRUE(mdns_responder);
451 ASSERT_TRUE(mdns_responder->running());
452 EXPECT_EQ(1u, mdns_responder->registered_services().size());
453
454 EXPECT_CALL(publisher_observer_, OnSuspended());
455 service_publisher_->Suspend();
456 EXPECT_EQ(0u, mdns_responder->registered_services().size());
457
458 EXPECT_CALL(publisher_observer_, OnStarted());
459 service_publisher_->Resume();
460 EXPECT_EQ(1u, mdns_responder->registered_services().size());
461
462 EXPECT_CALL(publisher_observer_, OnStopped());
463 service_publisher_->Stop();
464 EXPECT_EQ(0u, mdns_responder_factory_->last_registered_services_size());
465
466 EXPECT_CALL(publisher_observer_, OnStarted());
467 service_publisher_->Start();
468 mdns_responder = mdns_responder_factory_->last_mdns_responder();
469 ASSERT_TRUE(mdns_responder);
470 ASSERT_TRUE(mdns_responder->running());
471 EXPECT_EQ(1u, mdns_responder->registered_services().size());
472 EXPECT_CALL(publisher_observer_, OnSuspended());
473 service_publisher_->Suspend();
474 EXPECT_EQ(0u, mdns_responder->registered_services().size());
475 EXPECT_CALL(publisher_observer_, OnStopped());
476 service_publisher_->Stop();
477 EXPECT_FALSE(mdns_responder_factory_->last_mdns_responder());
478 EXPECT_EQ(0u, mdns_responder_factory_->last_registered_services_size());
479 }
480
TEST_F(MdnsResponderServiceTest,PublisherObeysInterfaceAllowlist)481 TEST_F(MdnsResponderServiceTest, PublisherObeysInterfaceAllowlist) {
482 {
483 mdns_service_->SetServiceConfig(kTestHostname, kTestServiceInstance,
484 kTestPort, {}, {{"model", "shifty"}});
485
486 EXPECT_CALL(publisher_observer_, OnStarted());
487 service_publisher_->Start();
488
489 auto* mdns_responder = mdns_responder_factory_->last_mdns_responder();
490 ASSERT_TRUE(mdns_responder);
491 ASSERT_TRUE(mdns_responder->running());
492 auto interfaces = mdns_responder->registered_interfaces();
493 ASSERT_EQ(2u, interfaces.size());
494 EXPECT_EQ(kDefaultSocket, interfaces[0].socket);
495 EXPECT_EQ(kSecondSocket, interfaces[1].socket);
496
497 EXPECT_CALL(publisher_observer_, OnStopped());
498 service_publisher_->Stop();
499 }
500 {
501 mdns_service_->SetServiceConfig(kTestHostname, kTestServiceInstance,
502 kTestPort, {1, 2}, {{"model", "shifty"}});
503
504 EXPECT_CALL(publisher_observer_, OnStarted());
505 service_publisher_->Start();
506
507 auto* mdns_responder = mdns_responder_factory_->last_mdns_responder();
508 ASSERT_TRUE(mdns_responder);
509 ASSERT_TRUE(mdns_responder->running());
510 auto interfaces = mdns_responder->registered_interfaces();
511 ASSERT_EQ(2u, interfaces.size());
512 EXPECT_EQ(kDefaultSocket, interfaces[0].socket);
513 EXPECT_EQ(kSecondSocket, interfaces[1].socket);
514
515 EXPECT_CALL(publisher_observer_, OnStopped());
516 service_publisher_->Stop();
517 }
518 {
519 mdns_service_->SetServiceConfig(kTestHostname, kTestServiceInstance,
520 kTestPort, {2}, {{"model", "shifty"}});
521
522 EXPECT_CALL(publisher_observer_, OnStarted());
523 service_publisher_->Start();
524
525 auto* mdns_responder = mdns_responder_factory_->last_mdns_responder();
526 ASSERT_TRUE(mdns_responder);
527 ASSERT_TRUE(mdns_responder->running());
528 auto interfaces = mdns_responder->registered_interfaces();
529 ASSERT_EQ(1u, interfaces.size());
530 EXPECT_EQ(kSecondSocket, interfaces[0].socket);
531
532 EXPECT_CALL(publisher_observer_, OnStopped());
533 service_publisher_->Stop();
534 }
535 }
536
TEST_F(MdnsResponderServiceTest,ListenAndPublish)537 TEST_F(MdnsResponderServiceTest, ListenAndPublish) {
538 EXPECT_CALL(observer_, OnStarted());
539 service_listener_->Start();
540
541 auto* mdns_responder = mdns_responder_factory_->last_mdns_responder();
542 ASSERT_TRUE(mdns_responder);
543 ASSERT_TRUE(mdns_responder->running());
544
545 {
546 auto interfaces = mdns_responder->registered_interfaces();
547 ASSERT_EQ(2u, interfaces.size());
548 EXPECT_EQ(kDefaultSocket, interfaces[0].socket);
549 EXPECT_EQ(kSecondSocket, interfaces[1].socket);
550 }
551
552 mdns_service_->SetServiceConfig(kTestHostname, kTestServiceInstance,
553 kTestPort, {2}, {{"model", "shifty"}});
554
555 auto instances = mdns_responder_factory_->instances();
556 EXPECT_CALL(publisher_observer_, OnStarted());
557 service_publisher_->Start();
558
559 EXPECT_EQ(instances, mdns_responder_factory_->instances());
560 ASSERT_TRUE(mdns_responder->running());
561 {
562 auto interfaces = mdns_responder->registered_interfaces();
563 ASSERT_EQ(1u, interfaces.size());
564 EXPECT_EQ(kSecondSocket, interfaces[0].socket);
565 }
566
567 EXPECT_CALL(observer_, OnStopped());
568 service_listener_->Stop();
569 ASSERT_TRUE(mdns_responder->running());
570 EXPECT_EQ(1u, mdns_responder->registered_interfaces().size());
571
572 EXPECT_CALL(publisher_observer_, OnStopped());
573 service_publisher_->Stop();
574 EXPECT_FALSE(mdns_responder_factory_->last_mdns_responder());
575 EXPECT_EQ(0u, mdns_responder_factory_->last_registered_services_size());
576 }
577
TEST_F(MdnsResponderServiceTest,PublishAndListen)578 TEST_F(MdnsResponderServiceTest, PublishAndListen) {
579 mdns_service_->SetServiceConfig(kTestHostname, kTestServiceInstance,
580 kTestPort, {2}, {{"model", "shifty"}});
581
582 EXPECT_CALL(publisher_observer_, OnStarted());
583 service_publisher_->Start();
584
585 auto* mdns_responder = mdns_responder_factory_->last_mdns_responder();
586 ASSERT_TRUE(mdns_responder);
587 ASSERT_TRUE(mdns_responder->running());
588 {
589 auto interfaces = mdns_responder->registered_interfaces();
590 ASSERT_EQ(1u, interfaces.size());
591 EXPECT_EQ(kSecondSocket, interfaces[0].socket);
592 }
593
594 auto instances = mdns_responder_factory_->instances();
595 EXPECT_CALL(observer_, OnStarted());
596 service_listener_->Start();
597
598 EXPECT_EQ(instances, mdns_responder_factory_->instances());
599 ASSERT_TRUE(mdns_responder->running());
600 {
601 auto interfaces = mdns_responder->registered_interfaces();
602 ASSERT_EQ(1u, interfaces.size());
603 EXPECT_EQ(kSecondSocket, interfaces[0].socket);
604 }
605
606 EXPECT_CALL(publisher_observer_, OnStopped());
607 service_publisher_->Stop();
608 ASSERT_TRUE(mdns_responder->running());
609 EXPECT_EQ(1u, mdns_responder->registered_interfaces().size());
610
611 EXPECT_CALL(observer_, OnStopped());
612 service_listener_->Stop();
613 EXPECT_FALSE(mdns_responder_factory_->last_mdns_responder());
614 EXPECT_EQ(0u, mdns_responder_factory_->last_registered_services_size());
615 }
616
TEST_F(MdnsResponderServiceTest,AddressQueryStopped)617 TEST_F(MdnsResponderServiceTest, AddressQueryStopped) {
618 EXPECT_CALL(observer_, OnStarted());
619 service_listener_->Start();
620
621 auto* mdns_responder = mdns_responder_factory_->last_mdns_responder();
622
623 AddEventsForNewService(mdns_responder, kTestServiceInstance, kTestServiceName,
624 kTestServiceProtocol, "gigliorononomicon", kTestPort,
625 {"model=shifty", "id=asdf"}, IPAddress{192, 168, 3, 7},
626 kDefaultSocket);
627
628 EXPECT_CALL(observer_, OnReceiverAdded(_));
629 mdns_service_->HandleNewEvents();
630
631 auto srv_remove =
632 MakeSrvEvent(kTestServiceInstance, kTestServiceName, kTestServiceProtocol,
633 "gigliorononomicon", kTestPort, kDefaultSocket);
634 srv_remove.header.response_type = QueryEventHeader::Type::kRemoved;
635 mdns_responder->AddSrvEvent(std::move(srv_remove));
636
637 EXPECT_CALL(observer_, OnReceiverRemoved(_));
638 mdns_service_->HandleNewEvents();
639
640 EXPECT_FALSE(mdns_responder->ptr_queries_empty());
641 EXPECT_FALSE(mdns_responder->srv_queries_empty());
642 EXPECT_FALSE(mdns_responder->txt_queries_empty());
643 EXPECT_TRUE(mdns_responder->a_queries_empty());
644 EXPECT_TRUE(mdns_responder->aaaa_queries_empty());
645 }
646
TEST_F(MdnsResponderServiceTest,AddressQueryRefCount)647 TEST_F(MdnsResponderServiceTest, AddressQueryRefCount) {
648 EXPECT_CALL(observer_, OnStarted());
649 service_listener_->Start();
650
651 auto* mdns_responder = mdns_responder_factory_->last_mdns_responder();
652
653 AddEventsForNewService(mdns_responder, kTestServiceInstance, kTestServiceName,
654 kTestServiceProtocol, "gigliorononomicon", kTestPort,
655 {"model=shifty", "id=asdf"}, IPAddress{192, 168, 3, 7},
656 kDefaultSocket);
657 AddEventsForNewService(mdns_responder, "instance-2", kTestServiceName,
658 kTestServiceProtocol, "gigliorononomicon", 4321,
659 {"model=shwofty", "id=asdf"},
660 IPAddress{192, 168, 3, 7}, kDefaultSocket);
661
662 EXPECT_CALL(observer_, OnReceiverAdded(_)).Times(2);
663 mdns_service_->HandleNewEvents();
664
665 auto srv_remove =
666 MakeSrvEvent(kTestServiceInstance, kTestServiceName, kTestServiceProtocol,
667 "gigliorononomicon", kTestPort, kDefaultSocket);
668 srv_remove.header.response_type = QueryEventHeader::Type::kRemoved;
669 mdns_responder->AddSrvEvent(std::move(srv_remove));
670
671 EXPECT_CALL(observer_, OnReceiverRemoved(_));
672 mdns_service_->HandleNewEvents();
673
674 EXPECT_FALSE(mdns_responder->ptr_queries_empty());
675 EXPECT_FALSE(mdns_responder->srv_queries_empty());
676 EXPECT_FALSE(mdns_responder->txt_queries_empty());
677 EXPECT_FALSE(mdns_responder->a_queries_empty());
678 EXPECT_FALSE(mdns_responder->aaaa_queries_empty());
679
680 srv_remove =
681 MakeSrvEvent("instance-2", kTestServiceName, kTestServiceProtocol,
682 "gigliorononomicon", 4321, kDefaultSocket);
683 srv_remove.header.response_type = QueryEventHeader::Type::kRemoved;
684 mdns_responder->AddSrvEvent(std::move(srv_remove));
685
686 EXPECT_CALL(observer_, OnReceiverRemoved(_));
687 mdns_service_->HandleNewEvents();
688
689 EXPECT_FALSE(mdns_responder->ptr_queries_empty());
690 EXPECT_FALSE(mdns_responder->srv_queries_empty());
691 EXPECT_FALSE(mdns_responder->txt_queries_empty());
692 EXPECT_TRUE(mdns_responder->a_queries_empty());
693 EXPECT_TRUE(mdns_responder->aaaa_queries_empty());
694 }
695
TEST_F(MdnsResponderServiceTest,ServiceQueriesStoppedSrvFirst)696 TEST_F(MdnsResponderServiceTest, ServiceQueriesStoppedSrvFirst) {
697 EXPECT_CALL(observer_, OnStarted());
698 service_listener_->Start();
699
700 auto* mdns_responder = mdns_responder_factory_->last_mdns_responder();
701
702 AddEventsForNewService(mdns_responder, kTestServiceInstance, kTestServiceName,
703 kTestServiceProtocol, "gigliorononomicon", kTestPort,
704 {"model=shifty", "id=asdf"}, IPAddress{192, 168, 3, 7},
705 kDefaultSocket);
706
707 EXPECT_CALL(observer_, OnReceiverAdded(_));
708 mdns_service_->HandleNewEvents();
709
710 auto srv_remove =
711 MakeSrvEvent(kTestServiceInstance, kTestServiceName, kTestServiceProtocol,
712 "gigliorononomicon", kTestPort, kDefaultSocket);
713 srv_remove.header.response_type = QueryEventHeader::Type::kRemoved;
714 mdns_responder->AddSrvEvent(std::move(srv_remove));
715
716 EXPECT_CALL(observer_, OnReceiverRemoved(_));
717 mdns_service_->HandleNewEvents();
718
719 EXPECT_FALSE(mdns_responder->ptr_queries_empty());
720 EXPECT_FALSE(mdns_responder->srv_queries_empty());
721 EXPECT_FALSE(mdns_responder->txt_queries_empty());
722 EXPECT_TRUE(mdns_responder->a_queries_empty());
723 EXPECT_TRUE(mdns_responder->aaaa_queries_empty());
724
725 auto ptr_remove = MakePtrEvent(kTestServiceInstance, kTestServiceName,
726 kTestServiceProtocol, kDefaultSocket);
727 ptr_remove.header.response_type = QueryEventHeader::Type::kRemoved;
728 mdns_responder->AddPtrEvent(std::move(ptr_remove));
729 mdns_service_->HandleNewEvents();
730
731 EXPECT_FALSE(mdns_responder->ptr_queries_empty());
732 EXPECT_TRUE(mdns_responder->srv_queries_empty());
733 EXPECT_TRUE(mdns_responder->txt_queries_empty());
734 EXPECT_TRUE(mdns_responder->a_queries_empty());
735 EXPECT_TRUE(mdns_responder->aaaa_queries_empty());
736 }
737
TEST_F(MdnsResponderServiceTest,ServiceQueriesStoppedPtrFirst)738 TEST_F(MdnsResponderServiceTest, ServiceQueriesStoppedPtrFirst) {
739 EXPECT_CALL(observer_, OnStarted());
740 service_listener_->Start();
741
742 auto* mdns_responder = mdns_responder_factory_->last_mdns_responder();
743
744 AddEventsForNewService(mdns_responder, kTestServiceInstance, kTestServiceName,
745 kTestServiceProtocol, "gigliorononomicon", kTestPort,
746 {"model=shifty", "id=asdf"}, IPAddress{192, 168, 3, 7},
747 kDefaultSocket);
748
749 EXPECT_CALL(observer_, OnReceiverAdded(_));
750 mdns_service_->HandleNewEvents();
751
752 auto ptr_remove = MakePtrEvent(kTestServiceInstance, kTestServiceName,
753 kTestServiceProtocol, kDefaultSocket);
754 ptr_remove.header.response_type = QueryEventHeader::Type::kRemoved;
755 mdns_responder->AddPtrEvent(std::move(ptr_remove));
756
757 EXPECT_CALL(observer_, OnReceiverRemoved(_));
758 mdns_service_->HandleNewEvents();
759
760 EXPECT_FALSE(mdns_responder->ptr_queries_empty());
761 EXPECT_FALSE(mdns_responder->srv_queries_empty());
762 EXPECT_FALSE(mdns_responder->txt_queries_empty());
763 EXPECT_FALSE(mdns_responder->a_queries_empty());
764 EXPECT_FALSE(mdns_responder->aaaa_queries_empty());
765
766 auto srv_remove =
767 MakeSrvEvent(kTestServiceInstance, kTestServiceName, kTestServiceProtocol,
768 "gigliorononomicon", kTestPort, kDefaultSocket);
769 srv_remove.header.response_type = QueryEventHeader::Type::kRemoved;
770 mdns_responder->AddSrvEvent(std::move(srv_remove));
771 mdns_service_->HandleNewEvents();
772
773 EXPECT_FALSE(mdns_responder->ptr_queries_empty());
774 EXPECT_TRUE(mdns_responder->srv_queries_empty());
775 EXPECT_TRUE(mdns_responder->txt_queries_empty());
776 EXPECT_TRUE(mdns_responder->a_queries_empty());
777 EXPECT_TRUE(mdns_responder->aaaa_queries_empty());
778 }
779
TEST_F(MdnsResponderServiceTest,MultipleInterfaceRemove)780 TEST_F(MdnsResponderServiceTest, MultipleInterfaceRemove) {
781 EXPECT_CALL(observer_, OnStarted());
782 service_listener_->Start();
783
784 auto* mdns_responder = mdns_responder_factory_->last_mdns_responder();
785
786 AddEventsForNewService(mdns_responder, kTestServiceInstance, kTestServiceName,
787 kTestServiceProtocol, "gigliorononomicon", kTestPort,
788 {"model=shifty", "id=asdf"}, IPAddress{192, 168, 3, 7},
789 kDefaultSocket);
790 AddEventsForNewService(mdns_responder, kTestServiceInstance, kTestServiceName,
791 kTestServiceProtocol, "gigliorononomicon", kTestPort,
792 {"model=shifty", "id=asdf"}, IPAddress{192, 168, 3, 7},
793 kSecondSocket);
794
795 EXPECT_CALL(observer_, OnReceiverAdded(_));
796 mdns_service_->HandleNewEvents();
797
798 auto srv_remove1 =
799 MakeSrvEvent(kTestServiceInstance, kTestServiceName, kTestServiceProtocol,
800 "gigliorononomicon", kTestPort, kSecondSocket);
801 srv_remove1.header.response_type = QueryEventHeader::Type::kRemoved;
802 mdns_responder->AddSrvEvent(std::move(srv_remove1));
803 EXPECT_CALL(observer_, OnReceiverChanged(_)).Times(0);
804 EXPECT_CALL(observer_, OnReceiverRemoved(_)).Times(0);
805 mdns_service_->HandleNewEvents();
806
807 auto srv_remove2 =
808 MakeSrvEvent(kTestServiceInstance, kTestServiceName, kTestServiceProtocol,
809 "gigliorononomicon", kTestPort, kDefaultSocket);
810 srv_remove2.header.response_type = QueryEventHeader::Type::kRemoved;
811 mdns_responder->AddSrvEvent(std::move(srv_remove2));
812 EXPECT_CALL(observer_, OnReceiverRemoved(_));
813 mdns_service_->HandleNewEvents();
814 EXPECT_TRUE(mdns_responder->a_queries_empty());
815
816 auto ptr_remove = MakePtrEvent(kTestServiceInstance, kTestServiceName,
817 kTestServiceProtocol, kDefaultSocket);
818 ptr_remove.header.response_type = QueryEventHeader::Type::kRemoved;
819 mdns_responder->AddPtrEvent(std::move(ptr_remove));
820 mdns_service_->HandleNewEvents();
821
822 EXPECT_FALSE(mdns_responder->ptr_queries_empty());
823 EXPECT_TRUE(mdns_responder->srv_queries_empty());
824 EXPECT_TRUE(mdns_responder->txt_queries_empty());
825 EXPECT_TRUE(mdns_responder->a_queries_empty());
826 EXPECT_TRUE(mdns_responder->aaaa_queries_empty());
827 }
828
TEST_F(MdnsResponderServiceTest,ResumeService)829 TEST_F(MdnsResponderServiceTest, ResumeService) {
830 EXPECT_CALL(publisher_observer_, OnStarted());
831 service_publisher_->Start();
832
833 auto* mdns_responder = mdns_responder_factory_->last_mdns_responder();
834 ASSERT_TRUE(mdns_responder);
835 ASSERT_TRUE(mdns_responder->running());
836
837 EXPECT_EQ(2u, mdns_responder->registered_interfaces().size());
838 ASSERT_EQ(1u, mdns_responder->registered_services().size());
839
840 EXPECT_CALL(publisher_observer_, OnSuspended());
841 service_publisher_->Suspend();
842
843 EXPECT_TRUE(mdns_responder_factory_->last_mdns_responder());
844 EXPECT_EQ(0u, mdns_responder->registered_services().size());
845
846 EXPECT_CALL(publisher_observer_, OnStarted());
847 service_publisher_->Resume();
848
849 EXPECT_EQ(2u, mdns_responder->registered_interfaces().size());
850 ASSERT_EQ(1u, mdns_responder->registered_services().size());
851 }
852
TEST_F(MdnsResponderServiceTest,RestorePtrNotifiesObserver)853 TEST_F(MdnsResponderServiceTest, RestorePtrNotifiesObserver) {
854 EXPECT_CALL(observer_, OnStarted());
855 service_listener_->Start();
856
857 auto* mdns_responder = mdns_responder_factory_->last_mdns_responder();
858
859 AddEventsForNewService(mdns_responder, kTestServiceInstance, kTestServiceName,
860 kTestServiceProtocol, "gigliorononomicon", kTestPort,
861 {"model=shifty", "id=asdf"}, IPAddress{192, 168, 3, 7},
862 kDefaultSocket);
863
864 EXPECT_CALL(observer_, OnReceiverAdded(_));
865 mdns_service_->HandleNewEvents();
866
867 auto ptr_remove = MakePtrEvent(kTestServiceInstance, kTestServiceName,
868 kTestServiceProtocol, kDefaultSocket);
869 ptr_remove.header.response_type = QueryEventHeader::Type::kRemoved;
870 mdns_responder->AddPtrEvent(std::move(ptr_remove));
871
872 EXPECT_CALL(observer_, OnReceiverRemoved(_));
873 mdns_service_->HandleNewEvents();
874
875 auto ptr_add = MakePtrEvent(kTestServiceInstance, kTestServiceName,
876 kTestServiceProtocol, kDefaultSocket);
877 mdns_responder->AddPtrEvent(std::move(ptr_add));
878
879 EXPECT_CALL(observer_, OnReceiverAdded(_));
880 mdns_service_->HandleNewEvents();
881 }
882
883 } // namespace osp
884 } // namespace openscreen
885