xref: /third_party/curl/tests/libtest/lib661.c (revision 13498266)
1/***************************************************************************
2 *                                  _   _ ____  _
3 *  Project                     ___| | | |  _ \| |
4 *                             / __| | | | |_) | |
5 *                            | (__| |_| |  _ <| |___
6 *                             \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 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.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 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24#include "test.h"
25#include "memdebug.h"
26
27int test(char *URL)
28{
29   CURLcode res;
30   CURL *curl = NULL;
31   char *newURL = NULL;
32   struct curl_slist *slist = NULL;
33
34   if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
35     fprintf(stderr, "curl_global_init() failed\n");
36     return TEST_ERR_MAJOR_BAD;
37   }
38
39   curl = curl_easy_init();
40   if(!curl) {
41     fprintf(stderr, "curl_easy_init() failed\n");
42     res = TEST_ERR_MAJOR_BAD;
43     goto test_cleanup;
44   }
45
46   /* test: CURLFTPMETHOD_SINGLECWD with absolute path should
47            skip CWD to entry path */
48   newURL = aprintf("%s/folderA/661", URL);
49   test_setopt(curl, CURLOPT_URL, newURL);
50   test_setopt(curl, CURLOPT_VERBOSE, 1L);
51   test_setopt(curl, CURLOPT_IGNORE_CONTENT_LENGTH, 1L);
52   test_setopt(curl, CURLOPT_FTP_FILEMETHOD, (long) CURLFTPMETHOD_SINGLECWD);
53   res = curl_easy_perform(curl);
54   if(res != CURLE_REMOTE_FILE_NOT_FOUND)
55     goto test_cleanup;
56
57   curl_free(newURL);
58   newURL = aprintf("%s/folderB/661", URL);
59   test_setopt(curl, CURLOPT_URL, newURL);
60   res = curl_easy_perform(curl);
61   if(res != CURLE_REMOTE_FILE_NOT_FOUND)
62     goto test_cleanup;
63
64   /* test: CURLFTPMETHOD_NOCWD with absolute path should
65      never emit CWD (for both new and reused easy handle) */
66   curl_easy_cleanup(curl);
67   curl = curl_easy_init();
68   if(!curl) {
69     fprintf(stderr, "curl_easy_init() failed\n");
70     res = TEST_ERR_MAJOR_BAD;
71     goto test_cleanup;
72   }
73
74   curl_free(newURL);
75   newURL = aprintf("%s/folderA/661", URL);
76   test_setopt(curl, CURLOPT_URL, newURL);
77   test_setopt(curl, CURLOPT_VERBOSE, 1L);
78   test_setopt(curl, CURLOPT_IGNORE_CONTENT_LENGTH, 1L);
79   test_setopt(curl, CURLOPT_FTP_FILEMETHOD, (long) CURLFTPMETHOD_NOCWD);
80   res = curl_easy_perform(curl);
81   if(res != CURLE_REMOTE_FILE_NOT_FOUND)
82     goto test_cleanup;
83
84   /* curve ball: CWD /folderB before reusing connection with _NOCWD */
85   curl_free(newURL);
86   newURL = aprintf("%s/folderB/661", URL);
87   test_setopt(curl, CURLOPT_URL, newURL);
88   test_setopt(curl, CURLOPT_FTP_FILEMETHOD, (long) CURLFTPMETHOD_SINGLECWD);
89   res = curl_easy_perform(curl);
90   if(res != CURLE_REMOTE_FILE_NOT_FOUND)
91     goto test_cleanup;
92
93   curl_free(newURL);
94   newURL = aprintf("%s/folderA/661", URL);
95   test_setopt(curl, CURLOPT_URL, newURL);
96   test_setopt(curl, CURLOPT_FTP_FILEMETHOD, (long) CURLFTPMETHOD_NOCWD);
97   res = curl_easy_perform(curl);
98   if(res != CURLE_REMOTE_FILE_NOT_FOUND)
99     goto test_cleanup;
100
101   /* test: CURLFTPMETHOD_NOCWD with home-relative path should
102      not emit CWD for first FTP access after login */
103   curl_easy_cleanup(curl);
104   curl = curl_easy_init();
105   if(!curl) {
106     fprintf(stderr, "curl_easy_init() failed\n");
107     res = TEST_ERR_MAJOR_BAD;
108     goto test_cleanup;
109   }
110
111   slist = curl_slist_append(NULL, "SYST");
112   if(!slist) {
113     fprintf(stderr, "curl_slist_append() failed\n");
114     res = TEST_ERR_MAJOR_BAD;
115     goto test_cleanup;
116   }
117
118   test_setopt(curl, CURLOPT_URL, URL);
119   test_setopt(curl, CURLOPT_VERBOSE, 1L);
120   test_setopt(curl, CURLOPT_NOBODY, 1L);
121   test_setopt(curl, CURLOPT_FTP_FILEMETHOD, (long) CURLFTPMETHOD_NOCWD);
122   test_setopt(curl, CURLOPT_QUOTE, slist);
123   res = curl_easy_perform(curl);
124   if(res)
125     goto test_cleanup;
126
127   /* test: CURLFTPMETHOD_SINGLECWD with home-relative path should
128      not emit CWD for first FTP access after login */
129   curl_easy_cleanup(curl);
130   curl = curl_easy_init();
131   if(!curl) {
132     fprintf(stderr, "curl_easy_init() failed\n");
133     res = TEST_ERR_MAJOR_BAD;
134     goto test_cleanup;
135   }
136
137   test_setopt(curl, CURLOPT_URL, URL);
138   test_setopt(curl, CURLOPT_VERBOSE, 1L);
139   test_setopt(curl, CURLOPT_NOBODY, 1L);
140   test_setopt(curl, CURLOPT_FTP_FILEMETHOD, (long) CURLFTPMETHOD_SINGLECWD);
141   test_setopt(curl, CURLOPT_QUOTE, slist);
142   res = curl_easy_perform(curl);
143   if(res)
144     goto test_cleanup;
145
146   /* test: CURLFTPMETHOD_NOCWD with home-relative path should
147      not emit CWD for second FTP access when not needed +
148      bonus: see if path buffering survives curl_easy_reset() */
149   curl_easy_reset(curl);
150   test_setopt(curl, CURLOPT_URL, URL);
151   test_setopt(curl, CURLOPT_VERBOSE, 1L);
152   test_setopt(curl, CURLOPT_NOBODY, 1L);
153   test_setopt(curl, CURLOPT_FTP_FILEMETHOD, (long) CURLFTPMETHOD_NOCWD);
154   test_setopt(curl, CURLOPT_QUOTE, slist);
155   res = curl_easy_perform(curl);
156
157
158test_cleanup:
159
160   if(res)
161     fprintf(stderr, "test encountered error %d\n", res);
162   curl_slist_free_all(slist);
163   curl_free(newURL);
164   curl_easy_cleanup(curl);
165   curl_global_cleanup();
166
167   return (int)res;
168}
169