1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-24 15:19:26 -05:00
denoland-deno/tests/unit/ffi_test.ts
Matt Mastracci f5e46c9bf2
chore: move cli/tests/ -> tests/ (#22369)
This looks like a massive PR, but it's only a move from cli/tests ->
tests, and updates of relative paths for files.

This is the first step towards aggregate all of the integration test
files under tests/, which will lead to a set of integration tests that
can run without the CLI binary being built.

While we could leave these tests under `cli`, it would require us to
keep a more complex directory structure for the various test runners. In
addition, we have a lot of complexity to ignore various test files in
the `cli` project itself (cargo publish exclusion rules, autotests =
false, etc).

And finally, the `tests/` folder will eventually house the `test_ffi`,
`test_napi` and other testing code, reducing the size of the root repo
directory.

For easier review, the extremely large and noisy "move" is in the first
commit (with no changes -- just a move), while the remainder of the
changes to actual files is in the second commit.
2024-02-10 20:22:13 +00:00

137 lines
4.2 KiB
TypeScript

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assertEquals, assertRejects, assertThrows } from "./test_util.ts";
Deno.test({ permissions: { ffi: true } }, function dlopenInvalidArguments() {
const filename = "/usr/lib/libc.so.6";
assertThrows(() => {
// @ts-expect-error: ForeignFunction cannot be null
Deno.dlopen(filename, { malloc: null });
}, TypeError);
assertThrows(() => {
Deno.dlopen(filename, {
// @ts-expect-error: invalid NativeType
malloc: { parameters: ["a"], result: "b" },
});
}, TypeError);
assertThrows(() => {
// @ts-expect-error: DynamicLibrary symbols cannot be null
Deno.dlopen(filename, null);
}, TypeError);
assertThrows(() => {
// @ts-expect-error: require 2 arguments
Deno.dlopen(filename);
}, TypeError);
});
Deno.test({ permissions: { ffi: false } }, function ffiPermissionDenied() {
assertThrows(() => {
Deno.dlopen("/usr/lib/libc.so.6", {});
}, Deno.errors.PermissionDenied);
const fnptr = new Deno.UnsafeFnPointer(
// @ts-expect-error: Not NonNullable but null check is after permissions check.
null,
{
parameters: ["u32", "pointer"],
result: "void",
} as const,
);
assertThrows(() => {
fnptr.call(123, null);
}, Deno.errors.PermissionDenied);
assertThrows(() => {
Deno.UnsafePointer.of(new Uint8Array(0));
}, Deno.errors.PermissionDenied);
const ptrView = new Deno.UnsafePointerView(
// @ts-expect-error: Not NonNullable but null check is after permissions check.
null,
);
assertThrows(() => {
ptrView.copyInto(new Uint8Array(0));
}, Deno.errors.PermissionDenied);
assertThrows(() => {
ptrView.getCString();
}, Deno.errors.PermissionDenied);
assertThrows(() => {
ptrView.getUint8();
}, Deno.errors.PermissionDenied);
assertThrows(() => {
ptrView.getInt8();
}, Deno.errors.PermissionDenied);
assertThrows(() => {
ptrView.getUint16();
}, Deno.errors.PermissionDenied);
assertThrows(() => {
ptrView.getInt16();
}, Deno.errors.PermissionDenied);
assertThrows(() => {
ptrView.getUint32();
}, Deno.errors.PermissionDenied);
assertThrows(() => {
ptrView.getInt32();
}, Deno.errors.PermissionDenied);
assertThrows(() => {
ptrView.getFloat32();
}, Deno.errors.PermissionDenied);
assertThrows(() => {
ptrView.getFloat64();
}, Deno.errors.PermissionDenied);
});
Deno.test({ permissions: { ffi: true } }, function pointerOf() {
const buffer = new ArrayBuffer(1024);
const baseAddress = Deno.UnsafePointer.value(Deno.UnsafePointer.of(buffer));
const uint8Address = Deno.UnsafePointer.value(
Deno.UnsafePointer.of(new Uint8Array(buffer)),
);
assertEquals(baseAddress, uint8Address);
const float64Address = Deno.UnsafePointer.value(
Deno.UnsafePointer.of(new Float64Array(buffer)),
);
assertEquals(baseAddress, float64Address);
const uint8AddressOffset = Deno.UnsafePointer.value(
Deno.UnsafePointer.of(new Uint8Array(buffer, 100)),
);
assertEquals(Number(baseAddress) + 100, uint8AddressOffset);
const float64AddressOffset = Deno.UnsafePointer.value(
Deno.UnsafePointer.of(new Float64Array(buffer, 80)),
);
assertEquals(Number(baseAddress) + 80, float64AddressOffset);
});
Deno.test({ permissions: { ffi: true } }, function callWithError() {
const throwCb = () => {
throw new Error("Error");
};
const cb = new Deno.UnsafeCallback({
parameters: [],
result: "void",
}, throwCb);
const fnPointer = new Deno.UnsafeFnPointer(cb.pointer, {
parameters: [],
result: "void",
});
assertThrows(() => fnPointer.call());
cb.close();
});
Deno.test(
{ permissions: { ffi: true }, ignore: true },
async function callNonBlockingWithError() {
const throwCb = () => {
throw new Error("Error");
};
const cb = new Deno.UnsafeCallback({
parameters: [],
result: "void",
}, throwCb);
const fnPointer = new Deno.UnsafeFnPointer(cb.pointer, {
parameters: [],
result: "void",
nonblocking: true,
});
// TODO(mmastrac): currently ignored as we do not thread callback exceptions through nonblocking pointers
await assertRejects(async () => await fnPointer.call());
cb.close();
},
);