Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/core/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export function assertUint8Array(
value: unknown,
path: string
): asserts value is Uint8Array {
if (!(value instanceof Uint8Array)) {
if (!isUint8Array(value)) {
throw new XdrError(`${path}: expected Uint8Array`);
}
// Check if the Uint8Array is detached or out of bounds by attempting to access an element.
Expand Down
7 changes: 7 additions & 0 deletions test/unit/opaque.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { runInNewContext } from 'node:vm';
import { describe, it, expect } from 'vitest';
import { opaque } from '../../src/index.js';
import { bytes, toArray, encodeInvalid } from './_helpers.js';
Expand Down Expand Up @@ -27,6 +28,12 @@ describe('opaque (fixed-length)', () => {
/expected Uint8Array/i
);
});

it('accepts a Uint8Array from another realm', () => {
const foreign = runInNewContext('new Uint8Array([1, 2, 3, 4])');
expect(toArray(opaque(4).encode(foreign))).toEqual([1, 2, 3, 4]);
expect(opaque(4).validate(foreign)).toBe(true);
});
});

describe('decode', () => {
Expand Down
9 changes: 9 additions & 0 deletions test/unit/string.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { runInNewContext } from 'node:vm';
import { describe, it, expect } from 'vitest';
import { string } from '../../src/index.js';
import { bytes, toArray, roundTrip, encodeInvalid } from './_helpers.js';
Expand Down Expand Up @@ -32,6 +33,14 @@ describe('string', () => {
expect(() => encodeInvalid(schema, 'Hi')).toThrow(/expected Uint8Array/i);
expect(() => encodeInvalid(schema, 123)).toThrow(/expected Uint8Array/i);
});

it('accepts a Uint8Array from another realm', () => {
const foreign = runInNewContext('new Uint8Array([104, 105])');
expect(toArray(schema.encode(foreign))).toEqual([
0, 0, 0, 2, 104, 105, 0, 0
]);
expect(schema.validate(foreign)).toBe(true);
});
});

describe('decode', () => {
Expand Down
12 changes: 11 additions & 1 deletion test/unit/struct.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { runInNewContext } from 'node:vm';
import { describe, it, expect } from 'vitest';
import { struct, int32, bool } from '../../src/index.js';
import { struct, int32, bool, varOpaque } from '../../src/index.js';
import { bytes, toArray, roundTrip, encodeInvalid } from './_helpers.js';

const Range = struct('Range', {
Expand Down Expand Up @@ -61,6 +62,15 @@ describe('struct', () => {
Range.encode({ begin: 2147483648, end: 0, inclusive: false })
).toThrow(/range/i);
});

it('accepts a byte field holding a Uint8Array from another realm', () => {
const Blob = struct('Blob', { data: varOpaque(10) });
const foreign = runInNewContext('new Uint8Array([1, 2, 3])');
expect(toArray(Blob.encode({ data: foreign }))).toEqual([
0, 0, 0, 3, 1, 2, 3, 0
]);
expect(Blob.validate({ data: foreign })).toBe(true);
});
});

describe('decode', () => {
Expand Down
20 changes: 20 additions & 0 deletions test/unit/var-opaque.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { runInNewContext } from 'node:vm';
import { describe, it, expect } from 'vitest';
import { varOpaque } from '../../src/index.js';
import { bytes, toArray, encodeInvalid } from './_helpers.js';
Expand Down Expand Up @@ -30,6 +31,25 @@ describe('varOpaque (variable-length)', () => {
/expected Uint8Array/i
);
});

it('accepts a Uint8Array from another realm', () => {
const foreign = runInNewContext('new Uint8Array([1, 2, 3])');
expect(toArray(schema.encode(foreign))).toEqual([0, 0, 0, 3, 1, 2, 3, 0]);
expect(schema.validate(foreign)).toBe(true);
});

it('rejects values that only look like a Uint8Array', () => {
const spoof = { [Symbol.toStringTag]: 'Uint8Array', length: 0 };
expect(() => encodeInvalid(schema, spoof)).toThrow(
/expected Uint8Array/i
);
expect(() => encodeInvalid(schema, new Proxy(bytes([1]), {}))).toThrow(
/expected Uint8Array/i
);
expect(() =>
encodeInvalid(schema, runInNewContext('new Uint16Array(1)'))
).toThrow(/expected Uint8Array/i);
});
});

describe('decode', () => {
Expand Down
Loading