103 lines
3.5 KiB
JavaScript
103 lines
3.5 KiB
JavaScript
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import crypto from "node:crypto";
|
|
|
|
import {
|
|
yuanToFen,
|
|
fenToYuan,
|
|
formatTimeExpire,
|
|
buildRequestSignature,
|
|
buildAuthHeader,
|
|
verifyMessageSignature,
|
|
decryptAesGcm,
|
|
} from "../utils/wechatpay.js";
|
|
|
|
test("元转分按小数点拆位,不经过浮点", () => {
|
|
assert.equal(yuanToFen("500.00"), 50000);
|
|
assert.equal(yuanToFen("500.5"), 50050);
|
|
assert.equal(yuanToFen(500), 50000);
|
|
assert.equal(yuanToFen("0.01"), 1);
|
|
assert.equal(yuanToFen("0.00"), 0);
|
|
// 超过两位小数、非数字、负数一律非法
|
|
assert.equal(yuanToFen("500.001"), null);
|
|
assert.equal(yuanToFen("abc"), null);
|
|
assert.equal(yuanToFen("-1"), null);
|
|
assert.equal(yuanToFen(""), null);
|
|
});
|
|
|
|
test("分转元固定两位小数", () => {
|
|
assert.equal(fenToYuan(50000), "500.00");
|
|
assert.equal(fenToYuan(1), "0.01");
|
|
assert.equal(fenToYuan(1.5), "");
|
|
});
|
|
|
|
test("time_expire 输出 RFC3339 北京时间", () => {
|
|
assert.equal(formatTimeExpire(new Date("2026-11-06T02:00:00.000Z")), "2026-11-06T10:00:00+08:00");
|
|
assert.equal(formatTimeExpire("invalid"), "");
|
|
});
|
|
|
|
test("请求签名可被同名公钥验证", () => {
|
|
const { privateKey, publicKey } = crypto.generateKeyPairSync("rsa", { modulusLength: 2048 });
|
|
const privatePem = privateKey.export({ type: "pkcs8", format: "pem" });
|
|
const publicPem = publicKey.export({ type: "spki", format: "pem" });
|
|
|
|
const method = "POST";
|
|
const urlPath = "/v3/pay/transactions/jsapi";
|
|
const timestamp = "1700000000";
|
|
const nonce = "abc123";
|
|
const body = '{"appid":"wx-test"}';
|
|
|
|
const signature = buildRequestSignature({ method, urlPath, timestamp, nonce, body, privateKey: privatePem });
|
|
const message = `${method}\n${urlPath}\n${timestamp}\n${nonce}\n${body}\n`;
|
|
assert.equal(verifyMessageSignature({ message, signature, publicKeyPem: publicPem }), true);
|
|
|
|
// 报文被篡改后验签失败
|
|
const tampered = `${method}\n${urlPath}\n${timestamp}\n${nonce}\n${body.replace("wx-test", "wx-evil")}\n`;
|
|
assert.equal(verifyMessageSignature({ message: tampered, signature, publicKeyPem: publicPem }), false);
|
|
});
|
|
|
|
test("Authorization 头使用 WECHATPAY2-SHA256-RSA2048 格式", () => {
|
|
const header = buildAuthHeader({
|
|
mchid: "1900000109",
|
|
serialNo: "SERIAL",
|
|
signature: "SIG",
|
|
timestamp: "1700000000",
|
|
nonce: "abc123",
|
|
});
|
|
assert.equal(
|
|
header,
|
|
'WECHATPAY2-SHA256-RSA2048 mchid="1900000109",nonce_str="abc123",signature="SIG",timestamp="1700000000",serial_no="SERIAL"'
|
|
);
|
|
});
|
|
|
|
test("AES-256-GCM 解密与 authTag 校验", () => {
|
|
const key = crypto.randomBytes(32);
|
|
const nonce = "abcdefghijkl";
|
|
const associatedData = "transaction";
|
|
const plaintext = JSON.stringify({ out_trade_no: "order-1", amount: { total: 1 } });
|
|
|
|
const cipher = crypto.createCipheriv("aes-256-gcm", key, Buffer.from(nonce, "utf-8"));
|
|
cipher.setAAD(Buffer.from(associatedData, "utf-8"));
|
|
const encrypted = Buffer.concat([cipher.update(plaintext, "utf-8"), cipher.final()]);
|
|
const authTag = cipher.getAuthTag();
|
|
const ciphertext = Buffer.concat([encrypted, authTag]).toString("base64");
|
|
|
|
const decrypted = decryptAesGcm({
|
|
ciphertext,
|
|
nonce,
|
|
associatedData,
|
|
apiV3Key: key,
|
|
});
|
|
assert.equal(decrypted, plaintext);
|
|
|
|
// 篡改密文 / 错误 authTag 必须抛错
|
|
const broken = Buffer.from(ciphertext, "base64");
|
|
broken[broken.length - 1] ^= 0xff;
|
|
assert.throws(() => decryptAesGcm({
|
|
ciphertext: broken.toString("base64"),
|
|
nonce,
|
|
associatedData,
|
|
apiV3Key: key,
|
|
}));
|
|
});
|