OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/memory/weak_ptr.h" | 5 #include "base/memory/weak_ptr.h" |
6 #include "base/run_loop.h" | 6 #include "base/run_loop.h" |
7 #include "chrome/utility/local_discovery/service_discovery_client_impl.h" | 7 #include "chrome/utility/local_discovery/service_discovery_client_impl.h" |
8 #include "net/base/net_errors.h" | 8 #include "net/base/net_errors.h" |
9 #include "net/dns/dns_protocol.h" | 9 #include "net/dns/dns_protocol.h" |
10 #include "net/dns/mdns_client_impl.h" | 10 #include "net/dns/mdns_client_impl.h" |
11 #include "net/dns/mock_mdns_socket_factory.h" | 11 #include "net/dns/mock_mdns_socket_factory.h" |
12 #include "testing/gmock/include/gmock/gmock.h" | 12 #include "testing/gmock/include/gmock/gmock.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 | 14 |
15 using ::testing::_; | 15 using ::testing::_; |
16 using ::testing::Invoke; | 16 using ::testing::Invoke; |
17 using ::testing::StrictMock; | 17 using ::testing::StrictMock; |
18 using ::testing::NiceMock; | 18 using ::testing::NiceMock; |
19 using ::testing::Mock; | 19 using ::testing::Mock; |
20 using ::testing::SaveArg; | 20 using ::testing::SaveArg; |
21 using ::testing::SetArgPointee; | 21 using ::testing::SetArgPointee; |
22 using ::testing::Return; | 22 using ::testing::Return; |
23 using ::testing::Exactly; | 23 using ::testing::Exactly; |
24 | 24 |
25 namespace local_discovery { | 25 namespace local_discovery { |
26 | 26 |
27 namespace { | 27 namespace { |
28 | 28 |
29 const char kSamplePacketPTR[] = { | 29 const uint8 kSamplePacketPTR[] = { |
30 // Header | 30 // Header |
31 '\x00', '\x00', // ID is zeroed out | 31 0x00, 0x00, // ID is zeroed out |
32 '\x81', '\x80', // Standard query response, RA, no error | 32 0x81, 0x80, // Standard query response, RA, no error |
33 '\x00', '\x00', // No questions (for simplicity) | 33 0x00, 0x00, // No questions (for simplicity) |
34 '\x00', '\x01', // 1 RR (answers) | 34 0x00, 0x01, // 1 RR (answers) |
35 '\x00', '\x00', // 0 authority RRs | 35 0x00, 0x00, // 0 authority RRs |
36 '\x00', '\x00', // 0 additional RRs | 36 0x00, 0x00, // 0 additional RRs |
37 | 37 |
38 '\x07', '_', 'p', 'r', 'i', 'v', 'e', 't', | 38 0x07, '_', 'p', 'r', 'i', 'v', 'e', 't', |
39 '\x04', '_', 't', 'c', 'p', | 39 0x04, '_', 't', 'c', 'p', |
40 '\x05', 'l', 'o', 'c', 'a', 'l', | 40 0x05, 'l', 'o', 'c', 'a', 'l', |
41 '\x00', | 41 0x00, |
42 '\x00', '\x0c', // TYPE is PTR. | 42 0x00, 0x0c, // TYPE is PTR. |
43 '\x00', '\x01', // CLASS is IN. | 43 0x00, 0x01, // CLASS is IN. |
44 '\x00', '\x00', // TTL (4 bytes) is 1 second. | 44 0x00, 0x00, // TTL (4 bytes) is 1 second. |
45 '\x00', '\x01', | 45 0x00, 0x01, |
46 '\x00', '\x08', // RDLENGTH is 8 bytes. | 46 0x00, 0x08, // RDLENGTH is 8 bytes. |
47 '\x05', 'h', 'e', 'l', 'l', 'o', | 47 0x05, 'h', 'e', 'l', 'l', 'o', |
48 '\xc0', '\x0c' | 48 0xc0, 0x0c |
49 }; | 49 }; |
50 | 50 |
51 const char kSamplePacketSRV[] = { | 51 const uint8 kSamplePacketSRV[] = { |
52 // Header | 52 // Header |
53 '\x00', '\x00', // ID is zeroed out | 53 0x00, 0x00, // ID is zeroed out |
54 '\x81', '\x80', // Standard query response, RA, no error | 54 0x81, 0x80, // Standard query response, RA, no error |
55 '\x00', '\x00', // No questions (for simplicity) | 55 0x00, 0x00, // No questions (for simplicity) |
56 '\x00', '\x01', // 1 RR (answers) | 56 0x00, 0x01, // 1 RR (answers) |
57 '\x00', '\x00', // 0 authority RRs | 57 0x00, 0x00, // 0 authority RRs |
58 '\x00', '\x00', // 0 additional RRs | 58 0x00, 0x00, // 0 additional RRs |
59 | 59 |
60 '\x05', 'h', 'e', 'l', 'l', 'o', | 60 0x05, 'h', 'e', 'l', 'l', 'o', |
61 '\x07', '_', 'p', 'r', 'i', 'v', 'e', 't', | 61 0x07, '_', 'p', 'r', 'i', 'v', 'e', 't', |
62 '\x04', '_', 't', 'c', 'p', | 62 0x04, '_', 't', 'c', 'p', |
63 '\x05', 'l', 'o', 'c', 'a', 'l', | 63 0x05, 'l', 'o', 'c', 'a', 'l', |
64 '\x00', | 64 0x00, |
65 '\x00', '\x21', // TYPE is SRV. | 65 0x00, 0x21, // TYPE is SRV. |
66 '\x00', '\x01', // CLASS is IN. | 66 0x00, 0x01, // CLASS is IN. |
67 '\x00', '\x00', // TTL (4 bytes) is 1 second. | 67 0x00, 0x00, // TTL (4 bytes) is 1 second. |
68 '\x00', '\x01', | 68 0x00, 0x01, |
69 '\x00', '\x15', // RDLENGTH is 21 bytes. | 69 0x00, 0x15, // RDLENGTH is 21 bytes. |
70 '\x00', '\x00', | 70 0x00, 0x00, |
71 '\x00', '\x00', | 71 0x00, 0x00, |
72 '\x22', '\xb8', // port 8888 | 72 0x22, 0xb8, // port 8888 |
73 '\x07', 'm', 'y', 'h', 'e', 'l', 'l', 'o', | 73 0x07, 'm', 'y', 'h', 'e', 'l', 'l', 'o', |
74 '\x05', 'l', 'o', 'c', 'a', 'l', | 74 0x05, 'l', 'o', 'c', 'a', 'l', |
75 '\x00', | 75 0x00, |
76 }; | 76 }; |
77 | 77 |
78 const char kSamplePacketTXT[] = { | 78 const uint8 kSamplePacketTXT[] = { |
79 // Header | 79 // Header |
80 '\x00', '\x00', // ID is zeroed out | 80 0x00, 0x00, // ID is zeroed out |
81 '\x81', '\x80', // Standard query response, RA, no error | 81 0x81, 0x80, // Standard query response, RA, no error |
82 '\x00', '\x00', // No questions (for simplicity) | 82 0x00, 0x00, // No questions (for simplicity) |
83 '\x00', '\x01', // 1 RR (answers) | 83 0x00, 0x01, // 1 RR (answers) |
84 '\x00', '\x00', // 0 authority RRs | 84 0x00, 0x00, // 0 authority RRs |
85 '\x00', '\x00', // 0 additional RRs | 85 0x00, 0x00, // 0 additional RRs |
86 | 86 |
87 '\x05', 'h', 'e', 'l', 'l', 'o', | 87 0x05, 'h', 'e', 'l', 'l', 'o', |
88 '\x07', '_', 'p', 'r', 'i', 'v', 'e', 't', | 88 0x07, '_', 'p', 'r', 'i', 'v', 'e', 't', |
89 '\x04', '_', 't', 'c', 'p', | 89 0x04, '_', 't', 'c', 'p', |
90 '\x05', 'l', 'o', 'c', 'a', 'l', | 90 0x05, 'l', 'o', 'c', 'a', 'l', |
91 '\x00', | 91 0x00, |
92 '\x00', '\x10', // TYPE is PTR. | 92 0x00, 0x10, // TYPE is PTR. |
93 '\x00', '\x01', // CLASS is IN. | 93 0x00, 0x01, // CLASS is IN. |
94 '\x00', '\x00', // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds. | 94 0x00, 0x00, // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds. |
95 '\x00', '\x01', | 95 0x00, 0x01, |
96 '\x00', '\x06', // RDLENGTH is 21 bytes. | 96 0x00, 0x06, // RDLENGTH is 21 bytes. |
97 '\x05', 'h', 'e', 'l', 'l', 'o' | 97 0x05, 'h', 'e', 'l', 'l', 'o' |
98 }; | 98 }; |
99 | 99 |
100 const char kSamplePacketSRVA[] = { | 100 const uint8 kSamplePacketSRVA[] = { |
101 // Header | 101 // Header |
102 '\x00', '\x00', // ID is zeroed out | 102 0x00, 0x00, // ID is zeroed out |
103 '\x81', '\x80', // Standard query response, RA, no error | 103 0x81, 0x80, // Standard query response, RA, no error |
104 '\x00', '\x00', // No questions (for simplicity) | 104 0x00, 0x00, // No questions (for simplicity) |
105 '\x00', '\x02', // 2 RR (answers) | 105 0x00, 0x02, // 2 RR (answers) |
106 '\x00', '\x00', // 0 authority RRs | 106 0x00, 0x00, // 0 authority RRs |
107 '\x00', '\x00', // 0 additional RRs | 107 0x00, 0x00, // 0 additional RRs |
108 | 108 |
109 '\x05', 'h', 'e', 'l', 'l', 'o', | 109 0x05, 'h', 'e', 'l', 'l', 'o', |
110 '\x07', '_', 'p', 'r', 'i', 'v', 'e', 't', | 110 0x07, '_', 'p', 'r', 'i', 'v', 'e', 't', |
111 '\x04', '_', 't', 'c', 'p', | 111 0x04, '_', 't', 'c', 'p', |
112 '\x05', 'l', 'o', 'c', 'a', 'l', | 112 0x05, 'l', 'o', 'c', 'a', 'l', |
113 '\x00', | 113 0x00, |
114 '\x00', '\x21', // TYPE is SRV. | 114 0x00, 0x21, // TYPE is SRV. |
115 '\x00', '\x01', // CLASS is IN. | 115 0x00, 0x01, // CLASS is IN. |
116 '\x00', '\x00', // TTL (4 bytes) is 16 seconds. | 116 0x00, 0x00, // TTL (4 bytes) is 16 seconds. |
117 '\x00', '\x10', | 117 0x00, 0x10, |
118 '\x00', '\x15', // RDLENGTH is 21 bytes. | 118 0x00, 0x15, // RDLENGTH is 21 bytes. |
119 '\x00', '\x00', | 119 0x00, 0x00, |
120 '\x00', '\x00', | 120 0x00, 0x00, |
121 '\x22', '\xb8', // port 8888 | 121 0x22, 0xb8, // port 8888 |
122 '\x07', 'm', 'y', 'h', 'e', 'l', 'l', 'o', | 122 0x07, 'm', 'y', 'h', 'e', 'l', 'l', 'o', |
123 '\x05', 'l', 'o', 'c', 'a', 'l', | 123 0x05, 'l', 'o', 'c', 'a', 'l', |
124 '\x00', | 124 0x00, |
125 | 125 |
126 '\x07', 'm', 'y', 'h', 'e', 'l', 'l', 'o', | 126 0x07, 'm', 'y', 'h', 'e', 'l', 'l', 'o', |
127 '\x05', 'l', 'o', 'c', 'a', 'l', | 127 0x05, 'l', 'o', 'c', 'a', 'l', |
128 '\x00', | 128 0x00, |
129 '\x00', '\x01', // TYPE is A. | 129 0x00, 0x01, // TYPE is A. |
130 '\x00', '\x01', // CLASS is IN. | 130 0x00, 0x01, // CLASS is IN. |
131 '\x00', '\x00', // TTL (4 bytes) is 16 seconds. | 131 0x00, 0x00, // TTL (4 bytes) is 16 seconds. |
132 '\x00', '\x10', | 132 0x00, 0x10, |
133 '\x00', '\x04', // RDLENGTH is 4 bytes. | 133 0x00, 0x04, // RDLENGTH is 4 bytes. |
134 '\x01', '\x02', | 134 0x01, 0x02, |
135 '\x03', '\x04', | 135 0x03, 0x04, |
136 }; | 136 }; |
137 | 137 |
138 class MockServiceWatcherClient { | 138 class MockServiceWatcherClient { |
139 public: | 139 public: |
140 MOCK_METHOD2(OnServiceUpdated, | 140 MOCK_METHOD2(OnServiceUpdated, |
141 void(ServiceWatcher::UpdateType, const std::string&)); | 141 void(ServiceWatcher::UpdateType, const std::string&)); |
142 | 142 |
143 ServiceWatcher::UpdatedCallback GetCallback() { | 143 ServiceWatcher::UpdatedCallback GetCallback() { |
144 return base::Bind(&MockServiceWatcherClient::OnServiceUpdated, | 144 return base::Bind(&MockServiceWatcherClient::OnServiceUpdated, |
145 base::Unretained(this)); | 145 base::Unretained(this)); |
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
418 ServiceResolver::STATUS_REQUEST_TIMEOUT, _, _, _)); | 418 ServiceResolver::STATUS_REQUEST_TIMEOUT, _, _, _)); |
419 | 419 |
420 // TODO(noamsml): When NSEC record support is added, change this to use an | 420 // TODO(noamsml): When NSEC record support is added, change this to use an |
421 // NSEC record. | 421 // NSEC record. |
422 RunFor(base::TimeDelta::FromSeconds(4)); | 422 RunFor(base::TimeDelta::FromSeconds(4)); |
423 }; | 423 }; |
424 | 424 |
425 } // namespace | 425 } // namespace |
426 | 426 |
427 } // namespace local_discovery | 427 } // namespace local_discovery |
OLD | NEW |