* @license BSD */ namespace Domframework\Tests; use Domframework\Outputdl; /** * Test the Outputdl.php file */ class OutputdlTest extends \PHPUnit_Framework_TestCase { public function testOutputdlInit() { exec("rm -f /tmp/testDFWoutputDL*"); file_put_contents( "/tmp/testDFWoutputDL", str_repeat( "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\n", 1000 ) ); symlink("/etc/passwd", "/tmp/testDFWoutputDL2"); symlink("/tmp/testDFWoutputDL", "/tmp/testDFWoutputDL3"); } public function testOutputdl1() { // Check the full download content $outputdl = new Outputdl(); $this->expectOutputString(str_repeat( "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\n", 1000 )); $outputdl->downloadFile("/tmp/testDFWoutputDL"); } public function testOutputdlAnnounce1() { // Check the announce of Resume mode Enabled $outputdl = new Outputdl(); $outputdl->downloadFile("/tmp/testDFWoutputDL"); $res = $outputdl->headers(); $this->assertSame(in_array("Accept-Ranges: bytes", $res), true); } public function testOutputdlAnnounce2() { // Check the announce of Resume mode Disabled $outputdl = new Outputdl(); $outputdl->resumeAllow(false); $outputdl->downloadFile("/tmp/testDFWoutputDL"); $res = $outputdl->headers(); $this->assertSame(in_array("Accept-Ranges: none", $res), true); } public function testOutputdlPartial1() { // Check the content get with provided range $outputdl = new Outputdl(); $_SERVER["HTTP_RANGE"] = "bytes=3-9"; $this->expectOutputString("4567890"); $outputdl->downloadFile("/tmp/testDFWoutputDL"); unset($_SERVER["HTTP_RANGE"]); } public function testOutputdlPartial2() { // Check the content get with provided range $outputdl = new Outputdl(); $_SERVER["HTTP_RANGE"] = "bytes=3-"; $this->expectOutputString(substr(str_repeat( "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\n", 1000 ), 3)); $outputdl->downloadFile("/tmp/testDFWoutputDL"); unset($_SERVER["HTTP_RANGE"]); } public function testOutputdlPartial3() { // Check the content get with provided range $outputdl = new Outputdl(); $_SERVER["HTTP_RANGE"] = "bytes=0-"; $this->expectOutputString(str_repeat( "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\n", 1000 )); $outputdl->downloadFile("/tmp/testDFWoutputDL"); unset($_SERVER["HTTP_RANGE"]); } public function testOutputdlPartial4() { // Check the content get with provided range $outputdl = new Outputdl(); $_SERVER["HTTP_RANGE"] = "bytes=-5"; $this->expectOutputString("wxyz\n"); $outputdl->downloadFile("/tmp/testDFWoutputDL"); unset($_SERVER["HTTP_RANGE"]); } public function testOutputdlPartial5() { // Check the content get with provided range $outputdl = new Outputdl(); $_SERVER["HTTP_RANGE"] = "bytes=0-3,6-9"; $this->expectOutputString("--Qm91bmRhcnk=\r Content-Range: bytes 0-3/63000\r Content-Type: application/octet-stream\r \r 1234\r --Qm91bmRhcnk=\r Content-Range: bytes 6-9/63000\r Content-Type: application/octet-stream\r \r 7890\r --Qm91bmRhcnk=--\r "); $outputdl->downloadFile("/tmp/testDFWoutputDL"); unset($_SERVER["HTTP_RANGE"]); } public function testOutputdlPartialWrong1() { // Check the invalid provided range unset($_SERVER["HTTP_RANGE"]); $outputdl = new Outputdl(); $_SERVER["HTTP_RANGE"] = "bytes=99999-"; $this->expectException("Exception", "Invalid range provided", 416); $outputdl->downloadFile("/tmp/testDFWoutputDL"); } public function testOutputdlPartialWrong2() { // Check the invalid provided range unset($_SERVER["HTTP_RANGE"]); $outputdl = new Outputdl(); $_SERVER["HTTP_RANGE"] = "bytes=9-3"; $this->expectException("Exception", "Invalid range provided", 416); $outputdl->downloadFile("/tmp/testDFWoutputDL"); } public function testOutputdlPartialWrong3() { // Check the invalid provided range unset($_SERVER["HTTP_RANGE"]); $outputdl = new Outputdl(); $_SERVER["HTTP_RANGE"] = "bytes=9-999999"; $this->expectException("Exception", "Invalid range provided", 416); $outputdl->downloadFile("/tmp/testDFWoutputDL"); } public function testOutputdlPartialWrong4() { // Check the invalid provided range unset($_SERVER["HTTP_RANGE"]); $outputdl = new Outputdl(); $_SERVER["HTTP_RANGE"] = "bytes=-"; $this->expectException("Exception", "Invalid range provided", 416); $outputdl->downloadFile("/tmp/testDFWoutputDL"); } public function testOutputdlInvalidBase1() { // Check the base comparison : OK unset($_SERVER["HTTP_RANGE"]); $outputdl = new Outputdl(); $outputdl->base("/tmp"); $this->expectOutputString(str_repeat( "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\n", 1000 )); $outputdl->downloadFile("/tmp/testDFWoutputDL"); } public function testOutputdlInvalidBase2() { // Check the base comparison : BAD unset($_SERVER["HTTP_RANGE"]); $outputdl = new Outputdl(); $outputdl->base("/tmp"); $this->expectException( "Exception", "Invalid file to download : out of base", 406 ); $outputdl->downloadFile("../../../../../..//etc/passwd"); } public function testOutputdlInvalidFile1() { // Check the base comparison : BAD Symlink unset($_SERVER["HTTP_RANGE"]); $outputdl = new Outputdl(); $outputdl->base("/tmp"); $this->expectException( "Exception", "Invalid file to download : not a file", 406 ); $outputdl->downloadFile("/tmp/testDFWoutputDL3"); } public function testOutputdlInvalidFile2() { // Check the base comparison : Non existing unset($_SERVER["HTTP_RANGE"]); $outputdl = new Outputdl(); $outputdl->base("/tmp"); $this->expectException( "Exception", "Invalid file to download : file doesn't exists", 404 ); $outputdl->downloadFile("/tmp/testDFWoutputNON"); } }