You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

119 lines
3.8 KiB

<?php
declare(strict_types=1);
namespace tests;
use app\BusinessException;
use app\service\JwtService;
use PHPUnit\Framework\TestCase;
/**
* JWT 服务测试
*
* 不依赖数据库、Redis、HTTP:只测签发与校验这一纯逻辑
*/
final class JwtServiceTest extends TestCase
{
private function createService($ttl = 3600): JwtService
{
return new JwtService(str_repeat('k', 40), $ttl);
}
/** 签发的 token 必须能被自己校验回来,且 sub 就是账号ID */
public function testCreateAndCheckToken(): void
{
$jwt_service = $this->createService();
$token_data = $jwt_service->createToken(42);
$payload = $jwt_service->checkToken($token_data['token']);
self::assertSame(42, (int) $payload['sub']);
self::assertNotEmpty($payload['jti']);
self::assertGreaterThan(time(), (int) $payload['exp']);
}
/** token 应为三段式 */
public function testTokenHasThreeSegments(): void
{
$jwt_service = $this->createService();
$token_data = $jwt_service->createToken(1);
self::assertCount(3, explode('.', $token_data['token']));
}
/** 密钥不足 32 位必须拒绝启动:短密钥可被暴力破解,签出去的 token 等于没签 */
public function testShortSecretRejected(): void
{
$this->expectException(BusinessException::class);
new JwtService(str_repeat('k', 10), 3600);
}
/** 篡改载荷后签名对不上,必须拒绝 */
public function testTamperedPayloadRejected(): void
{
$jwt_service = $this->createService();
$token_data = $jwt_service->createToken(1);
$parts = explode('.', $token_data['token']);
// 把 sub 改掉但保留原签名
$parts[1] = rtrim(strtr(base64_encode(json_encode([
'sub' => 999,
'exp' => time() + 3600
])), '+/', '-_'), '=');
$this->expectException(BusinessException::class);
$jwt_service->checkToken(implode('.', $parts));
}
/** 换一个密钥签的 token 不能通过本服务的校验 */
public function testOtherSecretRejected(): void
{
$other = new JwtService(str_repeat('z', 40), 3600);
$token = $other->createToken(1)['token'];
$this->expectException(BusinessException::class);
$this->createService()->checkToken($token);
}
/** 过期的 token 必须拒绝 */
public function testExpiredTokenRejected(): void
{
// ttl 传负数,签出来就是已过期
$jwt_service = new JwtService(str_repeat('k', 40), -10);
$token = $jwt_service->createToken(1)['token'];
$this->expectException(BusinessException::class);
$jwt_service->checkToken($token);
}
/** 格式不对的 token 必须拒绝,不能当成合法载荷 */
public function testMalformedTokenRejected(): void
{
$this->expectException(BusinessException::class);
$this->createService()->checkToken('not-a-token');
}
/** tryCheckToken 失败时返回空数组而不是抛异常,供中间件安全使用 */
public function testTryCheckTokenReturnsEmptyOnFailure(): void
{
$jwt_service = $this->createService();
self::assertSame([], $jwt_service->tryCheckToken('bad'));
self::assertNotEmpty($jwt_service->tryCheckToken($jwt_service->createToken(7)['token']));
}
/** 载荷里不得出现 openid 之类的身份标识:token 会被前端持有并来回传输 */
public function testPayloadHasNoIdentityField(): void
{
$jwt_service = $this->createService();
$payload = $jwt_service->checkToken($jwt_service->createToken(7)['token']);
self::assertArrayNotHasKey('openid', $payload);
self::assertArrayNotHasKey('unionid', $payload);
}
}