diff options
author | Stevan Radakovic <stevan.radakovic@linaro.org> | 2012-05-09 10:08:14 +0200 |
---|---|---|
committer | Stevan Radakovic <stevan.radakovic@linaro.org> | 2012-05-09 10:08:14 +0200 |
commit | 716594af8fddd535a0c36a9daf60c791cd010ac1 (patch) | |
tree | e64b35d18a8440cf338be8e3b800174b8062a60c | |
parent | b9189031ed4bf4255c70ab3e71afee12a471253c (diff) | |
parent | 0c6c9a4bcfd445f4cd3de03d22371b8707eda68b (diff) |
License protection unit tests implemented, also light refactoring of license.php. Reviewed by danilo, gesha.
-rw-r--r-- | README | 17 | ||||
-rw-r--r-- | licenses/LicenseHelper.php | 102 | ||||
-rw-r--r-- | licenses/license.php | 127 | ||||
-rw-r--r-- | testing/LicenseHelperTest.php | 167 |
4 files changed, 304 insertions, 109 deletions
@@ -82,3 +82,20 @@ Python and Apache2: $ testr init $ testr run + + +Tests for PHP license-matching logic +.................................... + +There's currently only one unit test file, LicenseHelperTest.php under testing directory. You first need to install the phpunit package from ubuntu repos: + $ sudo apt-get install php-unit + + +Recent (as of 2012-05-08) Ubuntu/Debian releases have a broken phpunit package and thus require installation in a different manner: + $ sudo apt-get install php-pear + $ sudo pear config-set auto_discover 1 + $ sudo pear install pear.phpunit.de/PHPUnit + +Then to run the test from the 'testing' directory: + $ cd testing/ + $ phpunit LicenseHelperTest diff --git a/licenses/LicenseHelper.php b/licenses/LicenseHelper.php new file mode 100644 index 0000000..b55ac49 --- /dev/null +++ b/licenses/LicenseHelper.php @@ -0,0 +1,102 @@ +<?php + +class LicenseHelper +{ + + /** + * Get list of files into array to process them later. + * Used to find special licenses and dirs with only subdirs. + */ + public static function checkFile($fn) + { + if (is_file($fn) or is_link($fn)) { + return true; + } + return false; + } + + /** + * Get list of filenames from a directory + */ + public static function getFilesList($dirname) + { + if (!is_dir($dirname)) { + throw new InvalidArgumentException('Method argument '. + 'should be a directory path'); + } + + $files = array(); + if ($handle = opendir($dirname)) { + while ($handle && false !== ($entry = readdir($handle))) { + if ($entry != "." && $entry != ".." && + !is_dir($dirname."/".$entry) && $entry != "HEADER.html") { + $files[] = $entry; + } + } + } + closedir($handle); + return $files; + } + + /** + * Find a matching filename in an array from given filename template. + */ + public static function findFileByPattern($fl, $pattern) + { + if (!empty($fl)) { + foreach ($fl as $f) { + if (preg_match($pattern, $f, $matches)) { + return $f; + } + } + } + return false; + } + + /** + * Get license theme name from EULA filename. + */ + public static function getTheme($eula, $down) + { + if ($eula != 'EULA.txt') { // Special EULA file was found + $theme = array_pop(explode(".", $eula)); + } else { // No special EULA file was found + $eula = "EULA.txt"; + if (preg_match("/.*snowball.*/", $down)) { + $theme = "ste"; + } elseif (preg_match("/.*origen.*/", $down)) { + $theme = "samsung"; + } else { + $theme = "linaro"; + } + } + return $theme; + } + + public static function status_forbidden($dir) + { + header("Status: 403"); + header("HTTP/1.1 403 Forbidden"); + echo "<h1>Forbidden</h1>"; + echo "You don't have permission to access ".$dir." on this server."; + exit; + } + + public static function status_ok($dir, $domain) + { + header("Status: 200"); + header("Location: ".$dir); + setcookie("redirectlicensephp", "yes", 0, "/", ".".$domain); + exit; + } + + public static function status_not_found() + { + header("Status: 404"); + header("HTTP/1.0 404 Not Found"); + echo "<h1>404 Not Found</h1>"; + echo "The requested URL was not found on this server."; + exit; + } + +}
\ No newline at end of file diff --git a/licenses/license.php b/licenses/license.php index ac4e279..aac10de 100644 --- a/licenses/license.php +++ b/licenses/license.php @@ -1,97 +1,6 @@ <?php -// Get list of files into array to process them later. -// Used to find special licenses and dirs with only subdirs. -function check_file($fn) -{ - if (is_file($fn) or is_link($fn)) { - return true; - } - return false; -} - -function getFilesList($dirname) -{ - $files = array(); - if ($handle = opendir($dirname)) { - while ($handle && false !== ($entry = readdir($handle))) { - if ($entry != "." && $entry != ".." && !is_dir($dirname."/".$entry) && $entry != "HEADER.html") { - $files[] = $entry; - } - } - } - closedir($handle); - return $files; -} - -// Get array of file name and extension from full filename. -function splitFilename($filename) -{ - $pos = strpos($filename, '.'); - if ($pos === false) { // dot is not found in the filename - return array($filename, ''); // no extension - } else { - $basename = substr($filename, 0, $pos); - $extension = substr($filename, $pos+1); - return array($basename, $extension); - } -} - -// Find special EULA based on filename template. -function findSpecialEULA($fl, $pattern) -{ - if (!empty($fl)) { - foreach ($fl as $f) { - if (preg_match($pattern, $f, $matches)) { - return $f; - } - } - } - return false; -} - -// Get license theme name from EULA filename. -function getTheme($eula, $down) -{ - if ($eula != 'EULA.txt') { // Special EULA file was found - $theme = array_pop(explode(".", $eula)); - } else { // No special EULA file was found - $eula = "EULA.txt"; - if (preg_match("/.*snowball.*/", $down)) { - $theme = "ste"; - } elseif (preg_match("/.*origen.*/", $down)) { - $theme = "samsung"; - } else { - $theme = "linaro"; - } - } - return $theme; -} -function status_forbidden($dir) -{ - header("Status: 403"); - header("HTTP/1.1 403 Forbidden"); - echo "<h1>Forbidden</h1>"; - echo "You don't have permission to access ".$dir." on this server."; - exit; -} - -function status_ok($dir, $domain) -{ - header("Status: 200"); - header("Location: ".$dir); - setcookie("redirectlicensephp", "yes", 0, "/", ".".$domain); - exit; -} - -function status_not_found() -{ - header("Status: 404"); - header("HTTP/1.0 404 Not Found"); - echo "<h1>404 Not Found</h1>"; - echo "The requested URL was not found on this server."; - exit; -} +require_once("LicenseHelper.php"); $down = $_COOKIE["downloadrequested"]; $host = $_SERVER["HTTP_HOST"]; @@ -102,10 +11,10 @@ $flist = array(); $eula = ''; if (preg_match("/.*openid.*/", $fn) or preg_match("/.*restricted.*/", $fn) or preg_match("/.*private.*/", $fn)) { - status_ok($down, $domain); + LicenseHelper::status_ok($down, $domain); } -if (file_exists($fn) and check_file($fn)) { // Requested download is file +if (file_exists($fn) and LicenseHelper::checkFile($fn)) { // Requested download is file $search_dir = dirname($fn); $repl = dirname($down); $name_only = array(basename($down), ''); @@ -114,36 +23,36 @@ if (file_exists($fn) and check_file($fn)) { // Requested download is file $repl = $down; $name_only = array(); } else { // Requested download not found on server - status_not_found(); + LicenseHelper::status_not_found(); } -$flist = getFilesList($search_dir); +$flist = LicenseHelper::getFilesList($search_dir); if (!empty($name_only)) { $pattern = "/^".$name_only[0]."\.EULA\.txt.*/"; - $eula = findSpecialEULA($flist, $pattern); + $eula = LicenseHelper::findFileByPattern($flist, $pattern); } -if (check_file($fn)) { - if (check_file($doc."/".$repl."/".$eula)) { // Special EULA found - $theme = getTheme($eula, $down); - } elseif (check_file($doc."/".$repl."/EULA.txt")) { // No special EULA found - $theme = getTheme("EULA.txt", $down); - } elseif (findSpecialEULA($flist, "/.*EULA.txt.*/")) { +if (LicenseHelper::checkFile($fn)) { + if (LicenseHelper::checkFile($doc."/".$repl."/".$eula)) { // Special EULA found + $theme = LicenseHelper::getTheme($eula, $down); + } elseif (LicenseHelper::checkFile($doc."/".$repl."/EULA.txt")) { // No special EULA found + $theme = LicenseHelper::getTheme("EULA.txt", $down); + } elseif (LicenseHelper::findFileByPattern($flist, "/.*EULA.txt.*/")) { // If file is requested but no special EULA for it and no EULA.txt is present, // look for any EULA and if found decide that current file is not protected. - status_ok($down, $domain); + LicenseHelper::status_ok($down, $domain); } else { - status_forbidden($down); + LicenseHelper::status_forbidden($down); } } elseif (is_dir($fn)) { - if (empty($flist) or findSpecialEULA($flist, "/.*EULA.txt.*/")) { // Directory contains only subdirs or any EULA - status_ok($down, $domain); + if (empty($flist) or LicenseHelper::findFileByPattern($flist, "/.*EULA.txt.*/")) { // Directory contains only subdirs or any EULA + LicenseHelper::status_ok($down, $domain); } else { // No special EULA, no EULA.txt, no OPEN-EULA.txt found - status_forbidden($down); + LicenseHelper::status_forbidden($down); } } else { - status_forbidden($down); + LicenseHelper::status_forbidden($down); } $template_content = file_get_contents($doc."/licenses/".$theme.".html"); diff --git a/testing/LicenseHelperTest.php b/testing/LicenseHelperTest.php new file mode 100644 index 0000000..f47c672 --- /dev/null +++ b/testing/LicenseHelperTest.php @@ -0,0 +1,167 @@ +<?php + +require_once("../licenses/LicenseHelper.php"); + +class LicenseHelperTest extends PHPUnit_Framework_TestCase +{ + + private $temp_filename; + + /** + * Running checkFile on a directory path returns false. + */ + public function test_checkFile_nonFile() + { + $this->assertFalse(LicenseHelper::checkFile(dirname(__FILE__))); + } + + /** + * Running checkFile on a symbolic link to an existing file returns true. + */ + public function test_checkFile_link() + { + $this->temp_filename = tempnam(sys_get_temp_dir(), "unittest"); + symlink($this->temp_filename, "test_link"); + + $this->assertTrue(LicenseHelper::checkFile("test_link")); + + unlink("test_link"); + unlink($this->temp_filename); + } + + /** + * Running checkFile on a broken symbolic link returns true. + * This is because PHP function is_link returns true on broken soft links. + */ + public function test_checkFile_brokenLink() + { + $this->temp_filename = tempnam(sys_get_temp_dir(), "unittest"); + symlink($this->temp_filename, "test_link"); + unlink($this->temp_filename); + + $this->assertTrue(LicenseHelper::checkFile("test_link")); + + unlink("test_link"); + } + + /** + * Running checkFile on a regular file returns true. + */ + public function test_checkFile_file() + { + $this->assertTrue(LicenseHelper::checkFile(__FILE__)); + } + + /** + * getFileList throws an InvalidArgumentException when passed + * an argument pointing to a file. + * @expectedException InvalidArgumentException + */ + public function test_getFilesList_file() + { + $file_list = LicenseHelper::getFilesList(__FILE__); + } + + /** + * getFileList returns a list of filenames in that directory. + */ + public function test_getFilesList_dir() + { + $temp_dir_name = tempnam(sys_get_temp_dir(), "unittest"); + if (file_exists($temp_dir_name)) { + unlink($temp_dir_name); + } + mkdir($temp_dir_name); + + touch($temp_dir_name . "/unittest1"); + touch($temp_dir_name . "/unittest2"); + + $file_list = LicenseHelper::getFilesList($temp_dir_name); + $this->assertCount(2, $file_list); + + // Sort the file list, this function returns the files as they are + // written on the filesystem. + sort($file_list); + $this->assertEquals("unittest1", $file_list[0]); + $this->assertEquals("unittest2", $file_list[1]); + + unlink($temp_dir_name . "/unittest1"); + unlink($temp_dir_name . "/unittest2"); + rmdir($temp_dir_name); + } + + /** + * Running findFileByPattern on an array without matches returns false. + */ + public function test_findFileByPattern_noMatch() + { + $file_list = array("test.txt", "new_file.pdf"); + $pattern = "/^abc/"; + $this->assertFalse( + LicenseHelper::findFileByPattern($file_list, $pattern)); + } + + /** + * Running findFileByPattern on an array with matches returns first + * matching element. + */ + public function test_findFileByPattern_match() + { + $file_list = array("test.txt", "new_file.pdf"); + $pattern = "/test/"; + $this->assertEquals("test.txt", + LicenseHelper::findFileByPattern($file_list, + $pattern)); + } + + /** + * getTheme returns a ST-E Linaro-branded template when + * no EULA is present (indicated by eula filename being named + * EULA.txt or not). + */ + public function test_getTheme_noEula_snowball() + { + $eula = "EULA.txt"; + $filename = "snowball.build.tar.bz2"; + $this->assertEquals("ste", LicenseHelper::getTheme($eula, $filename)); + } + + /** + * getTheme returns a Samsung Linaro-branded template when + * no EULA is present (indicated by eula filename being named + * EULA.txt or not). + */ + public function test_getTheme_noEula_origen() + { + $eula = "EULA.txt"; + $filename = "origen.build.tar.bz2"; + $this->assertEquals("samsung", + LicenseHelper::getTheme($eula, $filename)); + } + + /** + * getTheme returns a generic Linaro-branded template when + * no EULA is present (indicated by eula filename being named + * EULA.txt or not). + */ + public function test_getTheme_noEula_generic() + { + $eula = "EULA.txt"; + $filename = "build.tar.bz2"; + $this->assertEquals("linaro", + LicenseHelper::getTheme($eula, $filename)); + } + + /** + * Running getTheme with eula file present (indicated by eula filename + * being named EULA.txt or not) returns extension of eula file. + */ + public function test_getTheme_eula() + { + $eula = "EULA.txt.test"; + $this->assertEquals("test", LicenseHelper::getTheme($eula, "")); + } + +} + +?>
\ No newline at end of file |