Files
coruna-lab/tests/Unit/CorunaCryptoTest.php
2026-08-13 06:30:07 +08:00

63 lines
2.0 KiB
PHP

<?php
namespace Tests\Unit;
use App\Services\CorunaCrypto;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
class CorunaCryptoTest extends TestCase
{
#[Test]
public function session_key_matches_python_vector(): void
{
$crypto = new CorunaCrypto;
$this->assertSame('d2dbe79fbd3f94d09c16a4958b78d1d3', $crypto->deriveArchivePassword(0));
$this->assertSame('7@Lb"`W5_7gXN;68', $crypto->sessionKey());
$this->assertSame(
'9bcc53d7b7523451c4cc2923205575087e6ed47c9fd733be68b76a31ef1be1e4',
hash('sha256', $crypto->sessionKey())
);
}
#[Test]
public function encrypt_decrypt_round_trip(): void
{
$crypto = new CorunaCrypto;
$payload = ['id' => 'lab-device', 'pn' => 'io.metamask', 'apps' => ['io.metamask']];
$ts = '1722585600000';
$enc = $crypto->encryptJson($payload, $ts);
$this->assertSame($ts, $enc['timestamp']);
$this->assertSame($payload, $crypto->decryptJsonBody($enc['body'], $enc['timestamp']));
}
#[Test]
public function null_payload_round_trip(): void
{
$crypto = new CorunaCrypto;
$enc = $crypto->encryptJson(null, '1722585600001');
$this->assertNull($crypto->decryptJsonBody($enc['body'], $enc['timestamp']));
}
#[Test]
public function archive_password_appends_batch_base(): void
{
$crypto = new CorunaCrypto;
$this->assertSame($crypto->sessionKey().'0', $crypto->archivePassword('0'));
$this->assertSame($crypto->sessionKey().'1722585600000', $crypto->archivePassword('1722585600000'));
}
#[Test]
public function xxbb_session_key_round_trip(): void
{
$crypto = new CorunaCrypto('Ek8pl31K2yeHgQwy');
$this->assertSame('Ek8pl31K2yeHgQwy', $crypto->sessionKey());
$payload = ['d' => '000C30D83CD0402E', 'et' => 'heartbeat'];
$ts = '1786468227899';
$enc = $crypto->encryptJson($payload, $ts);
$this->assertSame($payload, $crypto->decryptJsonBody($enc['body'], $ts));
}
}