2023-02-19 02:32:10 -05:00
|
|
|
# Running All Unit Tests
|
2019-05-11 14:51:18 -04:00
|
|
|
|
|
|
|
Since it is non-trivial to setup all dependencies for our unit testing suite,
|
|
|
|
we provide a docker image that bundles all of them into one container, both
|
|
|
|
phpunit for PHP and mocha for JS.
|
|
|
|
|
|
|
|
You can fetch and run the image from the docker hub like this:
|
|
|
|
|
|
|
|
```console
|
|
|
|
docker run --rm --read-only -v ~/PrivateBin:/srv:ro privatebin/unit-testing
|
|
|
|
```
|
|
|
|
|
|
|
|
The parameters in detail:
|
|
|
|
|
|
|
|
- `-v ~/PrivateBin:/srv:ro` - Replace `~/PrivateBin` with the location of
|
|
|
|
the checked out PrivateBin repository on your machine. It is recommended to
|
|
|
|
mount it read-only, which guarantees that your repository isn't damaged by
|
|
|
|
an accidentally destructive test case in it.
|
|
|
|
- `--read-only` - This image supports running in read-only mode. Only /tmp
|
|
|
|
may be written into.
|
|
|
|
- `-rm` - Remove the container after the run. This saves you doing a cleanup
|
|
|
|
on your docker environment, if you run the image frequently.
|
|
|
|
|
|
|
|
You can also run just the php and javascript test suites instead of both:
|
|
|
|
|
|
|
|
```console
|
|
|
|
docker run --rm --read-only -v ~/PrivateBin:/srv:ro privatebin/unit-testing phpunit
|
|
|
|
docker run --rm --read-only -v ~/PrivateBin:/srv:ro privatebin/unit-testing mocha
|
|
|
|
```
|
|
|
|
|
|
|
|
We also provide a Janitor image that includes the Cloud9 and Theia WebIDEs as
|
|
|
|
well as the integrated unit testing utilities. See our [docker wiki
|
|
|
|
page](https://github.com/PrivateBin/PrivateBin/wiki/Docker#janitor-image-with-cloud9-and-theia-webide-janitortechnologyprivatebin)
|
|
|
|
for further details on this.
|
|
|
|
|
2023-02-19 02:32:10 -05:00
|
|
|
## Running PHP Unit Tests
|
2012-08-25 18:49:11 -04:00
|
|
|
|
|
|
|
In order to run these tests, you will need to install the following packages
|
2017-03-26 00:47:57 -04:00
|
|
|
and their dependencies:
|
2012-08-25 18:49:11 -04:00
|
|
|
* phpunit
|
2016-11-13 05:01:51 -05:00
|
|
|
* php-gd
|
2016-12-25 04:58:35 -05:00
|
|
|
* php-sqlite3
|
2017-01-29 08:31:44 -05:00
|
|
|
* php-xdebug (for code coverage reports)
|
2012-08-25 18:49:11 -04:00
|
|
|
|
|
|
|
Example for Debian and Ubuntu:
|
2019-05-11 13:57:13 -04:00
|
|
|
|
2017-01-29 08:31:44 -05:00
|
|
|
```console
|
2017-04-11 11:23:26 -04:00
|
|
|
$ sudo apt install phpunit php-gd php-sqlite3 php-xdebug
|
2016-09-14 17:55:55 -04:00
|
|
|
```
|
2012-08-25 18:49:11 -04:00
|
|
|
|
2017-03-26 00:47:57 -04:00
|
|
|
To run the tests, change into the `tst` directory and run phpunit:
|
2019-05-11 13:57:13 -04:00
|
|
|
|
2017-01-29 08:31:44 -05:00
|
|
|
```console
|
2016-09-14 17:55:55 -04:00
|
|
|
$ cd PrivateBin/tst
|
|
|
|
$ phpunit
|
|
|
|
```
|
2017-01-29 08:31:44 -05:00
|
|
|
|
2017-03-26 00:47:57 -04:00
|
|
|
Additionally there is the `ConfigurationTestGenerator`. Based on the
|
|
|
|
configurations defined in its constructor, it generates the unit test file
|
|
|
|
`tst/ConfigurationCombinationsTest.php`, containing all possible combinations
|
|
|
|
of these configurations and tests for (most of the) valid combinations. Some of
|
|
|
|
combinations can't be tested with this method, i.e. a valid option combined with
|
|
|
|
an invalid one. Other very specific test cases (i.e. to trigger multiple errors)
|
|
|
|
are covered in `tst/PrivateBinTest.php`. Here is how to generate the
|
|
|
|
configuration test and run it:
|
|
|
|
|
|
|
|
```console
|
|
|
|
$ cd PrivateBin/tst
|
|
|
|
$ php ConfigurationTestGenerator.php
|
|
|
|
$ phpunit ConfigurationCombinationsTest.php
|
|
|
|
```
|
|
|
|
|
|
|
|
Note that it can take an hour or longer to run the several thousand tests.
|
|
|
|
|
|
|
|
|
2023-02-19 02:32:10 -05:00
|
|
|
## Running JavaScript Unit Tests
|
2017-01-29 08:31:44 -05:00
|
|
|
|
|
|
|
In order to run these tests, you will need to install the following packages
|
|
|
|
and its dependencies:
|
|
|
|
* npm
|
|
|
|
|
|
|
|
Then you can use the node package manager to install the latest stable release
|
2019-05-11 14:51:18 -04:00
|
|
|
of mocha and nyc (for code coverage reports) globally and jsVerify, jsdom
|
2017-01-29 08:31:44 -05:00
|
|
|
and jsdom-global locally:
|
|
|
|
|
|
|
|
```console
|
2019-05-11 14:51:18 -04:00
|
|
|
$ npm install -g mocha nyc
|
2017-01-29 08:31:44 -05:00
|
|
|
$ cd PrivateBin/js
|
2019-06-24 01:41:12 -04:00
|
|
|
$ npm install
|
2017-01-29 08:31:44 -05:00
|
|
|
```
|
|
|
|
|
2017-03-26 00:47:57 -04:00
|
|
|
Example for Debian and Ubuntu, including steps to allow the current user to
|
|
|
|
install node modules globally:
|
2017-01-29 08:31:44 -05:00
|
|
|
```console
|
|
|
|
$ sudo apt install npm
|
|
|
|
$ sudo mkdir /usr/local/lib/node_modules
|
|
|
|
$ sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}
|
|
|
|
$ ln -s /usr/bin/nodejs /usr/local/bin/node
|
2019-05-11 14:51:18 -04:00
|
|
|
$ npm install -g mocha nyc
|
2017-01-29 08:31:44 -05:00
|
|
|
$ cd PrivateBin/js
|
2019-06-24 01:41:12 -04:00
|
|
|
$ npm install
|
2017-01-29 08:31:44 -05:00
|
|
|
```
|
|
|
|
|
2020-06-01 02:07:25 -04:00
|
|
|
To run the tests, just change into the `js` directory and run nyc (will produce
|
|
|
|
coverage report) or just mocha:
|
2019-05-11 13:57:13 -04:00
|
|
|
|
2017-01-29 08:31:44 -05:00
|
|
|
```console
|
|
|
|
$ cd PrivateBin/js
|
2019-05-11 14:51:18 -04:00
|
|
|
$ nyc mocha
|
2017-01-29 08:31:44 -05:00
|
|
|
```
|
|
|
|
|
2023-02-19 02:32:10 -05:00
|
|
|
### Property Based Unit Testing
|
2017-03-26 00:47:57 -04:00
|
|
|
|
|
|
|
In the JavaScript unit tests we use the JSVerify library to leverage property
|
|
|
|
based unit testing. Instead of artificially creating specific test cases to
|
|
|
|
cover all relevant paths of the tested code (with the generated coverage reports
|
|
|
|
providing means to check the tested paths), property based testing allows us to
|
|
|
|
describe the patterns of data that are valid input.
|
|
|
|
|
|
|
|
With each run of the tests, for each `jsc.property` 100 random inputs are
|
|
|
|
generated and tested. For example we tell the test to generate random strings,
|
|
|
|
which will include empty strings, numeric strings, long strings, unicode
|
|
|
|
sequences, etc. This is great for finding corner cases that one might not think
|
|
|
|
of when explicitly writing one test case at a time.
|
|
|
|
|
|
|
|
There is another benefit, too: When an error is found, JSVerify will try to find
|
|
|
|
the smallest, still failing test case for you and print this out including the
|
|
|
|
associated random number generator (RNG) state, so you can reproduce it easily:
|
|
|
|
|
|
|
|
```console
|
|
|
|
[...]
|
|
|
|
|
|
|
|
30 passing (3s)
|
|
|
|
1 failing
|
|
|
|
|
|
|
|
1) Helper getCookie returns the requested cookie:
|
|
|
|
Error: Failed after 30 tests and 11 shrinks. rngState: 88caf85079d32e416b; Counterexample: ["{", "9", "9", "YD8%fT"]; [" ", "_|K:"];
|
|
|
|
|
|
|
|
[...]
|
|
|
|
```
|
|
|
|
|
|
|
|
Of course it may just be that you need to adjust a test case if the random
|
|
|
|
pattern generated is ambiguous. In the above example the cookie string would
|
|
|
|
contain two identical keys "9", something that may not be valid, but that our
|
|
|
|
code could encounter and needs to be able to handle.
|
|
|
|
|
|
|
|
After you adjusted the code of the library or the test you can rerun the test
|
|
|
|
with the same RNG state as follows:
|
|
|
|
|
|
|
|
```console
|
2019-05-11 14:51:18 -04:00
|
|
|
$ nyc mocha test --jsverifyRngState 88caf85079d32e416b
|
2017-03-26 00:47:57 -04:00
|
|
|
```
|