xref: /third_party/curl/tests/libtest/lib1507.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 "test.h"
25
26#include "testutil.h"
27#include "timediff.h"
28#include "warnless.h"
29#include "memdebug.h"
30
31/*
32 * This is the list of basic details you need to tweak to get things right.
33 */
34#define USERNAME "user@example.com"
35#define PASSWORD "123qwerty"
36#define RECIPIENT "<1507-recipient@example.com>"
37#define MAILFROM "<1507-realuser@example.com>"
38
39#define MULTI_PERFORM_HANG_TIMEOUT 60 * 1000
40
41static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *userp)
42{
43  (void)ptr;
44  (void)size;
45  (void)nmemb;
46  (void)userp;
47  return CURL_READFUNC_ABORT;
48}
49
50int test(char *URL)
51{
52   int res = 0;
53   CURL *curl = NULL;
54   CURLM *mcurl = NULL;
55   int still_running = 1;
56   struct timeval mp_start;
57   struct curl_slist *rcpt_list = NULL;
58
59   curl_global_init(CURL_GLOBAL_DEFAULT);
60
61   easy_init(curl);
62
63   multi_init(mcurl);
64
65   rcpt_list = curl_slist_append(rcpt_list, RECIPIENT);
66   /* more addresses can be added here
67      rcpt_list = curl_slist_append(rcpt_list, "<others@example.com>");
68   */
69
70   curl_easy_setopt(curl, CURLOPT_URL, URL);
71#if 0
72   curl_easy_setopt(curl, CURLOPT_USERNAME, USERNAME);
73   curl_easy_setopt(curl, CURLOPT_PASSWORD, PASSWORD);
74#endif
75   curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
76   curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
77   curl_easy_setopt(curl, CURLOPT_MAIL_FROM, MAILFROM);
78   curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, rcpt_list);
79   curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
80   multi_add_handle(mcurl, curl);
81
82   mp_start = tutil_tvnow();
83
84  /* we start some action by calling perform right away */
85  curl_multi_perform(mcurl, &still_running);
86
87  while(still_running) {
88    struct timeval timeout;
89    int rc; /* select() return code */
90
91    fd_set fdread;
92    fd_set fdwrite;
93    fd_set fdexcep;
94    int maxfd = -1;
95
96    long curl_timeo = -1;
97
98    FD_ZERO(&fdread);
99    FD_ZERO(&fdwrite);
100    FD_ZERO(&fdexcep);
101
102    /* set a suitable timeout to play around with */
103    timeout.tv_sec = 1;
104    timeout.tv_usec = 0;
105
106    curl_multi_timeout(mcurl, &curl_timeo);
107    if(curl_timeo >= 0) {
108      curlx_mstotv(&timeout, curl_timeo);
109      if(timeout.tv_sec > 1) {
110        timeout.tv_sec = 1;
111        timeout.tv_usec = 0;
112      }
113    }
114
115    /* get file descriptors from the transfers */
116    curl_multi_fdset(mcurl, &fdread, &fdwrite, &fdexcep, &maxfd);
117
118    /* In a real-world program you OF COURSE check the return code of the
119       function calls.  On success, the value of maxfd is guaranteed to be
120       greater or equal than -1.  We call select(maxfd + 1, ...), specially in
121       case of (maxfd == -1), we call select(0, ...), which is basically equal
122       to sleep. */
123
124    rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
125
126    if(tutil_tvdiff(tutil_tvnow(), mp_start) > MULTI_PERFORM_HANG_TIMEOUT) {
127      fprintf(stderr, "ABORTING TEST, since it seems "
128              "that it would have run forever.\n");
129      break;
130    }
131
132    switch(rc) {
133    case -1:
134      /* select error */
135      break;
136    case 0: /* timeout */
137    default: /* action */
138      curl_multi_perform(mcurl, &still_running);
139      break;
140    }
141  }
142
143test_cleanup:
144
145  curl_slist_free_all(rcpt_list);
146  curl_multi_remove_handle(mcurl, curl);
147  curl_multi_cleanup(mcurl);
148  curl_easy_cleanup(curl);
149  curl_global_cleanup();
150
151  return res;
152}
153