Skip to content

Commit a8ee5a5

Browse files
committedApr 25, 2020
added tests for unsafe mode and fixed bug
1 parent 7f7baad commit a8ee5a5

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed
 

‎src/better_sqlite3.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ void Database::JS_unsafeMode (v8::FunctionCallbackInfo <v8 :: Value> const & inf
651651
Database* db = node :: ObjectWrap :: Unwrap <Database>(info.This());
652652
if (info.Length() == 0) db->unsafe_mode = true;
653653
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 ( ) ; }
654-
sqlite3_db_config(db->db_handle, SQLITE_DBCONFIG_DEFENSIVE, static_cast<int>(!db->unsafe_mode));
654+
sqlite3_db_config(db->db_handle, SQLITE_DBCONFIG_DEFENSIVE, static_cast<int>(!db->unsafe_mode), NULL);
655655
info.GetReturnValue().Set(info.This());
656656
}
657657
#line 355 "./src/objects/database.lzz"

‎src/objects/database.lzz

+1-1
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ private:
348348
Database* db = Unwrap<Database>(info.This());
349349
if (info.Length() == 0) db->unsafe_mode = true;
350350
else { REQUIRE_ARGUMENT_BOOLEAN(first, db->unsafe_mode); }
351-
sqlite3_db_config(db->db_handle, SQLITE_DBCONFIG_DEFENSIVE, static_cast<int>(!db->unsafe_mode));
351+
sqlite3_db_config(db->db_handle, SQLITE_DBCONFIG_DEFENSIVE, static_cast<int>(!db->unsafe_mode), NULL);
352352
info.GetReturnValue().Set(info.This());
353353
}
354354

‎test/45.unsafe-mode.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'use strict';
2+
const Database = require('../.');
3+
4+
describe('Database#unsafeMode()', function () {
5+
beforeEach(function () {
6+
this.db = new Database(util.next());
7+
this.db.exec('create table foo (x)');
8+
this.read = this.db.prepare('select 5');
9+
this.write = this.db.prepare('insert into foo values (0)');
10+
});
11+
afterEach(function () {
12+
this.db.close();
13+
});
14+
15+
it('should not allow unsafe operations by default', function () {
16+
let hadRow = false;
17+
for (const row of this.read.iterate()) {
18+
expect(() => this.write.run()).to.throw(TypeError);
19+
expect(() => this.db.exec('select 5')).to.throw(TypeError);
20+
expect(() => this.db.pragma('cache_size')).to.throw(TypeError);
21+
hadRow = true;
22+
}
23+
expect(hadRow).to.be.true;
24+
25+
this.db.pragma('journal_mode = OFF');
26+
this.db.pragma('writable_schema = ON');
27+
expect(this.db.pragma('journal_mode', { simple: true })).to.equal('delete');
28+
expect(() => this.db.exec("update sqlite_master set name = 'bar' where name = 'foo'")).to.throw(Database.SqliteError);
29+
});
30+
it('should allow unsafe operations when toggled on', function () {
31+
this.db.unsafeMode();
32+
33+
let hadRow = false;
34+
for (const row of this.read.iterate()) {
35+
this.write.run();
36+
this.db.exec('select 5');
37+
this.db.pragma('cache_size');
38+
hadRow = true;
39+
}
40+
expect(hadRow).to.be.true;
41+
42+
this.db.pragma('journal_mode = OFF');
43+
this.db.pragma('writable_schema = ON');
44+
expect(this.db.pragma('journal_mode', { simple: true })).to.equal('off');
45+
this.db.exec("update sqlite_master set name = 'bar' where name = 'foo'");
46+
47+
this.db.unsafeMode(false);
48+
expect(() => this.db.exec("update sqlite_master set name = 'foo' where name = 'bar'")).to.throw(Database.SqliteError);
49+
this.db.unsafeMode(true);
50+
this.db.exec("update sqlite_master set name = 'foo' where name = 'bar'");
51+
});
52+
});

0 commit comments

Comments
 (0)
Please sign in to comment.