繁体   English   中英

PHPUnit-使用realpath()的测试函数

[英]PHPUnit - testing function which uses realpath()

我有一个检查用户提供的文件名是否在/ opt /目录下的函数。 它使用realpath,因此出于安全原因,用户无法传递“ ../etc/passwd”之类的内容。

function check_file_path($relative_filename) {
    $abspath = realpath('/opt/' . $relative_filename);
    return ($abspath && strpos($abspath , '/opt/') === 0);
}

现在,如果文件在fs上不存在,则realpath将返回false。 当PHPUnit运行时(在开发机上,而不在服务器上),该路径不存在,因此我们无法进行肯定的测试。

我研究了vfsstream,但与realpath()配合使用效果不佳

org\bovigo\vfs\vfsStream::setup('home');
$file = org\bovigo\vfs\vfsStream::url('home/test.txt');
file_put_contents($file, "The new contents of the file");
$abspath = realpath('vfs://home/test.txt');
// $abspath will be false

有什么建议可以模拟文件系统以使realpath正常工作吗?

  1. 您可以使用runkit 它是php的扩展。

  2. 但是,如果您的函数在任何名称空间中调用,则可以在该名称空间中定义自己的函数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM