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/service_key.h"
6
7 #include <unordered_map>
8
9 #include "absl/hash/hash.h"
10 #include "discovery/dnssd/testing/fake_dns_record_factory.h"
11 #include "gtest/gtest.h"
12
13 namespace openscreen {
14 namespace discovery {
15
TEST(DnsSdServiceKeyTest,TestServiceKeyEquals)16 TEST(DnsSdServiceKeyTest, TestServiceKeyEquals) {
17 ServiceKey key1("_service._udp", "domain");
18 ServiceKey key2("_service._udp", "domain");
19 EXPECT_TRUE(key1 == key2);
20 EXPECT_FALSE(key1 != key2);
21
22 key1 = ServiceKey("_service2._udp", "domain");
23 EXPECT_FALSE(key1 == key2);
24 EXPECT_TRUE(key1 != key2);
25 key2 = ServiceKey("_service2._udp", "domain");
26 EXPECT_TRUE(key1 == key2);
27 EXPECT_FALSE(key1 != key2);
28
29 key1 = ServiceKey("_service._udp", "domain2");
30 EXPECT_FALSE(key1 == key2);
31 EXPECT_TRUE(key1 != key2);
32 key2 = ServiceKey("_service._udp", "domain2");
33 EXPECT_TRUE(key1 == key2);
34 EXPECT_FALSE(key1 != key2);
35 }
36
TEST(DnsSdServiceKeyTest,ServiceKeyInMap)37 TEST(DnsSdServiceKeyTest, ServiceKeyInMap) {
38 ServiceKey ptr{"_service._udp", "domain"};
39 ServiceKey ptr2{"_service._udp", "domain"};
40 ServiceKey ptr3{"_service2._udp", "domain"};
41 ServiceKey ptr4{"_service._udp", "domain2"};
42 std::unordered_map<ServiceKey, int32_t, absl::Hash<ServiceKey>> map;
43 map[ptr] = 1;
44 map[ptr2] = 2;
45 EXPECT_EQ(map[ptr], 2);
46
47 map[ptr3] = 3;
48 EXPECT_EQ(map[ptr], 2);
49 EXPECT_EQ(map[ptr3], 3);
50
51 map[ptr4] = 4;
52 EXPECT_EQ(map[ptr], 2);
53 EXPECT_EQ(map[ptr3], 3);
54 EXPECT_EQ(map[ptr4], 4);
55 }
56
TEST(DnsSdServiceKeyTest,CreateFromRecordTest)57 TEST(DnsSdServiceKeyTest, CreateFromRecordTest) {
58 MdnsRecord record = FakeDnsRecordFactory::CreateFullyPopulatedSrvRecord();
59 ServiceKey key(record);
60 EXPECT_EQ(key.service_id(), FakeDnsRecordFactory::kServiceName);
61 EXPECT_EQ(key.domain_id(), FakeDnsRecordFactory::kDomainName);
62 }
63
64 } // namespace discovery
65 } // namespace openscreen
66