summaryrefslogtreecommitdiffstats
path: root/net/dns/address_sorter_unittest.cc
blob: 0c2be884d293a1c030160c7c6594f71e43dec5dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/dns/address_sorter.h"

#if defined(OS_WIN)
#include <winsock2.h>
#endif

#include "base/bind.h"
#include "base/logging.h"
#include "net/base/address_list.h"
#include "net/base/net_util.h"
#include "net/base/test_completion_callback.h"
#include "testing/gtest/include/gtest/gtest.h"

#if defined(OS_WIN)
#include "net/base/winsock_init.h"
#endif

namespace net {
namespace {

IPEndPoint MakeEndPoint(const std::string& str) {
  IPAddressNumber addr;
  CHECK(ParseIPLiteralToNumber(str, &addr));
  return IPEndPoint(addr, 0);
}

void OnSortComplete(AddressList* result_buf,
                    const CompletionCallback& callback,
                    bool success,
                    const AddressList& result) {
  if (success)
    *result_buf = result;
  callback.Run(success ? OK : ERR_FAILED);
}

TEST(AddressSorterTest, Sort) {
  int expected_result = OK;
#if defined(OS_WIN)
  EnsureWinsockInit();
  SOCKET sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
  if (sock == INVALID_SOCKET) {
    expected_result = ERR_FAILED;
  } else {
    closesocket(sock);
  }
#endif
  scoped_ptr<AddressSorter> sorter(AddressSorter::CreateAddressSorter());
  AddressList list;
  list.push_back(MakeEndPoint("10.0.0.1"));
  list.push_back(MakeEndPoint("8.8.8.8"));
  list.push_back(MakeEndPoint("::1"));
  list.push_back(MakeEndPoint("2001:4860:4860::8888"));

  AddressList result;
  TestCompletionCallback callback;
  sorter->Sort(list, base::Bind(&OnSortComplete, &result,
                                callback.callback()));
  EXPECT_EQ(expected_result, callback.WaitForResult());
}

}  // namespace
}  // namespace net