314 lines
7.8 KiB
JavaScript
Raw Normal View History

2020-10-04 12:18:39 +02:00
/*
* Copyright (C) Sapphirecode - All Rights Reserved
* This file is part of crypto-helper which is released under MIT.
* See file 'LICENSE' for full license details.
* Created by Timo Hocker <timo@scode.ovh>, May 2020
*/
// @ts-nocheck
'use strict';
const crypto = require ('../../index');
// eslint-disable-next-line max-lines-per-function
describe ('crypto helper', () => {
it ('random_hex', () => {
const hex = crypto.random_hex (16);
expect (hex.length)
.toEqual (16);
expect (hex)
.toMatch (/^[0-9a-f]+$/iu);
});
it ('random_hex with default length', () => {
const hex = crypto.random_hex ();
expect (hex.length)
.toEqual (8);
expect (hex)
.toMatch (/^[0-9a-f]+$/iu);
});
2020-11-29 12:01:43 +01:00
it ('random_hex should refuse length smaller 1', () => {
2020-10-04 12:18:39 +02:00
expect (
() => (crypto.random_hex (0))
)
.toThrowError ('invalid length');
});
it ('random_hex should always return correct length', () => {
for (let i = 1; i < 32; i++) {
const hex = crypto.random_hex (i);
expect (hex.length)
.toEqual (i);
}
});
it ('random_string', () => {
const str = crypto.random_string (16);
expect (str.length)
.toEqual (16);
});
it ('random_string with default length', () => {
const str = crypto.random_string ();
expect (str.length)
.toEqual (8);
});
2020-11-29 12:01:43 +01:00
it ('random_string should refuse length smaller 1', () => {
2020-10-04 12:18:39 +02:00
expect (
() => (crypto.random_string (0))
)
.toThrowError ('invalid length');
});
it ('random_string should always return correct length', () => {
for (let i = 1; i < 32; i++) {
const str = crypto.random_string (i);
expect (str.length)
.toEqual (i);
}
});
it ('hash_sha512', () => {
const hash = crypto.hash_sha512 ('a', 'b');
expect (
hash
)
.toEqual (
// eslint-disable-next-line max-len
'2d408a0717ec188158278a796c689044361dc6fdde28d6f04973b80896e1823975cdbf12eb63f9e0591328ee235d80e9b5bf1aa6a44f4617ff3caf6400eb172d'
);
});
it ('checksum', () => {
const hash = crypto.checksum ('foo');
expect (
hash
)
.toEqual (
// eslint-disable-next-line max-len
'2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae'
);
});
it ('create_salt', () => {
const salt = crypto.create_salt ();
expect (salt.length)
.toEqual (64);
expect (salt)
.toMatch (/^[0-9a-f]+$/iu);
});
it ('sign_object', () => {
const obj = { foo: 'bar' };
expect (() => {
const str = crypto.sign_object (obj, 'baz');
expect (typeof str)
.toEqual ('string');
}).not.toThrow ();
});
2020-12-13 13:09:34 +01:00
it ('should sign object with key info', () => {
const obj = { foo: 'bar' };
expect (() => {
const str = crypto.sign_object (obj, 'baz', 'baz');
const res = crypto.get_signature_info (str);
expect (res.key_info)
.toEqual ('baz');
}).not.toThrow ();
});
it ('should sign object with custom properties', () => {
const obj = { foo: 'bar' };
expect (() => {
const str = crypto.sign_object (obj, 'baz', { bar: 'baz' });
const res = crypto.get_signature_info (str);
expect (res.bar)
.toEqual ('baz');
}).not.toThrow ();
});
it ('should sign object with custom override properties', () => {
const obj = { foo: 'bar' };
expect (() => {
const str = crypto.sign_object (obj, 'baz', { iat: 'baz' });
const res = crypto.get_signature_info (str);
expect (res.iat)
.toEqual ('baz');
}).not.toThrow ();
});
2020-10-04 12:18:39 +02:00
it ('decode_signed', () => {
const obj = { foo: 'bar' };
const str = crypto.sign_object (obj, 'baz');
const dec = crypto.decode_signed (str);
expect (obj)
.toEqual (dec);
});
it ('verify_signature', () => {
const obj = { foo: 'bar' };
const str = crypto.sign_object (obj, 'baz');
const dec = crypto.verify_signature (str, 'baz');
expect (obj)
.toEqual (dec);
});
2020-12-13 13:09:34 +01:00
it ('should verify and return all info', () => {
const obj = { foo: 'bar' };
const str = crypto.sign_object (obj, 'baz', { iat: 'baz' });
const dec = crypto.verify_signature_get_info (str, 'baz');
expect (dec.obj)
.toEqual (obj);
expect (dec.iat)
.toEqual ('baz');
});
it ('should verify signature using function retrieved key', () => {
const obj = { foo: 'bar' };
const str = crypto.sign_object (obj, 'baz');
const dec = crypto.verify_signature (str, () => 'baz');
expect (obj)
.toEqual (dec);
});
2020-12-13 13:31:48 +01:00
it ('should verify signature using function retrieved timeout 0', () => {
const obj = { foo: 'bar' };
const str = crypto.sign_object (obj, 'baz');
const dec = crypto.verify_signature (str, 'baz', () => 0);
expect (obj)
.toEqual (dec);
});
2020-11-29 12:01:43 +01:00
it ('should reject tampered signatures', () => {
const obj = { foo: 'bar' };
const str = crypto.sign_object (obj, 'baz');
const dec = crypto.verify_signature (str, 'foo');
expect (dec)
.toEqual (null);
});
2020-10-04 12:18:39 +02:00
it ('should return null on invalid input', () => {
const ver = crypto.verify_signature (null, 'foo');
expect (ver)
.toEqual (null);
const dec = crypto.decode_signed (null, 'foo');
expect (dec)
.toEqual (null);
});
2020-11-29 12:05:51 +01:00
it ('should not fail verification if timeout unspecified', () => {
2020-11-29 12:01:43 +01:00
jasmine.clock ()
.install ();
const base_time = (new Date);
jasmine.clock ()
.mockDate (base_time);
2020-10-04 12:18:39 +02:00
const obj = { foo: 'bar' };
const str = crypto.sign_object (obj, 'baz');
2020-11-29 12:01:43 +01:00
jasmine.clock ()
.tick (36e5);
2020-10-04 12:18:39 +02:00
const dec = crypto.verify_signature (str, 'baz');
expect (obj)
.toEqual (dec);
2020-11-29 12:01:43 +01:00
jasmine.clock ()
.uninstall ();
2020-10-04 12:18:39 +02:00
});
2020-11-29 12:05:51 +01:00
it ('should reject old signatures', () => {
2020-11-29 12:01:43 +01:00
jasmine.clock ()
.install ();
const base_time = (new Date);
jasmine.clock ()
.mockDate (base_time);
2020-10-04 12:18:39 +02:00
const obj = { foo: 'bar' };
const str = crypto.sign_object (obj, 'baz');
2020-11-29 12:01:43 +01:00
jasmine.clock ()
.tick (50);
2020-10-04 12:18:39 +02:00
const dec = crypto.verify_signature (str, 'baz', 1);
expect (dec)
.toEqual (null);
2020-11-29 12:01:43 +01:00
jasmine.clock ()
.uninstall ();
2020-10-04 12:18:39 +02:00
});
2020-11-29 12:05:51 +01:00
it ('should not reject valid signatures', () => {
2020-11-29 12:01:43 +01:00
jasmine.clock ()
.install ();
const base_time = (new Date);
jasmine.clock ()
.mockDate (base_time);
2020-10-04 12:18:39 +02:00
const obj = { foo: 'bar' };
const str = crypto.sign_object (obj, 'baz');
2020-11-29 12:01:43 +01:00
jasmine.clock ()
.tick (50);
2020-10-04 12:18:39 +02:00
const dec = crypto.verify_signature (str, 'baz', 100);
expect (obj)
.toEqual (dec);
2020-11-29 12:01:43 +01:00
jasmine.clock ()
.uninstall ();
});
2020-12-13 13:31:48 +01:00
it ('should verify signature using function retrieved timeout', () => {
jasmine.clock ()
.install ();
const base_time = (new Date);
jasmine.clock ()
.mockDate (base_time);
const obj = { foo: 'bar' };
const str = crypto.sign_object (obj, 'baz', { to: 100 });
jasmine.clock ()
.tick (50);
const dec = crypto.verify_signature (str, 'baz', (info) => info.to);
expect (obj)
.toEqual (dec);
jasmine.clock ()
.uninstall ();
});
2020-11-29 12:01:43 +01:00
it ('verify_signature on almost timed out packet', () => {
jasmine.clock ()
.install ();
const base_time = (new Date);
jasmine.clock ()
.mockDate (base_time);
const obj = { foo: 'bar' };
const str = crypto.sign_object (obj, 'baz');
jasmine.clock ()
.tick (10);
const dec = crypto.verify_signature (str, 'baz', 10);
expect (obj)
.toEqual (dec);
jasmine.clock ()
.uninstall ();
2020-10-04 12:18:39 +02:00
});
it ('should decode problematic token', () => {
// eslint-disable-next-line max-len
const str = 'eyJpYXQiOjE1ODE0NDAwMTIyODgsIm9iaiI6eyJpZCI6MX19.24ZOsWrnfkNe%2FbM0r7DaVJMqE2bfn2aAM%2BZSzWeSf31OCTlXXNWD34RBL2X5v3UliYQ4IIsLNBFbaW9texPHug%3D%3D';
const obj = crypto.decode_signed (str);
expect (obj)
.toEqual ({ id: 1 });
});
});