2024-09-17 19:08:56 +09:00
|
|
|
import assert from "node:assert";
|
|
|
|
import { test } from "node:test";
|
2024-10-02 18:50:34 +09:00
|
|
|
import { IntegrityMetadata } from "../../src/index.ts";
|
2024-09-17 19:08:56 +09:00
|
|
|
|
|
|
|
test("supports SHA-256", function () {
|
|
|
|
const integrityMetadata = new IntegrityMetadata(
|
|
|
|
"sha256-MV9b23bQeMQ7isAGTkoBZGErH853yGk0W/yUx1iU7dM=",
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.deepEqual(integrityMetadata, {
|
|
|
|
alg: "sha256",
|
|
|
|
val: "MV9b23bQeMQ7isAGTkoBZGErH853yGk0W/yUx1iU7dM=",
|
|
|
|
opt: [],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test("supports SHA-384", function () {
|
|
|
|
const integrityMetadata = new IntegrityMetadata(
|
|
|
|
"sha384-VbxVaw0v4Pzlgrpf4Huq//A1ZTY4x6wNVJTCpkwL6hzFczHHwSpFzbyn9MNKCJ7r",
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.deepEqual(integrityMetadata, {
|
|
|
|
alg: "sha384",
|
|
|
|
val: "VbxVaw0v4Pzlgrpf4Huq//A1ZTY4x6wNVJTCpkwL6hzFczHHwSpFzbyn9MNKCJ7r",
|
|
|
|
opt: [],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test("supports SHA-512", function () {
|
|
|
|
const integrityMetadata = new IntegrityMetadata(
|
|
|
|
"sha512-wVJ82JPBJHc9gRkRlwyP5uhX1t9dySJr2KFgYUwM2WOk3eorlLt9NgIe+dhl1c6ilKgt1JoLsmn1H256V/eUIQ==",
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.deepEqual(integrityMetadata, {
|
|
|
|
alg: "sha512",
|
|
|
|
val: "wVJ82JPBJHc9gRkRlwyP5uhX1t9dySJr2KFgYUwM2WOk3eorlLt9NgIe+dhl1c6ilKgt1JoLsmn1H256V/eUIQ==",
|
|
|
|
opt: [],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-09-18 14:28:56 +09:00
|
|
|
test("accepts options", function () {
|
2024-09-17 19:08:56 +09:00
|
|
|
const integrityMetadata = new IntegrityMetadata(
|
|
|
|
"sha256-MV9b23bQeMQ7isAGTkoBZGErH853yGk0W/yUx1iU7dM=?foo?bar",
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.deepEqual(integrityMetadata, {
|
|
|
|
alg: "sha256",
|
|
|
|
val: "MV9b23bQeMQ7isAGTkoBZGErH853yGk0W/yUx1iU7dM=",
|
|
|
|
opt: ["foo", "bar"],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-09-18 15:05:26 +09:00
|
|
|
test("accepts an IntegrityMetadata like object as input", function () {
|
2024-09-17 19:08:56 +09:00
|
|
|
const integrityMetadata = new IntegrityMetadata({
|
|
|
|
alg: "sha256",
|
|
|
|
val: "MV9b23bQeMQ7isAGTkoBZGErH853yGk0W/yUx1iU7dM=",
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.deepEqual(
|
|
|
|
integrityMetadata,
|
|
|
|
new IntegrityMetadata(
|
|
|
|
"sha256-MV9b23bQeMQ7isAGTkoBZGErH853yGk0W/yUx1iU7dM=",
|
|
|
|
),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2024-09-18 14:48:24 +09:00
|
|
|
test("trims leading and trailing whitespace", function () {
|
2024-09-17 19:08:56 +09:00
|
|
|
const integrityMetadata = new IntegrityMetadata(
|
2024-09-18 14:48:24 +09:00
|
|
|
"\t sha256-MV9b23bQeMQ7isAGTkoBZGErH853yGk0W/yUx1iU7dM=\u0020\u00a0\u1680\u2000\u2001\u2002\u3000",
|
2024-09-17 19:08:56 +09:00
|
|
|
);
|
|
|
|
|
|
|
|
assert.deepEqual(
|
|
|
|
integrityMetadata,
|
|
|
|
new IntegrityMetadata(
|
|
|
|
"sha256-MV9b23bQeMQ7isAGTkoBZGErH853yGk0W/yUx1iU7dM=",
|
|
|
|
),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("discards unsupported hash algorithm", function () {
|
|
|
|
const integrityMetadata = new IntegrityMetadata(
|
|
|
|
"sha1-lDpwLQbzRZmu4fjajvn3KWAx1pk=",
|
|
|
|
);
|
|
|
|
|
2024-10-02 18:50:34 +09:00
|
|
|
assert.deepEqual(integrityMetadata, new IntegrityMetadata(""));
|
2024-09-17 19:08:56 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
test("discards null input", function () {
|
|
|
|
const integrityMetadata = new IntegrityMetadata(null);
|
|
|
|
|
2024-10-02 18:50:34 +09:00
|
|
|
assert.deepEqual(integrityMetadata, new IntegrityMetadata(""));
|
2024-09-17 19:08:56 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
test("discards empty string input", function () {
|
|
|
|
const integrityMetadata = new IntegrityMetadata("");
|
|
|
|
|
2024-10-02 18:50:34 +09:00
|
|
|
assert.deepEqual(integrityMetadata, new IntegrityMetadata(""));
|
2024-09-17 19:08:56 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
test("discards invalid value", function () {
|
|
|
|
const integrityMetadata = new IntegrityMetadata("md5\0/..invalid-value");
|
|
|
|
|
2024-10-02 18:50:34 +09:00
|
|
|
assert.deepEqual(integrityMetadata, new IntegrityMetadata(""));
|
2024-09-17 19:08:56 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
test("discards invalid values in a list of multiple inputs", function () {
|
|
|
|
const integrityMetadata = new IntegrityMetadata(
|
|
|
|
"sha1-lDpwLQbzRZmu4fjajvn3KWAx1pk= md5\0/..invalid-value",
|
|
|
|
);
|
|
|
|
|
2024-10-02 18:50:34 +09:00
|
|
|
assert.deepEqual(integrityMetadata, new IntegrityMetadata(""));
|
2024-09-17 19:08:56 +09:00
|
|
|
});
|