Skip to content

Commit 6b0bdfe

Browse files
committed
Fix shim unit tests
1 parent 457995d commit 6b0bdfe

File tree

2 files changed

+45
-20
lines changed

2 files changed

+45
-20
lines changed

instrumentation-wasm/src/js_transformer/transformer.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ pub fn transform_code_str(
7777
.with_options(CodegenOptions {
7878
comments: true,
7979
minify: false,
80+
// Todo add source map using source_map_path
8081
..CodegenOptions::default()
8182
})
8283
.build(&program);
Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
import * as t from "tap";
22
import { generateBuildinShim } from "./builtinShim";
3+
import { setBuiltinsToInstrument } from "./instructions";
4+
import { BuiltinModule } from "../BuiltinModule";
5+
import { createTestAgent } from "../../../helpers/createTestAgent";
36

4-
t.test("generate fs/promises shim", async (t) => {
5-
// Todo update
6-
const shim = generateBuildinShim("fs/promises", "fs/promises", [
7-
{
8-
name: "readFile",
9-
inspectArgs: true,
10-
modifyArgs: false,
11-
modifyReturnValue: false,
12-
},
13-
]);
7+
t.test("Wrap fs/promises", (t) => {
8+
const agent = createTestAgent();
9+
10+
const fsPromises = new BuiltinModule("fs/promises");
11+
12+
fsPromises.onRequire((exports, pkgInfo) => {
13+
exports.test = 42;
14+
exports.__aikido_is_awesome = true;
15+
});
16+
17+
setBuiltinsToInstrument([fsPromises]);
18+
19+
const shim = generateBuildinShim("fs/promises", "fs/promises", true);
1420
if (!shim) {
1521
t.fail("shim is undefined");
1622
return;
1723
}
1824

1925
t.match(
2026
shim.replace(/\s+/g, " "),
21-
`const orig = process.getBuiltinModule("fs/promises");
22-
const { __instrumentInspectArgs } = require('@aikidosec/firewall/instrument/internals');
23-
24-
orig.readFile = function() {
25-
__instrumentInspectArgs("fs/promises.readFile", arguments);
26-
return orig.readFile(...arguments);
27-
};
28-
29-
exports = orig;
27+
`const orig = process.getBuiltinModule("fs/promises");
28+
const { __wrapBuiltinExports } = require('@aikidosec/firewall/instrument/internals');
29+
module.exports = __wrapBuiltinExports("fs/promises", orig);
3030
`.replace(/\s+/g, " ")
3131
);
3232

@@ -36,5 +36,29 @@ exports = orig;
3636
);
3737

3838
let modifiedExports = eval(modifiedShim);
39-
console.log(modifiedExports);
39+
t.equal(modifiedExports.test, 42);
40+
t.equal(modifiedExports.__aikido_is_awesome, true);
41+
t.ok(typeof modifiedExports.readFile === "function");
42+
t.ok(typeof modifiedExports.writeFile === "function");
43+
44+
const shimESM = generateBuildinShim("fs/promises", "fs/promises", false);
45+
if (!shimESM) {
46+
t.fail("shim is undefined");
47+
return;
48+
}
49+
50+
t.match(
51+
shimESM.replace(/\s+/g, " "),
52+
`const orig = process.getBuiltinModule("fs/promises");
53+
const { __wrapBuiltinExports } = require('@aikidosec/firewall/instrument/internals');
54+
55+
const wrapped = __wrapBuiltinExports("fs/promises", orig);
56+
57+
Object.defineProperty(exports, "__esModule", { value: true });
58+
exports.default = wrapped.default;
59+
exports.access = wrapped.access;
60+
`.replace(/\s+/g, " ")
61+
);
62+
63+
t.end();
4064
});

0 commit comments

Comments
 (0)