Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add unit testing fix some bugs #4

Open
wants to merge 86 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 83 commits
Commits
Show all changes
86 commits
Select commit Hold shift + click to select a range
1dbca17
add editor config and phpunit lib
peter279k Sep 30, 2016
0fc70e5
use phpunit ^5.5
peter279k Sep 30, 2016
8ee6040
fix the HTTPoxy vulnerability
peter279k Sep 30, 2016
babd232
modify the editorconfig
peter279k Sep 30, 2016
03a0c15
add unit testing (part)
peter279k Sep 30, 2016
6d27628
complete the unit testing (initial)
peter279k Oct 1, 2016
5310c40
complete the exception unit testing
peter279k Oct 1, 2016
f345bee
modify the domain name and add encrypted user & password
peter279k Oct 1, 2016
1e6244f
add travis command for testing
peter279k Oct 1, 2016
55e794f
make generate_faek no time limit
peter279k Oct 1, 2016
faaf57d
move to install section
peter279k Oct 1, 2016
9b20f81
move to before_script section
peter279k Oct 1, 2016
ba81b74
move to script section
peter279k Oct 1, 2016
af0f307
fix the syntax error
peter279k Oct 1, 2016
6f4837d
add createKey and step 0
peter279k Oct 1, 2016
0af0489
add step0 (step 0 1 2 3)
peter279k Oct 1, 2016
ffd8821
add one line "brefore install" section
peter279k Oct 1, 2016
afe7c9f
move to before_install section
peter279k Oct 1, 2016
a9a2654
move to before script section
peter279k Oct 1, 2016
fe74ef2
modify upload file path and remote file path
peter279k Oct 1, 2016
e8a8388
modify the steps
peter279k Oct 1, 2016
3824bae
modify the steps add run step 2
peter279k Oct 1, 2016
1138551
add getPrivateKey
peter279k Oct 1, 2016
bd5edbc
add getPrivateKey before post request
peter279k Oct 1, 2016
a645dee
modify steps
peter279k Oct 1, 2016
b9b071f
add "" avoid access denied
peter279k Oct 1, 2016
00c74b4
add ftp_connect in generate_fake.php
peter279k Oct 1, 2016
7502bed
modify variable
peter279k Oct 1, 2016
edafd33
add the $domain value
peter279k Oct 1, 2016
fd481c3
modify ftp_connect
peter279k Oct 1, 2016
96cda34
modify variable
peter279k Oct 1, 2016
a8e7adc
fix variable
peter279k Oct 1, 2016
5532e4e
fix bugs
peter279k Oct 1, 2016
45249e5
use ftp_ssl_connect
peter279k Oct 1, 2016
9725b7e
modify the local file path
peter279k Oct 1, 2016
2d13787
modify the env
peter279k Oct 1, 2016
f7339dd
use php 5.6.26 set passive mode true
peter279k Oct 1, 2016
a342f7e
use 5.6.26 php version
peter279k Oct 1, 2016
f83016e
use phpenv switch 5.6.26 (perhaps)
peter279k Oct 1, 2016
789f097
add php-build for phpenv
peter279k Oct 1, 2016
bb07200
move phpenv other section
peter279k Oct 1, 2016
2812bab
add phpenv 5.6
peter279k Oct 1, 2016
e66482b
set timezone set
peter279k Oct 1, 2016
4bb0e45
add exit
peter279k Oct 1, 2016
2471ebd
add travis_wait
peter279k Oct 1, 2016
e6ce6b3
use secure
peter279k Oct 1, 2016
1689b45
change secure value
peter279k Oct 1, 2016
f1fb12a
use $arg pass to php file
peter279k Oct 1, 2016
72a8601
use real password(temporary) and username
peter279k Oct 1, 2016
2992749
using temporarily web hosting to test the Let's encrypt
peter279k Oct 2, 2016
3c0156d
modify testing method
peter279k Oct 2, 2016
24b7c58
modify method
peter279k Oct 2, 2016
6497c39
add deleteRemoteFile method
peter279k Oct 2, 2016
34be77a
modify testing methodf
peter279k Oct 2, 2016
750f7c8
add passive mode
peter279k Oct 2, 2016
504b974
remove phpenv to build the php5.6.26
peter279k Oct 2, 2016
1ad2054
use ftp_put ti add challenge file back
peter279k Oct 2, 2016
be2bd28
debug variable
peter279k Oct 2, 2016
ee092c0
modify exception class
peter279k Oct 2, 2016
8980b4b
add two testing method
peter279k Oct 2, 2016
3b23b04
finish the unit teston ver1
peter279k Oct 3, 2016
bbae44c
complete unit testing ver2
peter279k Oct 3, 2016
fab6203
testing only two core php file
peter279k Oct 3, 2016
83c3a28
change the editorconfig file
peter279k Oct 3, 2016
a62af70
remove the phpunit from the project
peter279k Oct 3, 2016
72d5476
revert the autoload inline
peter279k Oct 3, 2016
c6308a9
replace the var_dump to echo
peter279k Oct 3, 2016
a9b1719
rename the phpunit.xml to phpunit.xml.dist
peter279k Oct 3, 2016
c3096f6
modify some code style
peter279k Oct 3, 2016
2c15dc1
remove /** @test */
peter279k Oct 3, 2016
e2a5f32
recover the annotation
peter279k Oct 3, 2016
280b289
fix the phpunit command
peter279k Oct 3, 2016
60a7046
checkout the phpunit version
peter279k Oct 3, 2016
e9266ec
add phpunit version
peter279k Oct 3, 2016
7c5c0d6
replace annotation with prefix keyword test
peter279k Oct 3, 2016
a6ea9e5
remove load xdebug setting
peter279k Oct 3, 2016
5813b3d
do unit testing (ver2)
peter279k Oct 3, 2016
c5e76b9
add cacert.pem
peter279k Oct 3, 2016
1f85d38
complete the unit testing
peter279k Oct 4, 2016
2fa9cb2
add cacert.pem and fix the verify SSL cert problem
peter279k Oct 4, 2016
99a98d5
add pass arguments and execute method
peter279k Oct 4, 2016
cc3884b
get the correct method arg
peter279k Oct 4, 2016
97ac8f0
read ftpserver.json to get the domain name
peter279k Oct 4, 2016
ae358c6
fix some little issues
peter279k Oct 4, 2016
bee3ed6
fix arg issue
peter279k Oct 4, 2016
93efbe3
remove the default verify key
peter279k Oct 4, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true

[composer.json]
indent_style = space
indent_size = 4
47 changes: 47 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
language: php

php:
- 5.6
- 7.0
- nightly

sudo: false

env:
global:
- PATH="$HOME/.composer/vendor/bin:$PATH"

cache:
directories:
- $HOME/.composer/cache

matrix:
fast_finish: true
include:
- php: 5.6
env: COMPOSER_FLAGS="--prefer-lowest"
allow_failures:
- php: nightly

before_install:
- phpenv global 5.6

install:
- mkdir -p ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d && echo "memory_limit=-1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini
- mkdir -p build/logs
- composer global require satooshi/php-coveralls:@stable --no-update
- composer global update --prefer-dist --no-interaction
- composer update --prefer-dist --no-interaction $COMPOSER_FLAGS

before_script:
- composer require phpunit/phpunit
- travis_wait php generate_fake.php 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the goal of this?

- travis_wait php generate_fake.php 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

travis_wait is not necessary here.

- travis_wait php generate_fake.php 2
- travis_wait php generate_fake.php 3

script:
- vendor/bin/phpunit
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use global PHPUnit instead.


after_script:
- coveralls -v
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
"require": {
"php": "^5.6 || ^7.0",
"ext-openssl": "*",
"guzzlehttp/guzzle": "^6.1",
"guzzlehttp/guzzle": "^6.2",
"spomky-labs/base64url": "^1.0",
"doctrine/collections": "^1.3"
},
"require-dev": {
"analogic/lescript": "^0.1.3",
"symfony/var-dumper": "^2.7 || ^3.0"
"symfony/var-dumper": "^2.7 || ^3.0",
"sllh/php-cs-fixer-styleci-bridge": "^2.1"
},
"autoload": {
"psr-4": { "Nexy\\NexyCrypt\\": "src" }
Expand Down
13 changes: 10 additions & 3 deletions example_dns01.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,17 @@
$client = new NexyCrypt(null, 'https://acme-staging.api.letsencrypt.org/');

try {
$client->register();
$client->agreeTerms();
if (0 === $step) {
//create the required account private key
$client->create();
}

if (1 === $step) {
$client->register();
$client->agreeTerms();
}

if (2 === $step) {
@mkdir('public');

foreach ($domains as $domain) {
Expand All @@ -52,7 +59,7 @@
}
}

if (2 === $step) {
if (3 === $step) {
foreach ($domains as $domain) {
/** @var Http01Challenge $challenge */
$challenge = unserialize(file_get_contents('public/'.$domain.'/challenge'));
Expand Down
19 changes: 15 additions & 4 deletions example_http01.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,21 @@
$client = new NexyCrypt(null, 'https://acme-staging.api.letsencrypt.org/');

try {
$client->register();
$client->agreeTerms();

if (0 === $step) {
//create the required account private key
$client->create();
}

if (1 === $step) {
$client->register();
$client->agreeTerms();
}

if (2 === $step) {
$client->register();
$client->agreeTerms();

@mkdir('public');

foreach ($domains as $domain) {
Expand All @@ -49,7 +60,7 @@
}
}

if (2 === $step) {
if (3 === $step) {
foreach ($domains as $domain) {
/** @var Http01Challenge $challenge */
$challenge = unserialize(file_get_contents('public/'.$domain.'/challenge'));
Expand All @@ -70,7 +81,7 @@
}
}
} catch (AcmeApiException $e) {
dump($e->getDetails());
echo $e->getDetails();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a newline after this?


exit(1);
}
100 changes: 100 additions & 0 deletions generate_fake.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

// generate the fake .well-known folder and upload the folder to the testing web hosting.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generates


use Nexy\NexyCrypt\Authorization\Challenge\Http01Challenge;
use Nexy\NexyCrypt\Exception\AcmeApiException;
use Nexy\NexyCrypt\NexyCrypt;

require_once __DIR__.'/vendor/autoload.php';

if ($argc !== 2) {
echo 'You have to pass too many arguments.'.PHP_EOL;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean? This sentence is not understandable.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, you're right . This syntax is not readable so I use > to make syntax easy to understand.

exit(1);
}

$step = intval($argv[1]);

$accounts = json_decode(file_get_contents('tests/ftpserver.json'), true);
$domain = $accounts['ftpserver'];

// First commented line is for production.
//$client = new NexyCrypt();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commented code should not be here. Plus, I don't think this should be used for production.

$client = new NexyCrypt(null, 'https://acme-staging.api.letsencrypt.org/');

try {
if (0 === $step) {
// create the required account private key
$client->create();
}

if (1 === $step) {
$client->register();
$client->agreeTerms();
}

if (2 === $step) {
$client->register();
$client->agreeTerms();

@mkdir('tests/public');

$authorization = $client->authorize($domain);

$challenge = $authorization->getChallenges()->getHttp01();

@mkdir('tests/public/acme-challenge');
file_put_contents('tests/public/'.'acme-challenge'.'/'.$challenge->getFileName(), $challenge->getFileContent());
file_put_contents('tests/public/'.'acme-challenge'.'/challenge', serialize($challenge));
}

if (3 === $step) {
// upload file to the remote server
$accounts = json_decode(file_get_contents('tests/ftpserver.json'), true);
$user = $accounts['username'];

// the ftp server password is temporarily created and DO NOT use this value to do other things.
// the free web hosting will be closed or reset at the irregular time.
$password = $accounts['password'];
$ftpServer = $accounts['ftpserver'];

// set up basic ftp connection
$connectId = ftp_connect($ftpServer);

// login with username and password
$loginResult = ftp_login($connectId, $user, $password);

ftp_pasv($connectId, true);

if (!$loginResult) {
// PHP will already have raised an E_WARNING level message in this case
echo "can't login";
exit(1);
}

@ftp_mkdir($connectId, '.well-known');
@ftp_mkdir($connectId, '.well-known/acme-challenge');

ftp_chdir($connectId, '.well-known/acme-challenge');

// upload the files from the folders
$filePath = 'tests/public/acme-challenge';
$filesArr = scandir($filePath);
$fileCount = count($filesArr);
for($index=2;$index<$fileCount;$index++) {
$result = ftp_put($connectId, $filesArr[$index], $filePath.'/'.$filesArr[$index], FTP_ASCII);
if ($result === false) {
echo 'cannot upload file: '.$filesArr[$index];
exit(1);
}
}

ftp_close($connectId);
}

exit(0);
} catch (AcmeApiException $e) {
var_dump($e->getDetails());

exit(1);
}
25 changes: 25 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit
colors="true"
bootstrap="./vendor/autoload.php"
charset="UTF-8">

<testsuites>
<testsuite name="Testing Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory suffix=".php">./src/</directory>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for suffix key.

Please use this file: https://github.com/nexylan/slack-bundle/blob/master/phpunit.xml.dist

We have the same for all Nexylan's projects. 😉

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I remove the suffix attribute.

</whitelist>
</filter>

<logging>
<log type="coverage-clover" target="build/logs/clover.xml"/>
<log type="coverage-html" target="result/" />
</logging>

</phpunit>
31 changes: 26 additions & 5 deletions src/NexyCrypt.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,21 @@ public function __construct($privateKeyPath = null, $endpoint = null)
}

/**
* Generates or read privates key and starts registration.
* Generates private key.
*/
public function register()
public function create()
{
if (null === $this->privateKey) {
$this->privateKey = new PrivateKey($this->privateKeyPath);
}
}

/**
* Read private key and starts registration.
*/
public function register()
{
$this->privateKey = $this->getPrivateKey();

try {
$this->signedPostRequest(null === $this->regLocation ? 'acme/new-reg' : $this->regLocation, [
Expand Down Expand Up @@ -120,6 +128,8 @@ public function agreeTerms()
*/
public function authorize($domain)
{
$this->privateKey = $this->getPrivateKey();

$response = $this->signedPostRequest('acme/new-authz', [
'resource' => 'new-authz',
'identifier' => [
Expand All @@ -138,6 +148,8 @@ public function authorize($domain)
*/
public function verifyChallenge(ChallengeInterface $challenge)
{
$this->privateKey = $this->getPrivateKey();

$this->signedPostRequest($challenge->getUri(), [
'resource' => 'challenge',
'type' => $challenge->getType(),
Expand Down Expand Up @@ -169,7 +181,11 @@ public function verifyChallenge(ChallengeInterface $challenge)
public function generateCertificate(array $domains)
{
$certificate = new Certificate();
$privateKey = openssl_pkey_new();
$config = array(
'digest_alg' => 'SHA256',
'private_key_bits' => 4096,
);
$privateKey = openssl_pkey_new($config);
$privateKeyDetails = openssl_pkey_get_details($privateKey);
openssl_pkey_export($privateKey, $privateKeyOutput);

Expand Down Expand Up @@ -201,7 +217,8 @@ public function generateCertificate(array $domains)
'O' => 'Unknown',
], $privateKey, [
'config' => $csrConfPath,
'digest_alg' => 'sha256',
'digest_alg' => 'SHA256',
'private_key_bits' => 4096,
]);
openssl_csr_export($csr, $csrOut);
$certificate->setCsr($csrOut);
Expand All @@ -222,6 +239,8 @@ public function generateCertificate(array $domains)
*/
public function signCertificate(Certificate $certificate)
{
$this->privateKey = $this->getPrivateKey();

$this->signedPostRequest('acme/new-cert', [
'resource' => 'new-cert',
'csr' => Base64Url::encode($certificate->getRawCsr()),
Expand Down Expand Up @@ -252,6 +271,7 @@ public function signCertificate(Certificate $certificate)
*/
public function getPrivateKey()
{
$this->privateKey = new PrivateKey($this->privateKeyPath);
return $this->privateKey;
}

Expand Down Expand Up @@ -281,6 +301,7 @@ private function signedPostRequest($uri, array $payload)
$signed64 = Base64Url::encode($this->privateKey->sign($protected64.'.'.$payload64));

return $this->request('POST', $uri, [
'verify' => __DIR__.'/cacert.pem',
'json' => [
'header' => $header,
'protected' => $protected64,
Expand All @@ -299,7 +320,7 @@ private function signedPostRequest($uri, array $payload)
*
* @return ResponseInterface
*/
private function request($method, $uri, array $options = [])
private function request($method, $uri, array $options = ['verify' => __DIR__.'/cacert.pem'])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is still the same: Passing a array as default argument.

BTW, if you pass a path, the option name should be changed to be more consistent.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's a proper way to solve the SSL certificate problem or what's your suggestion ?
And the which the pem file name is more consistent ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see: #4 (comment)

And I'm not talking about the filename but the option name.

verify is not very appropriate for a path.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I know it. I will remove the default verify key then it should be fine.

{
try {
$response = $this->httpClient->request($method, $uri, $options);
Expand Down
7 changes: 6 additions & 1 deletion src/PrivateKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,14 @@ public function __construct($path)
if (is_file($this->path)) {
$this->key = openssl_pkey_get_private('file://'.$this->path);
} else {
$this->key = openssl_pkey_new();
$config = array(
'digest_alg' => 'SHA256',
'private_key_bits' => 2048,
);
$this->key = openssl_pkey_new($config);
file_put_contents($this->path, $this->getOutput());
}

$this->details = openssl_pkey_get_details($this->key);
}

Expand Down
Loading