Skip to content

Commit

Permalink
fix: relative paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Mastercuber committed Aug 2, 2023
1 parent 4320002 commit addf274
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 56 deletions.
42 changes: 16 additions & 26 deletions test/cert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,8 @@ describe("Certification Tests", () => {
describe("resolveCert", () => {
test("Resolves to certificate and key", async () => {
const c = await _private.resolveCert({
key: resolve("test", "fixture", "cert", "key.pem"),
cert: resolve("test", "fixture", "cert", "cert.pem"),
key: resolve("test", "cert", "key.pem"),
cert: resolve("test", "cert", "cert.pem"),
});
expect(c.key).toBeTruthy();
expect(c.key).toContain("-----BEGIN RSA PRIVATE KEY-----");
Expand All @@ -344,8 +344,8 @@ describe("Certification Tests", () => {

test("Resolves to certificate and encrypted key", async () => {
const c = await _private.resolveCert({
key: resolve("test", "fixture", "cert", "encrypted-key.pem"),
cert: resolve("test", "fixture", "cert", "cert.pem"),
key: resolve("test", "cert", "encrypted-key.pem"),
cert: resolve("test", "cert", "cert.pem"),
});
expect(c.key).toBeTruthy();
expect(c.key).toContain("-----BEGIN ENCRYPTED PRIVATE KEY-----");
Expand Down Expand Up @@ -389,15 +389,15 @@ describe("Certification Tests", () => {

expect(
_private.resolveCert({
key: resolve("test", "fixture", "cert", "key.pem"),
key: resolve("test", "cert", "key.pem"),
cert: "non-existing.pem",
}),
).rejects.toThrowError("ENOENT: no such file or directory");

expect(
_private.resolveCert({
key: "non-existing.pem",
cert: resolve("test", "fixture", "cert", "cert.pem"),
cert: resolve("test", "cert", "cert.pem"),
}),
).rejects.toThrowError("ENOENT: no such file or directory");
});
Expand All @@ -406,11 +406,11 @@ describe("Certification Tests", () => {
describe("resolvePfx", () => {
test("Resolves certificate and key from store (without store passphrase)", async () => {
const certs = await _private.resolveCert({
cert: resolve("test", "fixture", "cert", "cert.pem"),
key: resolve("test", "fixture", "cert", "key.pem"),
cert: resolve("test", "cert", "cert.pem"),
key: resolve("test", "cert", "key.pem"),
});
const pfx = await _private.resolvePfx({
pfx: resolve("test", "fixture", "cert", "keystore2.p12"),
pfx: resolve("test", "cert", "keystore2.p12"),
});
expect(pfx).toBeTruthy();
expect(pfx.safeContents.length).toEqual(2);
Expand All @@ -425,11 +425,11 @@ describe("Certification Tests", () => {
});
test("Resolves certificate and key from store (with store passphrase)", async () => {
const certs = await _private.resolveCert({
cert: resolve("test", "fixture", "cert", "cert.pem"),
key: resolve("test", "fixture", "cert", "key.pem"),
cert: resolve("test", "cert", "cert.pem"),
key: resolve("test", "cert", "key.pem"),
});
const pfx = await _private.resolvePfx({
pfx: resolve("test", "fixture", "cert", "keystore.p12"),
pfx: resolve("test", "cert", "keystore.p12"),
passphrase: "store-pw",
});
expect(pfx).toBeTruthy();
Expand All @@ -447,7 +447,7 @@ describe("Certification Tests", () => {
test("Throws error on wrong store password", () => {
expect(
_private.resolvePfx({
pfx: resolve("test", "fixture", "cert", "keystore.p12"),
pfx: resolve("test", "cert", "keystore.p12"),
passphrase: "wrong-pw",
}),
).rejects.toThrowError(
Expand All @@ -456,7 +456,7 @@ describe("Certification Tests", () => {

expect(
_private.resolvePfx({
pfx: resolve("test", "fixture", "cert", "keystore.p12"),
pfx: resolve("test", "cert", "keystore.p12"),
passphrase: "",
}),
).rejects.toThrowError(
Expand All @@ -467,23 +467,13 @@ describe("Certification Tests", () => {
test("Throws error on non existing store", () => {
expect(
_private.resolvePfx({
pfx: resolve(
"test",
"fixture",
"cert",
"non-existing-keystore.p12",
),
pfx: resolve("test", "cert", "non-existing-keystore.p12"),
}),
).rejects.toThrowError("ENOENT: no such file or directory");

expect(
_private.resolvePfx({
pfx: resolve(
"test",
"fixture",
"cert",
"non-existing-keystore.p12",
),
pfx: resolve("test", "cert", "non-existing-keystore.p12"),
passphrase: "wrong-pw",
}),
).rejects.toThrowError("ENOENT: no such file or directory");
Expand Down
26 changes: 6 additions & 20 deletions test/generate-cert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,17 @@ export default async function generateCert() {
});

const decryptedKey = forge.pki.decryptRsaPrivateKey(certs.cert.key, pw);
mkdirSync(resolve("test", "fixture", "cert"));
mkdirSync(resolve("test", "cert"));
writeFileSync(resolve("test", "cert", "cert.pem"), certs.cert.cert);
writeFileSync(resolve("test", "cert", "encrypted-key.pem"), certs.cert.key);
writeFileSync(
resolve("test", "fixture", "cert", "cert.pem"),
certs.cert.cert,
);
writeFileSync(
resolve("test", "fixture", "cert", "encrypted-key.pem"),
certs.cert.key,
);
writeFileSync(
resolve("test", "fixture", "cert", "key.pem"),
resolve("test", "cert", "key.pem"),
forge.pki.privateKeyToPem(decryptedKey),
);
const pfx = convertToPFX(certs.cert.cert, certs.cert.key, "store-pw");
const pfx2 = convertToPFX(certs.cert.cert, certs.cert.key, "");
writeFileSync(
resolve("test", "fixture", "cert", "keystore.p12"),
pfx,
"binary",
);
writeFileSync(
resolve("test", "fixture", "cert", "keystore2.p12"),
pfx2,
"binary",
);
writeFileSync(resolve("test", "cert", "keystore.p12"), pfx, "binary");
writeFileSync(resolve("test", "cert", "keystore2.p12"), pfx2, "binary");
}

function convertToPFX(
Expand Down
18 changes: 9 additions & 9 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ describe("listhen", () => {
listener = await listen(handle, {
https: {
// eslint-disable-next-line unicorn/prefer-module
key: resolve(__dirname, "fixture", "cert", "key.pem"),
key: resolve(__dirname, "cert", "key.pem"),
// eslint-disable-next-line unicorn/prefer-module
cert: resolve(__dirname, "fixture", "cert", "cert.pem"),
cert: resolve(__dirname, "cert", "cert.pem"),
},
hostname: "localhost",
});
Expand All @@ -70,9 +70,9 @@ describe("listhen", () => {
listener = await listen(handle, {
https: {
// eslint-disable-next-line unicorn/prefer-module
key: resolve(__dirname, "fixture", "cert", "encrypted-key.pem"),
key: resolve(__dirname, "cert", "encrypted-key.pem"),
// eslint-disable-next-line unicorn/prefer-module
cert: resolve(__dirname, "fixture", "cert", "cert.pem"),
cert: resolve(__dirname, "cert", "cert.pem"),
passphrase: "cert-pw",
},
hostname: "localhost",
Expand All @@ -85,9 +85,9 @@ describe("listhen", () => {
listen(handle, {
https: {
// eslint-disable-next-line unicorn/prefer-module
key: resolve(__dirname, "fixture/cert/encrypted-key.pem"),
key: resolve(__dirname, "cert", "encrypted-key.pem"),
// eslint-disable-next-line unicorn/prefer-module
cert: resolve(__dirname, "fixture/cert/cert.pem"),
cert: resolve(__dirname, "cert", "cert.pem"),
passphrase: "wrong-pw",
},
hostname: "localhost",
Expand All @@ -99,7 +99,7 @@ describe("listhen", () => {
const listener = await listen(handle, {
https: {
// eslint-disable-next-line unicorn/prefer-module
pfx: resolve(__dirname, "fixture/cert/keystore.p12"),
pfx: resolve(__dirname, "cert", "keystore.p12"),
passphrase: "store-pw",
},
hostname: "localhost",
Expand All @@ -112,7 +112,7 @@ describe("listhen", () => {
listen(handle, {
https: {
// eslint-disable-next-line unicorn/prefer-module
pfx: resolve(__dirname, "fixture/cert/keystore.p12"),
pfx: resolve(__dirname, "cert", "keystore.p12"),
},
hostname: "localhost",
}),
Expand All @@ -126,7 +126,7 @@ describe("listhen", () => {
listen(handle, {
https: {
// eslint-disable-next-line unicorn/prefer-module
pfx: resolve(__dirname, "fixture/cert/keystore.p12"),
pfx: resolve(__dirname, "cert", "keystore.p12"),
passphrase: "wrong-pw",
},
hostname: "localhost",
Expand Down
2 changes: 1 addition & 1 deletion test/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import generateCert from "./generate-cert";

export async function setup() {
process.env.NO_COLOR = "1";
if (!existsSync(resolve("test", "fixture", "cert", "cert.pem"))) {
if (!existsSync(resolve("test", "cert", "cert.pem"))) {
// eslint-disable-next-line unicorn/prefer-top-level-await
await generateCert();
}
Expand Down

0 comments on commit addf274

Please sign in to comment.