Skip to content

Commit dc0656a

Browse files
committed
fixup! test add
1 parent 33cca0c commit dc0656a

File tree

3 files changed

+66
-13
lines changed

3 files changed

+66
-13
lines changed

src/node_errors.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ void OOMErrorHandler(const char* location, const v8::OOMDetails& details);
9191
V(ERR_INVALID_THIS, TypeError) \
9292
V(ERR_INVALID_URL, TypeError) \
9393
V(ERR_INVALID_URL_SCHEME, TypeError) \
94+
V(ERR_LOAD_SQLITE_EXTENSION, Error) \
9495
V(ERR_MEMORY_ALLOCATION_FAILED, Error) \
9596
V(ERR_MESSAGE_TARGET_CONTEXT_UNAVAILABLE, Error) \
9697
V(ERR_MISSING_ARGS, TypeError) \
@@ -186,6 +187,7 @@ ERRORS_WITH_CODE(V)
186187
V(ERR_INVALID_STATE, "Invalid state") \
187188
V(ERR_INVALID_THIS, "Value of \"this\" is the wrong type") \
188189
V(ERR_INVALID_URL_SCHEME, "The URL must be of scheme file:") \
190+
V(ERR_LOAD_SQLITE_EXTENSION, "Failed to load SQLite extension") \
189191
V(ERR_MEMORY_ALLOCATION_FAILED, "Failed to allocate memory") \
190192
V(ERR_OSSL_EVP_INVALID_DIGEST, "Invalid digest used") \
191193
V(ERR_MESSAGE_TARGET_CONTEXT_UNAVAILABLE, \

src/node_sqlite.cc

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,13 +253,18 @@ void DatabaseSync::LoadExtension(const FunctionCallbackInfo<Value>& args) {
253253
return;
254254
}
255255

256-
BufferValue path(env->isolate(), args[0]);
256+
auto isolate = env->isolate();
257+
258+
BufferValue path(isolate, args[0]);
257259
CHECK_NOT_NULL(*path);
258260
ToNamespacedPath(env, &path);
259261
THROW_IF_INSUFFICIENT_PERMISSIONS(
260262
env, permission::PermissionScope::kFileSystemRead, path.ToStringView());
261-
int r = sqlite3_load_extension(db->connection_, *path, nullptr, nullptr);
262-
CHECK_ERROR_OR_THROW(env->isolate(), db->connection_, r, SQLITE_OK, void());
263+
char* errmsg = nullptr;
264+
int r = sqlite3_load_extension(db->connection_, *path, nullptr, &errmsg);
265+
if (r != SQLITE_OK) {
266+
isolate->ThrowException(node::ERR_LOAD_SQLITE_EXTENSION(isolate, errmsg));
267+
}
263268
}
264269

265270
StatementSync::StatementSync(Environment* env,

test/parallel/test-sqlite.js

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,29 +92,75 @@ suite('DatabaseSync() constructor', () => {
9292
});
9393

9494
suite('DatabaseSync.prototype.loadExtension()', () => {
95+
test('throws if database is not open', (t) => {
96+
const db = new DatabaseSync(nextDb(), { open: false });
97+
t.assert.throws(() => {
98+
db.loadExtension();
99+
}, {
100+
code: 'ERR_INVALID_STATE',
101+
message: /database is not open/,
102+
});
103+
});
104+
105+
test('throws if path is not a valid sqlite extension', (t) => {
106+
const db = new DatabaseSync(nextDb(), {
107+
allowLoadExtension: true,
108+
});
109+
// Try to load a non-existent file
110+
const files = [
111+
'/dev/null',
112+
path('a.js'),
113+
path('shared-memory.wasm'),
114+
path('crash.wat'),
115+
path('doc_inc_1.md'),
116+
path('utf8-bom.json'),
117+
path('x.txt'),
118+
];
119+
for (const file of files) {
120+
t.assert.throws(() => {
121+
db.loadExtension(file);
122+
}, {
123+
code: 'ERR_LOAD_SQLITE_EXTENSION',
124+
}, `loadExtension("${file}") should throw an error`);
125+
}
126+
});
127+
95128
test('loads an extension', (t) => {
96129
const dbPath = nextDb();
97130
const db = new DatabaseSync(dbPath, { allowLoadExtension: true });
98131
const supportedPlatforms = [
99-
["macos","x86_64"],
100-
["windows","x86_64"],
101-
["linux","x86_64"],
102-
["macos","aarch64"]
132+
['macos', 'x86_64'],
133+
['windows', 'x86_64'],
134+
['linux', 'x86_64'],
135+
['macos', 'aarch64'],
103136
];
104137
function validPlatform(platform, arch) {
105138
return (
106-
supportedPlatforms.find(([p, a]) => platform == p && arch === a) !== null
139+
supportedPlatforms.find(([p, a]) => platform === p && arch === a) !== null
107140
);
108141
}
142+
143+
function getExtension(platform, arch) {
144+
switch (platform) {
145+
case 'darwin':
146+
return arch === 'arm64' ? '.aarch64.dylib' : '.x86_64.dylib';
147+
case 'windows':
148+
return '.dll';
149+
case 'linux':
150+
return '.so';
151+
default:
152+
return null;
153+
}
154+
}
109155
const platform = os.platform();
110-
const arch = os.arch();
156+
const arch = process.arch;
111157
if (!validPlatform(platform, arch)) {
112-
t.skip();
158+
t.skip('Unsupported platform');
113159
return;
114160
}
115-
const ext = platform === 'win32' ? 'dll' : platform === 'darwin' ? process.arch === 'arm64' ? '.aarch64.dylib' : '.x86_64.dylib' : 'so';
116-
const extension = path('sqlite', `vec0${ext}`)
117-
t.assert.strictEqual(db.loadExtension(extension), undefined);
161+
const ext = getExtension(platform, arch);
162+
const filePath = path('sqlite', `vec0${ext}`);
163+
t.assert.strictEqual(db.loadExtension(filePath), undefined);
118164
});
119165
});
120166

0 commit comments

Comments
 (0)