Skip to content

Commit

Permalink
ad circle ci test
Browse files Browse the repository at this point in the history
  • Loading branch information
fritx committed Jun 23, 2017
1 parent f9670a8 commit 9913b45
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 19 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# unix-sqlcipher

<a href="https://circleci.com/gh/fritx/unix-sqlcipher/tree/dev"><img height="20" src="https://circleci.com/gh/fritx/unix-sqlcipher/tree/dev.svg?style=svg"></a>

> Encrypted sqlite3 for MacOS and Linux
> See also: [cross-sqlcipher](https://github.com/fritx/cross-sqlcipher)
Expand Down
3 changes: 3 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
machine:
node:
version: 8.1.2
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
],
"repository": "git://github.com/fritx/unix-sqlcipher.git",
"scripts": {
"test": "node test",
"test": "ava test.js",
"nwtest": "nw nw",
"electron-test": "electron electron",
"nwbuild": "npm i --runtime=node-webkit --target=0.12.3 --target_arch=x64",
"preinstall": "node preinstall",
"postinstall": "node postinstall"
},
"devDependencies": {
"ava": "^0.19.1"
},
"dependencies": {
"shelljs": "^0.7.4",
"sqlite3": "3.x"
Expand Down
109 changes: 91 additions & 18 deletions test.js
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()
})
})
})

0 comments on commit 9913b45

Please sign in to comment.