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. 4 5#include "net/dns/address_sorter.h" 6 7#if defined(OS_WIN) 8#include <winsock2.h> 9#endif 10 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" 17 18#if defined(OS_WIN) 19#include "net/base/winsock_init.h" 20#endif 21 22namespace net { 23namespace { 24 25IPEndPoint MakeEndPoint(const std::string& str) { 26 IPAddressNumber addr; 27 CHECK(ParseIPLiteralToNumber(str, &addr)); 28 return IPEndPoint(addr, 0); 29} 30 31void OnSortComplete(AddressList* result_buf, 32 const CompletionCallback& callback, 33 bool success, 34 const AddressList& result) { 35 if (success) 36 *result_buf = result; 37 callback.Run(success ? OK : ERR_FAILED); 38} 39 40TEST(AddressSorterTest, Sort) { 41 int expected_result = OK; 42#if defined(OS_WIN) 43 EnsureWinsockInit(); 44 SOCKET sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); 45 if (sock == INVALID_SOCKET) { 46 expected_result = ERR_FAILED; 47 } else { 48 closesocket(sock); 49 } 50#endif 51 scoped_ptr<AddressSorter> sorter(AddressSorter::CreateAddressSorter()); 52 AddressList list; 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")); 57 58 AddressList result; 59 TestCompletionCallback callback; 60 sorter->Sort(list, base::Bind(&OnSortComplete, &result, 61 callback.callback())); 62 EXPECT_EQ(expected_result, callback.WaitForResult()); 63} 64 65} // namespace 66} // namespace net 67