-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Improve tests for Attribute and EcAlgorithm
- Loading branch information
1 parent
003c47f
commit c6cfc9e
Showing
2 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import * as assert from "node:assert"; | ||
import { Attribute, TextObject } from "../src"; | ||
|
||
context("Attribute", function () { | ||
context("#toTextObject()", function () { | ||
it("should return a TextObject with correct values", function () { | ||
const attribute = new Attribute("1.2.3", [new ArrayBuffer(8)]); | ||
const textObject = attribute.toTextObject(); | ||
|
||
assert.strictEqual(textObject[TextObject.NAME], "1.2.3"); | ||
assert.ok(Array.isArray(textObject.Value)); | ||
assert.strictEqual(textObject.Value.length, 1); | ||
assert(textObject.Value[0] instanceof TextObject); | ||
}); | ||
}); | ||
|
||
context("#toTextObjectWithoutValue()", function () { | ||
it("should return a TextObject without the Value property", function () { | ||
const attribute = new Attribute("1.2.3", [new ArrayBuffer(8)]); | ||
const textObject = attribute.toTextObjectWithoutValue(); | ||
|
||
assert.strictEqual(textObject[TextObject.NAME], "1.2.3"); | ||
assert.strictEqual(textObject.Value, undefined); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
import * as assert from "assert"; | ||
import { EcAlgorithm, HashedAlgorithm } from "../src"; | ||
import { AlgorithmIdentifier } from "@peculiar/asn1-x509"; | ||
import { ECParameters, id_ecPublicKey, id_secp256r1 } from "@peculiar/asn1-ecc"; | ||
import { AsnConvert } from "@peculiar/asn1-schema"; | ||
|
||
context("EcAlgorithm", () => { | ||
let ecAlgorithm: EcAlgorithm; | ||
|
||
beforeEach(() => { | ||
ecAlgorithm = new EcAlgorithm(); | ||
}); | ||
|
||
const testVectors: { | ||
asnAlgorithm: string; | ||
webAlgorithm: HashedAlgorithm | EcKeyGenParams; | ||
}[] = [ | ||
{ | ||
asnAlgorithm: "1.2.840.10045.4.1", | ||
webAlgorithm: { name: "ECDSA", hash: { name: "SHA-1" } } | ||
}, | ||
{ | ||
asnAlgorithm: "1.2.840.10045.4.3.2", | ||
webAlgorithm: { name: "ECDSA", hash: { name: "SHA-256" } } | ||
}, | ||
{ | ||
asnAlgorithm: "1.2.840.10045.4.3.3", | ||
webAlgorithm: { name: "ECDSA", hash: { name: "SHA-384" } } | ||
}, | ||
{ | ||
asnAlgorithm: "1.2.840.10045.4.3.4", | ||
webAlgorithm: { name: "ECDSA", hash: { name: "SHA-512" } } | ||
}, | ||
{ | ||
asnAlgorithm: id_secp256r1, | ||
webAlgorithm: { name: "ECDSA", namedCurve: "P-256" } | ||
}, | ||
{ | ||
asnAlgorithm: "1.3.132.0.10", | ||
webAlgorithm: { name: "ECDSA", namedCurve: "K-256" } | ||
}, | ||
{ | ||
asnAlgorithm: "1.3.132.0.34", | ||
webAlgorithm: { name: "ECDSA", namedCurve: "P-384" } | ||
}, | ||
{ | ||
asnAlgorithm: "1.3.132.0.35", | ||
webAlgorithm: { name: "ECDSA", namedCurve: "P-521" } | ||
}, | ||
{ | ||
asnAlgorithm: "1.3.36.3.3.2.8.1.1.1", | ||
webAlgorithm: { name: "ECDSA", namedCurve: "brainpoolP160r1" } | ||
}, | ||
{ | ||
asnAlgorithm: "1.3.36.3.3.2.8.1.1.2", | ||
webAlgorithm: { name: "ECDSA", namedCurve: "brainpoolP160t1" } | ||
}, | ||
{ | ||
asnAlgorithm: "1.3.36.3.3.2.8.1.1.3", | ||
webAlgorithm: { name: "ECDSA", namedCurve: "brainpoolP192r1" } | ||
}, | ||
{ | ||
asnAlgorithm: "1.3.36.3.3.2.8.1.1.4", | ||
webAlgorithm: { name: "ECDSA", namedCurve: "brainpoolP192t1" } | ||
}, | ||
{ | ||
asnAlgorithm: "1.3.36.3.3.2.8.1.1.5", | ||
webAlgorithm: { name: "ECDSA", namedCurve: "brainpoolP224r1" } | ||
}, | ||
{ | ||
asnAlgorithm: "1.3.36.3.3.2.8.1.1.6", | ||
webAlgorithm: { name: "ECDSA", namedCurve: "brainpoolP224t1" } | ||
}, | ||
{ | ||
asnAlgorithm: "1.3.36.3.3.2.8.1.1.7", | ||
webAlgorithm: { name: "ECDSA", namedCurve: "brainpoolP256r1" } | ||
}, | ||
{ | ||
asnAlgorithm: "1.3.36.3.3.2.8.1.1.8", | ||
webAlgorithm: { name: "ECDSA", namedCurve: "brainpoolP256t1" } | ||
}, | ||
{ | ||
asnAlgorithm: "1.3.36.3.3.2.8.1.1.9", | ||
webAlgorithm: { name: "ECDSA", namedCurve: "brainpoolP320r1" } | ||
}, | ||
{ | ||
asnAlgorithm: "1.3.36.3.3.2.8.1.1.10", | ||
webAlgorithm: { name: "ECDSA", namedCurve: "brainpoolP320t1" } | ||
}, | ||
{ | ||
asnAlgorithm: "1.3.36.3.3.2.8.1.1.11", | ||
webAlgorithm: { name: "ECDSA", namedCurve: "brainpoolP384r1" } | ||
}, | ||
{ | ||
asnAlgorithm: "1.3.36.3.3.2.8.1.1.12", | ||
webAlgorithm: { name: "ECDSA", namedCurve: "brainpoolP384t1" } | ||
}, | ||
{ | ||
asnAlgorithm: "1.3.36.3.3.2.8.1.1.13", | ||
webAlgorithm: { name: "ECDSA", namedCurve: "brainpoolP512r1" } | ||
}, | ||
{ | ||
asnAlgorithm: "1.3.36.3.3.2.8.1.1.14", | ||
webAlgorithm: { name: "ECDSA", namedCurve: "brainpoolP512t1" } | ||
}, | ||
]; | ||
|
||
|
||
testVectors.forEach(({ asnAlgorithm, webAlgorithm }) => { | ||
const withValue = "hash" in webAlgorithm ? webAlgorithm.hash.name : webAlgorithm.namedCurve; | ||
|
||
context(`Algorithm ${webAlgorithm.name} with ${withValue}`, () => { | ||
|
||
it("#toAsnAlgorithm()", () => { | ||
const result = ecAlgorithm.toAsnAlgorithm(webAlgorithm); | ||
assert.ok(result); | ||
if ("hash" in webAlgorithm) { | ||
assert.strictEqual(result.algorithm, asnAlgorithm); | ||
} else { | ||
assert.strictEqual(result.algorithm, id_ecPublicKey); | ||
assert.ok(result.parameters); | ||
const asnParameters = AsnConvert.parse(result.parameters, ECParameters); | ||
assert.strictEqual(asnParameters.namedCurve, asnAlgorithm); | ||
} | ||
}); | ||
|
||
it("#toWebAlgorithm()", () => { | ||
const algIdentifier = "hash" in webAlgorithm | ||
? new AlgorithmIdentifier({ algorithm: asnAlgorithm }) | ||
: new AlgorithmIdentifier({ | ||
algorithm: id_ecPublicKey, | ||
parameters: AsnConvert.serialize(new ECParameters({ namedCurve: asnAlgorithm })), | ||
}); | ||
const result = ecAlgorithm.toWebAlgorithm(algIdentifier); | ||
assert.ok(result); | ||
assert.deepStrictEqual(result, webAlgorithm); | ||
}); | ||
}); | ||
}); | ||
}); |