forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-sqlite-extensions.mjs
More file actions
112 lines (102 loc) · 3.35 KB
/
Copy pathtest-sqlite-extensions.mjs
File metadata and controls
112 lines (102 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import * as common from '../common/index.mjs';
import assert from 'node:assert';
import path from 'node:path';
import sqlite from 'node:sqlite';
import test from 'node:test';
import fs from 'node:fs';
import childProcess from 'child_process';
if (process.config.variables.node_shared_sqlite) {
common.skip('Missing libsqlite_extension binary');
}
// Lib extension binary is named differently on different platforms
function resolveBuiltBinary(binary) {
const targetFile = fs.readdirSync(path.dirname(process.execPath)).find((file) => file.startsWith(binary));
return path.join(path.dirname(process.execPath), targetFile);
}
const binary = resolveBuiltBinary('libsqlite_extension');
test('should load extension successfully', () => {
const db = new sqlite.DatabaseSync(':memory:', {
allowExtension: true,
});
db.loadExtension(binary);
db.exec('SELECT noop(\'Hello, world!\');');
const query = db.prepare('SELECT noop(\'Hello, World!\') AS result');
const { result } = query.get();
assert.strictEqual(result, 'Hello, World!');
});
test('should not load extension', () => {
const db = new sqlite.DatabaseSync(':memory:', {
allowExtension: false,
});
assert.throws(() => {
db.exec('SELECT noop(\'Hello, world!\');');
}, {
message: 'no such function: noop',
code: 'ERR_SQLITE_ERROR',
});
assert.throws(() => {
db.loadExtension(binary);
}, {
message: 'extension loading is not allowed',
code: 'ERR_INVALID_STATE',
});
assert.throws(() => {
const query = db.prepare('SELECT load_extension(?)');
query.run(binary);
}, {
message: 'not authorized',
code: 'ERR_SQLITE_ERROR',
});
assert.throws(() => {
db.enableLoadExtension();
}, {
message: 'The "allow" argument must be a boolean.',
code: 'ERR_INVALID_ARG_TYPE',
});
assert.throws(() => {
db.enableLoadExtension(true);
}, {
message: 'Cannot enable extension loading because it was disabled at database creation.',
});
});
test('should load extension successfully with enableLoadExtension', () => {
const db = new sqlite.DatabaseSync(':memory:', {
allowExtension: true,
});
db.loadExtension(binary);
db.enableLoadExtension(false);
db.exec('SELECT noop(\'Hello, world!\');');
const query = db.prepare('SELECT noop(\'Hello, World!\') AS result');
const { result } = query.get();
assert.strictEqual(result, 'Hello, World!');
});
test('should not load extension with enableLoadExtension', () => {
const db = new sqlite.DatabaseSync(':memory:', {
allowExtension: true,
});
db.enableLoadExtension(false);
assert.throws(() => {
db.loadExtension(binary);
}, {
message: 'extension loading is not allowed',
});
});
test('should throw error if permission is enabled', async () => {
const [cmd, opts] = common.escapePOSIXShell`"${process.execPath}" `;
const code = `const sqlite = require('node:sqlite');
const db = new sqlite.DatabaseSync(':memory:', { allowExtension: true });`;
return new Promise((resolve) => {
childProcess.exec(
`${cmd} --permission -e "${code}"`,
{
...opts,
},
common.mustCall((err, _, stderr) => {
assert.strictEqual(err.code, 1);
assert.match(stderr, /Error: Cannot load SQLite extensions when the permission model is enabled/);
assert.match(stderr, /code: 'ERR_LOAD_SQLITE_EXTENSION'/);
resolve();
}),
);
});
});