mirror of
https://github.com/php/php-src.git
synced 2024-12-13 20:05:26 +08:00
MFB
This commit is contained in:
parent
75f45d8458
commit
d12df980e5
36
ext/ftp/tests/001.phpt
Normal file
36
ext/ftp/tests/001.phpt
Normal file
@ -0,0 +1,36 @@
|
||||
--TEST--
|
||||
FTP login
|
||||
--SKIPIF--
|
||||
<?php
|
||||
require 'skipif.inc';
|
||||
?>
|
||||
--FILE--
|
||||
<?php
|
||||
require 'server.inc';
|
||||
|
||||
$ftp = ftp_connect('127.0.0.1', $port);
|
||||
if (!$ftp) die("Couldn't connect to the server");
|
||||
|
||||
var_dump(ftp_login($ftp, 'user', 'pass'));
|
||||
var_dump(ftp_raw($ftp, 'HELP'));
|
||||
var_dump(ftp_raw($ftp, 'HELP HELP'));
|
||||
|
||||
var_dump(ftp_close($ftp));
|
||||
?>
|
||||
--EXPECT--
|
||||
bool(true)
|
||||
array(4) {
|
||||
[0]=>
|
||||
string(55) "214-There is help available for the following commands:"
|
||||
[1]=>
|
||||
string(5) " USER"
|
||||
[2]=>
|
||||
string(5) " HELP"
|
||||
[3]=>
|
||||
string(15) "214 end of list"
|
||||
}
|
||||
array(1) {
|
||||
[0]=>
|
||||
string(39) "214 Syntax: HELP [<SP> <string>] <CRLF>"
|
||||
}
|
||||
bool(true)
|
38
ext/ftp/tests/002.phpt
Normal file
38
ext/ftp/tests/002.phpt
Normal file
@ -0,0 +1,38 @@
|
||||
--TEST--
|
||||
FTP login (SSL)
|
||||
--SKIPIF--
|
||||
<?php
|
||||
$ssl = 1;
|
||||
require 'skipif.inc';
|
||||
?>
|
||||
--FILE--
|
||||
<?php
|
||||
$ssl = 1;
|
||||
require 'server.inc';
|
||||
|
||||
$ftp = ftp_ssl_connect('127.0.0.1', $port);
|
||||
if (!$ftp) die("Couldn't connect to the server");
|
||||
|
||||
var_dump(ftp_login($ftp, 'user', 'pass'));
|
||||
var_dump(ftp_raw($ftp, 'HELP'));
|
||||
var_dump(ftp_raw($ftp, 'HELP HELP'));
|
||||
|
||||
var_dump(ftp_close($ftp));
|
||||
?>
|
||||
--EXPECT--
|
||||
bool(true)
|
||||
array(4) {
|
||||
[0]=>
|
||||
string(55) "214-There is help available for the following commands:"
|
||||
[1]=>
|
||||
string(5) " USER"
|
||||
[2]=>
|
||||
string(5) " HELP"
|
||||
[3]=>
|
||||
string(15) "214 end of list"
|
||||
}
|
||||
array(1) {
|
||||
[0]=>
|
||||
string(39) "214 Syntax: HELP [<SP> <string>] <CRLF>"
|
||||
}
|
||||
bool(true)
|
43
ext/ftp/tests/003.phpt
Normal file
43
ext/ftp/tests/003.phpt
Normal file
@ -0,0 +1,43 @@
|
||||
--TEST--
|
||||
FTP cwd
|
||||
--SKIPIF--
|
||||
<?php
|
||||
require 'skipif.inc';
|
||||
?>
|
||||
--FILE--
|
||||
<?php
|
||||
require 'server.inc';
|
||||
|
||||
$ftp = ftp_connect('127.0.0.1', $port);
|
||||
if (!$ftp) die("Couldn't connect to the server");
|
||||
|
||||
var_dump(ftp_login($ftp, 'user', 'pass'));
|
||||
|
||||
var_dump(ftp_pwd($ftp));
|
||||
|
||||
var_dump(ftp_chdir($ftp, 'mydir'));
|
||||
var_dump(ftp_pwd($ftp));
|
||||
|
||||
var_dump(ftp_chdir($ftp, '/xpto/mydir'));
|
||||
var_dump(ftp_pwd($ftp));
|
||||
|
||||
var_dump(ftp_cdup($ftp));
|
||||
var_dump(ftp_pwd($ftp));
|
||||
|
||||
var_dump(ftp_chdir($ftp, '..'));
|
||||
var_dump(ftp_pwd($ftp));
|
||||
|
||||
var_dump(ftp_close($ftp));
|
||||
?>
|
||||
--EXPECT--
|
||||
bool(true)
|
||||
string(1) "/"
|
||||
bool(true)
|
||||
string(6) "/mydir"
|
||||
bool(true)
|
||||
string(11) "/xpto/mydir"
|
||||
bool(true)
|
||||
string(5) "/xpto"
|
||||
bool(true)
|
||||
string(1) "/"
|
||||
bool(true)
|
48
ext/ftp/tests/cert.pem
Normal file
48
ext/ftp/tests/cert.pem
Normal file
@ -0,0 +1,48 @@
|
||||
-----BEGIN CERTIFICATE REQUEST-----
|
||||
MIIBmzCCAQQCAQAwWzELMAkGA1UEBhMCUFQxCzAJBgNVBAgTAkx4MQswCQYDVQQH
|
||||
EwJMeDEcMBoGA1UEChMTQSBtaW5oYSBlbXByZXNhLCBTQTEUMBIGA1UECxMLUEhQ
|
||||
IFFBIFRlYW0wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAM9mfEOSYwXf58ch
|
||||
4NyO1QOU1XMfquz8OVpvMUITABLAevZpeQn6vZPHNyXHFQC0QC8scydK1rAYd2U+
|
||||
9K2aPub6ioMjYyjPpAE07l9EAAPUEBlqqsziB/wT8QjWkByyJEkYu+o0Wyjokhfn
|
||||
BMPvm52wLWUx9nvUeNDCftnKg1wxAgMBAAGgADANBgkqhkiG9w0BAQQFAAOBgQDD
|
||||
s1FeqPxnF2bWj8/dG8MyPaRfOAMVz1UsCZUciXIVG5LSIvR2qnMC3iEYt3s13sEq
|
||||
z8VJlNHa8nniE+VFNv093yIu+PlWXMEvb5y5EFqP2AYq3RAT+SJsSxGqIdzPZiKY
|
||||
INaktLCZmQ/E1v7/4hFzVRq9ydJI82DVS1nv282Whw==
|
||||
-----END CERTIFICATE REQUEST-----
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIIC4zCCAkygAwIBAgIBADANBgkqhkiG9w0BAQQFADBbMQswCQYDVQQGEwJQVDEL
|
||||
MAkGA1UECBMCTHgxCzAJBgNVBAcTAkx4MRwwGgYDVQQKExNBIG1pbmhhIGVtcHJl
|
||||
c2EsIFNBMRQwEgYDVQQLEwtQSFAgUUEgVGVhbTAeFw0wNjExMTkxODIzNTNaFw0w
|
||||
NzExMTkxODIzNTNaMFsxCzAJBgNVBAYTAlBUMQswCQYDVQQIEwJMeDELMAkGA1UE
|
||||
BxMCTHgxHDAaBgNVBAoTE0EgbWluaGEgZW1wcmVzYSwgU0ExFDASBgNVBAsTC1BI
|
||||
UCBRQSBUZWFtMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDPZnxDkmMF3+fH
|
||||
IeDcjtUDlNVzH6rs/DlabzFCEwASwHr2aXkJ+r2TxzclxxUAtEAvLHMnStawGHdl
|
||||
PvStmj7m+oqDI2Moz6QBNO5fRAAD1BAZaqrM4gf8E/EI1pAcsiRJGLvqNFso6JIX
|
||||
5wTD75udsC1lMfZ71HjQwn7ZyoNcMQIDAQABo4G2MIGzMB0GA1UdDgQWBBTIga5L
|
||||
q+Ub1SWXgNZRYCpq3c8Z+jCBgwYDVR0jBHwweoAUyIGuS6vlG9Ull4DWUWAqat3P
|
||||
GfqhX6RdMFsxCzAJBgNVBAYTAlBUMQswCQYDVQQIEwJMeDELMAkGA1UEBxMCTHgx
|
||||
HDAaBgNVBAoTE0EgbWluaGEgZW1wcmVzYSwgU0ExFDASBgNVBAsTC1BIUCBRQSBU
|
||||
ZWFtggEAMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEEBQADgYEAe6AA8aC3KDI8
|
||||
smd+7XWjaTSp1Q0uMkEZ2PEBzif2I1aPPqw1CQykJ1iDdC/8PJ1yEIezloP2XQoZ
|
||||
NjTaCO+uubay03ncoPTZvDUwExN9BYFAYgc2z3tLMHYbA7kM2sIbKys7ZQegLibr
|
||||
TSKYQOBeYA/FB9GHECJGU3zBRvYi+Og=
|
||||
-----END CERTIFICATE-----
|
||||
-----BEGIN RSA PRIVATE KEY-----
|
||||
Proc-Type: 4,ENCRYPTED
|
||||
DEK-Info: DES-EDE3-CBC,928762DB6DE222AD
|
||||
|
||||
oOxNUBX0wrqmRqb3IEZMogc1bnVm6JoW6YFjGfHNcIz0jS7UPDhUFDR26y0dYujL
|
||||
LEgxOcYo8ItvGcXSRbs+3W7lISbosgkB0DOaKx5jVmOGwUVRergUUSY8rbf93FtP
|
||||
27CEvAfsU6do5HmlJ34mYZW1k+onCznlJXJkupQ5jmiily3GwEdr/5mMIVOmXQ6p
|
||||
xWkxHySDKyVbR0v4JY3SJLRBuhgofYNG5155PiqZ7KwYY4Aw60eVgINsvJCF9/8b
|
||||
kEj+lecHbBdAf7N82320Ga+F+VeFnUl0gWFjoIF9UFCO80+7ZvIGdGlyPkr4zMvt
|
||||
TsC1snJQdHg+IlT3sGayYrQANpTG6GPYhn3KEvK5aqq+bPEe5lija0gw34jbPCo+
|
||||
TjHR76lToxzubGZODyyF/rjl5KwUbqTCNuv1PX1jTx7n7sCbu+KHpqXMhTHLKtby
|
||||
+Wh7WAfsVrbIW+P85/mkfhPbPZ2621f9cyStdFGgWU4dHdD00HIGOgAJvUSbC2Au
|
||||
oVUoKf2818t1s9aA4ptog04sNi+Ixu+z+3yYNLZj51j4ZX3KuXxLIiQvlvFQ8LQi
|
||||
RHGQk3u2W3iNtDKKUQjMPaB2FlVtC7FmtHBCpRmos6ld240DDyucqMdIDTMaqV0+
|
||||
sL4X+LIeBM/hP/IquRTuQBHBmgjkN4845ihTUJOanyKx605ANq/roHzXrbIxhR5p
|
||||
pcJLCBMLMWgdOCJMZRavSq04iXeNfP6Mk/joVpHS62Ljdfc94BBLfsOKOErA20Nq
|
||||
lfvbZqy2tI5IIDoq05S8FU0DYNqq/hyrv9Udo8IAo+WkBOABm0x/WA==
|
||||
-----END RSA PRIVATE KEY-----
|
||||
|
160
ext/ftp/tests/server.inc
Normal file
160
ext/ftp/tests/server.inc
Normal file
@ -0,0 +1,160 @@
|
||||
<?php
|
||||
|
||||
$socket = null;
|
||||
$errno = 0;
|
||||
$context = stream_context_create(array('ssl' => array('local_cert' => dirname(__FILE__).'/cert.pem', 'passphrase' => 'pass')));
|
||||
|
||||
for ($i=0; $i<10 && !$socket; ++$i) {
|
||||
$port = rand(50000, 65535);
|
||||
$socket = stream_socket_server("tcp://127.0.0.1:$port", $errno, $errstr, STREAM_SERVER_BIND|STREAM_SERVER_LISTEN, $context);
|
||||
}
|
||||
|
||||
if (!$socket) {
|
||||
die("could not start/bind the ftp server\n");
|
||||
}
|
||||
|
||||
$pid = pcntl_fork();
|
||||
|
||||
if ($pid) {
|
||||
|
||||
function dump_and_exit($buf)
|
||||
{
|
||||
var_dump($buf);
|
||||
fclose($GLOBALS['s']);
|
||||
exit;
|
||||
}
|
||||
|
||||
function anonymous()
|
||||
{
|
||||
return $GLOBALS['user'] === 'anonymous';
|
||||
}
|
||||
|
||||
/* quick&dirty realpath() like function */
|
||||
function change_dir($dir)
|
||||
{
|
||||
global $cwd;
|
||||
|
||||
if ($dir[0] == '/') {
|
||||
$cwd = $dir;
|
||||
return;
|
||||
}
|
||||
|
||||
$cwd = "$cwd/$dir";
|
||||
|
||||
do {
|
||||
$old = $cwd;
|
||||
$cwd = preg_replace('@/?[^/]+/\.\.@', '', $cwd);
|
||||
} while ($old != $cwd);
|
||||
|
||||
$cwd = strtr($cwd, array('//' => '/'));
|
||||
if (!$cwd) $cwd = '/';
|
||||
}
|
||||
|
||||
|
||||
$s = stream_socket_accept($socket);
|
||||
if (!$s) die("Error accepting a new connection\n");
|
||||
|
||||
fputs($s, "220----- PHP FTP server 0.3 -----\r\n220 Service ready\r\n");
|
||||
$buf = fread($s, 2048);
|
||||
|
||||
if (!empty($ssl)) {
|
||||
if ($buf !== "AUTH TLS\r\n") {
|
||||
fputs($s, "500 Syntax error, command unrecognized.\r\n");
|
||||
dump_and_exit($buf);
|
||||
}
|
||||
|
||||
fputs($s, "234 auth type accepted\r\n");
|
||||
|
||||
if (!stream_socket_enable_crypto($s, true, STREAM_CRYPTO_METHOD_SSLv23_SERVER)) {
|
||||
die("SSLv23 handshake failed.\n");
|
||||
}
|
||||
|
||||
if (!preg_match('/^PBSZ \d+\r\n$/', $buf = fread($s, 2048))) {
|
||||
fputs($s, "501 bogus data\r\n");
|
||||
dump_and_exit($buf);
|
||||
}
|
||||
|
||||
fputs($s, "200 OK\r\n");
|
||||
$buf = fread($s, 2048);
|
||||
|
||||
if ($buf !== "PROT P\r\n") {
|
||||
fputs($s, "504 Wrong protection.\r\n");
|
||||
dump_and_exit($buf);
|
||||
}
|
||||
|
||||
fputs($s, "200 OK\r\n");
|
||||
|
||||
$buf = fread($s, 2048);
|
||||
}
|
||||
|
||||
if (!preg_match('/^USER (\w+)\r\n$/', $buf, $m)) {
|
||||
fputs($s, "500 Syntax error, command unrecognized.\r\n");
|
||||
dump_and_exit($buf);
|
||||
}
|
||||
$user = $m[1];
|
||||
if ($user !== 'user' && $user !== 'anonymous') {
|
||||
fputs($s, "530 Not logged in.\r\n");
|
||||
fclose($s);
|
||||
exit;
|
||||
}
|
||||
|
||||
if (anonymous()) {
|
||||
fputs($s, "230 Anonymous user logged in\r\n");
|
||||
|
||||
} else {
|
||||
fputs($s, "331 User name ok, need password\r\n");
|
||||
|
||||
if (!preg_match('/^PASS (\w+)\r\n$/', $buf = fread($s, 100), $m)) {
|
||||
fputs($s, "500 Syntax error, command unrecognized.\r\n");
|
||||
dump_and_exit($buf);
|
||||
}
|
||||
|
||||
$pass = $m[1];
|
||||
if ($pass === 'pass') {
|
||||
fputs($s, "230 User logged in\r\n");
|
||||
} else {
|
||||
fputs($s, "530 Not logged in.\r\n");
|
||||
fclose($s);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
$cwd = '/';
|
||||
|
||||
while($buf = fread($s, 4098)) {
|
||||
|
||||
if ($buf === "HELP\r\n") {
|
||||
fputs($s, "214-There is help available for the following commands:\r\n");
|
||||
fputs($s, " USER\r\n");
|
||||
fputs($s, " HELP\r\n");
|
||||
fputs($s, "214 end of list\r\n");
|
||||
|
||||
} elseif ($buf === "HELP HELP\r\n") {
|
||||
fputs($s, "214 Syntax: HELP [<SP> <string>] <CRLF>\r\n");
|
||||
|
||||
} elseif ($buf === "PWD\r\n") {
|
||||
fputs($s, "257 \"$cwd\" is current directory.\r\n");
|
||||
|
||||
} elseif ($buf === "CDUP\r\n") {
|
||||
change_dir('..');
|
||||
fputs($s, "250 CDUP command successful.\r\n");
|
||||
|
||||
} elseif ($buf === "QUIT\r\n") {
|
||||
break;
|
||||
|
||||
} elseif (preg_match("~^CWD ([A-Za-z./]+)\r\n$~", $buf, $m)) {
|
||||
change_dir($m[1]);
|
||||
fputs($s, "250 CWD command successful.\r\n");
|
||||
|
||||
} else {
|
||||
fputs($s, "500 Syntax error, command unrecognized.\r\n");
|
||||
dump_and_exit($buf);
|
||||
}
|
||||
}
|
||||
|
||||
fclose($s);
|
||||
exit;
|
||||
}
|
||||
|
||||
fclose($socket);
|
||||
?>
|
5
ext/ftp/tests/skipif.inc
Normal file
5
ext/ftp/tests/skipif.inc
Normal file
@ -0,0 +1,5 @@
|
||||
<?php
|
||||
if (!extension_loaded('ftp')) die('skip ftp extension not loaded');
|
||||
if (!function_exists('pcntl_fork')) die('skip fork not available');
|
||||
if (!empty($ssl) && !extension_loaded('openssl')) die('skip openssl extension not loaded');
|
||||
?>
|
Loading…
Reference in New Issue
Block a user