added tests for unsafe mode and fixed bug

This commit is contained in:
Joshua Wise
2020-04-25 09:14:52 -05:00
parent 202abe7f80
commit 6a1a1d50e0
3 changed files with 54 additions and 2 deletions
+1 -1
View File
@@ -651,7 +651,7 @@ void Database::JS_unsafeMode (v8::FunctionCallbackInfo <v8 :: Value> const & inf
Database* db = node :: ObjectWrap :: Unwrap <Database>(info.This());
if (info.Length() == 0) db->unsafe_mode = true;
else { if ( info . Length ( ) <= ( 0 ) || ! info [ 0 ] -> IsBoolean ( ) ) return ThrowTypeError ( "Expected " "first" " argument to be " "a boolean" ) ; db -> unsafe_mode = v8 :: Local < v8 :: Boolean > :: Cast ( info [ 0 ] ) -> Value ( ) ; }
sqlite3_db_config(db->db_handle, SQLITE_DBCONFIG_DEFENSIVE, static_cast<int>(!db->unsafe_mode));
sqlite3_db_config(db->db_handle, SQLITE_DBCONFIG_DEFENSIVE, static_cast<int>(!db->unsafe_mode), NULL);
info.GetReturnValue().Set(info.This());
}
#line 355 "./src/objects/database.lzz"
+1 -1
View File
@@ -348,7 +348,7 @@ private:
Database* db = Unwrap<Database>(info.This());
if (info.Length() == 0) db->unsafe_mode = true;
else { REQUIRE_ARGUMENT_BOOLEAN(first, db->unsafe_mode); }
sqlite3_db_config(db->db_handle, SQLITE_DBCONFIG_DEFENSIVE, static_cast<int>(!db->unsafe_mode));
sqlite3_db_config(db->db_handle, SQLITE_DBCONFIG_DEFENSIVE, static_cast<int>(!db->unsafe_mode), NULL);
info.GetReturnValue().Set(info.This());
}
+52
View File
@@ -0,0 +1,52 @@
'use strict';
const Database = require('../.');
describe('Database#unsafeMode()', function () {
beforeEach(function () {
this.db = new Database(util.next());
this.db.exec('create table foo (x)');
this.read = this.db.prepare('select 5');
this.write = this.db.prepare('insert into foo values (0)');
});
afterEach(function () {
this.db.close();
});
it('should not allow unsafe operations by default', function () {
let hadRow = false;
for (const row of this.read.iterate()) {
expect(() => this.write.run()).to.throw(TypeError);
expect(() => this.db.exec('select 5')).to.throw(TypeError);
expect(() => this.db.pragma('cache_size')).to.throw(TypeError);
hadRow = true;
}
expect(hadRow).to.be.true;
this.db.pragma('journal_mode = OFF');
this.db.pragma('writable_schema = ON');
expect(this.db.pragma('journal_mode', { simple: true })).to.equal('delete');
expect(() => this.db.exec("update sqlite_master set name = 'bar' where name = 'foo'")).to.throw(Database.SqliteError);
});
it('should allow unsafe operations when toggled on', function () {
this.db.unsafeMode();
let hadRow = false;
for (const row of this.read.iterate()) {
this.write.run();
this.db.exec('select 5');
this.db.pragma('cache_size');
hadRow = true;
}
expect(hadRow).to.be.true;
this.db.pragma('journal_mode = OFF');
this.db.pragma('writable_schema = ON');
expect(this.db.pragma('journal_mode', { simple: true })).to.equal('off');
this.db.exec("update sqlite_master set name = 'bar' where name = 'foo'");
this.db.unsafeMode(false);
expect(() => this.db.exec("update sqlite_master set name = 'foo' where name = 'bar'")).to.throw(Database.SqliteError);
this.db.unsafeMode(true);
this.db.exec("update sqlite_master set name = 'foo' where name = 'bar'");
});
});