aboutsummaryrefslogtreecommitdiff
path: root/tests/libtest/lib575.c
blob: 3bf15ea26511af883a55f525e657a904c01547d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*****************************************************************************
 *                                  _   _ ____  _
 *  Project                     ___| | | |  _ \| |
 *                             / __| | | | |_) | |
 *                            | (__| |_| |  _ <| |___
 *                             \___|\___/|_| \_\_____|
 *
 */

#include "test.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "testutil.h"
#include "memdebug.h"

/* 3x download!
 * 1. normal
 * 2. dup handle
 * 3. with multi interface
 */

int test(char *URL)
{
  CURLMcode m;
  CURL *handle = NULL, *duphandle;
  CURLM *mhandle = NULL;
  int res = 0;
  int still_running = 0;

  if(curl_global_init(CURL_GLOBAL_ALL)) {
    fprintf(stderr, "curl_global_init() failed\n");
    goto test_cleanup;
  }

  handle = curl_easy_init();
  if(!handle) {
    res = CURLE_OUT_OF_MEMORY;
    goto test_cleanup;
  }

  test_setopt(handle, CURLOPT_URL, URL);
  test_setopt(handle, CURLOPT_WILDCARDMATCH, 1L);
  test_setopt(handle, CURLOPT_VERBOSE, 1L);

  res = curl_easy_perform(handle);
  if(res)
    goto test_cleanup;

  res = curl_easy_perform(handle);
  if(res)
    goto test_cleanup;

  duphandle = curl_easy_duphandle(handle);
  if(!duphandle)
    goto test_cleanup;
  curl_easy_cleanup(handle);
  handle = duphandle;

  mhandle = curl_multi_init();
  if(!mhandle) {
    fprintf(stderr, "curl_multi_init() failed\n");
    goto test_cleanup;
  }

  curl_multi_add_handle(mhandle, handle);

  while(CURLM_CALL_MULTI_PERFORM ==
        curl_multi_perform(mhandle, &still_running));

  while(still_running) {
    struct timeval timeout;
    int rc;
    fd_set fdread;
    fd_set fdwrite;
    fd_set fdexcep;
    int max_fdset = -1;
    FD_ZERO(&fdread);
    FD_ZERO(&fdwrite);
    FD_ZERO(&fdexcep);
    timeout.tv_sec = 3;
    timeout.tv_usec = 0;

    m = curl_multi_fdset(mhandle, &fdread, &fdwrite, &fdexcep, &max_fdset);
    rc = select(max_fdset + 1, &fdread, &fdwrite, &fdexcep, &timeout);
    if(rc == -1) {
      fprintf(stderr, "select() error\n");
      goto test_cleanup;
    }
    else if(rc == 0) {
      fprintf(stderr, "select() timeout!\n");
      goto test_cleanup;
    }
    else {
      while(CURLM_CALL_MULTI_PERFORM ==
          curl_multi_perform(mhandle, &still_running));
    }
  }

test_cleanup:
  if(mhandle)
    curl_multi_cleanup(mhandle);
  if(handle)
    curl_easy_cleanup(handle);
  curl_global_cleanup();
  return res;
}