• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/discovery/mdns/mdns_responder_adapter_impl.h"
6 
7 #include <memory>
8 #include <string>
9 
10 #include "gmock/gmock.h"
11 #include "gtest/gtest.h"
12 
13 namespace openscreen {
14 namespace osp {
15 namespace {
16 
17 using ::testing::ElementsAre;
18 using ::testing::ElementsAreArray;
19 
20 // Example response for _openscreen._udp.  Contains PTR, SRV, TXT, A records.
21 uint8_t data[] = {
22     0x00, 0x00, 0x84, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x03,
23     0x06, 0x74, 0x75, 0x72, 0x74, 0x6c, 0x65, 0x0b, 0x5f, 0x6f, 0x70, 0x65,
24     0x6e, 0x73, 0x63, 0x72, 0x65, 0x65, 0x6e, 0x04, 0x5f, 0x75, 0x64, 0x70,
25     0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x00, 0x00, 0x10, 0x80, 0x01, 0x00,
26     0x00, 0x11, 0x94, 0x00, 0x0e, 0x06, 0x79, 0x75, 0x72, 0x74, 0x6c, 0x65,
27     0x06, 0x74, 0x75, 0x72, 0x74, 0x6c, 0x65, 0x09, 0x5f, 0x73, 0x65, 0x72,
28     0x76, 0x69, 0x63, 0x65, 0x73, 0x07, 0x5f, 0x64, 0x6e, 0x73, 0x2d, 0x73,
29     0x64, 0xc0, 0x1f, 0x00, 0x0c, 0x00, 0x01, 0x00, 0x00, 0x11, 0x94, 0x00,
30     0x02, 0xc0, 0x13, 0xc0, 0x13, 0x00, 0x0c, 0x00, 0x01, 0x00, 0x00, 0x11,
31     0x94, 0x00, 0x02, 0xc0, 0x0c, 0x11, 0x67, 0x69, 0x67, 0x6c, 0x69, 0x6f,
32     0x72, 0x6f, 0x6e, 0x6f, 0x6e, 0x6f, 0x6d, 0x69, 0x63, 0x6f, 0x6e, 0xc0,
33     0x24, 0x00, 0x01, 0x80, 0x01, 0x00, 0x00, 0x00, 0x78, 0x00, 0x04, 0xac,
34     0x11, 0x20, 0x96, 0xc0, 0x0c, 0x00, 0x21, 0x80, 0x01, 0x00, 0x00, 0x00,
35     0x78, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x30, 0x39, 0xc0, 0x71, 0xc0,
36     0x0c, 0x00, 0x2f, 0x80, 0x01, 0x00, 0x00, 0x11, 0x94, 0x00, 0x09, 0xc0,
37     0x0c, 0x00, 0x05, 0x00, 0x00, 0x80, 0x00, 0x40, 0xc0, 0x71, 0x00, 0x2f,
38     0x80, 0x01, 0x00, 0x00, 0x00, 0x78, 0x00, 0x05, 0xc0, 0x71, 0x00, 0x01,
39     0x40, 0x00, 0x00, 0x29, 0x05, 0xa0, 0x00, 0x00, 0x11, 0x94, 0x00, 0x12,
40     0x00, 0x04, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
41     0x50, 0x65, 0xf3, 0x41, 0x27, 0x01,
42 };
43 
44 }  // namespace
45 
TEST(MdnsResponderAdapterImplTest,ExampleData)46 TEST(MdnsResponderAdapterImplTest, ExampleData) {
47   const DomainName openscreen_service{{11,  '_', 'o', 'p', 'e', 'n', 's', 'c',
48                                        'r', 'e', 'e', 'n', 4,   '_', 'u', 'd',
49                                        'p', 5,   'l', 'o', 'c', 'a', 'l', 0}};
50   const IPEndpoint mdns_endpoint{{224, 0, 0, 251}, 5353};
51 
52   UdpPacket packet(std::begin(data), std::end(data));
53   packet.set_source({{192, 168, 0, 2}, 6556});
54   packet.set_destination(mdns_endpoint);
55   packet.set_socket(nullptr);
56 
57   auto mdns_adapter =
58       std::unique_ptr<MdnsResponderAdapter>(new MdnsResponderAdapterImpl);
59   mdns_adapter->Init();
60   mdns_adapter->StartPtrQuery(0, openscreen_service);
61   mdns_adapter->OnRead(nullptr, std::move(packet));
62   mdns_adapter->RunTasks();
63 
64   auto ptr = mdns_adapter->TakePtrResponses();
65   ASSERT_EQ(1u, ptr.size());
66   ASSERT_THAT(ptr[0].service_instance.GetLabels(),
67               ElementsAre("turtle", "_openscreen", "_udp", "local"));
68   mdns_adapter->StartSrvQuery(0, ptr[0].service_instance);
69   mdns_adapter->StartTxtQuery(0, ptr[0].service_instance);
70   mdns_adapter->RunTasks();
71 
72   auto srv = mdns_adapter->TakeSrvResponses();
73   ASSERT_EQ(1u, srv.size());
74   ASSERT_THAT(srv[0].domain_name.GetLabels(),
75               ElementsAre("gigliorononomicon", "local"));
76   EXPECT_EQ(12345, srv[0].port);
77 
78   auto txt = mdns_adapter->TakeTxtResponses();
79   ASSERT_EQ(1u, txt.size());
80   const std::string expected_txt[] = {"yurtle", "turtle"};
81   EXPECT_THAT(txt[0].txt_info, ElementsAreArray(expected_txt));
82 
83   mdns_adapter->StartAQuery(0, srv[0].domain_name);
84   mdns_adapter->RunTasks();
85 
86   auto a = mdns_adapter->TakeAResponses();
87   ASSERT_EQ(1u, a.size());
88   EXPECT_EQ((IPAddress{172, 17, 32, 150}), a[0].address);
89 
90   mdns_adapter->Close();
91 }
92 
93 }  // namespace osp
94 }  // namespace openscreen
95