Skip to content

Commit

Permalink
Calling to invalid function pointer (Sync/Async).
Browse files Browse the repository at this point in the history
Add sqlite3 testing .
  • Loading branch information
lygstate committed May 5, 2021
1 parent bcbd9c2 commit 3157d9c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
27 changes: 25 additions & 2 deletions test/foreign_function.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ describe('ForeignFunction', function () {

// these structs are also defined in ffi_tests.cc
const box = StructType({
width: ref.types.int,
height: ref.types.int
width: ffi.types.int,
height: ffi.types.int
});

const arst = StructType({
Expand Down Expand Up @@ -209,5 +209,28 @@ describe('ForeignFunction', function () {
}
});
});

it('async with error', function(done) {
const funcPtr = Buffer.alloc(10);
const func = ffi.ForeignFunction(funcPtr, ffi.types.int, [ffi.types.int]);
assert(typeof func === "function");
// Calling to this function will result crash.
// As it's not the real code, it's just 0 filled byte array
assert.throws(function () {
func(-5);
})
func.async(-5, (err, res) => {
assert(err === "The content of funcPtr pointed are invalid(empty)!")
done()
});
})
});

it('check uv version', function() {
const uv_func = ffi.Library(null, {
uv_version_string: [ffi.types.CString, []],
});
const uv_version = uv_func.uv_version_string();
assert(typeof uv_version === 'string')
})
});
35 changes: 35 additions & 0 deletions test/sqlite3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const path = require("path");
const ffi = require("../");
const { ref, types } = ffi;

describe("sqlite3", function () {
let libsqlite3
const sqlite3 = types.void;
const sqlite3Ptr = ref.refType(sqlite3);
const sqlite3PtrPtr = ref.refType(sqlite3Ptr);
const stringPtr = ref.refType(types.CString);
before(function() {
const lib =
process.platform == "win32"
? path.join(
process.env.ProgramData || "",
"chocolatey/lib/SQLite/tools/sqlite3.dll"
)
: "libsqlite3";

libsqlite3 = ffi.Library(lib, {
sqlite3_open: ["int", ["string", sqlite3PtrPtr]],
sqlite3_close: ["int", [sqlite3PtrPtr]],
sqlite3_exec: [
"int",
[sqlite3PtrPtr, "string", "pointer", "pointer", stringPtr],
],
sqlite3_changes: ["int", [sqlite3PtrPtr]],
});
})
it("open close", function () {
const dbPtrPtr = ref.alloc(sqlite3PtrPtr);
libsqlite3.sqlite3_open("test.sqlite3", dbPtrPtr);
libsqlite3.sqlite3_close(ref.deref(dbPtrPtr));
});
});

0 comments on commit 3157d9c

Please sign in to comment.