xref: /third_party/curl/tests/unit/unit1608.c (revision 13498266)
1/***************************************************************************
2 *                                  _   _ ____  _
3 *  Project                     ___| | | |  _ \| |
4 *                             / __| | | | |_) | |
5 *                            | (__| |_| |  _ <| |___
6 *                             \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24#include "curlcheck.h"
25
26#include "hostip.h"
27
28#ifndef CURL_DISABLE_SHUFFLE_DNS
29
30CURLcode Curl_shuffle_addr(struct Curl_easy *data,
31                           struct Curl_addrinfo **addr);
32
33#define NUM_ADDRS 8
34static struct Curl_addrinfo addrs[NUM_ADDRS];
35
36static CURLcode unit_setup(void)
37{
38  int i;
39  for(i = 0; i < NUM_ADDRS - 1; i++) {
40    addrs[i].ai_next = &addrs[i + 1];
41  }
42
43  return CURLE_OK;
44}
45
46static void unit_stop(void)
47{
48  curl_global_cleanup();
49}
50
51UNITTEST_START
52
53  int i;
54  CURLcode code;
55  struct Curl_addrinfo *addrhead = addrs;
56
57  struct Curl_easy *easy = curl_easy_init();
58  abort_unless(easy, "out of memory");
59
60  code = curl_easy_setopt(easy, CURLOPT_DNS_SHUFFLE_ADDRESSES, 1L);
61  abort_unless(code == CURLE_OK, "curl_easy_setopt failed");
62
63  /* Shuffle repeatedly and make sure that the list changes */
64  for(i = 0; i < 10; i++) {
65    if(CURLE_OK != Curl_shuffle_addr(easy, &addrhead))
66      break;
67    if(addrhead != addrs)
68      break;
69  }
70
71  curl_easy_cleanup(easy);
72  curl_global_cleanup();
73
74  abort_unless(addrhead != addrs, "addresses are not being reordered");
75
76UNITTEST_STOP
77
78#else
79static CURLcode unit_setup(void)
80{
81  return CURLE_OK;
82}
83static void unit_stop(void)
84{
85}
86UNITTEST_START
87UNITTEST_STOP
88
89#endif
90