OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "net/base/dnsrr_resolver.h" | 5 #include "net/base/dnsrr_resolver.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/synchronization/lock.h" | 8 #include "base/synchronization/lock.h" |
9 #include "net/base/dns_util.h" | 9 #include "net/base/dns_util.h" |
10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
11 #include "net/base/net_log.h" | 11 #include "net/base/net_log.h" |
12 #include "net/base/test_completion_callback.h" | 12 #include "net/base/test_completion_callback.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 | 14 |
15 namespace net { | 15 namespace net { |
16 | 16 |
17 class DnsRRResolverTest : public testing::Test { | 17 class DnsRRResolverTest : public testing::Test { |
18 }; | 18 }; |
19 | 19 |
20 #if defined(OS_LINUX) | |
21 | |
22 class ExplodingCallback : public CallbackRunner<Tuple1<int> > { | 20 class ExplodingCallback : public CallbackRunner<Tuple1<int> > { |
23 public: | 21 public: |
24 virtual void RunWithParams(const Tuple1<int>& params) { | 22 virtual void RunWithParams(const Tuple1<int>& params) { |
25 FAIL(); | 23 FAIL(); |
26 } | 24 } |
27 }; | 25 }; |
28 | 26 |
29 // This test is disabled because it depends on the external network to pass. | 27 // These tests are disabled because they depend on the external network to |
30 // However, it may be useful when chaging the code. | 28 // pass. However, they may be useful when chaging the code. |
| 29 TEST_F(DnsRRResolverTest, DISABLED_ResolveReal) { |
| 30 RRResponse response; |
| 31 TestCompletionCallback callback; |
| 32 DnsRRResolver resolver; |
| 33 DnsRRResolver::Handle handle; |
| 34 |
| 35 handle = resolver.Resolve("test.imperialviolet.org", 13172, 0, |
| 36 &callback, &response, 0, BoundNetLog()); |
| 37 ASSERT_TRUE(handle != DnsRRResolver::kInvalidHandle); |
| 38 ASSERT_EQ(OK, callback.WaitForResult()); |
| 39 ASSERT_EQ(1u, response.rrdatas.size()); |
| 40 LOG(ERROR) << "result length " << response.rrdatas[0].size(); |
| 41 LOG(ERROR) << "result is " << response.rrdatas[0]; |
| 42 } |
| 43 |
| 44 TEST_F(DnsRRResolverTest, DISABLED_ResolveReal2) { |
| 45 RRResponse response; |
| 46 TestCompletionCallback callback; |
| 47 DnsRRResolver resolver; |
| 48 DnsRRResolver::Handle handle; |
| 49 |
| 50 handle = resolver.Resolve("google.com", kDNS_TXT, 0, |
| 51 &callback, &response, 0, BoundNetLog()); |
| 52 ASSERT_TRUE(handle != DnsRRResolver::kInvalidHandle); |
| 53 ASSERT_EQ(OK, callback.WaitForResult()); |
| 54 ASSERT_EQ(1u, response.rrdatas.size()); |
| 55 LOG(ERROR) << "result length " << response.rrdatas[0].size(); |
| 56 LOG(ERROR) << "result is " << response.rrdatas[0]; |
| 57 } |
| 58 |
| 59 |
31 TEST_F(DnsRRResolverTest, Resolve) { | 60 TEST_F(DnsRRResolverTest, Resolve) { |
32 RRResponse response; | 61 RRResponse response; |
33 TestCompletionCallback callback; | 62 TestCompletionCallback callback; |
34 DnsRRResolver resolver; | 63 DnsRRResolver resolver; |
35 DnsRRResolver::Handle handle; | 64 DnsRRResolver::Handle handle; |
36 | 65 |
37 handle = resolver.Resolve("www.testing.notatld", kDNS_TESTING, 0, | 66 handle = resolver.Resolve("www.testing.notatld", kDNS_TESTING, 0, |
38 &callback, &response, 0, BoundNetLog()); | 67 &callback, &response, 0, BoundNetLog()); |
39 ASSERT_TRUE(handle != DnsRRResolver::kInvalidHandle); | 68 ASSERT_TRUE(handle != DnsRRResolver::kInvalidHandle); |
40 ASSERT_EQ(OK, callback.WaitForResult()); | 69 ASSERT_EQ(OK, callback.WaitForResult()); |
41 ASSERT_EQ(1u, response.rrdatas.size()); | 70 ASSERT_EQ(1u, response.rrdatas.size()); |
42 ASSERT_STREQ("goats!", response.rrdatas[0].c_str()); | 71 ASSERT_STREQ("goats!", response.rrdatas[0].c_str()); |
43 ASSERT_EQ(1u, resolver.requests()); | 72 ASSERT_EQ(1u, resolver.requests()); |
44 ASSERT_EQ(0u, resolver.cache_hits()); | 73 ASSERT_EQ(0u, resolver.cache_hits()); |
45 ASSERT_EQ(0u, resolver.inflight_joins()); | 74 ASSERT_EQ(0u, resolver.inflight_joins()); |
46 | 75 |
47 // Test a cache hit. | 76 // Test a cache hit. |
48 handle = resolver.Resolve("www.testing.notatld", kDNS_TESTING, 0, | 77 handle = resolver.Resolve("www.testing.notatld", kDNS_TESTING, 0, |
49 &callback, &response, 0, BoundNetLog()); | 78 &callback, &response, 0, BoundNetLog()); |
50 ASSERT_TRUE(handle != DnsRRResolver::kInvalidHandle); | 79 ASSERT_TRUE(handle != DnsRRResolver::kInvalidHandle); |
51 ASSERT_EQ(OK, callback.WaitForResult()); | 80 ASSERT_EQ(OK, callback.WaitForResult()); |
52 ASSERT_EQ(1u, response.rrdatas.size()); | 81 ASSERT_EQ(1u, response.rrdatas.size()); |
53 ASSERT_STREQ("goats!", response.rrdatas[0].c_str()); | 82 ASSERT_STREQ("goats!", response.rrdatas[0].c_str()); |
54 ASSERT_EQ(2u, resolver.requests()); | 83 ASSERT_EQ(2u, resolver.requests()); |
55 ASSERT_EQ(1u, resolver.cache_hits()); | 84 ASSERT_EQ(1u, resolver.cache_hits()); |
56 ASSERT_EQ(0u, resolver.inflight_joins()); | 85 ASSERT_EQ(0u, resolver.inflight_joins()); |
57 | 86 |
58 // Test that a callback is never made. This depends on there before another | 87 // Test that a callback is never made. This depends on there being another |
59 // test after this one which will pump the MessageLoop. | 88 // test after this one which will pump the MessageLoop. |
60 ExplodingCallback callback3; | 89 ExplodingCallback callback3; |
61 handle = resolver.Resolve("www.testing.notatld", kDNS_TESTING, 0, | 90 handle = resolver.Resolve("www.testing.notatld", kDNS_TESTING, 0, |
62 &callback3, &response, 0, BoundNetLog()); | 91 &callback3, &response, 0, BoundNetLog()); |
63 ASSERT_TRUE(handle != DnsRRResolver::kInvalidHandle); | 92 ASSERT_TRUE(handle != DnsRRResolver::kInvalidHandle); |
64 resolver.CancelResolve(handle); | 93 resolver.CancelResolve(handle); |
65 ASSERT_EQ(3u, resolver.requests()); | 94 ASSERT_EQ(3u, resolver.requests()); |
66 ASSERT_EQ(2u, resolver.cache_hits()); | 95 ASSERT_EQ(2u, resolver.cache_hits()); |
67 ASSERT_EQ(0u, resolver.inflight_joins()); | 96 ASSERT_EQ(0u, resolver.inflight_joins()); |
68 | 97 |
(...skipping 18 matching lines...) Expand all Loading... |
87 handle2 = resolver.Resolve("nx.testing.notatld", kDNS_TESTING, 0, | 116 handle2 = resolver.Resolve("nx.testing.notatld", kDNS_TESTING, 0, |
88 &callback2, &response, 0, BoundNetLog()); | 117 &callback2, &response, 0, BoundNetLog()); |
89 ASSERT_TRUE(handle2 != DnsRRResolver::kInvalidHandle); | 118 ASSERT_TRUE(handle2 != DnsRRResolver::kInvalidHandle); |
90 ASSERT_EQ(ERR_NAME_NOT_RESOLVED, callback.WaitForResult()); | 119 ASSERT_EQ(ERR_NAME_NOT_RESOLVED, callback.WaitForResult()); |
91 ASSERT_EQ(ERR_NAME_NOT_RESOLVED, callback2.WaitForResult()); | 120 ASSERT_EQ(ERR_NAME_NOT_RESOLVED, callback2.WaitForResult()); |
92 ASSERT_EQ(6u, resolver.requests()); | 121 ASSERT_EQ(6u, resolver.requests()); |
93 ASSERT_EQ(2u, resolver.cache_hits()); | 122 ASSERT_EQ(2u, resolver.cache_hits()); |
94 ASSERT_EQ(1u, resolver.inflight_joins()); | 123 ASSERT_EQ(1u, resolver.inflight_joins()); |
95 } | 124 } |
96 | 125 |
| 126 #if defined(OS_POSIX) |
97 // This is a DNS packet resulting from querying a recursive resolver for a TXT | 127 // This is a DNS packet resulting from querying a recursive resolver for a TXT |
98 // record for agl._pka.imperialviolet.org. You should be able to get a | 128 // record for agl._pka.imperialviolet.org. You should be able to get a |
99 // replacement from a packet capture should it ever be needed. | 129 // replacement from a packet capture should it ever be needed. |
100 static const uint8 kExamplePacket[] = { | 130 static const uint8 kExamplePacket[] = { |
101 0xce, 0xfe, 0x81, 0x80, 0x00, 0x01, 0x00, 0x02, 0x00, 0x06, 0x00, 0x01, 0x03, | 131 0xce, 0xfe, 0x81, 0x80, 0x00, 0x01, 0x00, 0x02, 0x00, 0x06, 0x00, 0x01, 0x03, |
102 0x61, 0x67, 0x6c, 0x04, 0x5f, 0x70, 0x6b, 0x61, 0x0e, 0x69, 0x6d, 0x70, 0x65, | 132 0x61, 0x67, 0x6c, 0x04, 0x5f, 0x70, 0x6b, 0x61, 0x0e, 0x69, 0x6d, 0x70, 0x65, |
103 0x72, 0x69, 0x61, 0x6c, 0x76, 0x69, 0x6f, 0x6c, 0x65, 0x74, 0x03, 0x6f, 0x72, | 133 0x72, 0x69, 0x61, 0x6c, 0x76, 0x69, 0x6f, 0x6c, 0x65, 0x74, 0x03, 0x6f, 0x72, |
104 0x67, 0x00, 0x00, 0x10, 0x00, 0x01, 0xc0, 0x0c, 0x00, 0x10, 0x00, 0x01, 0x00, | 134 0x67, 0x00, 0x00, 0x10, 0x00, 0x01, 0xc0, 0x0c, 0x00, 0x10, 0x00, 0x01, 0x00, |
105 0x00, 0x01, 0x2c, 0x00, 0x5e, 0x5d, 0x76, 0x3d, 0x70, 0x6b, 0x61, 0x31, 0x3b, | 135 0x00, 0x01, 0x2c, 0x00, 0x5e, 0x5d, 0x76, 0x3d, 0x70, 0x6b, 0x61, 0x31, 0x3b, |
106 0x66, 0x70, 0x72, 0x3d, 0x32, 0x41, 0x46, 0x30, 0x30, 0x33, 0x32, 0x42, 0x34, | 136 0x66, 0x70, 0x72, 0x3d, 0x32, 0x41, 0x46, 0x30, 0x30, 0x33, 0x32, 0x42, 0x34, |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 bit_to_corrupt++) { | 201 bit_to_corrupt++) { |
172 unsigned byte = bit_to_corrupt >> 3; | 202 unsigned byte = bit_to_corrupt >> 3; |
173 unsigned bit = bit_to_corrupt & 7; | 203 unsigned bit = bit_to_corrupt & 7; |
174 | 204 |
175 memcpy(copy, kExamplePacket, sizeof(copy)); | 205 memcpy(copy, kExamplePacket, sizeof(copy)); |
176 copy[byte] ^= (1 << bit); | 206 copy[byte] ^= (1 << bit); |
177 | 207 |
178 response.ParseFromResponse(copy, sizeof(copy), kDNS_TXT); | 208 response.ParseFromResponse(copy, sizeof(copy), kDNS_TXT); |
179 } | 209 } |
180 } | 210 } |
181 | 211 #endif |
182 #endif // OS_LINUX | |
183 | 212 |
184 } // namespace net | 213 } // namespace net |
OLD | NEW |