113498266Sopenharmony_ci/***************************************************************************
213498266Sopenharmony_ci *                                  _   _ ____  _
313498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
413498266Sopenharmony_ci *                             / __| | | | |_) | |
513498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
613498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
713498266Sopenharmony_ci *
813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
913498266Sopenharmony_ci *
1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which
1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms
1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html.
1313498266Sopenharmony_ci *
1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is
1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file.
1713498266Sopenharmony_ci *
1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
1913498266Sopenharmony_ci * KIND, either express or implied.
2013498266Sopenharmony_ci *
2113498266Sopenharmony_ci * SPDX-License-Identifier: curl
2213498266Sopenharmony_ci *
2313498266Sopenharmony_ci ***************************************************************************/
2413498266Sopenharmony_ci
2513498266Sopenharmony_ci/*
2613498266Sopenharmony_ci * Check for bugs #1303 and #1327: libcurl should never remove DNS entries
2713498266Sopenharmony_ci * created via CURLOPT_RESOLVE, neither after DNS_CACHE_TIMEOUT elapses
2813498266Sopenharmony_ci * (test1515) nor a dead connection is detected (test1616).
2913498266Sopenharmony_ci */
3013498266Sopenharmony_ci
3113498266Sopenharmony_ci#include "test.h"
3213498266Sopenharmony_ci#include "testutil.h"
3313498266Sopenharmony_ci#include "warnless.h"
3413498266Sopenharmony_ci#include "memdebug.h"
3513498266Sopenharmony_ci
3613498266Sopenharmony_ci#define TEST_HANG_TIMEOUT 60 * 1000
3713498266Sopenharmony_ci
3813498266Sopenharmony_ci#define DNS_TIMEOUT 1
3913498266Sopenharmony_ci
4013498266Sopenharmony_cistatic int debug_callback(CURL *curl, curl_infotype info, char *msg,
4113498266Sopenharmony_ci                          size_t len, void *ptr)
4213498266Sopenharmony_ci{
4313498266Sopenharmony_ci  (void)curl;
4413498266Sopenharmony_ci  (void)ptr;
4513498266Sopenharmony_ci
4613498266Sopenharmony_ci  if(info == CURLINFO_TEXT)
4713498266Sopenharmony_ci    fprintf(stderr, "debug: %.*s", (int) len, msg);
4813498266Sopenharmony_ci
4913498266Sopenharmony_ci  return 0;
5013498266Sopenharmony_ci}
5113498266Sopenharmony_ci
5213498266Sopenharmony_cistatic int do_one_request(CURLM *m, char *URL, char *resolve)
5313498266Sopenharmony_ci{
5413498266Sopenharmony_ci  CURL *curls;
5513498266Sopenharmony_ci  struct curl_slist *resolve_list = NULL;
5613498266Sopenharmony_ci  int still_running;
5713498266Sopenharmony_ci  int res = 0;
5813498266Sopenharmony_ci  CURLMsg *msg;
5913498266Sopenharmony_ci  int msgs_left;
6013498266Sopenharmony_ci
6113498266Sopenharmony_ci  resolve_list = curl_slist_append(resolve_list, resolve);
6213498266Sopenharmony_ci
6313498266Sopenharmony_ci  easy_init(curls);
6413498266Sopenharmony_ci
6513498266Sopenharmony_ci  easy_setopt(curls, CURLOPT_URL, URL);
6613498266Sopenharmony_ci  easy_setopt(curls, CURLOPT_RESOLVE, resolve_list);
6713498266Sopenharmony_ci  easy_setopt(curls, CURLOPT_DEBUGFUNCTION, debug_callback);
6813498266Sopenharmony_ci  easy_setopt(curls, CURLOPT_VERBOSE, 1);
6913498266Sopenharmony_ci  easy_setopt(curls, CURLOPT_DNS_CACHE_TIMEOUT, DNS_TIMEOUT);
7013498266Sopenharmony_ci
7113498266Sopenharmony_ci  multi_add_handle(m, curls);
7213498266Sopenharmony_ci  multi_perform(m, &still_running);
7313498266Sopenharmony_ci
7413498266Sopenharmony_ci  abort_on_test_timeout();
7513498266Sopenharmony_ci
7613498266Sopenharmony_ci  while(still_running) {
7713498266Sopenharmony_ci    struct timeval timeout;
7813498266Sopenharmony_ci    fd_set fdread, fdwrite, fdexcep;
7913498266Sopenharmony_ci    int maxfd = -99;
8013498266Sopenharmony_ci
8113498266Sopenharmony_ci    FD_ZERO(&fdread);
8213498266Sopenharmony_ci    FD_ZERO(&fdwrite);
8313498266Sopenharmony_ci    FD_ZERO(&fdexcep);
8413498266Sopenharmony_ci    timeout.tv_sec = 1;
8513498266Sopenharmony_ci    timeout.tv_usec = 0;
8613498266Sopenharmony_ci
8713498266Sopenharmony_ci    multi_fdset(m, &fdread, &fdwrite, &fdexcep, &maxfd);
8813498266Sopenharmony_ci    select_test(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
8913498266Sopenharmony_ci
9013498266Sopenharmony_ci    abort_on_test_timeout();
9113498266Sopenharmony_ci    multi_perform(m, &still_running);
9213498266Sopenharmony_ci
9313498266Sopenharmony_ci    abort_on_test_timeout();
9413498266Sopenharmony_ci  }
9513498266Sopenharmony_ci
9613498266Sopenharmony_ci  do {
9713498266Sopenharmony_ci    msg = curl_multi_info_read(m, &msgs_left);
9813498266Sopenharmony_ci    if(msg && msg->msg == CURLMSG_DONE && msg->easy_handle == curls) {
9913498266Sopenharmony_ci      res = msg->data.result;
10013498266Sopenharmony_ci      break;
10113498266Sopenharmony_ci    }
10213498266Sopenharmony_ci  } while(msg);
10313498266Sopenharmony_ci
10413498266Sopenharmony_citest_cleanup:
10513498266Sopenharmony_ci
10613498266Sopenharmony_ci  curl_multi_remove_handle(m, curls);
10713498266Sopenharmony_ci  curl_easy_cleanup(curls);
10813498266Sopenharmony_ci  curl_slist_free_all(resolve_list);
10913498266Sopenharmony_ci
11013498266Sopenharmony_ci  return res;
11113498266Sopenharmony_ci}
11213498266Sopenharmony_ci
11313498266Sopenharmony_ciint test(char *URL)
11413498266Sopenharmony_ci{
11513498266Sopenharmony_ci  CURLM *multi = NULL;
11613498266Sopenharmony_ci  int res = 0;
11713498266Sopenharmony_ci  char *address = libtest_arg2;
11813498266Sopenharmony_ci  char *port = libtest_arg3;
11913498266Sopenharmony_ci  char *path = URL;
12013498266Sopenharmony_ci  char dns_entry[256];
12113498266Sopenharmony_ci  int i;
12213498266Sopenharmony_ci  int count = 2;
12313498266Sopenharmony_ci
12413498266Sopenharmony_ci  msnprintf(dns_entry, sizeof(dns_entry), "testserver.example.com:%s:%s",
12513498266Sopenharmony_ci            port, address);
12613498266Sopenharmony_ci
12713498266Sopenharmony_ci  start_test_timing();
12813498266Sopenharmony_ci
12913498266Sopenharmony_ci  global_init(CURL_GLOBAL_ALL);
13013498266Sopenharmony_ci  multi_init(multi);
13113498266Sopenharmony_ci
13213498266Sopenharmony_ci  for(i = 1; i <= count; i++) {
13313498266Sopenharmony_ci    char target_url[256];
13413498266Sopenharmony_ci    msnprintf(target_url, sizeof(target_url),
13513498266Sopenharmony_ci              "http://testserver.example.com:%s/%s%04d", port, path, i);
13613498266Sopenharmony_ci
13713498266Sopenharmony_ci    /* second request must succeed like the first one */
13813498266Sopenharmony_ci    res = do_one_request(multi, target_url, dns_entry);
13913498266Sopenharmony_ci    if(res)
14013498266Sopenharmony_ci      goto test_cleanup;
14113498266Sopenharmony_ci
14213498266Sopenharmony_ci    if(i < count)
14313498266Sopenharmony_ci      sleep(DNS_TIMEOUT + 1);
14413498266Sopenharmony_ci  }
14513498266Sopenharmony_ci
14613498266Sopenharmony_citest_cleanup:
14713498266Sopenharmony_ci
14813498266Sopenharmony_ci  curl_multi_cleanup(multi);
14913498266Sopenharmony_ci  curl_global_cleanup();
15013498266Sopenharmony_ci
15113498266Sopenharmony_ci  return (int) res;
15213498266Sopenharmony_ci}
153