Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests files #5

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs

name: Node.js CI

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: 8.x
cache: 'npm'
- run: npm install
- run: npm test
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "",
"main": "crypto.js",
"scripts": {
"test": "node test.js",
"test": "node_modules/.bin/tape ./tests/test_*.js",
"lint": "jshint . --config .jshintrc"
},
"repository": {
Expand All @@ -18,6 +18,7 @@
},
"homepage": "https://github.com/xwiki-labs/chainpad-crypto#readme",
"dependencies": {
"tape": "^5.6.1",
"tweetnacl": "~0.12.2"
}
}
212 changes: 0 additions & 212 deletions test.js

This file was deleted.

49 changes: 49 additions & 0 deletions tests/test_curve.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
var Crypto = require("../crypto");
var Nacl = require("tweetnacl");
var test = require('tape');

test('Test basic one to one curve encryption', function (t) {
var Alice = Nacl.box.keyPair();
var Alice_public = Nacl.util.encodeBase64(Alice.publicKey);

var Bob = Nacl.box.keyPair();
var Bob_public = Nacl.util.encodeBase64(Bob.publicKey);

var Curve = Crypto.Curve;

// Basic one to one curve encryption used in CryptPad's chat

// Alice and Bob can use their own private keys and the other's public key
// to derive some shared values for their pairwise-encrypted channel
// that includes a pre-computed secret key for curve encryption, a signing key, and a validate key

// Alice derives the keys
var Alice_set = Curve.deriveKeys(Bob_public, Nacl.util.encodeBase64(Alice.secretKey));

// Bob does the same
var Bob_set = Curve.deriveKeys(Alice_public, Nacl.util.encodeBase64(Bob.secretKey));

['cryptKey', 'signKey', 'validateKey'].forEach(function (k) {
// these should all be strings
t.equal('string', typeof(Alice_set[k]));
t.equal('string', typeof(Bob_set[k]));

// and Alice and Bob should have exactly the same values
t.equal(Alice_set[k], Bob_set[k]);
});

var Alice_cryptor = Curve.createEncryptor(Alice_set);
var Bob_cryptor = Curve.createEncryptor(Bob_set);

// Now Alice should be able to send Bob a message

var message = 'pewpewpew';

var Alice_ciphertext = Alice_cryptor.encrypt(message);

var Bob_plaintext = Bob_cryptor.decrypt(Alice_ciphertext);

t.equal(message, Bob_plaintext);
t.end();
});

37 changes: 37 additions & 0 deletions tests/test_editcryptor2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
var Crypto = require("../crypto");
var Nacl = require("tweetnacl");
var test = require('tape');

// EditCryptor2
test('Test EditCryptor2 without passwords', function (t) {
t.pass("passed");
var message = "EditCryptor2";
var alice_cryptor = Crypto.createEditCryptor2(void 0, void 0, void 0);
var bob_cryptor = Crypto.createEditCryptor2(alice_cryptor.editKeyStr, void 0, void 0 );
var charlie_cryptor = Crypto.createEditCryptor2("abcd", void 0, void 0);

// Alice and Bob should generate the same keys
t.deepEqual(alice_cryptor, bob_cryptor);
['cryptKey', "chanId"].forEach(function (k) {
t.notDeepEqual(alice_cryptor[k], charlie_cryptor[k], "Do NOT generate the same keys under wrong keystr");
});
t.end();
});

test('Test EditCryptor2 with passwords', function (t) {
t.pass("passed");
var message = "EditCryptor2";
var password = "SuperSecretPassword";
var alice_cryptor = Crypto.createEditCryptor2(void 0, void 0, password);
var bob_cryptor = Crypto.createEditCryptor2(alice_cryptor.editKeyStr, void 0, password );
var charlie_cryptor = Crypto.createEditCryptor2(alice_cryptor.editKeyStr, void 0, "Wrong" );
var diana_cryptor = Crypto.createEditCryptor2("abcd", void 0, password);

// Alice and Bob should generate the same keys
t.deepEqual(alice_cryptor, bob_cryptor);
['cryptKey', "chanId"].forEach(function (k) {
t.notEqual(alice_cryptor[k], diana_cryptor[k], "Do NOT generate the same keys under wrong keystr");
});
t.end();
});

36 changes: 36 additions & 0 deletions tests/test_filecryptor2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
var Crypto = require("../crypto");
var Nacl = require("tweetnacl");
var test = require('tape');

test('Test FileCryptor2 without passwords', function (t) {
var message = "FileCryptor2";
var alice_cryptor = Crypto.createFileCryptor2(void 0, void 0);
var bob_cryptor = Crypto.createFileCryptor2(alice_cryptor.fileKeyStr, void 0);
var charlie_cryptor = Crypto.createFileCryptor2("abcd", void 0);

t.deepEqual(alice_cryptor, bob_cryptor, "Generate the same keys");

t.deepEqual(alice_cryptor, bob_cryptor);
["fileKeyStr", "cryptKey", "chanId"].forEach(function (k) {
t.notDeepEqual(alice_cryptor[k], charlie_cryptor[k], "Different keys under wrong keystr");
});
t.end();
});


test('Test FileCryptor2 with passwords', function (t) {
var message = "FileCryptor2";
var password = "SuperSecretPassword";
var alice_cryptor = Crypto.createFileCryptor2(void 0, password);
var bob_cryptor = Crypto.createFileCryptor2(alice_cryptor.fileKeyStr, password );
var charlie_cryptor = Crypto.createFileCryptor2(alice_cryptor.fileKeyStr, "wrong" );
var diana_cryptor = Crypto.createFileCryptor2("abcd", password);
t.deepEqual(alice_cryptor, bob_cryptor, "Generate the same keys");
['cryptKey', "chanId"].forEach(function (k) {
// Alice and Charlie should NOT generate the same keys (wrong password)
t.notDeepEqual(alice_cryptor[k], charlie_cryptor[k], "Different keys under wrong password");
// Alice and Charlie should NOT generate the same keys (wrong password)
t.notDeepEqual(alice_cryptor[k], diana_cryptor[k], "Different keys under wrong keystr");
});
t.end();
});
Loading