~ubuntu-branches/ubuntu/lucid/curl/lucid-201101212007

« back to all changes in this revision

Viewing changes to tests/libtest/lib525.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-05-16 15:16:54 UTC
  • mto: (3.1.1 lenny) (1.2.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: james.westby@ubuntu.com-20070516151654-x9nkigtr2j0i8d0v
Tags: upstream-7.16.2
ImportĀ upstreamĀ versionĀ 7.16.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 *                            | (__| |_| |  _ <| |___
6
6
 *                             \___|\___/|_| \_\_____|
7
7
 *
8
 
 * $Id: lib525.c,v 1.2 2006-06-09 08:25:16 bagder Exp $
 
8
 * $Id: lib525.c,v 1.16 2007-03-10 00:19:05 yangtse Exp $
9
9
 */
10
10
 
11
11
#include "test.h"
14
14
#include <sys/stat.h>
15
15
#include <fcntl.h>
16
16
 
 
17
#include "testutil.h"
 
18
 
 
19
#define MAIN_LOOP_HANG_TIMEOUT     90 * 1000
 
20
#define MULTI_PERFORM_HANG_TIMEOUT 60 * 1000
 
21
 
17
22
int test(char *URL)
18
23
{
19
24
  int res = 0;
20
25
  CURL *curl;
21
26
  FILE *hd_src ;
22
27
  int hd ;
23
 
  struct stat file_info;
 
28
  int error;
 
29
  struct_stat file_info;
24
30
  int running;
25
31
  char done=FALSE;
26
32
  CURLM *m;
 
33
  struct timeval ml_start;
 
34
  struct timeval mp_start;
 
35
  char ml_timedout = FALSE;
 
36
  char mp_timedout = FALSE;
 
37
 
 
38
  if (!arg2) {
 
39
    fprintf(stderr, "Usage: lib525 [url] [uploadfile]\n");
 
40
    return -1;
 
41
  }
27
42
 
28
43
  /* get the file size of the local file */
29
44
  hd = open(arg2, O_RDONLY) ;
34
49
     fdopen() from the previous descriptor, but hey this is just
35
50
     an example! */
36
51
  hd_src = fopen(arg2, "rb");
37
 
 
38
 
  /* In windows, this will init the winsock stuff */
39
 
  curl_global_init(CURL_GLOBAL_ALL);
40
 
 
41
 
  /* get a curl handle */
42
 
  curl = curl_easy_init();
43
 
  if(!curl)
44
 
    return 100; /* major bad */
45
 
 
 
52
  if(NULL == hd_src) {
 
53
    error = ERRNO;
 
54
    fprintf(stderr, "fopen() failed with error: %d %s\n",
 
55
            error, strerror(error));
 
56
    fprintf(stderr, "Error opening file: %s\n", arg2);
 
57
    return TEST_ERR_MAJOR_BAD;
 
58
  }
 
59
 
 
60
  if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
 
61
    fprintf(stderr, "curl_global_init() failed\n");
 
62
    fclose(hd_src);
 
63
    return TEST_ERR_MAJOR_BAD;
 
64
  }
 
65
 
 
66
  if ((curl = curl_easy_init()) == NULL) {
 
67
    fprintf(stderr, "curl_easy_init() failed\n");
 
68
    fclose(hd_src);
 
69
    curl_global_cleanup();
 
70
    return TEST_ERR_MAJOR_BAD;
 
71
  }
46
72
 
47
73
  /* enable uploading */
48
74
  curl_easy_setopt(curl, CURLOPT_UPLOAD, TRUE) ;
71
97
  curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
72
98
                   (curl_off_t)file_info.st_size);
73
99
 
74
 
  m = curl_multi_init();
75
 
 
76
 
  res = (int)curl_multi_add_handle(m, curl);
77
 
 
78
 
  while(!done) {
 
100
  if ((m = curl_multi_init()) == NULL) {
 
101
    fprintf(stderr, "curl_multi_init() failed\n");
 
102
    curl_easy_cleanup(curl);
 
103
    curl_global_cleanup();
 
104
    fclose(hd_src);
 
105
    return TEST_ERR_MAJOR_BAD;
 
106
  }
 
107
 
 
108
  if ((res = (int)curl_multi_add_handle(m, curl)) != CURLM_OK) {
 
109
    fprintf(stderr, "curl_multi_add_handle() failed, "
 
110
            "with code %d\n", res);
 
111
    curl_multi_cleanup(m);
 
112
    curl_easy_cleanup(curl);
 
113
    curl_global_cleanup();
 
114
    fclose(hd_src);
 
115
    return TEST_ERR_MAJOR_BAD;
 
116
  }
 
117
 
 
118
  ml_timedout = FALSE;
 
119
  ml_start = tutil_tvnow();
 
120
 
 
121
  while (!done) {
79
122
    fd_set rd, wr, exc;
80
123
    int max_fd;
81
124
    struct timeval interval;
83
126
    interval.tv_sec = 1;
84
127
    interval.tv_usec = 0;
85
128
 
 
129
    if (tutil_tvdiff(tutil_tvnow(), ml_start) >
 
130
        MAIN_LOOP_HANG_TIMEOUT) {
 
131
      ml_timedout = TRUE;
 
132
      break;
 
133
    }
 
134
    mp_timedout = FALSE;
 
135
    mp_start = tutil_tvnow();
 
136
 
86
137
    while (res == CURLM_CALL_MULTI_PERFORM) {
87
138
      res = (int)curl_multi_perform(m, &running);
 
139
      if (tutil_tvdiff(tutil_tvnow(), mp_start) >
 
140
          MULTI_PERFORM_HANG_TIMEOUT) {
 
141
        mp_timedout = TRUE;
 
142
        break;
 
143
      }
88
144
      if (running <= 0) {
89
145
        done = TRUE;
90
146
        break;
91
147
      }
92
148
    }
93
 
    if(done)
 
149
    if (mp_timedout || done)
94
150
      break;
95
151
 
96
152
    if (res != CURLM_OK) {
109
165
      break;
110
166
    }
111
167
 
112
 
    if (select(max_fd+1, &rd, &wr, &exc, &interval) == -1) {
 
168
    if (select_test(max_fd+1, &rd, &wr, &exc, &interval) == -1) {
113
169
      fprintf(stderr, "bad select??\n");
114
170
      res = 195;
115
171
      break;
118
174
    res = CURLM_CALL_MULTI_PERFORM;
119
175
  }
120
176
 
121
 
  curl_multi_remove_handle(m, curl);
122
 
  curl_easy_cleanup(curl);
123
 
  curl_multi_cleanup(m);
 
177
  if (ml_timedout || mp_timedout) {
 
178
    if (ml_timedout) fprintf(stderr, "ml_timedout\n");
 
179
    if (mp_timedout) fprintf(stderr, "mp_timedout\n");
 
180
    fprintf(stderr, "ABORTING TEST, since it seems "
 
181
            "that it would have run forever.\n");
 
182
    res = TEST_ERR_RUNS_FOREVER;
 
183
  }
 
184
 
 
185
#ifdef LIB529
 
186
  /* test 529 */
 
187
  curl_multi_remove_handle(m, curl);
 
188
  curl_multi_cleanup(m);
 
189
  curl_easy_cleanup(curl);
 
190
#else
 
191
  /* test 525 */
 
192
  curl_multi_remove_handle(m, curl);
 
193
  curl_easy_cleanup(curl);
 
194
  curl_multi_cleanup(m);
 
195
#endif
124
196
 
125
197
  fclose(hd_src); /* close the local file */
126
198