| 1 |
<?php |
| 2 |
|
| 3 |
use PHPUnit\Framework\TestCase; |
| 4 |
use AltchaOrg\Altcha\Algorithm; |
| 5 |
use AltchaOrg\Altcha\ChallengeOptions; |
| 6 |
use AltchaOrg\Altcha\Altcha; |
| 7 |
use AltchaOrg\Altcha\Challenge; |
| 8 |
use AltchaOrg\Altcha\Solution; |
| 9 |
|
| 10 |
class AltchaTest extends TestCase |
| 11 |
{ |
| 12 |
public function testCreateChallenge() |
| 13 |
{ |
| 14 |
$options = new ChallengeOptions([ |
| 15 |
'algorithm' => Algorithm::SHA256, |
| 16 |
'hmacKey' => 'test-key' |
| 17 |
]); |
| 18 |
|
| 19 |
$challenge = Altcha::createChallenge($options); |
| 20 |
|
| 21 |
$this->assertInstanceOf(Challenge::class, $challenge); |
| 22 |
$this->assertEquals(Algorithm::SHA256, $challenge->algorithm); |
| 23 |
$this->assertNotEmpty($challenge->challenge); |
| 24 |
$this->assertEquals(1e6, $challenge->maxnumber); |
| 25 |
$this->assertNotEmpty($challenge->salt); |
| 26 |
$this->assertNotEmpty($challenge->signature); |
| 27 |
} |
| 28 |
|
| 29 |
public function testVerifySolution() |
| 30 |
{ |
| 31 |
$options = new ChallengeOptions([ |
| 32 |
'algorithm' => Algorithm::SHA256, |
| 33 |
'number' => 10, |
| 34 |
'hmacKey' => 'test-key' |
| 35 |
]); |
| 36 |
|
| 37 |
$challenge = Altcha::createChallenge($options); |
| 38 |
$payload = [ |
| 39 |
'algorithm' => $challenge->algorithm, |
| 40 |
'challenge' => $challenge->challenge, |
| 41 |
'number' => 10, |
| 42 |
'salt' => $challenge->salt, |
| 43 |
'signature' => $challenge->signature, |
| 44 |
]; |
| 45 |
|
| 46 |
$isValid = Altcha::verifySolution($payload, 'test-key'); |
| 47 |
|
| 48 |
$this->assertTrue($isValid); |
| 49 |
} |
| 50 |
|
| 51 |
public function testVerifyFieldsHash() |
| 52 |
{ |
| 53 |
$formData = [ |
| 54 |
'field1' => 'value1', |
| 55 |
'field2' => 'value2' |
| 56 |
]; |
| 57 |
|
| 58 |
$fields = ['field1', 'field2']; |
| 59 |
$fieldsHash = Altcha::hashHex(Algorithm::SHA256, "value1\nvalue2"); |
| 60 |
|
| 61 |
$isValid = Altcha::verifyFieldsHash($formData, $fields, $fieldsHash, Algorithm::SHA256); |
| 62 |
|
| 63 |
$this->assertTrue($isValid); |
| 64 |
} |
| 65 |
|
| 66 |
public function testSolveChallenge() |
| 67 |
{ |
| 68 |
$options = new ChallengeOptions([ |
| 69 |
'algorithm' => Algorithm::SHA256, |
| 70 |
'hmacKey' => 'test-key', |
| 71 |
'maxNumber' => 100 |
| 72 |
]); |
| 73 |
|
| 74 |
$challenge = Altcha::createChallenge($options); |
| 75 |
|
| 76 |
$solution = Altcha::solveChallenge($challenge->challenge, $challenge->salt, $challenge->algorithm, $challenge->maxnumber); |
| 77 |
|
| 78 |
$this->assertInstanceOf(Solution::class, $solution); |
| 79 |
$this->assertEquals($solution->number, $solution->number); |
| 80 |
$this->assertGreaterThan(0, $solution->took); |
| 81 |
} |
| 82 |
|
| 83 |
public function testInvalidPayload() |
| 84 |
{ |
| 85 |
$isValid = Altcha::verifySolution('I am invalid', 'key'); |
| 86 |
$this->assertFalse($isValid); |
| 87 |
|
| 88 |
$isValid = Altcha::verifyServerSignature('I am invalid', 'key'); |
| 89 |
$this->assertFalse($isValid); |
| 90 |
} |
| 91 |
} |
| 92 |
|