Key a TLV input like the read side, drop tagId, copy TLV octets at parse, and export only Tlvs, TlvInputs and Tlv
Mirror / push (push) Has been cancelled
Test / lint (pull_request) Successful in 29s
Test / test (18) (pull_request) Successful in 31s
Test / test (20) (pull_request) Successful in 31s
Test / test (22) (pull_request) Successful in 37s
Test / test (24) (pull_request) Successful in 31s
Test / test (26) (pull_request) Successful in 38s

This commit is contained in:
2026-09-27 15:18:08 +02:00
parent 75d4794522
commit 939eda7269
13 changed files with 118 additions and 138 deletions
+2 -2
View File
@@ -4,7 +4,7 @@ import { consts } from '../src/defs/constants.ts';
import { dlrFromPdu, parseReceipt, receiptCodes } from '../src/dlr.ts';
import { encodeMessage } from '../src/message.ts';
import { objToPdu, pduToObj } from '../src/pdu.ts';
import type { PduObject, TlvInput } from '../src/pdu.ts';
import type { PduObject, TlvInputs } from '../src/pdu.ts';
const receiptText = 'id:0195f0c7 sub:001 dlvrd:001 submit date:2508251430 done date:2508251431 stat:DELIVRD err:000 text:hello there';
@@ -14,7 +14,7 @@ const textReceipt = `id:${textReceiptId} sub:001 dlvrd:001 submit date:250905143
function deliverSm(
message: Buffer | string,
tlvs?: Record<string, TlvInput>,
tlvs?: TlvInputs,
esmClass: number = consts.ESM_CLASS.MC_DELIVERY_RECEIPT,
dataCoding = 0,
): PduObject {
+2 -2
View File
@@ -95,8 +95,8 @@ describe('our encoder against the reference parser', () => {
},
seqNr: 77,
tlvs: {
message_state: { tagId: 0x0427, tagValue: 2 },
receipted_message_id: { tagId: 0x001E, tagValue: 'abc123' },
message_state: { tagValue: 2 },
receipted_message_id: { tagValue: 'abc123' },
},
}));
+3 -3
View File
@@ -2,7 +2,7 @@ import assert from 'node:assert/strict';
import test, { describe } from 'node:test';
import type { Dlr, Receipt } from '../src/dlr.ts';
import type { MessageDlr } from '../src/session.ts';
import type { PduObject, TlvInput } from '../src/pdu.ts';
import type { PduObject, TlvInputs } from '../src/pdu.ts';
import { bindToSmsc, dummySmsc } from './dummy-smsc.ts';
import { consts } from '../src/defs/constants.ts';
import { dlrFromPdu, parseReceipt, receiptCodes, transientStates } from '../src/dlr.ts';
@@ -16,7 +16,7 @@ import { objToPdu, pduToObj } from '../src/pdu.ts';
function deliverSm(
body: string,
tlvs?: Record<string, TlvInput>,
tlvs?: TlvInputs,
esmClass: number = consts.ESM_CLASS.MC_DELIVERY_RECEIPT,
): PduObject {
const { buffer } = objToPdu({
@@ -53,7 +53,7 @@ type ReceiptFixture = {
name: string;
receipt: Receipt;
source: string;
tlvs?: Record<string, TlvInput>;
tlvs?: TlvInputs;
};
const fixtures: readonly ReceiptFixture[] = [
+41 -22
View File
@@ -3,6 +3,7 @@ import test, { describe } from 'node:test';
import { PduRefusedError, refusalAnswer } from '../src/pdu-refusal.ts';
import { isCommand, isResp, objToPdu, pduReturn, pduToObj } from '../src/pdu.ts';
import { paramText } from '../src/defs/types.ts';
import { tlvsById } from '../src/defs/tlvs.ts';
function encode(...args: Parameters<typeof objToPdu>): Buffer {
const { buffer, err } = objToPdu(...args);
@@ -377,7 +378,7 @@ describe('TLVs', () => {
},
seqNr: 393,
tlvs: {
5142: { tagId: 5142, tagValue: Buffer.from('blajfoo', 'ascii') },
5142: { tagValue: Buffer.from('blajfoo', 'ascii') },
receipted_message_id: { tagValue: '293f293' },
},
}));
@@ -432,25 +433,22 @@ describe('TLVs', () => {
assert.deepEqual(pduObj.tlvs.source_port, { tagId: 0x020A, tagName: 'source_port', tagValue: 1234 });
});
test('refuses an unknown tag name rather than putting a wrong tag on the wire', () => {
const { buffer, err } = objToPdu({
cmdName: 'deliver_sm',
params: { destination_addr: '46709771337', short_message: 'hi', source_addr: '46701113311' },
tlvs: { nils: { tagValue: 'blajfoo' } },
});
test('refuses a TLV keyed any way but by its name, or by its decimal id where the table names none', () => {
const params = { destination_addr: '46709771337', short_message: 'hi', source_addr: '46701113311' };
const refusals = [
// @ts-expect-error nils is no tag name
{ built: objToPdu({ cmdName: 'deliver_sm', params, tlvs: { nils: { tagValue: 'blajfoo' } } }), reason: /decimal id/ },
{ built: objToPdu({ cmdName: 'deliver_sm', params, tlvs: { 5142: { tagId: 5142, tagValue: 'blajfoo' } } }), reason: /instead of giving a tagId/ },
{ built: objToPdu({ cmdName: 'deliver_sm', params, tlvs: { 65536: { tagValue: 'blajfoo' } } }), reason: /out of range/ },
{ built: objToPdu({ cmdName: 'deliver_sm', params, tlvs: { '05142': { tagValue: 'blajfoo' } } }), reason: /decimal id/ },
{ built: objToPdu({ cmdName: 'deliver_sm', params, tlvs: { 1063: { tagValue: 2 } } }), reason: /message_state/ },
];
assert.equal(buffer, undefined);
assert.ok(err instanceof Error);
});
test('refuses a tag id that does not fit the two octet field', () => {
const { err } = objToPdu({
cmdName: 'deliver_sm',
params: { destination_addr: '46709771337', short_message: 'hi', source_addr: '46701113311' },
tlvs: { nils: { tagId: 0x10000, tagValue: 'blajfoo' } },
});
assert.ok(err instanceof Error);
for (const { built: { buffer, err }, reason } of refusals) {
assert.equal(buffer, undefined);
assert.ok(err instanceof Error);
assert.match(err.message, reason);
}
});
test('refuses a TLV too long for the two octet length field', () => {
@@ -509,12 +507,33 @@ describe('TLVs', () => {
}
});
test('reads every tag in the table as the type its entry declares', () => {
const bare = encode({ cmdName: 'deliver_sm', params: { destination_addr: '46709771337', source_addr: '46701113311' } });
for (const { id, tag, type } of Object.values(tlvsById)) {
const sized = type.size(type.default);
assert.ok(sized.size !== undefined);
const tlv = Buffer.alloc(4 + sized.size);
tlv.writeUInt16BE(id, 0);
tlv.writeUInt16BE(sized.size, 2);
assert.equal(type.write(type.default, tlv, 4).err, undefined);
const pdu = Buffer.concat([bare, tlv]);
pdu.writeUInt32BE(pdu.length, 0);
assert.ok(Object.hasOwn(decode(pdu).tlvs, tag), tag);
}
});
test('types each known TLV by its tag, and an unknown one as octets', () => {
const pduObj = decode(encode({
cmdName: 'deliver_sm',
params: { destination_addr: '46709771337', short_message: 'hi', source_addr: '46701113311' },
tlvs: {
5142: { tagId: 5142, tagValue: Buffer.from('01', 'hex') },
5142: { tagValue: Buffer.from('01', 'hex') },
message_state: { tagValue: 2 },
receipted_message_id: { tagValue: '0199d8a4-5e2c-7b3f-9a61-c4e07f2d8b15' },
},
@@ -541,8 +560,8 @@ describe('TLVs', () => {
},
seqNr: 323,
tlvs: {
message_state: { tagId: 1063, tagValue: 2 },
receipted_message_id: { tagId: 30, tagValue: 450 },
message_state: { tagValue: 2 },
receipted_message_id: { tagValue: 450 },
},
}));
+5 -22
View File
@@ -173,18 +173,17 @@ describe('a body the PDU\'s own data_coding cannot carry', () => {
assert.match(built.err.message, /U\+3042/);
});
test('refuses the body TLV under whatever name the caller keyed its tagId to', () => {
test('refuses the body TLV keyed by its id, so only message_payload is written as text', () => {
const built = objToPdu({
cmdName: 'data_sm',
params: { data_coding: 0x03, destination_addr: to, source_addr: from },
tlvs: { body: { tagId: 0x0424, tagValue: 'あいう' } },
params: { data_coding: 0x08, destination_addr: to, source_addr: from },
tlvs: { 1060: { tagValue: 'あいう' }, message_payload: { tagValue: 'あいう' } },
});
assert.ok(built.err instanceof Error);
assert.equal(built.buffer, undefined);
assert.match(built.err.message, /"body"/);
assert.match(built.err.message, /LATIN1/);
assert.match(built.err.message, /U\+3042/);
assert.match(built.err.message, /"1060"/);
assert.match(built.err.message, /message_payload/);
});
test('leaves data_coding to short_message wherever it carries octets, as messageOctets() reads it', () => {
@@ -205,22 +204,6 @@ describe('a body the PDU\'s own data_coding cannot carry', () => {
assert.deepEqual(messageOctets(pduObj), short);
});
test('encodes every entry carrying the body tag, so a second one cannot go out truncated', () => {
const built = objToPdu({
cmdName: 'data_sm',
params: { data_coding: 0x08, destination_addr: to, source_addr: from },
tlvs: { alias: { tagId: 0x0424, tagValue: 'あいう' }, message_payload: { tagValue: 'あいう' } },
});
assert.equal(built.err, undefined);
assert.ok(built.buffer);
const hex = built.buffer.toString('hex');
assert.equal(hex.split('304230443046').length - 1, 2, 'both entries carry the UCS2 octets');
assert.ok(!hex.includes('424446'), 'no entry goes out as the low octets of its code points');
});
test('leaves the alphabet to the body TLV wherever short_message carries no octets', () => {
for (const short of [undefined, '', Buffer.alloc(0)]) {
const built = objToPdu({