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
3 // found in the LICENSE file.
5 #include "net/dns/address_sorter.h"
11 #include "base/bind.h"
12 #include "base/logging.h"
13 #include "net/base/address_list.h"
14 #include "net/base/net_util.h"
15 #include "net/base/test_completion_callback.h"
16 #include "testing/gtest/include/gtest/gtest.h"
19 #include "net/base/winsock_init.h"
25 IPEndPoint
MakeEndPoint(const std::string
& str
) {
27 CHECK(ParseIPLiteralToNumber(str
, &addr
));
28 return IPEndPoint(addr
, 0);
31 void OnSortComplete(AddressList
* result_buf
,
32 const CompletionCallback
& callback
,
34 const AddressList
& result
) {
37 callback
.Run(success
? OK
: ERR_FAILED
);
40 TEST(AddressSorterTest
, Sort
) {
41 int expected_result
= OK
;
44 SOCKET sock
= socket(AF_INET6
, SOCK_DGRAM
, IPPROTO_UDP
);
45 if (sock
== INVALID_SOCKET
) {
46 expected_result
= ERR_FAILED
;
51 scoped_ptr
<AddressSorter
> sorter(AddressSorter::CreateAddressSorter());
53 list
.push_back(MakeEndPoint("10.0.0.1"));
54 list
.push_back(MakeEndPoint("8.8.8.8"));
55 list
.push_back(MakeEndPoint("::1"));
56 list
.push_back(MakeEndPoint("2001:4860:4860::8888"));
59 TestCompletionCallback callback
;
60 sorter
->Sort(list
, base::Bind(&OnSortComplete
, &result
,
61 callback
.callback()));
62 EXPECT_EQ(expected_result
, callback
.WaitForResult());