Skip to content

Commit 3fff948

Browse files
author
Zoe Slattery
committed
New FTP test from Nathaniel McHugh
1 parent 435cf74 commit 3fff948

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

ext/ftp/tests/ftp_get_basic.phpt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
--TEST--
2+
FTP ftp_get file for both binary and ASCII transfer modes
3+
--CREDITS--
4+
Nathaniel McHugh
5+
--SKIPIF--
6+
<?php
7+
require 'skipif.inc';
8+
?>
9+
--FILE--
10+
<?php
11+
require 'server.inc';
12+
13+
$ftp = ftp_connect('127.0.0.1', $port);
14+
if (!$ftp) die("Couldn't connect to the server");
15+
16+
var_dump(ftp_login($ftp, 'user', 'pass'));
17+
18+
//test simple text transfer
19+
$tmpfname = tempnam(".", "ftp_test");
20+
var_dump(ftp_get($ftp, $tmpfname ,'a story.txt', FTP_ASCII));
21+
echo file_get_contents($tmpfname);
22+
23+
//test binary data transfer
24+
$tmpfname = tempnam(".", "ftp_test");
25+
var_dump(ftp_get($ftp, $tmpfname, 'binary data.bin', FTP_BINARY));
26+
var_dump(urlencode(file_get_contents($tmpfname)));
27+
unlink($tmpfname);
28+
29+
//test non-existant file request
30+
ftp_get($ftp, $tmpfname ,'a warning.txt', FTP_ASCII);
31+
?>
32+
--EXPECTF--
33+
bool(true)
34+
bool(true)
35+
For sale: baby shoes, never worn.
36+
bool(true)
37+
string(21) "BINARYFoo%00Bar%0D%0A"
38+
39+
Warning: ftp_get(): a warning: No such file or directory in %sftp_get_basic.php on line %d

ext/ftp/tests/server.inc

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ $cwd = '/';
136136
$num_bogus_cmds = 0;
137137

138138
while($buf = fread($s, 4098)) {
139-
140139
if (!empty($bogus)) {
141140
fputs($s, "502 Command not implemented (".$num_bogus_cmds++.").\r\n");
142141

@@ -262,7 +261,29 @@ while($buf = fread($s, 4098)) {
262261
fputs($s, "550 No file named \"{$matches [1]}\"\r\n");
263262
break;
264263
}
265-
}
264+
}elseif (preg_match('/^RETR ([\w\h]+)/', $buf, $matches)) {
265+
if (!$fs = stream_socket_client("tcp://$host:$port")) {
266+
fputs($s, "425 Can't open data connection\r\n");
267+
continue;
268+
}
269+
switch($matches[1]){
270+
case "a story":
271+
fputs($s, "150 File status okay; about to open data connection.\r\n");
272+
fputs($fs, "For sale: baby shoes, never worn.\r\n");
273+
fputs($s, "226 Closing data Connection.\r\n");
274+
break;
275+
case "binary data":
276+
fputs($s, "150 File status okay; about to open data connection.\r\n");
277+
$transfer_type = $ascii? 'ASCII' : 'BINARY' ;
278+
fputs($fs, $transfer_type."Foo\0Bar\r\n");
279+
fputs($s, "226 Closing data Connection.\r\n");
280+
break;
281+
default:
282+
fputs($s, "550 {$matches[1]}: No such file or directory \r\n");
283+
break;
284+
}
285+
fclose($fs);
286+
}
266287
else {
267288
fputs($s, "500 Syntax error, command unrecognized.\r\n");
268289
dump_and_exit($buf);

0 commit comments

Comments
 (0)