mirror of
https://github.com/Drop-OSS/droplet.git
synced 2025-11-09 20:12:18 +10:00
fix: move to js tests
This commit is contained in:
96
__test__/manifest.spec.mjs
Normal file
96
__test__/manifest.spec.mjs
Normal file
@ -0,0 +1,96 @@
|
||||
import test from "ava";
|
||||
import fs from "node:fs";
|
||||
import path from "path";
|
||||
|
||||
import { generateManifest } from "../index.js";
|
||||
|
||||
test("numerous small file", async (t) => {
|
||||
// Setup test dir
|
||||
const dirName = "./.test/nsf";
|
||||
if (fs.existsSync(dirName)) fs.rmSync(dirName, { recursive: true });
|
||||
fs.mkdirSync(dirName, { recursive: true });
|
||||
|
||||
// Config
|
||||
const testAmount = 100;
|
||||
|
||||
for (let i = 0; i < testAmount; i++) {
|
||||
const fileName = path.join(dirName, i.toString());
|
||||
fs.writeFileSync(fileName, i.toString());
|
||||
}
|
||||
|
||||
const manifest = JSON.parse(
|
||||
await new Promise((r, e) =>
|
||||
generateManifest(
|
||||
dirName,
|
||||
(_, __) => {},
|
||||
(_, __) => {},
|
||||
(err, manifest) => (err ? e(err) : r(manifest))
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
// Check the first few checksums
|
||||
const checksums = [
|
||||
"cfcd208495d565ef66e7dff9f98764da",
|
||||
"c4ca4238a0b923820dcc509a6f75849b",
|
||||
"c81e728d9d4c2f636f067f89cc14862c",
|
||||
];
|
||||
for (let index in checksums) {
|
||||
const entry = manifest[index.toString()];
|
||||
if (!entry) return t.fail(`manifest missing file ${index}`);
|
||||
|
||||
const checksum = entry.checksums[0];
|
||||
t.is(checksum, checksums[index], `checksums do not match for ${index}`);
|
||||
}
|
||||
|
||||
// Check all entries are there, and the right length
|
||||
for (let i = 0; i < testAmount; i++) {
|
||||
const entry = manifest[i.toString()];
|
||||
if (!entry) return t.fail(`manifest missing file ${i}`);
|
||||
|
||||
t.is(entry.lengths[0], i.toString().length);
|
||||
}
|
||||
|
||||
fs.rmSync(dirName, { recursive: true });
|
||||
});
|
||||
|
||||
test("single large file", async (t) => {
|
||||
// Setup test dir
|
||||
const dirName = "./.test/slf";
|
||||
if (fs.existsSync(dirName)) fs.rmSync(dirName, { recursive: true });
|
||||
fs.mkdirSync(dirName, { recursive: true });
|
||||
|
||||
// Config
|
||||
const chunkSize = 1024 * 1024 * 64;
|
||||
const fileSize = chunkSize * 2 - 1; // Should be 4 chunks
|
||||
|
||||
const testFile = path.join(dirName, "test.bin");
|
||||
const randomReadStream = fs.createReadStream("/dev/random", {
|
||||
end: fileSize,
|
||||
start: 0,
|
||||
});
|
||||
|
||||
const writeStream = fs.createWriteStream(testFile);
|
||||
randomReadStream.pipe(writeStream);
|
||||
|
||||
await new Promise((r) => randomReadStream.on("end", r));
|
||||
|
||||
const manifest = JSON.parse(
|
||||
await new Promise((r, e) =>
|
||||
generateManifest(
|
||||
dirName,
|
||||
(_, __) => {},
|
||||
(_, __) => {},
|
||||
(err, manifest) => (err ? e(err) : r(manifest))
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
for (const [key, value] of Object.entries(manifest)) {
|
||||
for (const length of value.lengths) {
|
||||
t.is(length, chunkSize, "chunk size is not as expected");
|
||||
}
|
||||
}
|
||||
|
||||
fs.rmSync(dirName, { recursive: true });
|
||||
});
|
||||
122
__test__/trust.spec.mjs
Normal file
122
__test__/trust.spec.mjs
Normal file
@ -0,0 +1,122 @@
|
||||
import test from "ava";
|
||||
|
||||
import {
|
||||
generateRootCa,
|
||||
generateClientCertificate,
|
||||
verifyClientCertificate,
|
||||
signNonce,
|
||||
verifyNonce,
|
||||
} from "../index.js";
|
||||
import { randomUUID, sign } from "crypto";
|
||||
|
||||
test("generate ca", (t) => {
|
||||
const [pub, priv] = generateRootCa();
|
||||
t.pass();
|
||||
});
|
||||
|
||||
test("generate ca & client certs", (t) => {
|
||||
const [pub, priv] = generateRootCa();
|
||||
|
||||
const clientName = "My Test Client";
|
||||
const [clientPub, clientPriv] = generateClientCertificate(
|
||||
clientName,
|
||||
clientName,
|
||||
pub,
|
||||
priv
|
||||
);
|
||||
|
||||
t.pass();
|
||||
});
|
||||
|
||||
test("trust chain", (t) => {
|
||||
const [pub, priv] = generateRootCa();
|
||||
|
||||
const clientName = "My Test Client";
|
||||
const [clientPub, clientPriv] = generateClientCertificate(
|
||||
clientName,
|
||||
clientName,
|
||||
pub,
|
||||
priv
|
||||
);
|
||||
|
||||
const [invalidPub, invalidPriv] = generateRootCa();
|
||||
|
||||
const valid = verifyClientCertificate(clientPub, pub);
|
||||
if (valid) return t.pass();
|
||||
|
||||
const invalid = verifyClientCertificate(invalidPub, pub);
|
||||
if (!invalid) return t.pass();
|
||||
|
||||
return t.fail();
|
||||
});
|
||||
|
||||
test("trust chain fails", (t) => {
|
||||
const [rootPub, rootPriv] = generateRootCa();
|
||||
|
||||
const [clientPub, _priv] = generateClientCertificate(
|
||||
"",
|
||||
"",
|
||||
rootPub,
|
||||
rootPriv
|
||||
);
|
||||
|
||||
const [otherRootPub, otherRootPriv] = generateRootCa();
|
||||
|
||||
const valid = verifyClientCertificate(clientPub, otherRootPub);
|
||||
if (!valid) return t.pass();
|
||||
|
||||
t.fail("client certificate verifies non-related certificate");
|
||||
});
|
||||
|
||||
test("nonce signing", (t) => {
|
||||
const [pub, priv] = generateRootCa();
|
||||
const [clientPub, clientPriv] = generateClientCertificate(
|
||||
"test",
|
||||
"test",
|
||||
pub,
|
||||
priv
|
||||
);
|
||||
|
||||
const nonce = randomUUID();
|
||||
const signature = signNonce(clientPriv, nonce);
|
||||
|
||||
return t.pass();
|
||||
});
|
||||
|
||||
test("nonce signing, and verification", (t) => {
|
||||
const [pub, priv] = generateRootCa();
|
||||
const [clientPub, clientPriv] = generateClientCertificate(
|
||||
"test",
|
||||
"test",
|
||||
pub,
|
||||
priv
|
||||
);
|
||||
|
||||
const nonce = randomUUID();
|
||||
|
||||
const signature = signNonce(clientPriv, nonce);
|
||||
const valid = verifyNonce(clientPub, nonce, signature);
|
||||
|
||||
if (!valid) return t.fail("nonce does not verify correctly");
|
||||
|
||||
return t.pass();
|
||||
});
|
||||
|
||||
test("nonce signing, fails verification", (t) => {
|
||||
const [rootPub, rootPriv] = generateRootCa();
|
||||
const [clientPub, clientPriv] = generateClientCertificate(
|
||||
"test",
|
||||
"test",
|
||||
rootPub,
|
||||
rootPriv
|
||||
);
|
||||
const [otherClientPub, otherClientPriv] = generateClientCertificate("test2", "test2", rootPub, rootPriv);
|
||||
|
||||
const nonce = randomUUID();
|
||||
const signature = signNonce(clientPriv, nonce);
|
||||
const valid = verifyNonce(otherClientPub, nonce, signature);
|
||||
|
||||
if(valid) return t.fail("succesfully verified an invalid nonce");
|
||||
|
||||
t.pass();
|
||||
});
|
||||
Reference in New Issue
Block a user