forked from openiap/opencore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCrypt.test.ts
More file actions
63 lines (61 loc) · 3.29 KB
/
Copy pathCrypt.test.ts
File metadata and controls
63 lines (61 loc) · 3.29 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const path = require("path");
const env = path.join(process.cwd(), 'config', '.env');
require("dotenv").config({ path: env }); // , debug: false
import { suite, test, timeout } from '@testdeck/mocha';
import { Config } from "../OpenFlow/src/Config";
import { DatabaseConnection } from '../OpenFlow/src/DatabaseConnection';
import assert = require('assert');
import { Logger } from '../OpenFlow/src/Logger';
import { User } from '@openiap/openflow-api';
import { Crypt } from '../OpenFlow/src/Crypt';
@suite class crypt_test {
private testUser: User;
@timeout(10000)
async before() {
Config.workitem_queue_monitoring_enabled = false;
Config.disablelogging();
Logger.configure(true, true);
Config.db = new DatabaseConnection(Config.mongodb_url, Config.mongodb_db, false);
await Config.db.connect(null);
this.testUser = await Logger.DBHelper.FindByUsername("testuser", Crypt.rootToken(), null)
}
async after() {
await Logger.shutdown();
}
@timeout(10000)
@test async 'ValidatePassword'() {
await Crypt.SetPassword(this.testUser, "randompassword", null);
var result = await Crypt.ValidatePassword(this.testUser, "randompassword", null);
assert.ok(result, "Failed validating with the correct password");
result = await Crypt.ValidatePassword(this.testUser, "not-my-randompassword", null);
assert.ok(!result, "ValidatePassword did not fail with wrong password");
var hash = await Crypt.hash("secondrandompassword");
result = await Crypt.compare("secondrandompassword", hash, null)
assert.ok(result, "Failed validating with the correct password");
result = await Crypt.compare("not-my-randompassword", hash, null);
assert.ok(!result, "compare did not fail with wrong password");
await assert.rejects(Crypt.SetPassword(null, "randompassword", null));
await assert.rejects(Crypt.SetPassword(this.testUser, null, null));
await assert.rejects(Crypt.SetPassword(null, null, null));
await assert.rejects(Crypt.ValidatePassword(null, "randompassword", null));
await assert.rejects(Crypt.ValidatePassword(this.testUser, null, null));
await assert.rejects(Crypt.ValidatePassword(null, null, null));
await assert.rejects(Crypt.compare(null, null, null));
}
@test async 'encrypt'() {
const basestring = "Hi mom, i miss you.";
var encryptedstring = Crypt.encrypt(basestring);
var decryptedstring = Crypt.decrypt(encryptedstring);
assert.ok(decryptedstring == basestring, "Failed encrypting and decrypting string");
assert.throws(() => { Crypt.decrypt("Bogusstring") }, Error, "Decrypt did not fail with an illegal string");
}
@test async 'decrypt'() {
const gcm = "8a23d6b7b2282b09a32994faf724f05e:8c2551427845c60b4e394302057bc4e4";
const cbc = "4beca50248100a14d06c8d284258eda7:aee11025ef03216d0068:4b23f4875b8bda4be5b1a0b3a4b4cd3c";
var gcmdecrypted = Crypt.decrypt(gcm);
assert.ok(gcmdecrypted == "teststring", "Failed decrypting string using gcm encryption");
var cbcdecrypted = Crypt.decrypt(cbc);
assert.ok(cbcdecrypted == "teststring", "Failed decrypting string using gcm encryption");
}
}
// cls | ./node_modules/.bin/_mocha 'test/**/Crypt.test.ts'