-
Notifications
You must be signed in to change notification settings - Fork 4
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
base: master
Are you sure you want to change the base?
Changes from 83 commits
1dbca17
0fc70e5
8ee6040
babd232
03a0c15
6d27628
5310c40
f345bee
1e6244f
55e794f
faaf57d
9b20f81
ba81b74
af0f307
6f4837d
0af0489
ffd8821
afe7c9f
a9a2654
fe74ef2
e8a8388
3824bae
1138551
bd5edbc
a645dee
b9b071f
00c74b4
7502bed
edafd33
fd481c3
96cda34
a8e7adc
5532e4e
45249e5
9725b7e
2d13787
f7339dd
a342f7e
f83016e
789f097
bb07200
2812bab
e66482b
4bb0e45
2471ebd
e6ce6b3
1689b45
f1fb12a
72a8601
2992749
3c0156d
24b7c58
6497c39
34be77a
750f7c8
504b974
1ad2054
be2bd28
ee092c0
8980b4b
3b23b04
bbae44c
fab6203
83c3a28
a62af70
72d5476
c6308a9
a9b1719
c3096f6
2c15dc1
e2a5f32
280b289
60a7046
e9266ec
7c5c0d6
a6ea9e5
5813b3d
c5e76b9
1f85d38
2fa9cb2
99a98d5
cc3884b
97ac8f0
ae358c6
bee3ed6
93efbe3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
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 | ||
- travis_wait php generate_fake.php 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
- travis_wait php generate_fake.php 2 | ||
- travis_wait php generate_fake.php 3 | ||
|
||
script: | ||
- vendor/bin/phpunit | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use global PHPUnit instead. |
||
|
||
after_script: | ||
- coveralls -v |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
|
@@ -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')); | ||
|
@@ -70,7 +81,7 @@ | |
} | ||
} | ||
} catch (AcmeApiException $e) { | ||
dump($e->getDetails()); | ||
echo $e->getDetails(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add a newline after this? |
||
|
||
exit(1); | ||
} |
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you mean? This sentence is not understandable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, you're right . This syntax is not readable so I use |
||
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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} |
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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for Please use this file: https://github.com/nexylan/slack-bundle/blob/master/phpunit.xml.dist We have the same for all Nexylan's projects. 😉 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, [ | ||
|
@@ -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' => [ | ||
|
@@ -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(), | ||
|
@@ -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); | ||
|
||
|
@@ -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); | ||
|
@@ -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()), | ||
|
@@ -252,6 +271,7 @@ public function signCertificate(Certificate $certificate) | |
*/ | ||
public function getPrivateKey() | ||
{ | ||
$this->privateKey = new PrivateKey($this->privateKeyPath); | ||
return $this->privateKey; | ||
} | ||
|
||
|
@@ -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, | ||
|
@@ -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']) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
There was a problem hiding this comment.
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?