-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
100 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
machine: | ||
node: | ||
version: 8.1.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,98 @@ | ||
let { test } = require('ava') | ||
|
||
// https://github.com/delaballe/node-sqlcipher#usage | ||
// https://coolaj86.com/articles/building-sqlcipher-for-node-js-on-raspberry-pi-2/ | ||
'use strict'; | ||
var sqlite3 = require('./').verbose(); | ||
var db = new sqlite3.Database('test.db'); | ||
// https://www.zetetic.net/sqlcipher/sqlcipher-api/ | ||
let sqlite3 = require('./').verbose() | ||
let dbFile = __dirname + '/test.db' | ||
|
||
test.serial.cb('encrypt with key/cipher', t => { | ||
let db = new sqlite3.Database(dbFile) | ||
|
||
db.serialize(() => { | ||
db.run("PRAGMA KEY = 'secret'") | ||
db.run("PRAGMA CIPHER = 'aes-256-cbc'") | ||
|
||
db.run("DROP TABLE IF EXISTS lorem") | ||
db.run("CREATE TABLE lorem (info TEXT)") | ||
|
||
let stmt = db.prepare("INSERT INTO lorem VALUES (?)") | ||
let total = 10 | ||
for (let i = 0; i < total; i++) { | ||
stmt.run("Ipsum " + i) | ||
} | ||
stmt.finalize() | ||
|
||
let count = 0 | ||
db.each("SELECT rowid AS id, info FROM lorem", (err, row) => { | ||
if (err) return t.end(err) | ||
count++ | ||
console.log(row.id + ": " + row.info) | ||
}) | ||
|
||
db.close(() => { | ||
t.is(count, total) | ||
t.end() | ||
}) | ||
}) | ||
}) | ||
|
||
test.serial.cb('decrypt with correct key/cipher', t => { | ||
let db = new sqlite3.Database(dbFile) | ||
|
||
db.serialize(() => { | ||
db.run("PRAGMA KEY = 'secret'") | ||
db.run("PRAGMA CIPHER = 'aes-256-cbc'") | ||
|
||
let total = 10 | ||
let count = 0 | ||
db.each("SELECT rowid AS id, info FROM lorem", function(err, row) { | ||
if (err) return t.end(err) | ||
count++ | ||
console.log(row.id + ": " + row.info) | ||
}) | ||
|
||
db.close(() => { | ||
t.is(count, total) | ||
t.end() | ||
}) | ||
}) | ||
}) | ||
|
||
test.serial.cb('decrypt with wrong key', t => { | ||
let db = new sqlite3.Database(dbFile) | ||
|
||
db.serialize(() => { | ||
db.run("PRAGMA KEY = 'sec321ret'") | ||
db.run("PRAGMA CIPHER = 'aes-256-cbc'") | ||
|
||
t.plan(1) | ||
db.each("SELECT rowid AS id, info FROM lorem", function(err) { | ||
// [Error: SQLITE_NOTADB: file is encrypted or is not a database] | ||
t.regex(err.message, /file is encrypted/) | ||
}) | ||
|
||
db.serialize(function() { | ||
db.run("PRAGMA KEY = 'secret'"); | ||
db.run("PRAGMA CIPHER = 'aes-128-cbc'"); | ||
db.close(() => { | ||
t.end() | ||
}) | ||
}) | ||
}) | ||
|
||
db.run("DROP TABLE IF EXISTS lorem"); | ||
db.run("CREATE TABLE lorem (info TEXT)"); | ||
test.serial.cb('decrypt with wrong cipher', t => { | ||
let db = new sqlite3.Database(dbFile) | ||
|
||
var stmt = db.prepare("INSERT INTO lorem VALUES (?)"); | ||
for (var i = 0; i < 10; i++) { | ||
stmt.run("Ipsum " + i); | ||
} | ||
stmt.finalize(); | ||
db.serialize(() => { | ||
db.run("PRAGMA KEY = 'secret'") | ||
db.run("PRAGMA CIPHER = 'aes-128-cbc'") | ||
|
||
db.each("SELECT rowid AS id, info FROM lorem", function(err, row) { | ||
console.log(row.id + ": " + row.info); | ||
}); | ||
}); | ||
t.plan(1) | ||
db.each("SELECT rowid AS id, info FROM lorem", function(err) { | ||
// [Error: SQLITE_NOTADB: file is encrypted or is not a database] | ||
t.regex(err.message, /file is encrypted/) | ||
}) | ||
|
||
db.close(); | ||
db.close(() => { | ||
t.end() | ||
}) | ||
}) | ||
}) |