1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
|
/*
* Copyright 1999-2006 University of Chicago
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "globus_ftp_control_test.h"
#define TEST_ITERATIONS 8
#define NAME "globus_ftp_control_test_file_name"
#define DIRECTION_GET 0
#define DIRECTION_PUT 1
#define CHUNK_SIZE 256
#define CHUNK_COUNT 10
typedef struct get_put_info_s
{
int direction;
int buffer_size;
globus_byte_t * buffer;
globus_bool_t result;
globus_ftp_control_fake_file_t * fake_file;
} get_put_info_t;
static void
eb_data_response_callback(
void * callback_arg,
globus_ftp_control_handle_t * handle,
globus_object_t * error,
globus_ftp_control_response_t * ftp_response);
void
eb_port_response_callback(
void * callback_arg,
globus_ftp_control_handle_t * handle,
globus_object_t * error,
globus_ftp_control_response_t * ftp_response);
static void
eb_write_data_callback(
void * callback_arg,
globus_ftp_control_handle_t * handle,
globus_object_t * error,
globus_byte_t * buffer,
globus_size_t length,
globus_off_t offset,
globus_bool_t eof);
static void
read_data_callback(
void * callback_arg,
globus_ftp_control_handle_t * handle,
globus_object_t * error,
globus_byte_t * buffer,
globus_size_t length,
globus_off_t offset,
globus_bool_t eof);
static void
signal_exit();
static globus_ftp_control_test_monitor_t monitor;
/*
* this is called with a connected handle
*/
globus_bool_t
eb_data_test(
globus_ftp_control_handle_t * handle)
{
globus_result_t result;
get_put_info_t get_put_info;
globus_ftp_control_fake_file_t fake_file;
globus_ftp_control_host_port_t host_port;
int ctr;
int hi;
int low;
globus_mutex_init(&monitor.mutex, GLOBUS_NULL);
globus_cond_init(&monitor.cond, GLOBUS_NULL);
/* once in default, once in image */
for(ctr = 0; ctr < 10; ctr++)
{
/* send type */
if(ctr == 0)
{
verbose_printf(2, "sending type command\n");
monitor.done = GLOBUS_FALSE;
monitor.count = 0;
monitor.rc = GLOBUS_TRUE;
result = globus_ftp_control_send_command(
handle,
"TYPE I\r\n",
eb_data_response_callback,
GLOBUS_NULL);
if(result != GLOBUS_SUCCESS)
{
verbose_printf(1, "send_command TYPE failed\n");
return GLOBUS_FALSE;
}
/* wait to get pasv back */
globus_mutex_lock(&monitor.mutex);
{
while(!monitor.done)
{
globus_cond_wait(&monitor.cond, &monitor.mutex);
}
}
globus_mutex_unlock(&monitor.mutex);
if(!monitor.rc)
{
verbose_printf(1, "TYPE failed\n");
return GLOBUS_FALSE;
}
verbose_printf(2, "sending MODE E command\n");
monitor.done = GLOBUS_FALSE;
monitor.count = 0;
monitor.rc = GLOBUS_TRUE;
result = globus_ftp_control_send_command(
handle,
"MODE E\r\n",
eb_data_response_callback,
GLOBUS_NULL);
if(result != GLOBUS_SUCCESS)
{
verbose_printf(1, "send_command MODE failed\n");
return GLOBUS_FALSE;
}
globus_mutex_lock(&monitor.mutex);
{
while(!monitor.done)
{
globus_cond_wait(&monitor.cond, &monitor.mutex);
}
}
globus_mutex_unlock(&monitor.mutex);
if(!monitor.rc)
{
verbose_printf(1, "MODE failed\n");
return GLOBUS_FALSE;
}
globus_ftp_control_local_mode(
handle,
GLOBUS_FTP_CONTROL_MODE_EXTENDED_BLOCK);
}
/* send pasv */
verbose_printf(2, "sending pasv command\n");
monitor.done = GLOBUS_FALSE;
result = globus_ftp_control_send_command(
handle,
"PASV\r\n",
eb_data_response_callback,
GLOBUS_NULL);
if(result != GLOBUS_SUCCESS)
{
verbose_printf(1, "send_command PASV failed\n");
return GLOBUS_FALSE;
}
/* wait to get pasv back */
globus_mutex_lock(&monitor.mutex);
{
while(!monitor.done)
{
globus_cond_wait(&monitor.cond, &monitor.mutex);
}
}
globus_mutex_unlock(&monitor.mutex);
if(!monitor.rc)
{
verbose_printf(1, "pasv command failed\n");
return GLOBUS_FALSE;
}
/* start writting */
verbose_printf(2, "sending stor command\n");
get_put_info.direction = DIRECTION_PUT;
monitor.rc = GLOBUS_TRUE;
fake_file_init(&fake_file, CHUNK_SIZE * CHUNK_COUNT + 12, CHUNK_SIZE);
get_put_info.fake_file = &fake_file;
monitor.done = GLOBUS_FALSE;
monitor.count = 0;
result = globus_ftp_control_send_command(
handle,
"STOR %s\r\n",
eb_data_response_callback,
&get_put_info,
NAME);
if(result != GLOBUS_SUCCESS)
{
verbose_printf(1, "send_command STOR failed\n");
return GLOBUS_FALSE;
}
result = globus_ftp_control_data_connect_write(
handle, GLOBUS_NULL, GLOBUS_NULL);
if(result != GLOBUS_SUCCESS)
{
verbose_printf(1, "connect_write failed\n");
return GLOBUS_FALSE;
}
/* wait to get put back */
globus_mutex_lock(&monitor.mutex);
{
while(monitor.count < 2)
{
globus_cond_wait(&monitor.cond, &monitor.mutex);
}
}
globus_mutex_unlock(&monitor.mutex);
if(!monitor.rc)
{
verbose_printf(1, "STOR failed\n");
return GLOBUS_FALSE;
}
verbose_printf(2, "data has been sent\n");
/* new pasv */
host_port.port = 0;
globus_ftp_control_local_pasv(
handle,
&host_port);
hi = host_port.port / 256;
low = host_port.port % 256;
verbose_printf(2, "sending port command %d,%d,%d,%d,%d,%d\n",
host_port.host[0],
host_port.host[1],
host_port.host[2],
host_port.host[3],
hi,
low);
monitor.done = GLOBUS_FALSE;
monitor.rc = GLOBUS_TRUE;
result = globus_ftp_control_send_command(
handle,
"PORT %d,%d,%d,%d,%d,%d\r\n",
eb_port_response_callback,
GLOBUS_NULL,
host_port.host[0],
host_port.host[1],
host_port.host[2],
host_port.host[3],
hi,
low);
if(result != GLOBUS_SUCCESS)
{
verbose_printf(1, "send_command PASV failed\n");
return GLOBUS_FALSE;
}
globus_mutex_lock(&monitor.mutex);
{
while(!monitor.done)
{
globus_cond_wait(&monitor.cond, &monitor.mutex);
}
}
globus_mutex_unlock(&monitor.mutex);
if(!monitor.rc)
{
verbose_printf(1, "PASV failed\n");
return GLOBUS_FALSE;
}
fake_file_seek(get_put_info.fake_file, 0);
verbose_printf(2, "sending retr command\n");
get_put_info.direction = DIRECTION_GET;
monitor.rc = GLOBUS_TRUE;
get_put_info.fake_file = &fake_file;
monitor.done = GLOBUS_FALSE;
monitor.count = 0;
result = globus_ftp_control_send_command(
handle,
"RETR %s\r\n",
eb_data_response_callback,
&get_put_info,
NAME);
if(result != GLOBUS_SUCCESS)
{
verbose_printf(1, "send_command PASV failed\n");
return GLOBUS_FALSE;
}
result = globus_ftp_control_data_connect_read(
handle, GLOBUS_NULL, GLOBUS_NULL);
if(result != GLOBUS_SUCCESS)
{
verbose_printf(1, "connect_read failed\n");
return GLOBUS_FALSE;
}
globus_mutex_lock(&monitor.mutex);
{
while(monitor.count < 2)
{
globus_cond_wait(&monitor.cond, &monitor.mutex);
}
}
globus_mutex_unlock(&monitor.mutex);
if(!monitor.rc)
{
verbose_printf(1, "RETR failed\n");
return GLOBUS_FALSE;
}
verbose_printf(2, "data has been received\n");
/* delete existing file */
verbose_printf(2, "sending dele command\n");
monitor.done = GLOBUS_FALSE;
monitor.rc = GLOBUS_TRUE;
result = globus_ftp_control_send_command(
handle,
"DELE %s\r\n",
eb_data_response_callback,
GLOBUS_NULL,
NAME);
if(result != GLOBUS_SUCCESS)
{
verbose_printf(1, "send_command DELE failed\n");
return GLOBUS_FALSE;
}
/* wait to get pasv back */
globus_mutex_lock(&monitor.mutex);
{
while(!monitor.done)
{
globus_cond_wait(&monitor.cond, &monitor.mutex);
}
}
globus_mutex_unlock(&monitor.mutex);
if(!monitor.rc)
{
verbose_printf(1, "DELE failed\n");
return GLOBUS_FALSE;
}
fake_file_destroy(&fake_file);
}
return GLOBUS_TRUE;
}
void
eb_port_response_callback(
void * callback_arg,
globus_ftp_control_handle_t * handle,
globus_object_t * error,
globus_ftp_control_response_t * ftp_response)
{
verbose_printf(2, "eb_port_response_callback() : start %d\n",
ftp_response->code);
{
if(error != GLOBUS_NULL)
{
verbose_printf(1,
"eb_port_response_callback() : result failuer\n");
monitor.rc = GLOBUS_FALSE;
}
else
{
verbose_printf(2,
"eb_port_response_callback() : port successful\n");
monitor.rc = GLOBUS_TRUE;
}
signal_exit();
return;
}
}
void
eb_data_response_callback(
void * callback_arg,
globus_ftp_control_handle_t * handle,
globus_object_t * error,
globus_ftp_control_response_t * ftp_response)
{
get_put_info_t * get_put_info;
globus_result_t result;
globus_ftp_control_host_port_t addr;
if(error != GLOBUS_NULL)
{
monitor.rc = GLOBUS_FALSE;
signal_exit();
return;
}
if(ftp_response->code == 227)
{
pasv_to_host_port(ftp_response->response_buffer, &addr);
result = globus_ftp_control_local_port(handle, &addr);
if(result != GLOBUS_SUCCESS)
{
monitor.rc = GLOBUS_FALSE;
}
else
{
monitor.rc = GLOBUS_TRUE;
}
signal_exit();
return;
}
else if(ftp_response->code == 226)
{
get_put_info = (get_put_info_t *)callback_arg;
if(get_put_info->direction == DIRECTION_PUT)
{
monitor.rc = GLOBUS_TRUE;
}
signal_exit();
}
else if(ftp_response->code == 150)
{
globus_byte_t * buf;
int buf_size;
get_put_info = (get_put_info_t *)callback_arg;
if(get_put_info->direction == DIRECTION_PUT)
{
buf = fake_file_get_chunk(get_put_info->fake_file, &buf_size);
result = globus_ftp_control_data_write(
handle,
buf,
buf_size,
0,
fake_file_is_eof(get_put_info->fake_file),
eb_write_data_callback,
(void *) get_put_info);
if(result != GLOBUS_SUCCESS)
{
verbose_printf(1, "intial write register failed\n");
monitor.rc = GLOBUS_FALSE;
signal_exit();
return;
}
}
else if(get_put_info->direction == DIRECTION_GET)
{
buf = globus_malloc(CHUNK_SIZE);
result = globus_ftp_control_data_read(
handle,
buf,
CHUNK_SIZE,
read_data_callback,
(void *) get_put_info);
if(result != GLOBUS_SUCCESS)
{
verbose_printf(1, "intial read register failed\n");
monitor.rc = GLOBUS_FALSE;
signal_exit();
return;
}
}
}
else if(ftp_response->code == 200)
{
result = globus_ftp_control_local_type(
handle,
GLOBUS_FTP_CONTROL_TYPE_IMAGE,
0);
if(result == GLOBUS_SUCCESS)
{
monitor.rc = GLOBUS_TRUE;
}
else
{
verbose_printf(1, "local_type failed\n");
monitor.rc = GLOBUS_FALSE;
}
signal_exit();
}
else if(ftp_response->code == 250)
{
verbose_printf(2, "delete succeded\n");
signal_exit();
}
else if(ftp_response->code >= 500)
{
verbose_printf(1, "error %s\n", ftp_response->response_buffer);
signal_exit();
}
else
{
verbose_printf(1, "error %d %s\n", ftp_response->code, ftp_response->response_buffer);
signal_exit();
}
}
void
signal_exit()
{
verbose_printf(2, "signal_exit() : start\n");
globus_mutex_lock(&monitor.mutex);
{
monitor.done = GLOBUS_TRUE;
monitor.count++;
globus_cond_signal(&monitor.cond);
}
globus_mutex_unlock(&monitor.mutex);
}
static
void
eb_write_data_callback(
void * callback_arg,
globus_ftp_control_handle_t * handle,
globus_object_t * error,
globus_byte_t * buffer,
globus_size_t length,
globus_off_t offset,
globus_bool_t eof)
{
get_put_info_t * get_put_info;
globus_byte_t * buf;
int buf_size;
globus_result_t result;
get_put_info = (get_put_info_t *)callback_arg;
if(error != GLOBUS_NULL)
{
monitor.rc = GLOBUS_FALSE;
signal_exit();
signal_exit();
return;
}
if(!eof)
{
buf = fake_file_get_chunk(get_put_info->fake_file, &buf_size);
result = globus_ftp_control_data_write(
handle,
buf,
buf_size,
offset + length,
fake_file_is_eof(get_put_info->fake_file),
eb_write_data_callback,
(void *) get_put_info);
if(result != GLOBUS_SUCCESS)
{
monitor.rc = GLOBUS_FALSE;
signal_exit();
signal_exit();
return;
}
}
else
{
signal_exit();
}
}
void
read_data_callback(
void * callback_arg,
globus_ftp_control_handle_t * handle,
globus_object_t * error,
globus_byte_t * buffer,
globus_size_t length,
globus_off_t offset,
globus_bool_t eof)
{
get_put_info_t * get_put_info;
globus_result_t result;
get_put_info = (get_put_info_t *)callback_arg;
if(error != GLOBUS_NULL)
{
monitor.rc = GLOBUS_FALSE;
signal_exit();
signal_exit();
return;
}
if(!fake_file_cmp(get_put_info->fake_file, buffer, offset, length))
{
monitor.rc = GLOBUS_FALSE;
verbose_printf(1, "file compare failed\n");
signal_exit();
signal_exit();
return;
}
if(eof)
{
verbose_printf(2, "read eof hit\n");
signal_exit();
globus_free(buffer);
}
else
{
verbose_printf(2, "register next read\n");
get_put_info = (get_put_info_t *)callback_arg;
result = globus_ftp_control_data_read(
handle,
buffer,
CHUNK_SIZE,
read_data_callback,
(void *) get_put_info);
if(result != GLOBUS_SUCCESS)
{
verbose_printf(1, "intial read register failed\n");
monitor.rc = GLOBUS_FALSE;
signal_exit();
return;
}
}
}
|