summaryrefslogtreecommitdiff
path: root/cli/tests/unit/ffi_test.ts
blob: 690b37afcb3b1e11d6928a611dd6fa69834e5b6f (plain)
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
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

import { 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(
    0n,
    {
      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(0n);
  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);
});