-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
40 lines (30 loc) · 1.25 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const fs = require("fs");
const assert = require("assert");
const { encrypt, decrypt } = require("./index");
describe("Encryption and Decryption", function () {
const passphrase = "testPassphrase";
const testData = "Hello, this is a test message.";
it("should encrypt and decrypt text correctly", function () {
const encrypted = encrypt(testData, passphrase);
const decrypted = decrypt(encrypted, passphrase);
assert.strictEqual(decrypted, testData);
});
it("should encrypt and decrypt file correctly", function () {
const testFileName = "testfile.txt";
const encryptedFileName = testFileName + ".enc";
// Write test data to file
fs.writeFileSync(testFileName, testData, "utf-8");
// Encrypt file
const encrypted = encrypt(fs.readFileSync(testFileName), passphrase);
fs.writeFileSync(encryptedFileName, encrypted);
// Decrypt file
const decrypted = decrypt(fs.readFileSync(encryptedFileName), passphrase);
fs.writeFileSync(testFileName, decrypted);
// Read decrypted data from file
const decryptedData = fs.readFileSync(testFileName, "utf-8");
assert.strictEqual(decryptedData, testData);
// Cleanup created files
fs.unlinkSync(testFileName);
fs.unlinkSync(encryptedFileName);
});
});