64
64
int main(int argc, char* argv[])
66
static const char kChdir[] = "CWD /toolbox/ncbi_tools\n";
67
static const char kFile[] = "RETR CURRENT/ncbi.tar.gz";
68
int/*bool*/ aborting = 0, first;
66
static const char kChdir[] = "CWD /toolbox/ncbi_tools++\n";
67
static const char kFile[] = "DATA/Misc/test_ncbi_conn_stream.FTP.data";
68
int/*bool*/ cancel = 0, first;
70
70
SConnNetInfo* net_info;
72
72
CONNECTOR connector;
79
79
g_NCBI_ConnectRandomSeed = (int) time(0) ^ NCBI_CONNECT_SRAND_ADDEND;
89
89
assert((net_info = ConnNetInfo_Create(0)) != 0);
90
90
if (net_info->debug_printout == eDebugPrintout_Some)
91
flag |= fFCDC_LogControl;
91
flag |= fFTP_LogControl;
92
92
else if (net_info->debug_printout == eDebugPrintout_Data) {
94
94
ConnNetInfo_GetValue(0, REG_CONN_DEBUG_PRINTOUT, val, sizeof(val),
95
95
DEF_CONN_DEBUG_PRINTOUT);
96
flag |= strcasecmp(val, "all") == 0 ? fFCDC_LogAll : fFCDC_LogData;
96
flag |= strcasecmp(val, "all") == 0 ? fFTP_LogAll : fFTP_LogData;
98
flag |= fFCDC_UseFeatures;
98
flag |= fFTP_UseFeatures;
101
101
sprintf(buf, ":%hu", TEST_PORT);
109
109
TEST_USER, TEST_PASS,
110
110
TEST_PATH, flag);
112
if (CONN_Create(connector, &conn) != eIO_Success)
112
if (CONN_CreateEx(connector,
113
fCONN_Supplement | fCONN_Untie, &conn) != eIO_Success) {
113
114
CORE_LOG(eLOG_Fatal, "Cannot create FTP download connection");
115
117
assert(CONN_SetTimeout(conn, eIO_Open, net_info->timeout)
133
135
CORE_LOG(eLOG_Fatal, "Test failed to reject multiple commands");
134
136
CORE_LOG(eLOG_Note, "Multiple commands correctly rejected");
136
status = CONN_Write(conn, "SIZE 1GB\n", 9, &n, eIO_WritePersist);
138
status = CONN_Write(conn, "SIZE 1GB", 9, &n, eIO_WritePlain);
137
139
if (status == eIO_Success) {
139
141
CONN_ReadLine(conn, buf, sizeof(buf) - 1, &n);
183
if (CONN_Write(conn, "SIZE ", 5, &n, eIO_WritePlain) != eIO_Success)
185
if (CONN_Write(conn, "SIZE ", 5, &n, eIO_WritePersist) != eIO_Success)
184
186
CORE_LOG(eLOG_Fatal, "Cannot write SIZE directory command");
185
187
size = strlen(kChdir + 4);
186
if (CONN_Write(conn, kChdir + 4, size, &n, eIO_WritePersist)==eIO_Success){
188
if (CONN_Write(conn, kChdir + 4, size, &n, eIO_WritePlain) == eIO_Success){
188
189
CONN_ReadLine(conn, buf, sizeof(buf) - 1, &n);
189
190
CORE_LOGF(eLOG_Note, ("SIZE directory returned: %.*s", (int) n, buf));
197
198
CORE_LOGF(eLOG_Fatal, ("Cannot execute %.*s",
198
199
(int) sizeof(kChdir) - 2, kChdir));
201
size = strlen(kFile + 5);
202
if ((status = CONN_Write(conn, "MDTM ", 5, &n, eIO_WritePlain))
204
(status = CONN_Write(conn, kFile + 5, size, &n, eIO_WritePersist))
201
if (CONN_Write(conn, "PWD\n", 4, &n, eIO_WritePlain) == eIO_Success) {
202
CONN_ReadLine(conn, buf, sizeof(buf) - 1, &n);
203
CORE_LOGF(eLOG_Note, ("PWD returned: %.*s", (int) n, buf));
205
CORE_LOG(eLOG_Fatal, "Cannot execute PWD");
207
if (CONN_Write(conn, "CDUP\n", 5, &n, eIO_WritePlain) != eIO_Success)
208
CORE_LOG(eLOG_Fatal, "Cannot execute CDUP");
209
if (CONN_Write(conn, "XCUP\n", 5, &n, eIO_WritePlain) != eIO_Success)
210
CORE_LOG(eLOG_Fatal, "Cannot execute XCUP");
212
if (CONN_Write(conn, kChdir, sizeof(kChdir) - 1, &n, eIO_WritePlain)
214
CORE_LOGF(eLOG_Fatal, ("Cannot re-execute %.*s",
215
(int) sizeof(kChdir) - 2, kChdir));
218
size = sizeof(kFile) - 1;
219
if ((status = CONN_Write(conn, "MDTM ", 5, &n, eIO_WritePersist))
220
== eIO_Success && n == 5 &&
221
(status = CONN_Write(conn, kFile, size, &n, eIO_WritePlain))
222
== eIO_Success && n == size) {
206
223
unsigned long val;
208
225
CONN_ReadLine(conn, buf, sizeof(buf) - 1, &n);
218
235
IO_StatusStr(status)));
221
if (CONN_Write(conn, kFile, sizeof(kFile) - 1, &n, eIO_WritePersist)
223
CORE_LOGF(eLOG_Fatal, ("Cannot write %s", kFile));
238
if ((status = CONN_Write(conn, "RETR ", 5, &n, eIO_WritePersist))
239
!= eIO_Success || n != 5 ||
240
(status = CONN_Write(conn, kFile, size, &n, eIO_WritePersist))
241
!= eIO_Success || n != size) {
242
CORE_LOGF(eLOG_Fatal, ("Cannot write 'RETR %s': %s",
243
kFile, IO_StatusStr(status)));
227
elapse = s_GetTime();
247
elapsed = s_GetTime();
229
249
status = CONN_Read(conn, buf, sizeof(buf), &n, eIO_ReadPlain);
242
262
CORE_LOGF(eLOG_Error, ("Read error: %s",IO_StatusStr(status)));
244
264
} while (status == eIO_Success);
245
elapse = s_GetTime() - elapse;
265
if (status != eIO_Success)
267
elapsed = s_GetTime() - elapsed;
247
if (!aborting || (rand() & 1) == 0) {
269
if (!cancel || !(rand() & 1)) {
271
CORE_LOG(eLOG_Note, "Cancelling download by a command");
248
272
if (CONN_Write(conn, "NLST blah*", 10, &n, eIO_WritePlain)
249
273
!= eIO_Success) {
250
274
CORE_LOG(eLOG_Fatal, "Cannot write garbled NLST command");
253
CORE_LOG(eLOG_Note, "Garbled NLST command output (should be empty):");
277
CORE_LOG(eLOG_Note, "Garbled NLST command output (expected empty):");
254
278
first = 1/*true*/;
256
280
status = CONN_Read(conn, buf, sizeof(buf), &n, eIO_ReadPlain);
264
288
printf("<EOF>\n");
292
CORE_LOG(eLOG_Note, "Closing with download still in progress");
269
if (CONN_Close(conn) != eIO_Success) {
270
CORE_LOGF(eLOG_Fatal, ("Error %s FTP connection",
271
aborting ? "aborting" : "closing"));
294
if (CONN_Close(conn) != eIO_Success)
295
CORE_LOG(eLOG_Fatal, "Error in closing FTP connection");
274
297
/* Cleanup and exit */
275
298
fclose(data_file);
277
300
CORE_LOGF(size ? eLOG_Note : eLOG_Fatal,
278
301
("%lu byte(s) downloaded in %.2f second(s) @ %.2fKB/s",
279
(unsigned long) size, elapse,
280
(unsigned long) size / (1024 * (elapse ? elapse : 1.0))));
302
(unsigned long) size, elapsed,
303
(unsigned long) size / (1024 * (elapsed ? elapsed : 1.0))));
282
305
remove("test_ncbi_ftp_connector.dat");
283
306
ConnNetInfo_Destroy(net_info);