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/* This is the 'proxyauth.c' test app posted by Shmulik Regev on the libcurl
2513498266Sopenharmony_ci * mailing list on 10 Jul 2007, converted to a test case.
2613498266Sopenharmony_ci *
2713498266Sopenharmony_ci * argv1 = URL
2813498266Sopenharmony_ci * argv2 = proxy
2913498266Sopenharmony_ci * argv3 = proxyuser:password
3013498266Sopenharmony_ci * argv4 = host name to use for the custom Host: header
3113498266Sopenharmony_ci */
3213498266Sopenharmony_ci
3313498266Sopenharmony_ci#include "test.h"
3413498266Sopenharmony_ci
3513498266Sopenharmony_ci#include <limits.h>
3613498266Sopenharmony_ci
3713498266Sopenharmony_ci#include "testutil.h"
3813498266Sopenharmony_ci#include "warnless.h"
3913498266Sopenharmony_ci#include "memdebug.h"
4013498266Sopenharmony_ci
4113498266Sopenharmony_ci#define TEST_HANG_TIMEOUT 60 * 1000
4213498266Sopenharmony_ci
4313498266Sopenharmony_ci#define PROXY libtest_arg2
4413498266Sopenharmony_ci#define PROXYUSERPWD libtest_arg3
4513498266Sopenharmony_ci#define HOST test_argv[4]
4613498266Sopenharmony_ci
4713498266Sopenharmony_ci#define NUM_HANDLES 2
4813498266Sopenharmony_ci
4913498266Sopenharmony_cistatic CURL *eh[NUM_HANDLES];
5013498266Sopenharmony_ci
5113498266Sopenharmony_cistatic int init(int num, CURLM *cm, const char *url, const char *userpwd,
5213498266Sopenharmony_ci                struct curl_slist *headers)
5313498266Sopenharmony_ci{
5413498266Sopenharmony_ci  int res = 0;
5513498266Sopenharmony_ci
5613498266Sopenharmony_ci  res_easy_init(eh[num]);
5713498266Sopenharmony_ci  if(res)
5813498266Sopenharmony_ci    goto init_failed;
5913498266Sopenharmony_ci
6013498266Sopenharmony_ci  res_easy_setopt(eh[num], CURLOPT_URL, url);
6113498266Sopenharmony_ci  if(res)
6213498266Sopenharmony_ci    goto init_failed;
6313498266Sopenharmony_ci
6413498266Sopenharmony_ci  res_easy_setopt(eh[num], CURLOPT_PROXY, PROXY);
6513498266Sopenharmony_ci  if(res)
6613498266Sopenharmony_ci    goto init_failed;
6713498266Sopenharmony_ci
6813498266Sopenharmony_ci  res_easy_setopt(eh[num], CURLOPT_PROXYUSERPWD, userpwd);
6913498266Sopenharmony_ci  if(res)
7013498266Sopenharmony_ci    goto init_failed;
7113498266Sopenharmony_ci
7213498266Sopenharmony_ci  res_easy_setopt(eh[num], CURLOPT_PROXYAUTH, (long)CURLAUTH_ANY);
7313498266Sopenharmony_ci  if(res)
7413498266Sopenharmony_ci    goto init_failed;
7513498266Sopenharmony_ci
7613498266Sopenharmony_ci  res_easy_setopt(eh[num], CURLOPT_VERBOSE, 1L);
7713498266Sopenharmony_ci  if(res)
7813498266Sopenharmony_ci    goto init_failed;
7913498266Sopenharmony_ci
8013498266Sopenharmony_ci  res_easy_setopt(eh[num], CURLOPT_HEADER, 1L);
8113498266Sopenharmony_ci  if(res)
8213498266Sopenharmony_ci    goto init_failed;
8313498266Sopenharmony_ci
8413498266Sopenharmony_ci  res_easy_setopt(eh[num], CURLOPT_HTTPHEADER, headers); /* custom Host: */
8513498266Sopenharmony_ci  if(res)
8613498266Sopenharmony_ci    goto init_failed;
8713498266Sopenharmony_ci
8813498266Sopenharmony_ci  res_multi_add_handle(cm, eh[num]);
8913498266Sopenharmony_ci  if(res)
9013498266Sopenharmony_ci    goto init_failed;
9113498266Sopenharmony_ci
9213498266Sopenharmony_ci  return 0; /* success */
9313498266Sopenharmony_ci
9413498266Sopenharmony_ciinit_failed:
9513498266Sopenharmony_ci
9613498266Sopenharmony_ci  curl_easy_cleanup(eh[num]);
9713498266Sopenharmony_ci  eh[num] = NULL;
9813498266Sopenharmony_ci
9913498266Sopenharmony_ci  return res; /* failure */
10013498266Sopenharmony_ci}
10113498266Sopenharmony_ci
10213498266Sopenharmony_cistatic int loop(int num, CURLM *cm, const char *url, const char *userpwd,
10313498266Sopenharmony_ci                struct curl_slist *headers)
10413498266Sopenharmony_ci{
10513498266Sopenharmony_ci  CURLMsg *msg;
10613498266Sopenharmony_ci  long L;
10713498266Sopenharmony_ci  int Q, U = -1;
10813498266Sopenharmony_ci  fd_set R, W, E;
10913498266Sopenharmony_ci  struct timeval T;
11013498266Sopenharmony_ci  int res = 0;
11113498266Sopenharmony_ci
11213498266Sopenharmony_ci  res = init(num, cm, url, userpwd, headers);
11313498266Sopenharmony_ci  if(res)
11413498266Sopenharmony_ci    return res;
11513498266Sopenharmony_ci
11613498266Sopenharmony_ci  while(U) {
11713498266Sopenharmony_ci
11813498266Sopenharmony_ci    int M = -99;
11913498266Sopenharmony_ci
12013498266Sopenharmony_ci    res_multi_perform(cm, &U);
12113498266Sopenharmony_ci    if(res)
12213498266Sopenharmony_ci      return res;
12313498266Sopenharmony_ci
12413498266Sopenharmony_ci    res_test_timedout();
12513498266Sopenharmony_ci    if(res)
12613498266Sopenharmony_ci      return res;
12713498266Sopenharmony_ci
12813498266Sopenharmony_ci    if(U) {
12913498266Sopenharmony_ci      FD_ZERO(&R);
13013498266Sopenharmony_ci      FD_ZERO(&W);
13113498266Sopenharmony_ci      FD_ZERO(&E);
13213498266Sopenharmony_ci
13313498266Sopenharmony_ci      res_multi_fdset(cm, &R, &W, &E, &M);
13413498266Sopenharmony_ci      if(res)
13513498266Sopenharmony_ci        return res;
13613498266Sopenharmony_ci
13713498266Sopenharmony_ci      /* At this point, M is guaranteed to be greater or equal than -1. */
13813498266Sopenharmony_ci
13913498266Sopenharmony_ci      res_multi_timeout(cm, &L);
14013498266Sopenharmony_ci      if(res)
14113498266Sopenharmony_ci        return res;
14213498266Sopenharmony_ci
14313498266Sopenharmony_ci      /* At this point, L is guaranteed to be greater or equal than -1. */
14413498266Sopenharmony_ci
14513498266Sopenharmony_ci      if(L != -1) {
14613498266Sopenharmony_ci        int itimeout;
14713498266Sopenharmony_ci#if LONG_MAX > INT_MAX
14813498266Sopenharmony_ci        itimeout = (L > INT_MAX) ? INT_MAX : (int)L;
14913498266Sopenharmony_ci#else
15013498266Sopenharmony_ci        itimeout = (int)L;
15113498266Sopenharmony_ci#endif
15213498266Sopenharmony_ci        T.tv_sec = itimeout/1000;
15313498266Sopenharmony_ci        T.tv_usec = (itimeout%1000)*1000;
15413498266Sopenharmony_ci      }
15513498266Sopenharmony_ci      else {
15613498266Sopenharmony_ci        T.tv_sec = 5;
15713498266Sopenharmony_ci        T.tv_usec = 0;
15813498266Sopenharmony_ci      }
15913498266Sopenharmony_ci
16013498266Sopenharmony_ci      res_select_test(M + 1, &R, &W, &E, &T);
16113498266Sopenharmony_ci      if(res)
16213498266Sopenharmony_ci        return res;
16313498266Sopenharmony_ci    }
16413498266Sopenharmony_ci
16513498266Sopenharmony_ci    while(1) {
16613498266Sopenharmony_ci      msg = curl_multi_info_read(cm, &Q);
16713498266Sopenharmony_ci      if(!msg)
16813498266Sopenharmony_ci        break;
16913498266Sopenharmony_ci      if(msg->msg == CURLMSG_DONE) {
17013498266Sopenharmony_ci        int i;
17113498266Sopenharmony_ci        CURL *e = msg->easy_handle;
17213498266Sopenharmony_ci        fprintf(stderr, "R: %d - %s\n", (int)msg->data.result,
17313498266Sopenharmony_ci                curl_easy_strerror(msg->data.result));
17413498266Sopenharmony_ci        curl_multi_remove_handle(cm, e);
17513498266Sopenharmony_ci        curl_easy_cleanup(e);
17613498266Sopenharmony_ci        for(i = 0; i < NUM_HANDLES; i++) {
17713498266Sopenharmony_ci          if(eh[i] == e) {
17813498266Sopenharmony_ci            eh[i] = NULL;
17913498266Sopenharmony_ci            break;
18013498266Sopenharmony_ci          }
18113498266Sopenharmony_ci        }
18213498266Sopenharmony_ci      }
18313498266Sopenharmony_ci      else
18413498266Sopenharmony_ci        fprintf(stderr, "E: CURLMsg (%d)\n", (int)msg->msg);
18513498266Sopenharmony_ci    }
18613498266Sopenharmony_ci
18713498266Sopenharmony_ci    res_test_timedout();
18813498266Sopenharmony_ci    if(res)
18913498266Sopenharmony_ci      return res;
19013498266Sopenharmony_ci  }
19113498266Sopenharmony_ci
19213498266Sopenharmony_ci  return 0; /* success */
19313498266Sopenharmony_ci}
19413498266Sopenharmony_ci
19513498266Sopenharmony_ciint test(char *URL)
19613498266Sopenharmony_ci{
19713498266Sopenharmony_ci  CURLM *cm = NULL;
19813498266Sopenharmony_ci  struct curl_slist *headers = NULL;
19913498266Sopenharmony_ci  char buffer[246]; /* naively fixed-size */
20013498266Sopenharmony_ci  int res = 0;
20113498266Sopenharmony_ci  int i;
20213498266Sopenharmony_ci
20313498266Sopenharmony_ci  for(i = 0; i < NUM_HANDLES; i++)
20413498266Sopenharmony_ci    eh[i] = NULL;
20513498266Sopenharmony_ci
20613498266Sopenharmony_ci  start_test_timing();
20713498266Sopenharmony_ci
20813498266Sopenharmony_ci  if(test_argc < 4)
20913498266Sopenharmony_ci    return 99;
21013498266Sopenharmony_ci
21113498266Sopenharmony_ci  msnprintf(buffer, sizeof(buffer), "Host: %s", HOST);
21213498266Sopenharmony_ci
21313498266Sopenharmony_ci  /* now add a custom Host: header */
21413498266Sopenharmony_ci  headers = curl_slist_append(headers, buffer);
21513498266Sopenharmony_ci  if(!headers) {
21613498266Sopenharmony_ci    fprintf(stderr, "curl_slist_append() failed\n");
21713498266Sopenharmony_ci    return TEST_ERR_MAJOR_BAD;
21813498266Sopenharmony_ci  }
21913498266Sopenharmony_ci
22013498266Sopenharmony_ci  res_global_init(CURL_GLOBAL_ALL);
22113498266Sopenharmony_ci  if(res) {
22213498266Sopenharmony_ci    curl_slist_free_all(headers);
22313498266Sopenharmony_ci    return res;
22413498266Sopenharmony_ci  }
22513498266Sopenharmony_ci
22613498266Sopenharmony_ci  res_multi_init(cm);
22713498266Sopenharmony_ci  if(res) {
22813498266Sopenharmony_ci    curl_global_cleanup();
22913498266Sopenharmony_ci    curl_slist_free_all(headers);
23013498266Sopenharmony_ci    return res;
23113498266Sopenharmony_ci  }
23213498266Sopenharmony_ci
23313498266Sopenharmony_ci  res = loop(0, cm, URL, PROXYUSERPWD, headers);
23413498266Sopenharmony_ci  if(res)
23513498266Sopenharmony_ci    goto test_cleanup;
23613498266Sopenharmony_ci
23713498266Sopenharmony_ci  fprintf(stderr, "lib540: now we do the request again\n");
23813498266Sopenharmony_ci
23913498266Sopenharmony_ci  res = loop(1, cm, URL, PROXYUSERPWD, headers);
24013498266Sopenharmony_ci
24113498266Sopenharmony_citest_cleanup:
24213498266Sopenharmony_ci
24313498266Sopenharmony_ci  /* proper cleanup sequence - type PB */
24413498266Sopenharmony_ci
24513498266Sopenharmony_ci  for(i = 0; i < NUM_HANDLES; i++) {
24613498266Sopenharmony_ci    curl_multi_remove_handle(cm, eh[i]);
24713498266Sopenharmony_ci    curl_easy_cleanup(eh[i]);
24813498266Sopenharmony_ci  }
24913498266Sopenharmony_ci
25013498266Sopenharmony_ci  curl_multi_cleanup(cm);
25113498266Sopenharmony_ci  curl_global_cleanup();
25213498266Sopenharmony_ci
25313498266Sopenharmony_ci  curl_slist_free_all(headers);
25413498266Sopenharmony_ci
25513498266Sopenharmony_ci  return res;
25613498266Sopenharmony_ci}
257