aboutsummaryrefslogtreecommitdiff
path: root/osp/impl/testing/fake_mdns_platform_service_unittest.cc
blob: 5a1d5f4ca10ea380a6bf02598b10f7a00b270412 (plain)
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
// Copyright 2018 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 "osp/impl/testing/fake_mdns_platform_service.h"

#include <cstdint>

#include "gtest/gtest.h"

namespace openscreen {
namespace osp {
namespace {

UdpSocket* const kDefaultSocket =
    reinterpret_cast<UdpSocket*>(static_cast<uintptr_t>(16));
UdpSocket* const kSecondSocket =
    reinterpret_cast<UdpSocket*>(static_cast<uintptr_t>(24));

class FakeMdnsPlatformServiceTest : public ::testing::Test {
 protected:
  const uint8_t mac1_[6] = {11, 22, 33, 44, 55, 66};
  const uint8_t mac2_[6] = {12, 23, 34, 45, 56, 67};
  const IPSubnet subnet1_{IPAddress{192, 168, 3, 2}, 24};
  const IPSubnet subnet2_{
      IPAddress{0x0102, 0x0304, 0x0504, 0x0302, 0x0102, 0x0304, 0x0506, 0x0708},
      24};
  std::vector<MdnsPlatformService::BoundInterface> bound_interfaces_{
      MdnsPlatformService::BoundInterface{
          InterfaceInfo{1,
                        mac1_,
                        "eth0",
                        InterfaceInfo::Type::kEthernet,
                        {subnet1_}},
          subnet1_, kDefaultSocket},
      MdnsPlatformService::BoundInterface{
          InterfaceInfo{2,
                        mac2_,
                        "eth1",
                        InterfaceInfo::Type::kEthernet,
                        {subnet2_}},
          subnet2_, kSecondSocket}};
};

}  // namespace

TEST_F(FakeMdnsPlatformServiceTest, SimpleRegistration) {
  FakeMdnsPlatformService platform_service;
  std::vector<MdnsPlatformService::BoundInterface> bound_interfaces{
      bound_interfaces_[0]};

  platform_service.set_interfaces(bound_interfaces);

  auto registered_interfaces = platform_service.RegisterInterfaces({});
  EXPECT_EQ(bound_interfaces, registered_interfaces);
  platform_service.DeregisterInterfaces(registered_interfaces);

  registered_interfaces = platform_service.RegisterInterfaces({});
  EXPECT_EQ(bound_interfaces, registered_interfaces);
  platform_service.DeregisterInterfaces(registered_interfaces);
  platform_service.set_interfaces({});

  registered_interfaces = platform_service.RegisterInterfaces({});
  EXPECT_TRUE(registered_interfaces.empty());
  platform_service.DeregisterInterfaces(registered_interfaces);

  std::vector<MdnsPlatformService::BoundInterface> new_interfaces{
      bound_interfaces_[1]};

  platform_service.set_interfaces(new_interfaces);

  registered_interfaces = platform_service.RegisterInterfaces({});
  EXPECT_EQ(new_interfaces, registered_interfaces);
  platform_service.DeregisterInterfaces(registered_interfaces);
}

TEST_F(FakeMdnsPlatformServiceTest, ObeyIndexAllowlist) {
  FakeMdnsPlatformService platform_service;
  platform_service.set_interfaces(bound_interfaces_);

  auto eth0_only = platform_service.RegisterInterfaces({1});
  EXPECT_EQ(
      (std::vector<MdnsPlatformService::BoundInterface>{bound_interfaces_[0]}),
      eth0_only);
  platform_service.DeregisterInterfaces(eth0_only);

  auto eth1_only = platform_service.RegisterInterfaces({2});
  EXPECT_EQ(
      (std::vector<MdnsPlatformService::BoundInterface>{bound_interfaces_[1]}),
      eth1_only);
  platform_service.DeregisterInterfaces(eth1_only);

  auto both = platform_service.RegisterInterfaces({1, 2});
  EXPECT_EQ(bound_interfaces_, both);
  platform_service.DeregisterInterfaces(both);
}

TEST_F(FakeMdnsPlatformServiceTest, PartialDeregister) {
  FakeMdnsPlatformService platform_service;
  platform_service.set_interfaces(bound_interfaces_);

  auto both = platform_service.RegisterInterfaces({});
  std::vector<MdnsPlatformService::BoundInterface> eth0_only{
      bound_interfaces_[0]};
  std::vector<MdnsPlatformService::BoundInterface> eth1_only{
      bound_interfaces_[1]};
  platform_service.DeregisterInterfaces(eth0_only);
  platform_service.DeregisterInterfaces(eth1_only);
}

}  // namespace osp
}  // namespace openscreen