From 9913b4526a50f8ffdf818809cfd0f02672488c75 Mon Sep 17 00:00:00 2001 From: Fritz Lin Date: Fri, 23 Jun 2017 23:53:16 +0800 Subject: [PATCH] ad circle ci test --- README.md | 2 + circle.yml | 3 ++ package.json | 5 ++- test.js | 109 ++++++++++++++++++++++++++++++++++++++++++--------- 4 files changed, 100 insertions(+), 19 deletions(-) create mode 100644 circle.yml diff --git a/README.md b/README.md index 5f0b3c1..8fc0fa0 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # unix-sqlcipher + + > Encrypted sqlite3 for MacOS and Linux > See also: [cross-sqlcipher](https://github.com/fritx/cross-sqlcipher) diff --git a/circle.yml b/circle.yml new file mode 100644 index 0000000..6274b92 --- /dev/null +++ b/circle.yml @@ -0,0 +1,3 @@ +machine: + node: + version: 8.1.2 diff --git a/package.json b/package.json index e17c571..dd89661 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/test.js b/test.js index 364dbf2..6736d37 100644 --- a/test.js +++ b/test.js @@ -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() + }) + }) +})