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 "chrome/utility/local_discovery/service_discovery_client_impl.h" | 5 #include "chrome/utility/local_discovery/service_discovery_client_impl.h" |
6 #include "net/dns/mdns_client_impl.h" | 6 #include "net/dns/mdns_client_impl.h" |
7 #include "net/dns/mock_mdns_socket_factory.h" | 7 #include "net/dns/mock_mdns_socket_factory.h" |
8 #include "testing/gmock/include/gmock/gmock.h" | 8 #include "testing/gmock/include/gmock/gmock.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 0x00, 0x10, | 53 0x00, 0x10, |
54 0x00, 0x10, // RDLENGTH is 4 bytes. | 54 0x00, 0x10, // RDLENGTH is 4 bytes. |
55 0x00, 0x0A, 0x00, 0x00, | 55 0x00, 0x0A, 0x00, 0x00, |
56 0x00, 0x00, 0x00, 0x00, | 56 0x00, 0x00, 0x00, 0x00, |
57 0x00, 0x01, 0x00, 0x02, | 57 0x00, 0x01, 0x00, 0x02, |
58 0x00, 0x03, 0x00, 0x04, | 58 0x00, 0x03, 0x00, 0x04, |
59 }; | 59 }; |
60 | 60 |
61 class LocalDomainResolverTest : public testing::Test { | 61 class LocalDomainResolverTest : public testing::Test { |
62 public: | 62 public: |
63 LocalDomainResolverTest() : socket_factory_(new net::MockMDnsSocketFactory), | |
64 mdns_client_( | |
65 scoped_ptr<net::MDnsConnection::SocketFactory>( | |
66 socket_factory_)) { | |
67 } | |
68 | |
69 ~LocalDomainResolverTest() { | |
70 } | |
71 | |
72 virtual void SetUp() OVERRIDE { | 63 virtual void SetUp() OVERRIDE { |
73 mdns_client_.StartListening(); | 64 mdns_client_.StartListening(&socket_factory_); |
74 } | 65 } |
75 | 66 |
76 std::string IPAddressToStringWithEmpty(const net::IPAddressNumber& address) { | 67 std::string IPAddressToStringWithEmpty(const net::IPAddressNumber& address) { |
77 if (address.empty()) return ""; | 68 if (address.empty()) return ""; |
78 return net::IPAddressToString(address); | 69 return net::IPAddressToString(address); |
79 } | 70 } |
80 | 71 |
81 void AddressCallback(bool resolved, | 72 void AddressCallback(bool resolved, |
82 const net::IPAddressNumber& address_ipv4, | 73 const net::IPAddressNumber& address_ipv4, |
83 const net::IPAddressNumber& address_ipv6) { | 74 const net::IPAddressNumber& address_ipv6) { |
(...skipping 11 matching lines...) Expand all Loading... |
95 | 86 |
96 base::MessageLoop::current()->Run(); | 87 base::MessageLoop::current()->Run(); |
97 callback.Cancel(); | 88 callback.Cancel(); |
98 } | 89 } |
99 | 90 |
100 MOCK_METHOD3(AddressCallbackInternal, | 91 MOCK_METHOD3(AddressCallbackInternal, |
101 void(bool resolved, | 92 void(bool resolved, |
102 std::string address_ipv4, | 93 std::string address_ipv4, |
103 std::string address_ipv6)); | 94 std::string address_ipv6)); |
104 | 95 |
105 net::MockMDnsSocketFactory* socket_factory_; | 96 net::MockMDnsSocketFactory socket_factory_; |
106 net::MDnsClientImpl mdns_client_; | 97 net::MDnsClientImpl mdns_client_; |
107 base::MessageLoop message_loop_; | 98 base::MessageLoop message_loop_; |
108 }; | 99 }; |
109 | 100 |
110 TEST_F(LocalDomainResolverTest, ResolveDomainA) { | 101 TEST_F(LocalDomainResolverTest, ResolveDomainA) { |
111 LocalDomainResolverImpl resolver( | 102 LocalDomainResolverImpl resolver( |
112 "myhello.local", net::ADDRESS_FAMILY_IPV4, | 103 "myhello.local", net::ADDRESS_FAMILY_IPV4, |
113 base::Bind(&LocalDomainResolverTest::AddressCallback, | 104 base::Bind(&LocalDomainResolverTest::AddressCallback, |
114 base::Unretained(this)), &mdns_client_); | 105 base::Unretained(this)), &mdns_client_); |
115 | 106 |
116 EXPECT_CALL(*socket_factory_, OnSendTo(_)) | 107 EXPECT_CALL(socket_factory_, OnSendTo(_)).Times(2); // Twice per query |
117 .Times(2); // Twice per query | |
118 | 108 |
119 resolver.Start(); | 109 resolver.Start(); |
120 | 110 |
121 EXPECT_CALL(*this, AddressCallbackInternal(true, "1.2.3.4", "")); | 111 EXPECT_CALL(*this, AddressCallbackInternal(true, "1.2.3.4", "")); |
122 | 112 |
123 socket_factory_->SimulateReceive( | 113 socket_factory_.SimulateReceive(kSamplePacketA, sizeof(kSamplePacketA)); |
124 kSamplePacketA, sizeof(kSamplePacketA)); | |
125 } | 114 } |
126 | 115 |
127 TEST_F(LocalDomainResolverTest, ResolveDomainAAAA) { | 116 TEST_F(LocalDomainResolverTest, ResolveDomainAAAA) { |
128 LocalDomainResolverImpl resolver( | 117 LocalDomainResolverImpl resolver( |
129 "myhello.local", net::ADDRESS_FAMILY_IPV6, | 118 "myhello.local", net::ADDRESS_FAMILY_IPV6, |
130 base::Bind(&LocalDomainResolverTest::AddressCallback, | 119 base::Bind(&LocalDomainResolverTest::AddressCallback, |
131 base::Unretained(this)), &mdns_client_); | 120 base::Unretained(this)), &mdns_client_); |
132 | 121 |
133 EXPECT_CALL(*socket_factory_, OnSendTo(_)) | 122 EXPECT_CALL(socket_factory_, OnSendTo(_)).Times(2); // Twice per query |
134 .Times(2); // Twice per query | |
135 | 123 |
136 resolver.Start(); | 124 resolver.Start(); |
137 | 125 |
138 EXPECT_CALL(*this, AddressCallbackInternal(true, "", "a::1:2:3:4")); | 126 EXPECT_CALL(*this, AddressCallbackInternal(true, "", "a::1:2:3:4")); |
139 | 127 |
140 socket_factory_->SimulateReceive( | 128 socket_factory_.SimulateReceive(kSamplePacketAAAA, sizeof(kSamplePacketAAAA)); |
141 kSamplePacketAAAA, sizeof(kSamplePacketAAAA)); | |
142 } | 129 } |
143 | 130 |
144 TEST_F(LocalDomainResolverTest, ResolveDomainAnyOneAvailable) { | 131 TEST_F(LocalDomainResolverTest, ResolveDomainAnyOneAvailable) { |
145 LocalDomainResolverImpl resolver( | 132 LocalDomainResolverImpl resolver( |
146 "myhello.local", net::ADDRESS_FAMILY_UNSPECIFIED, | 133 "myhello.local", net::ADDRESS_FAMILY_UNSPECIFIED, |
147 base::Bind(&LocalDomainResolverTest::AddressCallback, | 134 base::Bind(&LocalDomainResolverTest::AddressCallback, |
148 base::Unretained(this)), &mdns_client_); | 135 base::Unretained(this)), &mdns_client_); |
149 | 136 |
150 EXPECT_CALL(*socket_factory_, OnSendTo(_)) | 137 EXPECT_CALL(socket_factory_, OnSendTo(_)).Times(4); // Twice per query |
151 .Times(4); // Twice per query | |
152 | 138 |
153 resolver.Start(); | 139 resolver.Start(); |
154 | 140 |
155 socket_factory_->SimulateReceive( | 141 socket_factory_.SimulateReceive(kSamplePacketAAAA, sizeof(kSamplePacketAAAA)); |
156 kSamplePacketAAAA, sizeof(kSamplePacketAAAA)); | |
157 | 142 |
158 EXPECT_CALL(*this, AddressCallbackInternal(true, "", "a::1:2:3:4")); | 143 EXPECT_CALL(*this, AddressCallbackInternal(true, "", "a::1:2:3:4")); |
159 | 144 |
160 RunFor(base::TimeDelta::FromMilliseconds(150)); | 145 RunFor(base::TimeDelta::FromMilliseconds(150)); |
161 } | 146 } |
162 | 147 |
163 | 148 |
164 TEST_F(LocalDomainResolverTest, ResolveDomainAnyBothAvailable) { | 149 TEST_F(LocalDomainResolverTest, ResolveDomainAnyBothAvailable) { |
165 LocalDomainResolverImpl resolver( | 150 LocalDomainResolverImpl resolver( |
166 "myhello.local", net::ADDRESS_FAMILY_UNSPECIFIED, | 151 "myhello.local", net::ADDRESS_FAMILY_UNSPECIFIED, |
167 base::Bind(&LocalDomainResolverTest::AddressCallback, | 152 base::Bind(&LocalDomainResolverTest::AddressCallback, |
168 base::Unretained(this)), &mdns_client_); | 153 base::Unretained(this)), &mdns_client_); |
169 | 154 |
170 EXPECT_CALL(*socket_factory_, OnSendTo(_)) | 155 EXPECT_CALL(socket_factory_, OnSendTo(_)).Times(4); // Twice per query |
171 .Times(4); // Twice per query | |
172 | 156 |
173 resolver.Start(); | 157 resolver.Start(); |
174 | 158 |
175 EXPECT_CALL(*this, AddressCallbackInternal(true, "1.2.3.4", "a::1:2:3:4")); | 159 EXPECT_CALL(*this, AddressCallbackInternal(true, "1.2.3.4", "a::1:2:3:4")); |
176 | 160 |
177 socket_factory_->SimulateReceive( | 161 socket_factory_.SimulateReceive(kSamplePacketAAAA, sizeof(kSamplePacketAAAA)); |
178 kSamplePacketAAAA, sizeof(kSamplePacketAAAA)); | |
179 | 162 |
180 socket_factory_->SimulateReceive( | 163 socket_factory_.SimulateReceive(kSamplePacketA, sizeof(kSamplePacketA)); |
181 kSamplePacketA, sizeof(kSamplePacketA)); | |
182 } | 164 } |
183 | 165 |
184 TEST_F(LocalDomainResolverTest, ResolveDomainNone) { | 166 TEST_F(LocalDomainResolverTest, ResolveDomainNone) { |
185 LocalDomainResolverImpl resolver( | 167 LocalDomainResolverImpl resolver( |
186 "myhello.local", net::ADDRESS_FAMILY_UNSPECIFIED, | 168 "myhello.local", net::ADDRESS_FAMILY_UNSPECIFIED, |
187 base::Bind(&LocalDomainResolverTest::AddressCallback, | 169 base::Bind(&LocalDomainResolverTest::AddressCallback, |
188 base::Unretained(this)), &mdns_client_); | 170 base::Unretained(this)), &mdns_client_); |
189 | 171 |
190 EXPECT_CALL(*socket_factory_, OnSendTo(_)) | 172 EXPECT_CALL(socket_factory_, OnSendTo(_)).Times(4); // Twice per query |
191 .Times(4); // Twice per query | |
192 | 173 |
193 resolver.Start(); | 174 resolver.Start(); |
194 | 175 |
195 EXPECT_CALL(*this, AddressCallbackInternal(false, "", "")); | 176 EXPECT_CALL(*this, AddressCallbackInternal(false, "", "")); |
196 | 177 |
197 RunFor(base::TimeDelta::FromSeconds(4)); | 178 RunFor(base::TimeDelta::FromSeconds(4)); |
198 } | 179 } |
199 | 180 |
200 } // namespace | 181 } // namespace |
201 | 182 |
202 } // namespace local_discovery | 183 } // namespace local_discovery |
OLD | NEW |