• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 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 "discovery/dnssd/impl/instance_key.h"
6 
7 #include <unordered_map>
8 
9 #include "absl/hash/hash.h"
10 #include "discovery/dnssd/impl/service_key.h"
11 #include "discovery/dnssd/testing/fake_dns_record_factory.h"
12 #include "gtest/gtest.h"
13 
14 namespace openscreen {
15 namespace discovery {
16 
TEST(DnsSdInstanceKeyTest,TestInstanceKeyEquals)17 TEST(DnsSdInstanceKeyTest, TestInstanceKeyEquals) {
18   InstanceKey key1("instance", "_service._udp", "domain");
19   InstanceKey key2("instance", "_service._udp", "domain");
20   EXPECT_TRUE(key1 == key2);
21   EXPECT_FALSE(key1 != key2);
22 
23   key1 = InstanceKey("instance", "_service2._udp", "domain");
24   EXPECT_FALSE(key1 == key2);
25   EXPECT_TRUE(key1 != key2);
26   key2 = InstanceKey("instance", "_service2._udp", "domain");
27   EXPECT_TRUE(key1 == key2);
28   EXPECT_FALSE(key1 != key2);
29 
30   key1 = InstanceKey("instance", "_service2._udp", "domain2");
31   EXPECT_FALSE(key1 == key2);
32   EXPECT_TRUE(key1 != key2);
33   key2 = InstanceKey("instance", "_service2._udp", "domain2");
34   EXPECT_TRUE(key1 == key2);
35   EXPECT_FALSE(key1 != key2);
36 
37   key1 = InstanceKey("instance2", "_service2._udp", "domain2");
38   EXPECT_FALSE(key1 == key2);
39   EXPECT_TRUE(key1 != key2);
40   key2 = InstanceKey("instance2", "_service2._udp", "domain2");
41   EXPECT_TRUE(key1 == key2);
42   EXPECT_FALSE(key1 != key2);
43 }
44 
TEST(DnsSdInstanceKeyTest,TestIsInstanceOf)45 TEST(DnsSdInstanceKeyTest, TestIsInstanceOf) {
46   ServiceKey ptr("_service._udp", "domain");
47   InstanceKey svc("instance", "_service._udp", "domain");
48   EXPECT_EQ(svc, ptr);
49 
50   svc = InstanceKey("other id", "_service._udp", "domain");
51   EXPECT_EQ(svc, ptr);
52 
53   svc = InstanceKey("instance", "_service._udp", "domain2");
54   EXPECT_FALSE(svc == ptr);
55   ptr = ServiceKey("_service._udp", "domain2");
56   EXPECT_EQ(svc, ptr);
57 
58   svc = InstanceKey("instance", "_service2._udp", "domain");
59   EXPECT_NE(svc, ptr);
60   ptr = ServiceKey("_service2._udp", "domain");
61   EXPECT_EQ(svc, ptr);
62 }
63 
TEST(DnsSdInstanceKeyTest,InstanceKeyInMap)64 TEST(DnsSdInstanceKeyTest, InstanceKeyInMap) {
65   InstanceKey key{"instance", "_service._udp", "domain"};
66   InstanceKey key2{"instance", "_service._udp", "domain"};
67   InstanceKey key3{"instance2", "_service._udp", "domain"};
68   InstanceKey key4{"instance", "_service2._udp", "domain"};
69   InstanceKey key5{"instance", "_service2._udp", "domain2"};
70   std::unordered_map<InstanceKey, int32_t, absl::Hash<InstanceKey>> map;
71   map[key] = 1;
72   map[key2] = 2;
73   EXPECT_EQ(map[key], 2);
74 
75   map[key3] = 3;
76   EXPECT_EQ(map[key], 2);
77   EXPECT_EQ(map[key3], 3);
78 
79   map[key4] = 4;
80   EXPECT_EQ(map[key], 2);
81   EXPECT_EQ(map[key3], 3);
82   EXPECT_EQ(map[key4], 4);
83 
84   map[key5] = 5;
85   EXPECT_EQ(map[key], 2);
86   EXPECT_EQ(map[key3], 3);
87   EXPECT_EQ(map[key4], 4);
88   EXPECT_EQ(map[key5], 5);
89 }
90 
TEST(DnsSdInstanceKeyTest,CreateFromRecordTest)91 TEST(DnsSdInstanceKeyTest, CreateFromRecordTest) {
92   MdnsRecord record = FakeDnsRecordFactory::CreateFullyPopulatedSrvRecord();
93   InstanceKey key(record);
94   EXPECT_EQ(key.instance_id(), FakeDnsRecordFactory::kInstanceName);
95   EXPECT_EQ(key.service_id(), FakeDnsRecordFactory::kServiceName);
96   EXPECT_EQ(key.domain_id(), FakeDnsRecordFactory::kDomainName);
97 }
98 
TEST(DnsSdInstanceKeyTest,GetNameTest)99 TEST(DnsSdInstanceKeyTest, GetNameTest) {
100   InstanceKey key("instance", "_service._udp", "domain");
101   DomainName expected{"instance", "_service", "_udp", "domain"};
102   EXPECT_EQ(expected, key.GetName());
103 
104   key = InstanceKey("foo", "_bar._tcp", "local");
105   expected = DomainName{"foo", "_bar", "_tcp", "local"};
106   EXPECT_EQ(expected, key.GetName());
107 }
108 
109 }  // namespace discovery
110 }  // namespace openscreen
111