Skip to content

Commit

Permalink
Prevent code coverage timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
paragonie-security committed Jul 21, 2021
1 parent 52b5ab1 commit a2ef137
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
1 change: 1 addition & 0 deletions phpunit.full.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
stopOnFailure="false">
<php>
<const name="PHPUNIT_DEBUG" value="false" />
<const name="PHPECC_COVERAGE" value="false" />
<ini name="display_errors" value="On" />
<ini name="error_reporting" value="32767" />
</php>
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
stopOnFailure="false">
<php>
<const name="PHPUNIT_DEBUG" value="false" />
<const name="PHPECC_COVERAGE" value="true" />
<ini name="display_errors" value="On" />
<ini name="error_reporting" value="32767" />
</php>
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

namespace Mdanter\Ecc\Tests;

use Mdanter\Ecc\Math\ConstantTimeMath;
use Mdanter\Ecc\Math\GmpMath;
use Mdanter\Ecc\Math\GmpMathInterface;
use Mdanter\Ecc\Math\MathAdapterFactory;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -42,6 +45,19 @@ protected function _getAdapters(array $extra = null)
return $result;
}

/**
* @param GmpMathInterface $math
* @return GmpMathInterface
*/
public function skipConstantTimeOnCoverage(GmpMathInterface $math): GmpMathInterface
{
if (!defined('PHPECC_COVERAGE')) define('PHPECC_COVERAGE', false);
if ($math instanceof ConstantTimeMath && !PHPECC_COVERAGE) {
return new GmpMath();
}
return $math;
}

public function getAdapters()
{
return $this->_getAdapters();
Expand Down
41 changes: 39 additions & 2 deletions tests/unit/Curves/SpecBasedCurveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ public function getKeypairsTestSet()
*/
public function testGetPublicKey($name, GeneratorPoint $generator, $k, $expectedX, $expectedY)
{
static $ran = 0;
if (PHPECC_COVERAGE) {
if (++$ran > 10) {
$this->markTestSkipped('This is way too slow during code coverage tests');
}
}
$adapter = $generator->getAdapter();

$privateKey = $generator->getPrivateKeyFrom(gmp_init($k, 10));
Expand Down Expand Up @@ -291,6 +297,12 @@ public function getDiffieHellmanTestSet()
*/
public function testGetDiffieHellmanSharedSecret(GeneratorPoint $generator, $alice, $bob, $expectedX)
{
static $ran = 0;
if (PHPECC_COVERAGE) {
if (++$ran > 10) {
$this->markTestSkipped('This is way too slow during code coverage tests');
}
}
$adapter = $generator->getAdapter();
$alicePrivKey = $generator->getPrivateKeyFrom(gmp_init($alice, 10));
$bobPrivKey = $generator->getPrivateKeyFrom(gmp_init($bob, 10));
Expand Down Expand Up @@ -340,6 +352,12 @@ public function getHmacTestSet()
*/
public function testHmacSignatures(GeneratorPoint $G, $privKey, $algo, $message, $eK, $eR, $eS)
{
static $ran = 0;
if (PHPECC_COVERAGE) {
if (++$ran > 10) {
$this->markTestSkipped('This is way too slow during code coverage tests');
}
}
$math = $G->getAdapter();

$privateKey = $G->getPrivateKeyFrom(gmp_init($privKey, 16));
Expand Down Expand Up @@ -417,7 +435,14 @@ public function getEcdsaSignFixtures()
*/
public function testEcdsaSignatureGeneration(GeneratorPoint $G, $privKeyHex, $kHex, $eR, $eS, $hashHex = null, $msg = null, $algo = null)
{
$math = $G->getAdapter();
static $ran = 0;
if (PHPECC_COVERAGE) {
if (++$ran > 10) {
$this->markTestSkipped('This is way too slow during code coverage tests');
}
}

$math = $this->skipConstantTimeOnCoverage($G->getAdapter());
$signer = new Signer($math);
$privateKey = $G->getPrivateKeyFrom(gmp_init($privKeyHex, 10));
$k = gmp_init($kHex, 16);
Expand Down Expand Up @@ -447,6 +472,12 @@ public function testEcdsaSignatureGeneration(GeneratorPoint $G, $privKeyHex, $kH
*/
public function getEcdsaVerifyFixtures()
{
static $ran = 0;
if (PHPECC_COVERAGE) {
if (++$ran > 10) {
$this->markTestSkipped('This is way too slow during code coverage tests');
}
}
$files = $this->readTestFixture('ecdsa-verify');
$datasets = [];

Expand Down Expand Up @@ -506,7 +537,13 @@ public function getEcdsaVerifyFixtures()
*/
public function testEcdsaSignatureVerification(GeneratorPoint $G, $eR, $eS, $x, $y, $result, $cause = null, $hashHex = null, $msg = null, $algo = null)
{
$math = $G->getAdapter();
static $ran = 0;
if (PHPECC_COVERAGE) {
if (++$ran > 10) {
$this->markTestSkipped('This is way too slow during code coverage tests');
}
}
$math = $this->skipConstantTimeOnCoverage($G->getAdapter());
$signer = new Signer($math);
try {
$publicKey = $G->getPublicKeyFrom(gmp_init($x, 16), gmp_init($y, 16));
Expand Down

0 comments on commit a2ef137

Please sign in to comment.