Merge lp:~stevanr/linaro-license-protection/phpunit-tests into lp:~linaro-automation/linaro-license-protection/trunk

Proposed by Stevan Radaković
Status: Merged
Approved by: Данило Шеган
Approved revision: 77
Merged at revision: 69
Proposed branch: lp:~stevanr/linaro-license-protection/phpunit-tests
Merge into: lp:~linaro-automation/linaro-license-protection/trunk
Diff against target: 474 lines (+305/-110)
4 files modified
README (+17/-0)
licenses/LicenseHelper.php (+102/-0)
licenses/license.php (+19/-110)
testing/LicenseHelperTest.php (+167/-0)
To merge this branch: bzr merge lp:~stevanr/linaro-license-protection/phpunit-tests
Reviewer Review Type Date Requested Status
Данило Шеган (community) Approve
Georgy Redkozubov Needs Fixing
Review via email: mp+104505@code.launchpad.net

Description of the change

Reorganize the code in license.php so the unit test in phpunit can be implemented.
Implement phpunit tests for all the functions from license.php.
Refactor functions where we have tests failing.
Update README so the unit tests are properly documented.

To post a comment you must log in.
Revision history for this message
Georgy Redkozubov (gesha) wrote :

The behaviour of click through license protection has slightly changed. As results following actions should be done:
1) Update filefetcher.py module in tests, now it is not in the branch.
2) Write new tests for "empty" directories handling.
3) Update old tests to use new filefetcher.py module.

review: Needs Fixing
Revision history for this message
Georgy Redkozubov (gesha) wrote :

1) Please revert your changes in 'testing/test_click_through_license.py' back.
2) Add instructions how to install PHP_CodeCoverage, I had to follow following steps to get running your test:
sudo apt-get remove phpunit
sudo apt-get upgrade pear
sudo pear channel-discover pear.phpunit.de
sudo pear channel-discover pear.symfony-project.com
sudo pear channel-discover components.ez.no
sudo pear update-channels
sudo pear upgrade-all
sudo pear install --alldeps phpunit/PHPUnit
sudo apt-get install phpunit
3) You have incorrect expected value for file.with.more.dots, should be:
$this->assertEquals("file", $filename_array[0]);
$this->assertEquals("with.more.dots", $filename_array[1]);

review: Needs Fixing
Revision history for this message
Stevan Radaković (stevanr) wrote :

> 1) Please revert your changes in 'testing/test_click_through_license.py' back.
> 2) Add instructions how to install PHP_CodeCoverage, I had to follow following
> steps to get running your test:
> sudo apt-get remove phpunit
> sudo apt-get upgrade pear
> sudo pear channel-discover pear.phpunit.de
> sudo pear channel-discover pear.symfony-project.com
> sudo pear channel-discover components.ez.no
> sudo pear update-channels
> sudo pear upgrade-all
> sudo pear install --alldeps phpunit/PHPUnit
> sudo apt-get install phpunit
> 3) You have incorrect expected value for file.with.more.dots, should be:
> $this->assertEquals("file", $filename_array[0]);
> $this->assertEquals("with.more.dots", $filename_array[1]);

2) I was able to do the tests on fresh 12.04 only by installing phpunit package. Maybe we should discuss this.
3) The test actually shows that the code should be refactored since the extension of 'file.with.more.dots' would be 'dots' and not 'with.more.dots'.

Revision history for this message
Georgy Redkozubov (gesha) wrote :

> > 1) Please revert your changes in 'testing/test_click_through_license.py'
> back.
> > 2) Add instructions how to install PHP_CodeCoverage, I had to follow
> following
> > steps to get running your test:
> > sudo apt-get remove phpunit
> > sudo apt-get upgrade pear
> > sudo pear channel-discover pear.phpunit.de
> > sudo pear channel-discover pear.symfony-project.com
> > sudo pear channel-discover components.ez.no
> > sudo pear update-channels
> > sudo pear upgrade-all
> > sudo pear install --alldeps phpunit/PHPUnit
> > sudo apt-get install phpunit
> > 3) You have incorrect expected value for file.with.more.dots, should be:
> > $this->assertEquals("file", $filename_array[0]);
> > $this->assertEquals("with.more.dots", $filename_array[1]);
>
> 2) I was able to do the tests on fresh 12.04 only by installing phpunit
> package. Maybe we should discuss this.

Anyway lets add information about if the execution of tests fails with PHP_CodeCoverage error

> 3) The test actually shows that the code should be refactored since the
> extension of 'file.with.more.dots' would be 'dots' and not 'with.more.dots'.

How about *.tar.bz2?

Revision history for this message
Stevan Radaković (stevanr) wrote :

> > > 1) Please revert your changes in 'testing/test_click_through_license.py'
> > back.
> > > 2) Add instructions how to install PHP_CodeCoverage, I had to follow
> > following
> > > steps to get running your test:
> > > sudo apt-get remove phpunit
> > > sudo apt-get upgrade pear
> > > sudo pear channel-discover pear.phpunit.de
> > > sudo pear channel-discover pear.symfony-project.com
> > > sudo pear channel-discover components.ez.no
> > > sudo pear update-channels
> > > sudo pear upgrade-all
> > > sudo pear install --alldeps phpunit/PHPUnit
> > > sudo apt-get install phpunit
> > > 3) You have incorrect expected value for file.with.more.dots, should be:
> > > $this->assertEquals("file", $filename_array[0]);
> > > $this->assertEquals("with.more.dots", $filename_array[1]);
> >
> > 2) I was able to do the tests on fresh 12.04 only by installing phpunit
> > package. Maybe we should discuss this.
>
> Anyway lets add information about if the execution of tests fails with
> PHP_CodeCoverage error

Ok

>
> > 3) The test actually shows that the code should be refactored since the
> > extension of 'file.with.more.dots' would be 'dots' and not 'with.more.dots'.
>
> How about *.tar.bz2?

Good point.. now I'm out of ideas atm.

Revision history for this message
Stevan Radaković (stevanr) wrote :

> > > 1) Please revert your changes in 'testing/test_click_through_license.py'
> > back.
> > > 2) Add instructions how to install PHP_CodeCoverage, I had to follow
> > following
> > > steps to get running your test:
> > > sudo apt-get remove phpunit
> > > sudo apt-get upgrade pear
> > > sudo pear channel-discover pear.phpunit.de
> > > sudo pear channel-discover pear.symfony-project.com
> > > sudo pear channel-discover components.ez.no
> > > sudo pear update-channels
> > > sudo pear upgrade-all
> > > sudo pear install --alldeps phpunit/PHPUnit
> > > sudo apt-get install phpunit
> > > 3) You have incorrect expected value for file.with.more.dots, should be:
> > > $this->assertEquals("file", $filename_array[0]);
> > > $this->assertEquals("with.more.dots", $filename_array[1]);
> >
> > 2) I was able to do the tests on fresh 12.04 only by installing phpunit
> > package. Maybe we should discuss this.
>
> Anyway lets add information about if the execution of tests fails with
> PHP_CodeCoverage error
>
> > 3) The test actually shows that the code should be refactored since the
> > extension of 'file.with.more.dots' would be 'dots' and not 'with.more.dots'.
>
> How about *.tar.bz2?

PHP function pathinfo returns only the last part of the filename with multiple dots, i.e. in example you provided it will return only bz2. Since it's a standard PHP function my opinion is that we should copy it's behavior.

Revision history for this message
Stevan Radaković (stevanr) wrote :

> > > > 1) Please revert your changes in 'testing/test_click_through_license.py'
> > > back.
> > > > 2) Add instructions how to install PHP_CodeCoverage, I had to follow
> > > following
> > > > steps to get running your test:
> > > > sudo apt-get remove phpunit
> > > > sudo apt-get upgrade pear
> > > > sudo pear channel-discover pear.phpunit.de
> > > > sudo pear channel-discover pear.symfony-project.com
> > > > sudo pear channel-discover components.ez.no
> > > > sudo pear update-channels
> > > > sudo pear upgrade-all
> > > > sudo pear install --alldeps phpunit/PHPUnit
> > > > sudo apt-get install phpunit
> > > > 3) You have incorrect expected value for file.with.more.dots, should be:
> > > > $this->assertEquals("file", $filename_array[0]);
> > > > $this->assertEquals("with.more.dots", $filename_array[1]);
> > >
> > > 2) I was able to do the tests on fresh 12.04 only by installing phpunit
> > > package. Maybe we should discuss this.
> >
> > Anyway lets add information about if the execution of tests fails with
> > PHP_CodeCoverage error
> >
> > > 3) The test actually shows that the code should be refactored since the
> > > extension of 'file.with.more.dots' would be 'dots' and not
> 'with.more.dots'.
> >
> > How about *.tar.bz2?
>
> PHP function pathinfo returns only the last part of the filename with multiple
> dots, i.e. in example you provided it will return only bz2. Since it's a
> standard PHP function my opinion is that we should copy it's behavior.

I've just realized that this function is not used at all in license.php, and basically there are some standard library functions in PHP that already do this task so I think we should be OK if we remove this method as part of the refactoring. What do you think?

Revision history for this message
Данило Шеган (danilo) wrote :

Thanks for working on this, Stevan.

First of, please try to follow the existing coding style for the code you are modifying. Existing code is using tabs (uhm, we should change that to 4-spaces like we do in the rest of Infrastructure projects, but that's a separate branch: do not, under any circumstances, do it in this branch).

For the python test failures you are seeing, please file a bug if there isn't one already (on the https://launchpad.net/linaro-license-protection project: there isn't one, so please do that).

Unfortunately, phpunit package is broken in Debian/Ubuntu (bug #701544), so we will have to include the workaround installation in the documentation until package itself is fixed (I expect it might only be fixed in 12.10, unless someone invests a lot of effort in fixing the 12.04 package as well, though that might be worth it considering it's an LTS).

For the function that is not used, please remove it altogether.

Next, I'll comment on the code changes directly.

Revision history for this message
Данило Шеган (danilo) wrote :
Download full text (12.0 KiB)

Hi Stevan,

I am going to be a bit more picky this time around, but please do not take it too bad.

> === modified file 'README'
...
> +Unit tests
> +----------

I don't think it's that interesting that these are unit-tests. FWIW, we can perfectly well have Python unit tests in this same branch, so this is a wrong title in my opinion.

Relevant title would be "Tests for PHP license-matching logic" or similar.

You should also make this a subsection of "Tests" section below it. This README file is using ReST, so all it takes to get a "subsection" is to use a different underlying character (the file already uses "." above).

> +
> +There's currently only one unit test file, LicenseHelperTest.php under testing directory. You first need to install the phpunit package from ubuntu repos:

It would be good to follow the 79-character limit in the README as well. It was probably me who messed this up originally, so let's not worry about it right now.

> +$ sudo apt-get install php-pear
> +$ sudo pear config-set auto_discover 1
> +$ sudo pear install pear.phpunit.de/PHPUnit

Generic instructions should be "sudo apt-get install phpunit". The fact that phpunit package is broken in current Ubuntu/Debian releases is not something we want to rely on. FWIW, Ubuntu 10.04 (previous LTS, which most of our servers still run and will run for another 6 months) has a working phpunit package (it doesn't seem to have assertNotEmpty method, though).

> +
> +Then to run the test from the 'testing' directory:
> +$ cd testing/
> +$ phpunit LicenseHelperTest
> +
> +In case the tests fail with PHP_CodeCoverage error, do the following to install the phpunit properly:
> +
> +$ sudo apt-get remove phpunit
> +$ sudo apt-get upgrade pear
> +$ sudo pear channel-discover pear.phpunit.de
> +$ sudo pear channel-discover pear.symfony-project.com
> +$ sudo pear channel-discover components.ez.no
> +$ sudo pear update-channels
> +$ sudo pear upgrade-all
> +$ sudo pear install --alldeps phpunit/PHPUnit
> +$ sudo apt-get install phpunit

Your former instructions solve that problem as well. You should list either one or the other, not both. You should also mention what we know of the problem (i.e. "recent (as of 2012-05-08) Ubuntu/Debian releases have a broken phpunit package and thus require installation in a different manner").

Also, please use some indentation for commands like it's already done in the 'Tests' section for testr ones.

> === added file 'licenses/LicenseHelper.php'
> --- licenses/LicenseHelper.php 1970-01-01 00:00:00 +0000
> +++ licenses/LicenseHelper.php 2012-05-07 15:13:20 +0000
> @@ -0,0 +1,104 @@
> +<?php
> +
> +class LicenseHelper

Trailing whitespace, please remove it. If you are using Emacs (I believe you are), you can have it highlighted by adding the following to your .emacs:

  (setq show-trailing-whitespace t)

Note, I understand you have mostly copied the code over in this file, so that makes it harder for me to comment on it. Thus, I'll focus on style first.

> +{
> +
> + // 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)
> + {
> + ...

review: Needs Fixing
Revision history for this message
Stevan Radaković (stevanr) wrote :
Download full text (13.4 KiB)

> Hi Stevan,
>
> I am going to be a bit more picky this time around, but please do not take it
> too bad.
>
> > === modified file 'README'
> ...
> > +Unit tests
> > +----------
>
> I don't think it's that interesting that these are unit-tests. FWIW, we can
> perfectly well have Python unit tests in this same branch, so this is a wrong
> title in my opinion.
>
> Relevant title would be "Tests for PHP license-matching logic" or similar.
>
> You should also make this a subsection of "Tests" section below it. This
> README file is using ReST, so all it takes to get a "subsection" is to use a
> different underlying character (the file already uses "." above).
>
> > +
> > +There's currently only one unit test file, LicenseHelperTest.php under
> testing directory. You first need to install the phpunit package from ubuntu
> repos:
>
> It would be good to follow the 79-character limit in the README as well. It
> was probably me who messed this up originally, so let's not worry about it
> right now.
>
> > +$ sudo apt-get install php-pear
> > +$ sudo pear config-set auto_discover 1
> > +$ sudo pear install pear.phpunit.de/PHPUnit
>
> Generic instructions should be "sudo apt-get install phpunit". The fact that
> phpunit package is broken in current Ubuntu/Debian releases is not something
> we want to rely on. FWIW, Ubuntu 10.04 (previous LTS, which most of our
> servers still run and will run for another 6 months) has a working phpunit
> package (it doesn't seem to have assertNotEmpty method, though).
>
> > +
> > +Then to run the test from the 'testing' directory:
> > +$ cd testing/
> > +$ phpunit LicenseHelperTest
> > +
> > +In case the tests fail with PHP_CodeCoverage error, do the following to
> install the phpunit properly:
> > +
> > +$ sudo apt-get remove phpunit
> > +$ sudo apt-get upgrade pear
> > +$ sudo pear channel-discover pear.phpunit.de
> > +$ sudo pear channel-discover pear.symfony-project.com
> > +$ sudo pear channel-discover components.ez.no
> > +$ sudo pear update-channels
> > +$ sudo pear upgrade-all
> > +$ sudo pear install --alldeps phpunit/PHPUnit
> > +$ sudo apt-get install phpunit
>
> Your former instructions solve that problem as well. You should list either
> one or the other, not both. You should also mention what we know of the
> problem (i.e. "recent (as of 2012-05-08) Ubuntu/Debian releases have a broken
> phpunit package and thus require installation in a different manner").
>
> Also, please use some indentation for commands like it's already done in the
> 'Tests' section for testr ones.
>
>
> > === added file 'licenses/LicenseHelper.php'
> > --- licenses/LicenseHelper.php 1970-01-01 00:00:00 +0000
> > +++ licenses/LicenseHelper.php 2012-05-07 15:13:20 +0000
> > @@ -0,0 +1,104 @@
> > +<?php
> > +
> > +class LicenseHelper
>
> Trailing whitespace, please remove it. If you are using Emacs (I believe you
> are), you can have it highlighted by adding the following to your .emacs:
>
> (setq show-trailing-whitespace t)
>
> Note, I understand you have mostly copied the code over in this file, so that
> makes it harder for me to comment on it. Thus, I'll focus on style first.
>
> > +{
> > +
...

Revision history for this message
Данило Шеган (danilo) wrote :
Download full text (6.5 KiB)

Hi Stevan,

Thanks for working on this. We are so close to getting this landed now.
Just a few more comments.

У уто, 08. 05 2012. у 16:11 +0000, Stevan Radaković пише:

> разлике међу датотекама прилог (review-diff.txt)
> === modified file 'README'
> --- README 2012-05-04 17:44:47 +0000
> +++ README 2012-05-08 14:30:26 +0000
> @@ -74,6 +74,8 @@
> build steps, and not as publishing steps.
>
>
> +
> +

Heh, how many newlines is enough? Please get rid of these two.

> Tests
> -----
>
> @@ -82,3 +84,20 @@
>
> $ 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

Indent these as well, please :)

> === added file 'licenses/LicenseHelper.php'

Looks so much better, thanks..

> === added file 'testing/LicenseHelperTest.php'
> --- testing/LicenseHelperTest.php 1970-01-01 00:00:00 +0000
> +++ testing/LicenseHelperTest.php 2012-05-08 14:30:26 +0000
> @@ -0,0 +1,175 @@
> +<?php
> +
> +class LicenseHelperTest extends PHPUnit_Framework_TestCase
> +{
> +
> + private $temp_filename;
> +
> + /**
> + * Include test class, create some help files for testing.
> + */
> + protected function setUp()
> + {
> + require_once("../licenses/LicenseHelper.php");
> + $this->temp_filename = tempnam(dirname(__FILE__), "unittest");
> + }

Why don't we get rid of the setUp function and move the require_once to
the top of the file, and temp_filename to the single test that requires
it?

> +
> + /**
> + * Remove helper files used in testing.
> + */
> + protected function tearDown()
> + {
> + unlink($this->temp_filename);
> + }

This will now try to unlink temp_filename even if it's only created in a
single test. Move this to that test, and get rid of th tearDown()
function.

> + /**
> + * Running checkFile on a symbolic link to an existing file returns true.
> + */
> + public function test_checkFile_link()
> + {
> + try {
> + symlink($this->temp_filename, "test_link");
> + $this->assertTrue(LicenseHelper::checkFile("test_link"));
> + unlink("test_link");
> + // PHP doesn't support finally block, ergo using this hack.
> + } catch (Exception $e) {
> + unlink("test_link");
> + throw $e;
> + }
> + }

Where's that test for the checkFile on a link to a non-existent file?
Perhaps PHP symlink(tempnam(), "test_link") doesn't work?

> + /**
> + * getFileList returns a list of filenames in that directory.
> + */
> + public function test_getFilesList_dir()
> +...

Read more...

Revision history for this message
Stevan Radaković (stevanr) wrote :
Download full text (7.1 KiB)

> Hi Stevan,
>
> Thanks for working on this. We are so close to getting this landed now.
> Just a few more comments.
>
> У уто, 08. 05 2012. у 16:11 +0000, Stevan Radaković пише:
>
> > разлике међу датотекама прилог (review-diff.txt)
> > === modified file 'README'
> > --- README 2012-05-04 17:44:47 +0000
> > +++ README 2012-05-08 14:30:26 +0000
> > @@ -74,6 +74,8 @@
> > build steps, and not as publishing steps.
> >
> >
> > +
> > +
>
> Heh, how many newlines is enough? Please get rid of these two.
>
> > Tests
> > -----
> >
> > @@ -82,3 +84,20 @@
> >
> > $ 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
>
> Indent these as well, please :)
>
> > === added file 'licenses/LicenseHelper.php'
>
> Looks so much better, thanks..
>
> > === added file 'testing/LicenseHelperTest.php'
> > --- testing/LicenseHelperTest.php 1970-01-01 00:00:00 +0000
> > +++ testing/LicenseHelperTest.php 2012-05-08 14:30:26 +0000
> > @@ -0,0 +1,175 @@
> > +<?php
> > +
> > +class LicenseHelperTest extends PHPUnit_Framework_TestCase
> > +{
> > +
> > + private $temp_filename;
> > +
> > + /**
> > + * Include test class, create some help files for testing.
> > + */
> > + protected function setUp()
> > + {
> > + require_once("../licenses/LicenseHelper.php");
> > + $this->temp_filename = tempnam(dirname(__FILE__), "unittest");
> > + }
>
> Why don't we get rid of the setUp function and move the require_once to
> the top of the file, and temp_filename to the single test that requires
> it?
>
> > +
> > + /**
> > + * Remove helper files used in testing.
> > + */
> > + protected function tearDown()
> > + {
> > + unlink($this->temp_filename);
> > + }
>
> This will now try to unlink temp_filename even if it's only created in a
> single test. Move this to that test, and get rid of th tearDown()
> function.
>
> > + /**
> > + * Running checkFile on a symbolic link to an existing file returns
> true.
> > + */
> > + public function test_checkFile_link()
> > + {
> > + try {
> > + symlink($this->temp_filename, "test_link");
> > + $this->assertTrue(LicenseHelper::checkFile("test_link"));
> > + unlink("test_link");
> > + // PHP doesn't support finally block, ergo using this hack.
> > + } catch (Exception $e) {
> > + unlink("test_link");
> > + throw $e;
> > + }
> > + }
>
> Where's that test for the checkFile on a link t...

Read more...

Revision history for this message
Данило Шеган (danilo) wrote :

Sorry: I missed this (I thought you just mistakenly quoted the entire
email). Can I ask you to cut out everything but the relevant bit when
replying, please?

У уто, 08. 05 2012. у 14:36 +0000, Stevan Radaković пише:
> >
> > "Running checkFile on a symbolic link to an existing file returns true."
> >
> > Which reminds me, what happens if it's run on a symbolic link to a non-
> > existant file?
> >
>
> Very good point. Unfortunately, I'm not sure what was the authors expected behavior for this method (suggestion below), but here's how is_file and is_link work for links and files in PHP.
>
> is_file(file) - TRUE
> is_file(hard_link) - TRUE
> is_file(soft_link) - TRUE
> is_file(broken_soft_link) - FALSE
>
> is_link(file) - FALSE
> is_link(hard_link) - FALSE
> is_link(soft_link) - TRUE
> is_link(broken_soft_link) - TRUE
>
> Some of those are really odd, and in my opinion we should only use the standard is_file function, because it basically serves to identify files and links from directories.

For exactly the reason you are not sure what the "expected behavior for
this method" is, we should document it. Let's add a test which confirms
the current behavior, and it will serve as documentation in the future
as well.

Revision history for this message
Данило Шеган (danilo) wrote :

Excellent, thanks a lot for working on this.

review: Approve
78. By Stevan Radaković

Add file list sorting in getFilesList test.

Revision history for this message
Данило Шеган (danilo) wrote :

Ok, there was another test instability (well, the same one). That's now fixed.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'README'
2--- README 2012-05-04 17:44:47 +0000
3+++ README 2012-05-09 07:56:17 +0000
4@@ -82,3 +82,20 @@
5
6 $ testr init
7 $ testr run
8+
9+
10+Tests for PHP license-matching logic
11+....................................
12+
13+There's currently only one unit test file, LicenseHelperTest.php under testing directory. You first need to install the phpunit package from ubuntu repos:
14+ $ sudo apt-get install php-unit
15+
16+
17+Recent (as of 2012-05-08) Ubuntu/Debian releases have a broken phpunit package and thus require installation in a different manner:
18+ $ sudo apt-get install php-pear
19+ $ sudo pear config-set auto_discover 1
20+ $ sudo pear install pear.phpunit.de/PHPUnit
21+
22+Then to run the test from the 'testing' directory:
23+ $ cd testing/
24+ $ phpunit LicenseHelperTest
25
26=== added file 'licenses/LicenseHelper.php'
27--- licenses/LicenseHelper.php 1970-01-01 00:00:00 +0000
28+++ licenses/LicenseHelper.php 2012-05-09 07:56:17 +0000
29@@ -0,0 +1,102 @@
30+<?php
31+
32+class LicenseHelper
33+{
34+
35+ /**
36+ * Get list of files into array to process them later.
37+ * Used to find special licenses and dirs with only subdirs.
38+ */
39+ public static function checkFile($fn)
40+ {
41+ if (is_file($fn) or is_link($fn)) {
42+ return true;
43+ }
44+ return false;
45+ }
46+
47+ /**
48+ * Get list of filenames from a directory
49+ */
50+ public static function getFilesList($dirname)
51+ {
52+ if (!is_dir($dirname)) {
53+ throw new InvalidArgumentException('Method argument '.
54+ 'should be a directory path');
55+ }
56+
57+ $files = array();
58+ if ($handle = opendir($dirname)) {
59+ while ($handle && false !== ($entry = readdir($handle))) {
60+ if ($entry != "." && $entry != ".." &&
61+ !is_dir($dirname."/".$entry) && $entry != "HEADER.html") {
62+ $files[] = $entry;
63+ }
64+ }
65+ }
66+ closedir($handle);
67+ return $files;
68+ }
69+
70+ /**
71+ * Find a matching filename in an array from given filename template.
72+ */
73+ public static function findFileByPattern($fl, $pattern)
74+ {
75+ if (!empty($fl)) {
76+ foreach ($fl as $f) {
77+ if (preg_match($pattern, $f, $matches)) {
78+ return $f;
79+ }
80+ }
81+ }
82+ return false;
83+ }
84+
85+ /**
86+ * Get license theme name from EULA filename.
87+ */
88+ public static function getTheme($eula, $down)
89+ {
90+ if ($eula != 'EULA.txt') { // Special EULA file was found
91+ $theme = array_pop(explode(".", $eula));
92+ } else { // No special EULA file was found
93+ $eula = "EULA.txt";
94+ if (preg_match("/.*snowball.*/", $down)) {
95+ $theme = "ste";
96+ } elseif (preg_match("/.*origen.*/", $down)) {
97+ $theme = "samsung";
98+ } else {
99+ $theme = "linaro";
100+ }
101+ }
102+ return $theme;
103+ }
104+
105+ public static function status_forbidden($dir)
106+ {
107+ header("Status: 403");
108+ header("HTTP/1.1 403 Forbidden");
109+ echo "<h1>Forbidden</h1>";
110+ echo "You don't have permission to access ".$dir." on this server.";
111+ exit;
112+ }
113+
114+ public static function status_ok($dir, $domain)
115+ {
116+ header("Status: 200");
117+ header("Location: ".$dir);
118+ setcookie("redirectlicensephp", "yes", 0, "/", ".".$domain);
119+ exit;
120+ }
121+
122+ public static function status_not_found()
123+ {
124+ header("Status: 404");
125+ header("HTTP/1.0 404 Not Found");
126+ echo "<h1>404 Not Found</h1>";
127+ echo "The requested URL was not found on this server.";
128+ exit;
129+ }
130+
131+}
132\ No newline at end of file
133
134=== modified file 'licenses/license.php'
135--- licenses/license.php 2012-05-02 10:56:00 +0000
136+++ licenses/license.php 2012-05-09 07:56:17 +0000
137@@ -1,97 +1,6 @@
138 <?php
139-// Get list of files into array to process them later.
140-// Used to find special licenses and dirs with only subdirs.
141-function check_file($fn)
142-{
143- if (is_file($fn) or is_link($fn)) {
144- return true;
145- }
146- return false;
147-}
148-
149-function getFilesList($dirname)
150-{
151- $files = array();
152- if ($handle = opendir($dirname)) {
153- while ($handle && false !== ($entry = readdir($handle))) {
154- if ($entry != "." && $entry != ".." && !is_dir($dirname."/".$entry) && $entry != "HEADER.html") {
155- $files[] = $entry;
156- }
157- }
158- }
159- closedir($handle);
160- return $files;
161-}
162-
163-// Get array of file name and extension from full filename.
164-function splitFilename($filename)
165-{
166- $pos = strpos($filename, '.');
167- if ($pos === false) { // dot is not found in the filename
168- return array($filename, ''); // no extension
169- } else {
170- $basename = substr($filename, 0, $pos);
171- $extension = substr($filename, $pos+1);
172- return array($basename, $extension);
173- }
174-}
175-
176-// Find special EULA based on filename template.
177-function findSpecialEULA($fl, $pattern)
178-{
179- if (!empty($fl)) {
180- foreach ($fl as $f) {
181- if (preg_match($pattern, $f, $matches)) {
182- return $f;
183- }
184- }
185- }
186- return false;
187-}
188-
189-// Get license theme name from EULA filename.
190-function getTheme($eula, $down)
191-{
192- if ($eula != 'EULA.txt') { // Special EULA file was found
193- $theme = array_pop(explode(".", $eula));
194- } else { // No special EULA file was found
195- $eula = "EULA.txt";
196- if (preg_match("/.*snowball.*/", $down)) {
197- $theme = "ste";
198- } elseif (preg_match("/.*origen.*/", $down)) {
199- $theme = "samsung";
200- } else {
201- $theme = "linaro";
202- }
203- }
204- return $theme;
205-}
206-
207-function status_forbidden($dir)
208-{
209- header("Status: 403");
210- header("HTTP/1.1 403 Forbidden");
211- echo "<h1>Forbidden</h1>";
212- echo "You don't have permission to access ".$dir." on this server.";
213- exit;
214-}
215-
216-function status_ok($dir, $domain)
217-{
218- header("Status: 200");
219- header("Location: ".$dir);
220- setcookie("redirectlicensephp", "yes", 0, "/", ".".$domain);
221- exit;
222-}
223-
224-function status_not_found()
225-{
226- header("Status: 404");
227- header("HTTP/1.0 404 Not Found");
228- echo "<h1>404 Not Found</h1>";
229- echo "The requested URL was not found on this server.";
230- exit;
231-}
232+
233+require_once("LicenseHelper.php");
234
235 $down = $_COOKIE["downloadrequested"];
236 $host = $_SERVER["HTTP_HOST"];
237@@ -102,10 +11,10 @@
238 $eula = '';
239
240 if (preg_match("/.*openid.*/", $fn) or preg_match("/.*restricted.*/", $fn) or preg_match("/.*private.*/", $fn)) {
241- status_ok($down, $domain);
242+ LicenseHelper::status_ok($down, $domain);
243 }
244
245-if (file_exists($fn) and check_file($fn)) { // Requested download is file
246+if (file_exists($fn) and LicenseHelper::checkFile($fn)) { // Requested download is file
247 $search_dir = dirname($fn);
248 $repl = dirname($down);
249 $name_only = array(basename($down), '');
250@@ -114,36 +23,36 @@
251 $repl = $down;
252 $name_only = array();
253 } else { // Requested download not found on server
254- status_not_found();
255+ LicenseHelper::status_not_found();
256 }
257
258-$flist = getFilesList($search_dir);
259+$flist = LicenseHelper::getFilesList($search_dir);
260
261 if (!empty($name_only)) {
262 $pattern = "/^".$name_only[0]."\.EULA\.txt.*/";
263- $eula = findSpecialEULA($flist, $pattern);
264+ $eula = LicenseHelper::findFileByPattern($flist, $pattern);
265 }
266
267-if (check_file($fn)) {
268- if (check_file($doc."/".$repl."/".$eula)) { // Special EULA found
269- $theme = getTheme($eula, $down);
270- } elseif (check_file($doc."/".$repl."/EULA.txt")) { // No special EULA found
271- $theme = getTheme("EULA.txt", $down);
272- } elseif (findSpecialEULA($flist, "/.*EULA.txt.*/")) {
273+if (LicenseHelper::checkFile($fn)) {
274+ if (LicenseHelper::checkFile($doc."/".$repl."/".$eula)) { // Special EULA found
275+ $theme = LicenseHelper::getTheme($eula, $down);
276+ } elseif (LicenseHelper::checkFile($doc."/".$repl."/EULA.txt")) { // No special EULA found
277+ $theme = LicenseHelper::getTheme("EULA.txt", $down);
278+ } elseif (LicenseHelper::findFileByPattern($flist, "/.*EULA.txt.*/")) {
279 // If file is requested but no special EULA for it and no EULA.txt is present,
280 // look for any EULA and if found decide that current file is not protected.
281- status_ok($down, $domain);
282+ LicenseHelper::status_ok($down, $domain);
283 } else {
284- status_forbidden($down);
285+ LicenseHelper::status_forbidden($down);
286 }
287 } elseif (is_dir($fn)) {
288- if (empty($flist) or findSpecialEULA($flist, "/.*EULA.txt.*/")) { // Directory contains only subdirs or any EULA
289- status_ok($down, $domain);
290+ if (empty($flist) or LicenseHelper::findFileByPattern($flist, "/.*EULA.txt.*/")) { // Directory contains only subdirs or any EULA
291+ LicenseHelper::status_ok($down, $domain);
292 } else { // No special EULA, no EULA.txt, no OPEN-EULA.txt found
293- status_forbidden($down);
294+ LicenseHelper::status_forbidden($down);
295 }
296 } else {
297- status_forbidden($down);
298+ LicenseHelper::status_forbidden($down);
299 }
300
301 $template_content = file_get_contents($doc."/licenses/".$theme.".html");
302
303=== added file 'testing/LicenseHelperTest.php'
304--- testing/LicenseHelperTest.php 1970-01-01 00:00:00 +0000
305+++ testing/LicenseHelperTest.php 2012-05-09 07:56:17 +0000
306@@ -0,0 +1,167 @@
307+<?php
308+
309+require_once("../licenses/LicenseHelper.php");
310+
311+class LicenseHelperTest extends PHPUnit_Framework_TestCase
312+{
313+
314+ private $temp_filename;
315+
316+ /**
317+ * Running checkFile on a directory path returns false.
318+ */
319+ public function test_checkFile_nonFile()
320+ {
321+ $this->assertFalse(LicenseHelper::checkFile(dirname(__FILE__)));
322+ }
323+
324+ /**
325+ * Running checkFile on a symbolic link to an existing file returns true.
326+ */
327+ public function test_checkFile_link()
328+ {
329+ $this->temp_filename = tempnam(sys_get_temp_dir(), "unittest");
330+ symlink($this->temp_filename, "test_link");
331+
332+ $this->assertTrue(LicenseHelper::checkFile("test_link"));
333+
334+ unlink("test_link");
335+ unlink($this->temp_filename);
336+ }
337+
338+ /**
339+ * Running checkFile on a broken symbolic link returns true.
340+ * This is because PHP function is_link returns true on broken soft links.
341+ */
342+ public function test_checkFile_brokenLink()
343+ {
344+ $this->temp_filename = tempnam(sys_get_temp_dir(), "unittest");
345+ symlink($this->temp_filename, "test_link");
346+ unlink($this->temp_filename);
347+
348+ $this->assertTrue(LicenseHelper::checkFile("test_link"));
349+
350+ unlink("test_link");
351+ }
352+
353+ /**
354+ * Running checkFile on a regular file returns true.
355+ */
356+ public function test_checkFile_file()
357+ {
358+ $this->assertTrue(LicenseHelper::checkFile(__FILE__));
359+ }
360+
361+ /**
362+ * getFileList throws an InvalidArgumentException when passed
363+ * an argument pointing to a file.
364+ * @expectedException InvalidArgumentException
365+ */
366+ public function test_getFilesList_file()
367+ {
368+ $file_list = LicenseHelper::getFilesList(__FILE__);
369+ }
370+
371+ /**
372+ * getFileList returns a list of filenames in that directory.
373+ */
374+ public function test_getFilesList_dir()
375+ {
376+ $temp_dir_name = tempnam(sys_get_temp_dir(), "unittest");
377+ if (file_exists($temp_dir_name)) {
378+ unlink($temp_dir_name);
379+ }
380+ mkdir($temp_dir_name);
381+
382+ touch($temp_dir_name . "/unittest1");
383+ touch($temp_dir_name . "/unittest2");
384+
385+ $file_list = LicenseHelper::getFilesList($temp_dir_name);
386+ $this->assertCount(2, $file_list);
387+
388+ // Sort the file list, this function returns the files as they are
389+ // written on the filesystem.
390+ sort($file_list);
391+ $this->assertEquals("unittest1", $file_list[0]);
392+ $this->assertEquals("unittest2", $file_list[1]);
393+
394+ unlink($temp_dir_name . "/unittest1");
395+ unlink($temp_dir_name . "/unittest2");
396+ rmdir($temp_dir_name);
397+ }
398+
399+ /**
400+ * Running findFileByPattern on an array without matches returns false.
401+ */
402+ public function test_findFileByPattern_noMatch()
403+ {
404+ $file_list = array("test.txt", "new_file.pdf");
405+ $pattern = "/^abc/";
406+ $this->assertFalse(
407+ LicenseHelper::findFileByPattern($file_list, $pattern));
408+ }
409+
410+ /**
411+ * Running findFileByPattern on an array with matches returns first
412+ * matching element.
413+ */
414+ public function test_findFileByPattern_match()
415+ {
416+ $file_list = array("test.txt", "new_file.pdf");
417+ $pattern = "/test/";
418+ $this->assertEquals("test.txt",
419+ LicenseHelper::findFileByPattern($file_list,
420+ $pattern));
421+ }
422+
423+ /**
424+ * getTheme returns a ST-E Linaro-branded template when
425+ * no EULA is present (indicated by eula filename being named
426+ * EULA.txt or not).
427+ */
428+ public function test_getTheme_noEula_snowball()
429+ {
430+ $eula = "EULA.txt";
431+ $filename = "snowball.build.tar.bz2";
432+ $this->assertEquals("ste", LicenseHelper::getTheme($eula, $filename));
433+ }
434+
435+ /**
436+ * getTheme returns a Samsung Linaro-branded template when
437+ * no EULA is present (indicated by eula filename being named
438+ * EULA.txt or not).
439+ */
440+ public function test_getTheme_noEula_origen()
441+ {
442+ $eula = "EULA.txt";
443+ $filename = "origen.build.tar.bz2";
444+ $this->assertEquals("samsung",
445+ LicenseHelper::getTheme($eula, $filename));
446+ }
447+
448+ /**
449+ * getTheme returns a generic Linaro-branded template when
450+ * no EULA is present (indicated by eula filename being named
451+ * EULA.txt or not).
452+ */
453+ public function test_getTheme_noEula_generic()
454+ {
455+ $eula = "EULA.txt";
456+ $filename = "build.tar.bz2";
457+ $this->assertEquals("linaro",
458+ LicenseHelper::getTheme($eula, $filename));
459+ }
460+
461+ /**
462+ * Running getTheme with eula file present (indicated by eula filename
463+ * being named EULA.txt or not) returns extension of eula file.
464+ */
465+ public function test_getTheme_eula()
466+ {
467+ $eula = "EULA.txt.test";
468+ $this->assertEquals("test", LicenseHelper::getTheme($eula, ""));
469+ }
470+
471+}
472+
473+?>
474\ No newline at end of file

Subscribers

People subscribed via source and target branches