OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/dns/address_sorter.h" | 5 #include "net/dns/address_sorter.h" |
6 | 6 |
7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
8 #include <winsock2.h> | 8 #include <winsock2.h> |
9 #endif | 9 #endif |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/message_loop/message_loop.h" | |
14 #include "base/test/scoped_task_scheduler.h" | |
15 #include "net/base/address_list.h" | 13 #include "net/base/address_list.h" |
16 #include "net/base/ip_address.h" | 14 #include "net/base/ip_address.h" |
17 #include "net/base/test_completion_callback.h" | 15 #include "net/base/test_completion_callback.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
19 | 17 |
20 #if defined(OS_WIN) | 18 #if defined(OS_WIN) |
21 #include "net/base/winsock_init.h" | 19 #include "net/base/winsock_init.h" |
22 #endif | 20 #endif |
23 | 21 |
24 namespace net { | 22 namespace net { |
25 namespace { | 23 namespace { |
26 | 24 |
27 IPEndPoint MakeEndPoint(const std::string& str) { | 25 IPEndPoint MakeEndPoint(const std::string& str) { |
28 IPAddress addr; | 26 IPAddress addr; |
29 CHECK(addr.AssignFromIPLiteral(str)); | 27 CHECK(addr.AssignFromIPLiteral(str)); |
30 return IPEndPoint(addr, 0); | 28 return IPEndPoint(addr, 0); |
31 } | 29 } |
32 | 30 |
33 void OnSortComplete(AddressList* result_buf, | 31 void OnSortComplete(AddressList* result_buf, |
34 const CompletionCallback& callback, | 32 const CompletionCallback& callback, |
35 bool success, | 33 bool success, |
36 const AddressList& result) { | 34 const AddressList& result) { |
37 if (success) | 35 if (success) |
38 *result_buf = result; | 36 *result_buf = result; |
39 callback.Run(success ? OK : ERR_FAILED); | 37 callback.Run(success ? OK : ERR_FAILED); |
40 } | 38 } |
41 | 39 |
42 TEST(AddressSorterTest, Sort) { | 40 TEST(AddressSorterTest, Sort) { |
43 base::test::ScopedTaskScheduler scoped_task_scheduler( | |
44 base::MessageLoop::current()); | |
45 int expected_result = OK; | 41 int expected_result = OK; |
46 #if defined(OS_WIN) | 42 #if defined(OS_WIN) |
47 EnsureWinsockInit(); | 43 EnsureWinsockInit(); |
48 SOCKET sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); | 44 SOCKET sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); |
49 if (sock == INVALID_SOCKET) { | 45 if (sock == INVALID_SOCKET) { |
50 expected_result = ERR_FAILED; | 46 expected_result = ERR_FAILED; |
51 } else { | 47 } else { |
52 closesocket(sock); | 48 closesocket(sock); |
53 } | 49 } |
54 #endif | 50 #endif |
55 std::unique_ptr<AddressSorter> sorter(AddressSorter::CreateAddressSorter()); | 51 std::unique_ptr<AddressSorter> sorter(AddressSorter::CreateAddressSorter()); |
56 AddressList list; | 52 AddressList list; |
57 list.push_back(MakeEndPoint("10.0.0.1")); | 53 list.push_back(MakeEndPoint("10.0.0.1")); |
58 list.push_back(MakeEndPoint("8.8.8.8")); | 54 list.push_back(MakeEndPoint("8.8.8.8")); |
59 list.push_back(MakeEndPoint("::1")); | 55 list.push_back(MakeEndPoint("::1")); |
60 list.push_back(MakeEndPoint("2001:4860:4860::8888")); | 56 list.push_back(MakeEndPoint("2001:4860:4860::8888")); |
61 | 57 |
62 AddressList result; | 58 AddressList result; |
63 TestCompletionCallback callback; | 59 TestCompletionCallback callback; |
64 sorter->Sort(list, base::Bind(&OnSortComplete, &result, | 60 sorter->Sort(list, base::Bind(&OnSortComplete, &result, |
65 callback.callback())); | 61 callback.callback())); |
66 EXPECT_EQ(expected_result, callback.WaitForResult()); | 62 EXPECT_EQ(expected_result, callback.WaitForResult()); |
67 } | 63 } |
68 | 64 |
69 } // namespace | 65 } // namespace |
70 } // namespace net | 66 } // namespace net |
OLD | NEW |