mirror of
https://github.com/kou029w/websri.git
synced 2025-01-18 16:08:16 +00:00
add match
This commit is contained in:
parent
ab57344af3
commit
90f6a0844a
2 changed files with 108 additions and 0 deletions
|
@ -191,6 +191,12 @@ export class IntegrityMetadataSet {
|
|||
return [...new Set(strongestHashAlgorithms)];
|
||||
}
|
||||
|
||||
match(integrity: IntegrityMetadataLike | string | null | undefined): boolean {
|
||||
return this.#set.some((integrityMetadata) =>
|
||||
integrityMetadata.match(integrity),
|
||||
);
|
||||
}
|
||||
|
||||
join(separator = " "): string {
|
||||
return this.#set.map(String).join(separator);
|
||||
}
|
||||
|
|
102
test/integrity-metadata-set/match.js
Normal file
102
test/integrity-metadata-set/match.js
Normal file
|
@ -0,0 +1,102 @@
|
|||
import assert from "node:assert/strict";
|
||||
import { test } from "node:test";
|
||||
import { IntegrityMetadataSet } from "../../dist/index.js";
|
||||
|
||||
test("if the hash values match, return true", function () {
|
||||
const integrityMetadata = new IntegrityMetadataSet(
|
||||
"sha256-MV9b23bQeMQ7isAGTkoBZGErH853yGk0W/yUx1iU7dM=",
|
||||
);
|
||||
|
||||
assert.strictEqual(
|
||||
integrityMetadata.match({
|
||||
alg: "sha256",
|
||||
val: "MV9b23bQeMQ7isAGTkoBZGErH853yGk0W/yUx1iU7dM=",
|
||||
}),
|
||||
true,
|
||||
);
|
||||
});
|
||||
|
||||
test("if the hash of one of the selected algorithms matches, return true", function () {
|
||||
const integrityMetadata = new IntegrityMetadataSet([
|
||||
"sha256-MV9b23bQeMQ7isAGTkoBZGErH853yGk0W/yUx1iU7dM=",
|
||||
"sha256-uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek=",
|
||||
]);
|
||||
|
||||
assert.strictEqual(
|
||||
integrityMetadata.match({
|
||||
alg: "sha256",
|
||||
val: "MV9b23bQeMQ7isAGTkoBZGErH853yGk0W/yUx1iU7dM=",
|
||||
}),
|
||||
true,
|
||||
);
|
||||
});
|
||||
|
||||
test("a string can be specified", function () {
|
||||
const integrityMetadata = new IntegrityMetadataSet(
|
||||
"sha256-MV9b23bQeMQ7isAGTkoBZGErH853yGk0W/yUx1iU7dM=",
|
||||
);
|
||||
|
||||
assert.strictEqual(
|
||||
integrityMetadata.match(
|
||||
"sha256-MV9b23bQeMQ7isAGTkoBZGErH853yGk0W/yUx1iU7dM=",
|
||||
),
|
||||
true,
|
||||
);
|
||||
});
|
||||
|
||||
test("if the hash algorithms are different, return false", function () {
|
||||
const integrityMetadata = new IntegrityMetadataSet(
|
||||
"sha256-MV9b23bQeMQ7isAGTkoBZGErH853yGk0W/yUx1iU7dM=",
|
||||
);
|
||||
|
||||
assert.strictEqual(
|
||||
integrityMetadata.match({
|
||||
alg: "sha512",
|
||||
val: "MJ7MSJwS1utMxA9QyQLytNDtd+5RGnx6m808qG1M2G+YndNbxf9JlnDaNCVbRbDP2DDoH2Bdz33FVC6TrpzXbw==",
|
||||
}),
|
||||
false,
|
||||
);
|
||||
});
|
||||
|
||||
test("if the hash values are different, return false", function () {
|
||||
const integrityMetadata = new IntegrityMetadataSet(
|
||||
"sha256-MV9b23bQeMQ7isAGTkoBZGErH853yGk0W/yUx1iU7dM=",
|
||||
);
|
||||
|
||||
assert.strictEqual(
|
||||
integrityMetadata.match({
|
||||
alg: "sha256",
|
||||
val: "uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek=",
|
||||
}),
|
||||
false,
|
||||
);
|
||||
});
|
||||
|
||||
test("if the hash algorithm is unsupported, return false", function () {
|
||||
const integrityMetadata = new IntegrityMetadataSet(
|
||||
"sha1-lDpwLQbzRZmu4fjajvn3KWAx1pk=",
|
||||
);
|
||||
|
||||
assert.strictEqual(
|
||||
integrityMetadata.match("sha1-lDpwLQbzRZmu4fjajvn3KWAx1pk="),
|
||||
false,
|
||||
);
|
||||
});
|
||||
|
||||
test("if null, return false", function () {
|
||||
const integrityMetadata = new IntegrityMetadataSet(null);
|
||||
|
||||
assert.strictEqual(integrityMetadata.match(null), false);
|
||||
});
|
||||
|
||||
test("if empty, return false", function () {
|
||||
const integrityMetadata = new IntegrityMetadataSet([]);
|
||||
|
||||
assert.strictEqual(integrityMetadata.match(""), false);
|
||||
});
|
||||
|
||||
test("if invalid value, return false", function () {
|
||||
const integrityMetadata = new IntegrityMetadataSet("md5\0/..invalid-value");
|
||||
|
||||
assert.strictEqual(integrityMetadata.match("md5\0/..invalid-value"), false);
|
||||
});
|
Loading…
Add table
Reference in a new issue