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/testing/fake_mdns_platform_service.h"
6
7 #include <cstdint>
8
9 #include "gtest/gtest.h"
10
11 namespace openscreen {
12 namespace osp {
13 namespace {
14
15 UdpSocket* const kDefaultSocket =
16 reinterpret_cast<UdpSocket*>(static_cast<uintptr_t>(16));
17 UdpSocket* const kSecondSocket =
18 reinterpret_cast<UdpSocket*>(static_cast<uintptr_t>(24));
19
20 class FakeMdnsPlatformServiceTest : public ::testing::Test {
21 protected:
22 const uint8_t mac1_[6] = {11, 22, 33, 44, 55, 66};
23 const uint8_t mac2_[6] = {12, 23, 34, 45, 56, 67};
24 const IPSubnet subnet1_{IPAddress{192, 168, 3, 2}, 24};
25 const IPSubnet subnet2_{
26 IPAddress{0x0102, 0x0304, 0x0504, 0x0302, 0x0102, 0x0304, 0x0506, 0x0708},
27 24};
28 std::vector<MdnsPlatformService::BoundInterface> bound_interfaces_{
29 MdnsPlatformService::BoundInterface{
30 InterfaceInfo{1,
31 mac1_,
32 "eth0",
33 InterfaceInfo::Type::kEthernet,
34 {subnet1_}},
35 subnet1_, kDefaultSocket},
36 MdnsPlatformService::BoundInterface{
37 InterfaceInfo{2,
38 mac2_,
39 "eth1",
40 InterfaceInfo::Type::kEthernet,
41 {subnet2_}},
42 subnet2_, kSecondSocket}};
43 };
44
45 } // namespace
46
TEST_F(FakeMdnsPlatformServiceTest,SimpleRegistration)47 TEST_F(FakeMdnsPlatformServiceTest, SimpleRegistration) {
48 FakeMdnsPlatformService platform_service;
49 std::vector<MdnsPlatformService::BoundInterface> bound_interfaces{
50 bound_interfaces_[0]};
51
52 platform_service.set_interfaces(bound_interfaces);
53
54 auto registered_interfaces = platform_service.RegisterInterfaces({});
55 EXPECT_EQ(bound_interfaces, registered_interfaces);
56 platform_service.DeregisterInterfaces(registered_interfaces);
57
58 registered_interfaces = platform_service.RegisterInterfaces({});
59 EXPECT_EQ(bound_interfaces, registered_interfaces);
60 platform_service.DeregisterInterfaces(registered_interfaces);
61 platform_service.set_interfaces({});
62
63 registered_interfaces = platform_service.RegisterInterfaces({});
64 EXPECT_TRUE(registered_interfaces.empty());
65 platform_service.DeregisterInterfaces(registered_interfaces);
66
67 std::vector<MdnsPlatformService::BoundInterface> new_interfaces{
68 bound_interfaces_[1]};
69
70 platform_service.set_interfaces(new_interfaces);
71
72 registered_interfaces = platform_service.RegisterInterfaces({});
73 EXPECT_EQ(new_interfaces, registered_interfaces);
74 platform_service.DeregisterInterfaces(registered_interfaces);
75 }
76
TEST_F(FakeMdnsPlatformServiceTest,ObeyIndexAllowlist)77 TEST_F(FakeMdnsPlatformServiceTest, ObeyIndexAllowlist) {
78 FakeMdnsPlatformService platform_service;
79 platform_service.set_interfaces(bound_interfaces_);
80
81 auto eth0_only = platform_service.RegisterInterfaces({1});
82 EXPECT_EQ(
83 (std::vector<MdnsPlatformService::BoundInterface>{bound_interfaces_[0]}),
84 eth0_only);
85 platform_service.DeregisterInterfaces(eth0_only);
86
87 auto eth1_only = platform_service.RegisterInterfaces({2});
88 EXPECT_EQ(
89 (std::vector<MdnsPlatformService::BoundInterface>{bound_interfaces_[1]}),
90 eth1_only);
91 platform_service.DeregisterInterfaces(eth1_only);
92
93 auto both = platform_service.RegisterInterfaces({1, 2});
94 EXPECT_EQ(bound_interfaces_, both);
95 platform_service.DeregisterInterfaces(both);
96 }
97
TEST_F(FakeMdnsPlatformServiceTest,PartialDeregister)98 TEST_F(FakeMdnsPlatformServiceTest, PartialDeregister) {
99 FakeMdnsPlatformService platform_service;
100 platform_service.set_interfaces(bound_interfaces_);
101
102 auto both = platform_service.RegisterInterfaces({});
103 std::vector<MdnsPlatformService::BoundInterface> eth0_only{
104 bound_interfaces_[0]};
105 std::vector<MdnsPlatformService::BoundInterface> eth1_only{
106 bound_interfaces_[1]};
107 platform_service.DeregisterInterfaces(eth0_only);
108 platform_service.DeregisterInterfaces(eth1_only);
109 }
110
111 } // namespace osp
112 } // namespace openscreen
113