Skip to content

Commit

Permalink
test: convert test_encoding_binding.cc to a JS test
Browse files Browse the repository at this point in the history
The cctest file `test_encoding_binding.cc` is never tested and it is
not a valid test. Binding functions should never be tested with V8 API
circumvented. A binding function should only be tested with JS calls.

PR-URL: #56791
Refs: #55275
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Daniel Lemire <daniel@lemire.me>
Reviewed-By: Richard Lau <rlau@redhat.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
legendecas authored and nodejs-github-bot committed Jan 29, 2025
1 parent 1b2a966 commit 3c105b6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 176 deletions.
176 changes: 0 additions & 176 deletions test/cctest/test_encoding_binding.cc

This file was deleted.

48 changes: 48 additions & 0 deletions test/parallel/test-internal-encoding-binding.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Flags: --expose-internals

'use strict';

require('../common');

const assert = require('node:assert');
const { internalBinding } = require('internal/test/binding');
const binding = internalBinding('encoding_binding');

{
// Valid input
const buf = Uint8Array.from([0xC1, 0xE9, 0xF3]);
assert.strictEqual(binding.decodeLatin1(buf, false, false), 'Áéó');
}

{
// Empty input
const buf = Uint8Array.from([]);
assert.strictEqual(binding.decodeLatin1(buf, false, false), '');
}

{
// Invalid input, but Latin1 has no invalid chars and should never throw.
const buf = new TextEncoder().encode('Invalid Latin1 🧑‍🧑‍🧒‍🧒');
assert.strictEqual(
binding.decodeLatin1(buf, false, false),
'Invalid Latin1 ð\x9F§\x91â\x80\x8Dð\x9F§\x91â\x80\x8Dð\x9F§\x92â\x80\x8Dð\x9F§\x92'
);
}

{
// IgnoreBOM with BOM
const buf = Uint8Array.from([0xFE, 0xFF, 0xC1, 0xE9, 0xF3]);
assert.strictEqual(binding.decodeLatin1(buf, true, false), 'þÿÁéó');
}

{
// Fatal and InvalidInput, but Latin1 has no invalid chars and should never throw.
const buf = Uint8Array.from([0xFF, 0xFF, 0xFF]);
assert.strictEqual(binding.decodeLatin1(buf, false, true), 'ÿÿÿ');
}

{
// IgnoreBOM and Fatal, but Latin1 has no invalid chars and should never throw.
const buf = Uint8Array.from([0xFE, 0xFF, 0xC1, 0xE9, 0xF3]);
assert.strictEqual(binding.decodeLatin1(buf, true, true), 'þÿÁéó');
}

0 comments on commit 3c105b6

Please sign in to comment.