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.
35 lines
1.7 KiB
35 lines
1.7 KiB
import 'dart:convert';
|
|
|
|
import 'package:eaimar/data/services/network/api_crypto.dart';
|
|
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
/// 网关加密协议的兼容性金测试。
|
|
/// 密文由 openssl 按服务端口径生成(lego/utils/crypto/aes/cbc.go:AES-CBC,IV 固定 16 个 '0',
|
|
/// PKCS5/7 填充,base64),不是用同一套 Dart 代码加密再解密——那只能证明自洽,证明不了与 Go 端一致。
|
|
void main() {
|
|
const key = '0123456789abcdef';
|
|
const cipher = '+DbMGxdFZ8VkBMivLb5FM7VgvUF+/ISnA8RRO6Nj3kyGbD3hV9fzAvC+DsTUoLiFVnHETIUGxCROudDtF4VyRJlAQQEMxpCf9Zx+19LMlFSPtWizfg+7SaJAJLZeY/lmZbCchmmMUkmFLmlyQMpaXw==';
|
|
const plain = '{"code":0,"msg":"ok","data":{"env":{"A":"1"},"thirdsvcs":[{"id":"stt_azure","fields":{"region":"eastasia"}}]}}';
|
|
|
|
test('能解开 Go 端口径生成的密文', () {
|
|
dotenv.testLoad(fileInput: 'GATEWAY_ENCRYPT_KEY=$key');
|
|
expect(ApiCrypto.enabled, isTrue);
|
|
final out = ApiCrypto.decrypt(cipher);
|
|
expect(out, plain);
|
|
final j = jsonDecode(out) as Map<String, dynamic>;
|
|
expect((j['data'] as Map)['thirdsvcs'], isA<List>());
|
|
});
|
|
|
|
test('密钥长度不是 16/24/32 → enabled 为 false,走明文 v1', () {
|
|
dotenv.testLoad(fileInput: 'GATEWAY_ENCRYPT_KEY=short');
|
|
expect(ApiCrypto.enabled, isFalse);
|
|
dotenv.testLoad(fileInput: 'GATEWAY_ENCRYPT_KEY=');
|
|
expect(ApiCrypto.enabled, isFalse);
|
|
});
|
|
|
|
test('密钥不一致 → 解密抛错(去 PKCS7 填充失败),不会静默给出乱码', () {
|
|
dotenv.testLoad(fileInput: 'GATEWAY_ENCRYPT_KEY=fedcba9876543210');
|
|
expect(() => ApiCrypto.decrypt(cipher), throwsA(anything));
|
|
});
|
|
}
|
|
|