1 /*************************************************************************** 2 * _ _ ____ _ 3 * Project ___| | | | _ \| | 4 * / __| | | | |_) | | 5 * | (__| |_| | _ <| |___ 6 * \___|\___/|_| \_\_____| 7 * 8 * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel (at) 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 http://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 int error; 39 struct_stat file_info; 40 CURLM *m = NULL; 41 int running; 42 43 start_test_timing(); 44 45 if(!libtest_arg2) { 46 #ifdef LIB529 47 /* test 529 */ 48 fprintf(stderr, "Usage: lib529 [url] [uploadfile]\n"); 49 #else 50 /* test 525 */ 51 fprintf(stderr, "Usage: lib525 [url] [uploadfile]\n"); 52 #endif 53 return TEST_ERR_USAGE; 54 } 55 56 hd_src = fopen(libtest_arg2, "rb"); 57 if(NULL == hd_src) { 58 error = ERRNO; 59 fprintf(stderr, "fopen() failed with error: %d (%s)\n", 60 error, strerror(error)); 61 fprintf(stderr, "Error opening file: (%s)\n", libtest_arg2); 62 return TEST_ERR_FOPEN; 63 } 64 65 /* get the file size of the local file */ 66 hd = fstat(fileno(hd_src), &file_info); 67 if(hd == -1) { 68 /* can't open file, bail out */ 69 error = ERRNO; 70 fprintf(stderr, "fstat() failed with error: %d (%s)\n", 71 error, strerror(error)); 72 fprintf(stderr, "ERROR: cannot open file (%s)\n", libtest_arg2); 73 fclose(hd_src); 74 return TEST_ERR_FSTAT; 75 } 76 77 res_global_init(CURL_GLOBAL_ALL); 78 if(res) { 79 fclose(hd_src); 80 return res; 81 } 82 83 easy_init(curl); 84 85 /* enable uploading */ 86 easy_setopt(curl, CURLOPT_UPLOAD, 1L); 87 88 /* specify target */ 89 easy_setopt(curl,CURLOPT_URL, URL); 90 91 /* go verbose */ 92 easy_setopt(curl, CURLOPT_VERBOSE, 1L); 93 94 /* use active FTP */ 95 easy_setopt(curl, CURLOPT_FTPPORT, "-"); 96 97 /* now specify which file to upload */ 98 easy_setopt(curl, CURLOPT_READDATA, hd_src); 99 100 /* NOTE: if you want this code to work on Windows with libcurl as a DLL, you 101 MUST also provide a read callback with CURLOPT_READFUNCTION. Failing to 102 do so will give you a crash since a DLL may not use the variable's memory 103 when passed in to it from an app like this. */ 104 105 /* Set the size of the file to upload (optional). If you give a *_LARGE 106 option you MUST make sure that the type of the passed-in argument is a 107 curl_off_t. If you use CURLOPT_INFILESIZE (without _LARGE) you must 108 make sure that to pass in a type 'long' argument. */ 109 easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_info.st_size); 110 111 multi_init(m); 112 113 multi_add_handle(m, curl); 114 115 for(;;) { 116 struct timeval interval; 117 fd_set rd, wr, exc; 118 int maxfd = -99; 119 120 interval.tv_sec = 1; 121 interval.tv_usec = 0; 122 123 multi_perform(m, &running); 124 125 abort_on_test_timeout(); 126 127 if(!running) 128 break; /* done */ 129 130 FD_ZERO(&rd); 131 FD_ZERO(&wr); 132 FD_ZERO(&exc); 133 134 multi_fdset(m, &rd, &wr, &exc, &maxfd); 135 136 /* At this point, maxfd is guaranteed to be greater or equal than -1. */ 137 138 select_test(maxfd+1, &rd, &wr, &exc, &interval); 139 140 abort_on_test_timeout(); 141 } 142 143 test_cleanup: 144 145 #ifdef LIB529 146 /* test 529 */ 147 /* proper cleanup sequence - type PA */ 148 curl_multi_remove_handle(m, curl); 149 curl_multi_cleanup(m); 150 curl_easy_cleanup(curl); 151 curl_global_cleanup(); 152 #else 153 /* test 525 */ 154 /* proper cleanup sequence - type PB */ 155 curl_multi_remove_handle(m, curl); 156 curl_easy_cleanup(curl); 157 curl_multi_cleanup(m); 158 curl_global_cleanup(); 159 #endif 160 161 /* close the local file */ 162 fclose(hd_src); 163 164 return res; 165 } 166