mirror of
https://github.com/hanzoai/better-sqlite3-multiple-ciphers.git
synced 2026-09-24 16:57:19 +00:00
Improve tests for key() and rekey()
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
'use strict';
|
||||
const Database = require('../.');
|
||||
|
||||
describe('Database#key()', function () {
|
||||
afterEach(function () {
|
||||
this.db.close();
|
||||
});
|
||||
|
||||
it('should throw error if a Buffer is not provided', function () {
|
||||
this.db = new Database(util.next());
|
||||
expect(() => this.db.key(123)).to.throw(TypeError);
|
||||
expect(() => this.db.key(0)).to.throw(TypeError);
|
||||
expect(() => this.db.key(null)).to.throw(TypeError);
|
||||
expect(() => this.db.key()).to.throw(TypeError);
|
||||
expect(() => this.db.key(new String('cache_size'))).to.throw(TypeError);
|
||||
});
|
||||
it('should execute key() without errors', function () {
|
||||
this.db = new Database(util.current());
|
||||
this.db.pragma(`cipher='aes256cbc'`);
|
||||
const status = this.db.key(Buffer.from('OkPassword'));
|
||||
this.db.exec('CREATE TABLE entries (a TEXT, b INTEGER)');
|
||||
expect(status).to.be.an('number');
|
||||
expect(status).to.equal(0);
|
||||
});
|
||||
it('should throw error when an incorrect key is provided', function () {
|
||||
this.db = new Database(util.current());
|
||||
this.db.pragma(`cipher='aes256cbc'`);
|
||||
this.db.key(Buffer.from('WrongPassword'));
|
||||
expect(() => this.db.exec('select * from sqlite_schema')).to.throw(Database.SqliteError).with.property('code', 'SQLITE_NOTADB');
|
||||
});
|
||||
it('should not throw error when the correct key is provided', function () {
|
||||
this.db = new Database(util.current());
|
||||
this.db.pragma(`cipher='aes256cbc'`);
|
||||
this.db.key(Buffer.from('OkPassword'));
|
||||
expect(() => this.db.exec('select * from sqlite_schema')).to.not.throw(Database.SqliteError);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,42 @@
|
||||
'use strict';
|
||||
const Database = require('../.');
|
||||
|
||||
describe('Database#rekey()', function () {
|
||||
afterEach(function () {
|
||||
this.db.close();
|
||||
});
|
||||
|
||||
it('should throw error if a Buffer is not provided', function () {
|
||||
this.db = new Database(util.next());
|
||||
expect(() => this.db.rekey(123)).to.throw(TypeError);
|
||||
expect(() => this.db.rekey(0)).to.throw(TypeError);
|
||||
expect(() => this.db.rekey(null)).to.throw(TypeError);
|
||||
expect(() => this.db.rekey()).to.throw(TypeError);
|
||||
expect(() => this.db.rekey(new String('cache_size'))).to.throw(TypeError);
|
||||
});
|
||||
it('should execute rekey() without errors', function () {
|
||||
this.db = new Database(util.current());
|
||||
this.db.pragma(`cipher='aes256cbc'`);
|
||||
const status = this.db.rekey(Buffer.from('OkPassword'));
|
||||
this.db.exec('CREATE TABLE entries (a TEXT, b INTEGER)');
|
||||
expect(status).to.be.an('number');
|
||||
expect(status).to.equal(0);
|
||||
});
|
||||
it('should throw error if an encrypted database is not decrypted before rekey()', function () {
|
||||
this.db = new Database(util.current());
|
||||
this.db.pragma(`cipher='aes256cbc'`);
|
||||
expect(() => this.db.rekey(Buffer.from('NewPassword'))).to.throw(Database.SqliteError).with.property('code', 'SQLITE_NOTADB');
|
||||
});
|
||||
it('should allow to rekey() if an already encrypted database is properly decrypted in advance', function () {
|
||||
this.db = new Database(util.current());
|
||||
this.db.pragma(`cipher='aes256cbc'`);
|
||||
this.db.key(Buffer.from('OkPassword'));
|
||||
this.db.rekey(Buffer.from('NewPassword'));
|
||||
this.db.exec('select * from sqlite_schema');
|
||||
this.db.close();
|
||||
this.db = new Database(util.current());
|
||||
this.db.pragma(`cipher='aes256cbc'`);
|
||||
this.db.key(Buffer.from('NewPassword'));
|
||||
expect(() => this.db.exec('select * from sqlite_schema')).to.not.throw(Database.SqliteError);
|
||||
});
|
||||
});
|
||||
@@ -1,50 +0,0 @@
|
||||
'use strict';
|
||||
const Database = require('../.');
|
||||
|
||||
describe('Database#key()', function () {
|
||||
beforeEach(function () {
|
||||
this.db = new Database(util.next());
|
||||
});
|
||||
afterEach(function () {
|
||||
this.db.close();
|
||||
});
|
||||
|
||||
it('should throw an exception if a Buffer is not provided', function () {
|
||||
expect(() => this.db.key(123)).to.throw(TypeError);
|
||||
expect(() => this.db.key(0)).to.throw(TypeError);
|
||||
expect(() => this.db.key(null)).to.throw(TypeError);
|
||||
expect(() => this.db.key()).to.throw(TypeError);
|
||||
expect(() => this.db.key(new String('cache_size'))).to.throw(TypeError);
|
||||
});
|
||||
it('should execute the key, and not throwing an exception', function () {
|
||||
const status = this.db.key(Buffer.from('aPassword'));
|
||||
expect(status).to.be.an('number');
|
||||
expect(status).to.equal(0);
|
||||
});
|
||||
it('should throw error when wrong key is provided', function () {
|
||||
const dbName = util.next();
|
||||
let testdb = new Database(dbName);
|
||||
testdb.pragma(`cipher='aes256cbc'`);
|
||||
testdb.key(Buffer.from('OkPassword'));
|
||||
testdb.exec('CREATE TABLE entries (a TEXT, b INTEGER)');
|
||||
testdb.close();
|
||||
testdb = new Database(dbName);
|
||||
testdb.pragma(`cipher='aes256cbc'`);
|
||||
testdb.key(Buffer.from('WrongPassword'));
|
||||
expect(() => testdb.exec('select * from sqlite_schema')).to.throw(Database.SqliteError).with.property('code', 'SQLITE_NOTADB');
|
||||
testdb.close();
|
||||
});
|
||||
it('should not throw error when right key is provided', function () {
|
||||
const dbName = util.next();
|
||||
let testdb = new Database(dbName);
|
||||
testdb.pragma(`cipher='aes256cbc'`);
|
||||
testdb.key(Buffer.from('OkPassword'));
|
||||
testdb.exec('CREATE TABLE entries (a TEXT, b INTEGER)');
|
||||
testdb.close();
|
||||
testdb = new Database(dbName);
|
||||
testdb.pragma(`cipher='aes256cbc'`);
|
||||
testdb.key(Buffer.from('OkPassword'));
|
||||
expect(() => testdb.exec('select * from sqlite_schema')).to.not.throw(Database.SqliteError);
|
||||
testdb.close();
|
||||
});
|
||||
});
|
||||
@@ -1,55 +0,0 @@
|
||||
'use strict';
|
||||
const Database = require('../.');
|
||||
|
||||
describe('Database#rekey()', function () {
|
||||
beforeEach(function () {
|
||||
this.db = new Database(util.next());
|
||||
});
|
||||
afterEach(function () {
|
||||
this.db.close();
|
||||
});
|
||||
|
||||
it('should throw an exception if a Buffer is not provided', function () {
|
||||
expect(() => this.db.rekey(123)).to.throw(TypeError);
|
||||
expect(() => this.db.rekey(0)).to.throw(TypeError);
|
||||
expect(() => this.db.rekey(null)).to.throw(TypeError);
|
||||
expect(() => this.db.rekey()).to.throw(TypeError);
|
||||
expect(() => this.db.rekey(new String('cache_size'))).to.throw(TypeError);
|
||||
});
|
||||
it('should execute the rekey, and not throwing an exception', function () {
|
||||
const status = this.db.rekey(Buffer.from('aPassword'));
|
||||
expect(status).to.be.an('number');
|
||||
expect(status).to.equal(0);
|
||||
});
|
||||
it('should throw error when key was not first provided', function () {
|
||||
const dbName = util.next();
|
||||
let testdb = new Database(dbName);
|
||||
testdb.pragma(`cipher='aes256cbc'`);
|
||||
testdb.key(Buffer.from('OkPassword'));
|
||||
testdb.exec('CREATE TABLE entries (a TEXT, b INTEGER)');
|
||||
testdb.close();
|
||||
testdb = new Database(dbName);
|
||||
testdb.pragma(`cipher='aes256cbc'`);
|
||||
expect(() => testdb.rekey(Buffer.from('NewPassword'))).to.throw(Database.SqliteError).with.property('code', 'SQLITE_NOTADB');
|
||||
testdb.close();
|
||||
});
|
||||
it('should not throw error when right key is provided after rekey', function () {
|
||||
const dbName = util.next();
|
||||
let testdb = new Database(dbName);
|
||||
testdb.pragma(`cipher='aes256cbc'`);
|
||||
testdb.key(Buffer.from('OkPassword'));
|
||||
testdb.exec('CREATE TABLE entries (a TEXT, b INTEGER)');
|
||||
testdb.close();
|
||||
testdb = new Database(dbName);
|
||||
testdb.pragma(`cipher='aes256cbc'`);
|
||||
testdb.key(Buffer.from('OkPassword'));
|
||||
testdb.rekey(Buffer.from('OtherOkPassword'));
|
||||
testdb.exec('select * from sqlite_schema');
|
||||
testdb.close();
|
||||
testdb = new Database(dbName);
|
||||
testdb.pragma(`cipher='aes256cbc'`);
|
||||
testdb.key(Buffer.from('OtherOkPassword'));
|
||||
expect(() => testdb.exec('select * from sqlite_schema')).to.not.throw(Database.SqliteError);
|
||||
testdb.close();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user