lib525.c
Go to the documentation of this file.
1 /***************************************************************************
2  * _ _ ____ _
3  * Project ___| | | | _ \| |
4  * / __| | | | |_) | |
5  * | (__| |_| | _ <| |___
6  * \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2017, 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.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 #include "test.h"
23 
24 #include <fcntl.h>
25 
26 #include "testutil.h"
27 #include "warnless.h"
28 #include "memdebug.h"
29 
30 #define TEST_HANG_TIMEOUT 60 * 1000
31 
32 int test(char *URL)
33 {
34  int res = 0;
35  CURL *curl = NULL;
36  FILE *hd_src = NULL;
37  int hd;
38  struct_stat file_info;
39  CURLM *m = NULL;
40  int running;
41 
43 
44  if(!libtest_arg2) {
45 #ifdef LIB529
46  /* test 529 */
47  fprintf(stderr, "Usage: lib529 [url] [uploadfile]\n");
48 #else
49  /* test 525 */
50  fprintf(stderr, "Usage: lib525 [url] [uploadfile]\n");
51 #endif
52  return TEST_ERR_USAGE;
53  }
54 
55  hd_src = fopen(libtest_arg2, "rb");
56  if(NULL == hd_src) {
57  fprintf(stderr, "fopen failed with error: %d (%s)\n",
58  errno, strerror(errno));
59  fprintf(stderr, "Error opening file: (%s)\n", libtest_arg2);
60  return TEST_ERR_FOPEN;
61  }
62 
63  /* get the file size of the local file */
64  hd = fstat(fileno(hd_src), &file_info);
65  if(hd == -1) {
66  /* can't open file, bail out */
67  fprintf(stderr, "fstat() failed with error: %d (%s)\n",
68  errno, strerror(errno));
69  fprintf(stderr, "ERROR: cannot open file (%s)\n", libtest_arg2);
70  fclose(hd_src);
71  return TEST_ERR_FSTAT;
72  }
73 
75  if(res) {
76  fclose(hd_src);
77  return res;
78  }
79 
80  easy_init(curl);
81 
82  /* enable uploading */
83  easy_setopt(curl, CURLOPT_UPLOAD, 1L);
84 
85  /* specify target */
86  easy_setopt(curl, CURLOPT_URL, URL);
87 
88  /* go verbose */
89  easy_setopt(curl, CURLOPT_VERBOSE, 1L);
90 
91  /* use active FTP */
92  easy_setopt(curl, CURLOPT_FTPPORT, "-");
93 
94  /* now specify which file to upload */
95  easy_setopt(curl, CURLOPT_READDATA, hd_src);
96 
97  /* NOTE: if you want this code to work on Windows with libcurl as a DLL, you
98  MUST also provide a read callback with CURLOPT_READFUNCTION. Failing to
99  do so will give you a crash since a DLL may not use the variable's memory
100  when passed in to it from an app like this. */
101 
102  /* Set the size of the file to upload (optional). If you give a *_LARGE
103  option you MUST make sure that the type of the passed-in argument is a
104  curl_off_t. If you use CURLOPT_INFILESIZE (without _LARGE) you must
105  make sure that to pass in a type 'long' argument. */
106  easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_info.st_size);
107 
108  multi_init(m);
109 
110  multi_add_handle(m, curl);
111 
112  for(;;) {
113  struct timeval interval;
114  fd_set rd, wr, exc;
115  int maxfd = -99;
116 
117  interval.tv_sec = 1;
118  interval.tv_usec = 0;
119 
120  multi_perform(m, &running);
121 
123 
124  if(!running)
125  break; /* done */
126 
127  FD_ZERO(&rd);
128  FD_ZERO(&wr);
129  FD_ZERO(&exc);
130 
131  multi_fdset(m, &rd, &wr, &exc, &maxfd);
132 
133  /* At this point, maxfd is guaranteed to be greater or equal than -1. */
134 
135  select_test(maxfd + 1, &rd, &wr, &exc, &interval);
136 
138  }
139 
140 test_cleanup:
141 
142 #ifdef LIB529
143  /* test 529 */
144  /* proper cleanup sequence - type PA */
145  curl_multi_remove_handle(m, curl);
147  curl_easy_cleanup(curl);
149 #else
150  /* test 525 */
151  /* proper cleanup sequence - type PB */
152  curl_multi_remove_handle(m, curl);
153  curl_easy_cleanup(curl);
156 #endif
157 
158  /* close the local file */
159  fclose(hd_src);
160 
161  return res;
162 }
#define select_test(A, B, C, D, E)
Definition: test.h:378
#define res_global_init(A)
Definition: test.h:419
#define easy_setopt(A, B, C)
Definition: test.h:190
#define multi_add_handle(A, B)
Definition: test.h:238
static int res
#define abort_on_test_timeout()
Definition: test.h:404
char * libtest_arg2
Definition: first.c:75
#define TEST_ERR_FSTAT
Definition: test.h:95
#define easy_init(A)
Definition: test.h:145
int test(char *URL)
Definition: lib525.c:32
#define multi_fdset(A, B, C, D, E)
Definition: test.h:323
CURL_EXTERN void curl_easy_cleanup(CURL *curl)
CURL_TYPEOF_CURL_OFF_T curl_off_t
Definition: system.h:420
#define TEST_ERR_USAGE
Definition: test.h:93
#define multi_perform(A, B)
Definition: test.h:293
CURL_EXTERN CURLMcode curl_multi_remove_handle(CURLM *multi_handle, CURL *curl_handle)
#define multi_init(A)
Definition: test.h:166
#define TEST_ERR_FOPEN
Definition: test.h:94
void CURLM
Definition: multi.h:58
void CURL
Definition: curl.h:102
#define fprintf
Definition: curl_printf.h:41
CURL_EXTERN void curl_global_cleanup(void)
curl_global_cleanup() globally cleanups curl, uses the value of "init_flags" to determine what needs ...
Definition: easy.c:312
#define CURL_GLOBAL_ALL
Definition: curl.h:2519
int fileno(FILE *stream)
static CURL * curl
Definition: sessioninfo.c:35
#define start_test_timing()
Definition: test.h:383
CURL_EXTERN CURLMcode curl_multi_cleanup(CURLM *multi_handle)
#define struct_stat
Definition: curl_setup.h:385


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:15