1/***************************************************************************
2 *                                  _   _ ____  _
3 *  Project                     ___| | | |  _ \| |
4 *                             / __| | | | |_) | |
5 *                            | (__| |_| |  _ <| |___
6 *                             \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2012, 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 http://curl.haxx.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 ***************************************************************************/
22/*
23 * This source code is used for lib1502, lib1503, lib1504 and lib1505 with
24 * only #ifdefs controlling the cleanup sequence.
25 *
26 * Test case 1502 converted from bug report #3575448, identifying a memory
27 * leak in the CURLOPT_RESOLVE handling with the multi interface.
28 */
29
30#include "test.h"
31
32#ifdef HAVE_LIMITS_H
33#include <limits.h>
34#endif
35
36#include "testutil.h"
37#include "warnless.h"
38#include "memdebug.h"
39
40#define TEST_HANG_TIMEOUT 60 * 1000
41
42int test(char *URL)
43{
44  CURL* easy = NULL;
45  CURLM* multi = NULL;
46  int still_running;
47  int res = 0;
48
49  char redirect[160];
50
51  /* DNS cache injection */
52  struct curl_slist *dns_cache_list;
53
54  sprintf(redirect, "google.com:%s:%s", libtest_arg2, libtest_arg3);
55
56  start_test_timing();
57
58  dns_cache_list = curl_slist_append(NULL, redirect);
59  if(!dns_cache_list) {
60    fprintf(stderr, "curl_slist_append() failed\n");
61    return TEST_ERR_MAJOR_BAD;
62  }
63
64  res_global_init(CURL_GLOBAL_ALL);
65  if(res) {
66    curl_slist_free_all(dns_cache_list);
67    return res;
68  }
69
70  easy_init(easy);
71
72  easy_setopt(easy, CURLOPT_URL, URL);
73  easy_setopt(easy, CURLOPT_HEADER, 1L);
74  easy_setopt(easy, CURLOPT_RESOLVE, dns_cache_list);
75
76  multi_init(multi);
77
78  multi_add_handle(multi, easy);
79
80  multi_perform(multi, &still_running);
81
82  abort_on_test_timeout();
83
84  while(still_running) {
85    struct timeval timeout;
86    fd_set fdread;
87    fd_set fdwrite;
88    fd_set fdexcep;
89    int maxfd = -99;
90
91    FD_ZERO(&fdread);
92    FD_ZERO(&fdwrite);
93    FD_ZERO(&fdexcep);
94    timeout.tv_sec = 1;
95    timeout.tv_usec = 0;
96
97    multi_fdset(multi, &fdread, &fdwrite, &fdexcep, &maxfd);
98
99    /* At this point, maxfd is guaranteed to be greater or equal than -1. */
100
101    select_test(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
102
103    abort_on_test_timeout();
104
105    multi_perform(multi, &still_running);
106
107    abort_on_test_timeout();
108  }
109
110test_cleanup:
111
112#ifdef LIB1502
113  /* undocumented cleanup sequence - type UA */
114  curl_multi_cleanup(multi);
115  curl_easy_cleanup(easy);
116  curl_global_cleanup();
117#endif
118
119#ifdef LIB1503
120  /* proper cleanup sequence - type PA */
121  curl_multi_remove_handle(multi, easy);
122  curl_multi_cleanup(multi);
123  curl_easy_cleanup(easy);
124  curl_global_cleanup();
125#endif
126
127#ifdef LIB1504
128  /* undocumented cleanup sequence - type UB */
129  curl_easy_cleanup(easy);
130  curl_multi_cleanup(multi);
131  curl_global_cleanup();
132#endif
133
134#ifdef LIB1505
135  /* proper cleanup sequence - type PB */
136  curl_multi_remove_handle(multi, easy);
137  curl_easy_cleanup(easy);
138  curl_multi_cleanup(multi);
139  curl_global_cleanup();
140#endif
141
142  curl_slist_free_all(dns_cache_list);
143
144  return res;
145}
146