- 添加了 Laravel 项目配置文件,包括 IDE 字典、模块定义和版本控制设置 - 配置了 PHP 开发环境,包含依赖路径、代码质量工具和测试框架设置 - 实现了模型转换特性 (ModelCastSetter),支持对象到数组的自动序列化 - 创建了通用数组处理类 (Arrayable),提供对象与数组间的便捷转换方法 - 开发了 RSA 加密解密工具类,支持证书解析、密钥验证和数据安全操作 - 编写了单元测试和功能测试示例,确保代码质量和功能正确性 - 设置了 Pest 测试框架基础配置,便于后续扩展测试用例 - 添加了 Carbon 和 Paratest 等常用工具的二进制代理脚本
125 lines
3.8 KiB
PHP
125 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace Jltx\Support\Tests\Unit;
|
|
|
|
use Jltx\Support\Security\RSA;
|
|
use RuntimeException;
|
|
|
|
beforeEach(function () {
|
|
// 生成测试用的密钥对
|
|
$this->keyPair = RSA::generateKeyPair();
|
|
$this->privateKeyContent = $this->keyPair['private_key'];
|
|
$this->publicKeyContent = $this->keyPair['public_key'];
|
|
|
|
// 格式化密钥
|
|
$this->formattedPrivateKey = RSA::privateKeyFormat($this->privateKeyContent);
|
|
$this->formattedPublicKey = RSA::publicKeyFormat($this->publicKeyContent);
|
|
|
|
// 获取密钥资源
|
|
$this->privateKey = RSA::getPrivateKey($this->formattedPrivateKey);
|
|
$this->publicKey = RSA::getPublicKey($this->formattedPublicKey);
|
|
});
|
|
|
|
it('can generate a key pair', function () {
|
|
$keyPair = RSA::generateKeyPair();
|
|
|
|
expect($keyPair)->toBeArray()
|
|
->and($keyPair)->toHaveKey('private_key')
|
|
->and($keyPair)->toHaveKey('public_key')
|
|
->and($keyPair['private_key'])->toBeString()
|
|
->and($keyPair['public_key'])->toBeString();
|
|
});
|
|
|
|
it('can format private key', function () {
|
|
$formatted = RSA::privateKeyFormat($this->privateKeyContent);
|
|
|
|
expect($formatted)->toContain('PRIVATE KEY');
|
|
});
|
|
|
|
it('can format public key', function () {
|
|
$formatted = RSA::publicKeyFormat($this->publicKeyContent);
|
|
|
|
expect($formatted)->toContain('PUBLIC KEY');
|
|
});
|
|
|
|
it('can get private key resource', function () {
|
|
$privateKey = RSA::getPrivateKey($this->formattedPrivateKey);
|
|
|
|
expect($privateKey)->toBeObject();
|
|
});
|
|
|
|
it('can get public key resource', function () {
|
|
$publicKey = RSA::getPublicKey($this->formattedPublicKey);
|
|
|
|
expect($publicKey)->toBeObject();
|
|
});
|
|
|
|
it('can encrypt and decrypt data', function () {
|
|
$plaintext = 'Hello, World!';
|
|
|
|
$encrypted = RSA::encrypt($plaintext, $this->publicKey);
|
|
$decrypted = RSA::decrypt($encrypted, $this->privateKey);
|
|
|
|
expect($encrypted)->toBeString()
|
|
->and($decrypted)->toBeString()
|
|
->and($decrypted)->toEqual($plaintext);
|
|
});
|
|
|
|
it('can sign and verify data', function () {
|
|
$message = 'Hello, World!';
|
|
$algorithm = OPENSSL_ALGO_SHA256;
|
|
|
|
$signature = RSA::sign($message, $this->privateKey, $algorithm);
|
|
$isValid = RSA::verify($message, $signature, $this->publicKey, $algorithm);
|
|
|
|
expect($signature)->toBeString()
|
|
->and($isValid)->toBeTrue();
|
|
});
|
|
|
|
it('returns false for invalid signature', function () {
|
|
$message = 'Hello, World!';
|
|
$fakeMessage = 'Fake Message';
|
|
$algorithm = OPENSSL_ALGO_SHA256;
|
|
|
|
$signature = RSA::sign($message, $this->privateKey, $algorithm);
|
|
$isValid = RSA::verify($fakeMessage, $signature, $this->publicKey, $algorithm);
|
|
|
|
expect($isValid)->toBeFalse();
|
|
});
|
|
|
|
it('validates private key content', function () {
|
|
$isValid = RSA::validatePrivateKeyContent($this->privateKeyContent);
|
|
|
|
expect($isValid)->toBeTrue();
|
|
});
|
|
|
|
it('validates invalid private key content as false', function () {
|
|
$isValid = RSA::validatePrivateKeyContent('invalid private key');
|
|
|
|
expect($isValid)->toBeFalse();
|
|
});
|
|
|
|
it('validates public key content', function () {
|
|
$isValid = RSA::validatePublicKeyContent($this->publicKeyContent);
|
|
|
|
expect($isValid)->toBeTrue();
|
|
});
|
|
|
|
it('validates invalid public key content as false', function () {
|
|
$isValid = RSA::validatePublicKeyContent('invalid public key');
|
|
|
|
expect($isValid)->toBeFalse();
|
|
});
|
|
|
|
it('throws exception for invalid private key', function () {
|
|
$invalidKey = "-----BEGIN PRIVATE KEY-----\ninvalidcontent\n-----END PRIVATE KEY-----";
|
|
|
|
expect(fn () => RSA::getPrivateKey($invalidKey))->toThrow(RuntimeException::class, '非法私钥');
|
|
});
|
|
|
|
it('throws exception for invalid public key', function () {
|
|
$invalidKey = "-----BEGIN PUBLIC KEY-----\ninvalidcontent\n-----END PUBLIC KEY-----";
|
|
|
|
expect(fn () => RSA::getPublicKey($invalidKey))->toThrow(RuntimeException::class, '非法公钥');
|
|
});
|