v3.33.0: Edit decoded — SPN, PLMNwAcT family, ADN family, PNN

The file manager gains a client-side decoded editor.  EF_ENCODERS pairs
each editable EF with a pure encoder and a form schema (fields / list /
items); the file is read raw, the decoder fills the form, the browser
re-encodes it and the existing update path writes it.

Shapes: SPN (display-condition bits + Annex A text), PLMNwAcT/
OPLMNwAcT/HPLMNwAcT (rows with PLMN + ACT, add/remove),
ADN/FDN/SDN/MSISDN/LND/BDN (one form per record with a checkbox that
auto-checks on change; BCD, length and EXT handling; BDN's own X+15
layout) and PNN (per-record TLV items 43/45/80, add/remove, unknown
TLVs preserved).

Unchanged fields are copied verbatim (SSC/extended-BCD numbers, PLMN
rows including unmodelled satellite ACT bits, PNN codings/AddCI bits,
all-FF files, the BDN comparison-method byte), every checked record is
validated and encoded before anything is written, and edits that would
break a reference are refused.

Also: docs/decoded-editing.md (the spec survey behind the design and
the release waves), the efPlmnAct EC-GSM-IoT-only 0x88 fix, the PNN
spare-bit decode fix, and the /api/write|read cmd2-exception
false-success fix.

1022 Python + 1013 frontend green; simple-v466.
This commit is contained in:
2026-10-23 11:55:31 +03:00
parent 9411fd775d
commit d036ef79a1
16 changed files with 2508 additions and 27 deletions
+2 -1
View File
@@ -408,7 +408,8 @@
<p class="text-sm mb-2">Дерево файловой системы отображается слева; выбор файла открывает панель деталей справа. Элементы сгруппированы: DF выше EF, сортировка по <strong>FID</strong> или символьному <strong>имени</strong> (пиллы и кнопка <strong>«Проверить все файлы»</strong> закреплены над прокручиваемым деревом; выбор сохраняется в <code class="font-mono text-sm">localStorage</code>). При выборе файла над содержимым также показываются FID, тип файла, размер / структура записей и декодированный FCI — в рамке с символьным именем файла. Если FCI ссылается на свои правила доступа через запись <code class="font-mono text-sm">EF.ARR</code> (атрибут безопасности <code class="font-mono text-sm">8B</code>), правила указанной записи показываются с отступом под этой строкой: они берутся из копий <code class="font-mono text-sm">EF.ARR</code> карты &mdash; копии MF (<code class="font-mono text-sm">2F06</code>), копий DF.TELECOM/DF.GSM (<code class="font-mono text-sm">6F06</code>) и копии ADF.USIM (<code class="font-mono text-sm">6F06</code>): читаются один раз за сессию при equip, обновляются при сбросе/equip и после записи <code class="font-mono text-sm">EF.ARR</code>; снимок разрешает те же ссылки из собственной захваченной копии. Побеждает копия собственного DF файла, с подъёмом до MF; ссылка, для которой в этой цепочке копии нет, остаётся неразрешённой, если только кандидат не единственный.</p>
<ul class="list-disc list-inside text-sm space-y-1 mb-3">
<li><strong>Читать сырые данные / Декодировать</strong> — чтение выбранного файла в виде hex-дампа или таблицы декодированных полей (клиентские декодеры EF: IMSI, ICCID, SPN, списки PLMN, LOCI/PSLOCI/EPSLOCI, ADN/MSISDN, таблицы сервисов, SUME, …); таблица декодируется в браузере из тех же сырых байт (без серверного декодирования). Подсвеченная кнопка — текущий вид; нажатие любой из них (пере)читывает файл. «Декодировать» недоступна, если для файла нет клиентского декодера (проверка по имени/FID до чтения).</li>
<li><strong>Редактировать сырые данные</strong> — изменение hex-данных, <strong>Сохранить</strong> для записи (или <strong>Отмена</strong>); transparent-файлы редактируются в textarea, record-файлы — по одному полю на запись с флажками выбора. Декодированный вид доступен только для чтения — «Редактировать сырые данные» сначала переключает на hex-вид и при необходимости читает файл.</li>
<li><strong>Редактировать сырые данные</strong> — изменение hex-данных, <strong>Сохранить</strong> для записи (или <strong>Отмена</strong>); transparent-файлы редактируются в textarea, record-файлы — по одному полю на запись с флажками выбора. «Редактировать сырые данные» сначала переключает на hex-вид и при необходимости читает файл.</li>
<li><strong>Редактировать декодированные</strong> — редактирование файла в его собственной декодированной форме; кодирование выполняется в браузере, запись идёт обычным путём обновления. Кодировщики покрывают <strong>EF.SPN</strong>, списки PLMN с технологиями доступа (<strong>PLMNwAcT / OPLMNwAcT / HPLMNwAcT</strong>), файлы номеров (<strong>ADN / FDN / SDN / MSISDN / LND / BDN</strong>) и <strong>EF.PNN</strong>. Для transparent-файла — одна форма (поля SPN, строки PLMN с добавлением/удалением, элементы PNN с добавлением/удалением); для record-файла — по форме на запись с флажком выбора, который ставится автоматически при изменении поля, поэтому записываются только изменённые записи. <strong>Сохранить</strong> проверяет все выбранные записи до записи; для файлов без кодировщика кнопка недоступна с подсказкой. Указатели расширений (EF.EXT1–EF.EXT5) и байт метода сравнения BDN сохраняются; редактирование номера, продолжение которого лежит в файле расширения, отклоняется.</li>
<li><strong>Проверить все файлы</strong> — обход всего дерева (включая пользовательские файлы) с пометкой каждого элемента: есть (обычный вид) или нет (красный ✗, без стрелки разворачивания); существующие пустые DF показывают <code class="font-mono text-sm">(пусто)</code>. Отображается прогресс <em>N / всего</em>, обход можно остановить; в конце — сводка «есть/нет». Файлы проверяются только при разворачивании или проверке — просмотр остаётся ленивым.</li>
<li><strong>ADM</strong> — файлы, требующие администраторский PIN, возвращают <code class="font-mono text-sm">6982</code>/<code class="font-mono text-sm">9804</code>; если в подходящем пресете карты (тот же ICCID) есть ключ ADM, рядом с ошибкой появляется кнопка <strong>«Проверить ADM»</strong>, а значок в заголовке (<code class="font-mono text-sm">ADM ✓/✗ ⚿</code>) становится кликабельным. Каждый неверный ключ расходует попытку: остаток показывается, а повторная попытка требует подтверждения. Заблокированный ADM здесь не восстановить — нужен ключ разблокировки карты.</li>
</ul>
+2 -1
View File
@@ -407,7 +407,8 @@
<p class="text-sm mb-2">The file system tree is displayed on the left; selecting a file opens its detail pane on the right. Entries are grouped with DFs above EFs and sorted by <strong>FID</strong> or symbolic <strong>Name</strong> (pills pinned above the scrolling tree together with <strong>Probe all files</strong>; the choice is remembered in <code class="font-mono text-sm">localStorage</code>). Selecting a file also shows its FID, file type, size / record layout and the decoded FCI — in a bordered block labelled with the file&rsquo;s symbolic name — above the content pane. When the FCI references its access rules through an <code class="font-mono text-sm">EF.ARR</code> record (security attribute <code class="font-mono text-sm">8B</code>), the rules of the referenced record are shown as indented lines under that row: they are resolved from the card&rsquo;s <code class="font-mono text-sm">EF.ARR</code> copies &mdash; the MF copy (<code class="font-mono text-sm">2F06</code>), the DF.TELECOM/DF.GSM copies (<code class="font-mono text-sm">6F06</code>) and the ADF.USIM copy (<code class="font-mono text-sm">6F06</code>): read once per card session at equip, refreshed on reset/equip and after an <code class="font-mono text-sm">EF.ARR</code> write; a snapshot resolves the same references from its own captured copy. The copy of the file&rsquo;s own DF wins, walking up to MF; a reference with no copy in that chain stays unresolved unless a single candidate exists.</p>
<ul class="list-disc list-inside text-sm space-y-1 mb-3">
<li><strong>Read raw / Read decoded</strong> — read the selected file as a hex dump or as a decoded field table (client-side EF decoders: IMSI, ICCID, SPN, PLMN lists, LOCI/PSLOCI/EPSLOCI, ADN/MSISDN, service tables, SUME, …); the decoded table is built in the browser from the same raw bytes (no server-side decode). The highlighted pill is the current view; clicking either pill (re-)reads the file. <strong>Read decoded</strong> is disabled when the client-side decoders do not cover the file (name/FID check, before reading).</li>
<li><strong>Edit raw</strong> — edit the raw hex data, <strong>Save</strong> to write back (or <strong>Cancel</strong>); transparent files use a textarea, record files one input per record with selection checkboxes. The decoded view is read-only — <strong>Edit raw</strong> switches to the hex view first and reads the file if needed.</li>
<li><strong>Edit raw</strong> — edit the raw hex data, <strong>Save</strong> to write back (or <strong>Cancel</strong>); transparent files use a textarea, record files one input per record with selection checkboxes. <strong>Edit raw</strong> switches to the hex view first and reads the file if needed.</li>
<li><strong>Edit decoded</strong> — edit the file through its own decoded form, encoded in the browser and written with the normal update path. The encoders cover <strong>EF.SPN</strong>, the PLMN lists with access technology (<strong>PLMNwAcT / OPLMNwAcT / HPLMNwAcT</strong>), the dialling-number files (<strong>ADN / FDN / SDN / MSISDN / LND / BDN</strong>) and <strong>EF.PNN</strong>. A transparent file gets one form (SPN fields, PLMN rows with add/remove, PNN items with add/remove); a record file gets one form per record with a selection checkbox that is auto-checked when a field changes, so only the edited records are written. <strong>Save</strong> validates every selected record before writing anything; files without an encoder keep the button disabled with a tooltip. Extension pointers (EF.EXT1–EF.EXT5) and the BDN comparison-method byte are preserved, and editing a number that continues in an extension file is refused.</li>
<li><strong>Probe all files</strong> — walks the whole tree (including custom files) and marks every entry present (normal) or absent (red ✗, no expand arrow); empty-but-present DFs show <code class="font-mono text-sm">(empty)</code>. Shows progress <em>N / total</em>, can be stopped, and finishes with a present/absent summary. Files are only verified when expanded or probed — browsing stays lazy.</li>
<li><strong>ADM</strong> — files that need the administrator PIN fail with <code class="font-mono text-sm">6982</code>/<code class="font-mono text-sm">9804</code>; if the matching card preset (same ICCID) carries an ADM key, a <strong>Verify ADM</strong> button appears next to the error and the header badge (<code class="font-mono text-sm">ADM ✓/✗ ⚿</code>) becomes clickable. Every wrong key consumes an attempt: the remaining attempts are shown and a retry asks for confirmation. A blocked ADM cannot be recovered here — it needs the card&rsquo;s unblock key.</li>
</ul>
+934 -17
View File
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -1,4 +1,4 @@
const CACHE = 'simple-v465';
const CACHE = 'simple-v466';
const URLS = [
'index.html',
'help.html',
+3 -2
View File
@@ -57,9 +57,10 @@ test('pysimAdmSecuritySw flags only the access-condition SWs', () => {
});
test('file-manager security failures route through the ADM hint', () => {
// no raw SW error rendering is left (all three sites use the hint helper)
// no raw SW error rendering is left (every read/write site, the raw and
// the decoded edit paths, uses the hint helper)
assert.ok(!/statusEl\.textContent = 'SW: ' \+ \(data\.sw/.test(html), 'raw SW error rendering is gone');
assert.strictEqual((html.match(/pysimFsShowError\(statusEl, data\.sw/g) || []).length, 3);
assert.strictEqual((html.match(/pysimFsShowError\(statusEl, data\.sw/g) || []).length, 6);
assert.ok(html.includes("btn.textContent = t('Verify ADM')"));
assert.ok(html.includes("el.setAttribute('onclick', 'pysimVerifyAdm()')"));
});
+26 -1
View File
@@ -325,7 +325,7 @@ test('efFlatten renders fields, services and PLMNs for display', () => {
[['Service 5', 'Call Control by USIM']]);
assert.deepStrictEqual(efFlatten({ plmns: [{ mcc: '262', mnc: '01', access_tech: 'GSM' }] }),
[['PLMN 1', '262-01 (GSM)']]);
assert.deepStrictEqual(efFlatten({ show_in_hplmn: true }), [['Show in HPLMN', 'yes']]);
assert.deepStrictEqual(efFlatten({ show_in_hplmn: true }), [['Display the network name in HPLMN / an SPDI PLMN', 'yes']]);
assert.deepStrictEqual(efFlatten({ title: 'Menu!', icon: '0123' }), [['Title', 'Menu!'], ['Icon', '0123']]);
});
@@ -438,3 +438,28 @@ test('profilerRenderReport shows raw and decoded comparisons for content mismatc
assert.match(same, /Decoded values match/);
assert.doesNotMatch(same, /Decoded comparison/);
});
test('EF.BDN uses the 15-byte ADN layout (TS 31.102 4.2.44)', () => {
// alpha 'TEST' | len 06 | TON/NPI 91 | BCD 21 43 65 87 09 + FF padding |
// CCP FF | EXT4 FF | EF.CMI comparison-method pointer 03
const hex = '54455354' + '06' + '91' + '2143658709' + 'FF'.repeat(7) + '03';
const bdn = efBytes(hex);
const want = { name: 'TEST', number: '+1234567890', cmi: 3 };
assert.strictEqual(bdn.length, 19);
assert.deepStrictEqual(efDecAdn(bdn, 15), want);
assert.deepStrictEqual(efFindDecoder('EF.BDN').fn(bdn), want);
// the 14-byte default and the EF.ADN registry entry are unchanged
const adn = efBytes('42204841203120536963FFFFFFFFFFFF06810628560810FFFFFFFFFFFFFF');
const adnWant = { name: 'B HA 1 Sic', number: '6082658001' };
assert.deepStrictEqual(efDecAdn(adn), adnWant);
assert.deepStrictEqual(efFindDecoder('EF.ADN').fn(adn), adnWant);
assert.notDeepStrictEqual(efDecAdn(adn, 15), adnWant);
});
test('the BDN extension and CMI fields carry readable labels', () => {
assert.deepStrictEqual(efFlatten({ cmi: 3 }), [['Comparison method pointer', '3']]);
assert.deepStrictEqual(efFlatten({ ext1: 2 }), [['Extension record', '2']]);
const bdn = efDecAdn(efBytes('54455354' + '06' + '91' + '2143658709' +
'FF'.repeat(7) + '03'), 15);
assert.ok(efFlatten(bdn).some(r => r[0] === 'Comparison method pointer' && r[1] === '3'));
});
+513
View File
@@ -0,0 +1,513 @@
const { test } = require('node:test');
const assert = require('node:assert');
const fs = require('node:fs');
const path = require('node:path');
const html = fs.readFileSync(path.join(__dirname, '..', 'index.html'), 'utf8');
function extractFunc(src, name) {
const re = new RegExp('function\\s+' + name + '\\s*\\([^)]*\\)\\s*\\{');
const m = re.exec(src);
if (!m) throw new Error('function ' + name + ' not found');
let i = m.index + m[0].length - 1;
let depth = 0;
for (; i < src.length; i++) {
if (src[i] === '{') depth++;
else if (src[i] === '}') {
depth--;
if (depth === 0) break;
}
}
return src.slice(m.index, i + 1);
}
// Pull a const's initializer by bracket depth (the schemas/registries are
// nested objects and arrays, so the line-based extraction used elsewhere does
// not work for them).
function extractConst(src, name) {
const m = new RegExp('const ' + name + ' = ').exec(src);
if (!m) throw new Error('const ' + name + ' not found');
const begin = m.index + m[0].length;
const opener = src[begin];
const closer = opener === '[' ? ']' : (opener === '{' ? '}' : null);
if (!closer) throw new Error('unsupported const ' + name);
let depth = 0, i = begin;
for (; i < src.length; i++) {
if (src[i] === opener) depth++;
else if (src[i] === closer) {
depth--;
if (depth === 0) { i++; break; }
}
}
return 'var ' + name + ' = ' + src.slice(begin, i) + ';\n';
}
const FNS = [
'efBytes', 'efHex', 'efSwapNibbles', 'efAllFf', 'efRstripFf', 'efUcs2Be',
'efGsm7Octets', 'efAnnexA', 'efBcdAddress', 'efPlmn', 'efPlmnAct',
'efDecSpn', 'efDecPlmnWact', 'efDecAdn', 'efDecPnn', 'efFindDecoder',
'efDecodeBytes', 'efTlv', 'efPnnText', 'efDecItemsFromHex',
'efGsm7PlainText', 'efEncTextBytes', 'efPlmnEnc', 'efActEnc', 'efEncSpn',
'efEncPlmnList', 'efPlmnRowOrig', 'efEncAdn', 'efEncAdnExt', 'efPnnNameBytes', 'efPnnHexBytes', 'efPnnOrigTlv', 'efEncPnn',
'efFindEncoder', 'efHasEncoder', 'gsm7Decode', 'gsm7TextToSeptets',
'gsm7Encode', 'efEncError',
];
let code = '';
for (const f of FNS) code += extractFunc(html, f) + '\n';
for (const c of ['GSM7_ALPHABET', 'GSM7_EXT_MAP', 'EF_ACT_TECHS', 'EF_SPN_FIELDS',
'EF_ADN_FIELDS', 'EF_PLMN_LIST', 'EF_PNN_ITEMS', 'EF_ENCODERS']) {
code += extractConst(html, c);
}
eval(code);
test('efPlmnEnc builds the TS 24.008 PLMN and round-trips through efPlmn', () => {
assert.strictEqual(efPlmnEnc('262', '01'), '62F210');
assert.strictEqual(efPlmnEnc('228', '06'), '22F860');
assert.deepStrictEqual(efPlmn(efBytes('22F860')), { mcc: '228', mnc: '06' });
assert.deepStrictEqual(efPlmn(efBytes(efPlmnEnc('310', '260'))), { mcc: '310', mnc: '260' });
assert.deepStrictEqual(efPlmn(efBytes(efPlmnEnc('250', '99'))), { mcc: '250', mnc: '99' });
assert.throws(() => efPlmnEnc('26', '01'), /MCC/);
assert.throws(() => efPlmnEnc('262', '1'), /MNC/);
assert.throws(() => efPlmnEnc('26a', '01'), /MCC/);
assert.throws(() => efPlmnEnc('262', '01a'), /MNC/);
});
test('efActEnc encodes every technology name and the spec bit tables', () => {
assert.strictEqual(efActEnc('UTRAN'), '8000');
assert.strictEqual(efActEnc('NG-RAN'), '0800');
assert.strictEqual(efActEnc('E-UTRAN WB-S1, E-UTRAN NB-S1'), '4000');
assert.strictEqual(efActEnc('E-UTRAN NB-S1'), '5000');
assert.strictEqual(efActEnc('E-UTRAN WB-S1'), '6000');
assert.strictEqual(efActEnc('GSM'), '0084');
assert.strictEqual(efActEnc('EC-GSM-IoT'), '0088');
assert.strictEqual(efActEnc('GSM, EC-GSM-IoT'), '0080');
assert.strictEqual(efActEnc('GSM COMPACT'), '0040');
assert.strictEqual(efActEnc('cdma2000 HRPD'), '0020');
assert.strictEqual(efActEnc('cdma2000 1xRTT'), '0010');
// every name the decoder prints survives an encode/decode round trip
for (const name of EF_ACT_TECHS) {
const act = parseInt(efActEnc(name), 16);
assert.ok(efPlmnAct(act).indexOf(name) !== -1, name + ' round trip');
}
assert.throws(() => efActEnc(''), /at least one/);
assert.throws(() => efActEnc([]), /at least one/);
assert.throws(() => efActEnc('BOGUS'), /Unknown access technology/);
});
test('efPlmnAct decodes the EC-GSM-IoT-only 0x0088 (the old 0x0086 branch was unreachable)', () => {
// Table 4.2.5.2: b8=1, b4=1, b3=0 = 0x0088; the mask 0x008C makes 0x0086
// collapse to 0x0084 (GSM), so the EC-only branch could never fire.
assert.deepStrictEqual(efPlmnAct(0x0088), ['EC-GSM-IoT']);
assert.deepStrictEqual(efPlmnAct(0x008C), ['EC-GSM-IoT', 'GSM']);
assert.deepStrictEqual(efPlmnAct(0x0080), ['EC-GSM-IoT', 'GSM']);
assert.deepStrictEqual(efPlmnAct(0x0084), ['GSM']);
assert.deepStrictEqual(efPlmnAct(0x0040), ['GSM COMPACT']);
assert.deepStrictEqual(efPlmnAct(0x0020), ['cdma2000 HRPD']);
assert.deepStrictEqual(efPlmnAct(0x0010), ['cdma2000 1xRTT']);
assert.deepStrictEqual(efPlmnAct(0x8000), ['UTRAN']);
assert.deepStrictEqual(efPlmnAct(0x0800), ['NG-RAN']);
assert.deepStrictEqual(efPlmnAct(0x4000), ['E-UTRAN NB-S1', 'E-UTRAN WB-S1']);
assert.deepStrictEqual(efPlmnAct(0x5000), ['E-UTRAN NB-S1']);
assert.deepStrictEqual(efPlmnAct(0x6000), ['E-UTRAN WB-S1']);
});
const SPN_ORIG = '0147534d2d52204348ffffffffffffffff';
test('EF.SPN round-trips, and an unchanged name keeps the original coding', () => {
const dec = efDecSpn(efBytes(SPN_ORIG));
assert.deepStrictEqual(dec, { show_in_hplmn: true, hide_in_oplmn: false, name: 'GSM-R CH' });
assert.strictEqual(efEncSpn(dec, SPN_ORIG), SPN_ORIG.toUpperCase());
// only the condition byte changes: the text bytes are copied verbatim
const flags = efEncSpn({ show_in_hplmn: false, hide_in_oplmn: true, name: 'GSM-R CH' }, SPN_ORIG);
assert.strictEqual(flags, '02' + SPN_ORIG.slice(2).toUpperCase());
// a different name is re-coded (GSM-7 octets, FF padding to 16 bytes)
const text = efEncSpn({ show_in_hplmn: true, hide_in_oplmn: false, name: 'GSM-R CH2' }, SPN_ORIG);
assert.strictEqual(text.slice(0, 2), '01');
assert.strictEqual(text.length, 34);
assert.strictEqual(text.slice(2), '47534D2D5220434832FFFFFFFFFFFFFF');
assert.strictEqual(efDecSpn(efBytes(text)).name, 'GSM-R CH2');
// a character outside the GSM 7-bit alphabet goes UCS-2BE behind 0x80
const ucs = efEncSpn({ name: '\u0416' }, SPN_ORIG);
assert.strictEqual(ucs, '00800416' + 'FF'.repeat(13));
assert.strictEqual(efDecSpn(efBytes(ucs)).name, '\u0416');
assert.throws(() => efEncSpn({ name: 'A'.repeat(17) }, SPN_ORIG), /too long/);
});
const PLMN_ORIG = '62F2100084';
test('EF.PLMNwAcT round-trips, adds/removes rows and refuses an overflow', () => {
const dec = efDecPlmnWact(efBytes(PLMN_ORIG));
assert.deepStrictEqual(dec.plmns, [{ mcc: '262', mnc: '01', access_tech: 'GSM' }]);
assert.strictEqual(efEncPlmnList(dec.plmns, PLMN_ORIG), PLMN_ORIG);
assert.strictEqual(efEncPlmnList([], PLMN_ORIG), 'FFFFFFFFFF');
// two entries in a ten-byte file
const ten = PLMN_ORIG + '22F8608000';
const rows = [
{ mcc: '262', mnc: '01', access_tech: 'GSM' },
{ mcc: '228', mnc: '06', access_tech: 'UTRAN' },
];
assert.strictEqual(efEncPlmnList(rows, ten), ten);
assert.strictEqual(efEncPlmnList([rows[1]], ten), '22F8608000' + 'FF'.repeat(5));
// Table 4.2.5.1 both-form round trip through the list encoder
const both = efEncPlmnList([{ mcc: '262', mnc: '01', access_tech: 'E-UTRAN WB-S1, E-UTRAN NB-S1' }], ten);
assert.strictEqual(both.slice(0, 10), '62F2104000');
assert.deepStrictEqual(efDecPlmnWact(efBytes(both)).plmns[0].access_tech, 'E-UTRAN NB-S1, E-UTRAN WB-S1');
assert.throws(() => efEncPlmnList(rows, PLMN_ORIG), /Too many entries/);
assert.throws(() => efEncPlmnList([{ mcc: '26', mnc: '01', access_tech: 'GSM' }], ten), /MCC/);
});
const ADN_1 = '42204841203120536963FFFFFFFFFFFF06810628560810FFFFFFFFFFFFFF';
const ADN_2 = 'ffffffffffffffffffffffffffffffffffffffff04b12143f5ffffffffffffffffff';
test('EF.ADN round-trips the decoder vectors byte-for-byte', () => {
const d1 = efDecAdn(efBytes(ADN_1));
assert.strictEqual(d1.name, 'B HA 1 Sic');
assert.strictEqual(d1.number, '6082658001');
assert.strictEqual(efEncAdn(d1, ADN_1), ADN_1.toUpperCase());
const d2 = efDecAdn(efBytes(ADN_2));
assert.strictEqual(d2.name, '');
assert.strictEqual(d2.number, '12345');
assert.strictEqual(efEncAdn(d2, ADN_2), ADN_2.toUpperCase());
assert.strictEqual(efEncAdn(d2, ADN_2).slice(-4), 'FFFF');
});
test('EF.ADN re-encodes an edited name and number in the record length', () => {
const out = efEncAdn({ name: 'Test', number: '+79001234567' }, ADN_1);
assert.strictEqual(out.length, ADN_1.length);
assert.strictEqual(out.slice(0, 8), '54657374'); // 'Test' as GSM-7 octets
assert.strictEqual(out.slice(32, 36), '0791'); // len 7 (1 + 6 bytes), TON/NPI +
const dec = efDecAdn(efBytes(out));
assert.strictEqual(dec.name, 'Test');
assert.strictEqual(dec.number, '+79001234567');
assert.strictEqual(dec.ext1, undefined);
// spaces are ignored
assert.strictEqual(efEncAdn({ name: 'Test', number: '+7 900 123 45 67' }, ADN_1), out);
// an emptied number: length 0, TON/NPI FF, digits FF
const empty = efEncAdn({ name: 'Test', number: '' }, ADN_1);
assert.strictEqual(empty.slice(32, 36), '00FF');
assert.strictEqual(efDecAdn(efBytes(empty)).number, '');
// a name-only edit of an all-FF record fills that record (32-byte record)
const fill = efEncAdn({ name: 'Ann', number: '123' }, 'FF'.repeat(32));
assert.strictEqual(fill.length, 64);
assert.strictEqual(efDecAdn(efBytes(fill)).name, 'Ann');
assert.strictEqual(efDecAdn(efBytes(fill)).number, '123');
});
test('EF.ADN refuses a number extension, a short record and a bad number', () => {
const ext1 = ADN_1.slice(0, -2) + '03';
assert.strictEqual(efEncAdn({ name: 'B HA 1 Sic', number: '6082658001' }, ext1).slice(-4), 'FF03');
assert.throws(() => efEncAdn({ name: 'B HA 1 Sic', number: '6082658002' }, ext1), /EF\.EXT1/);
assert.throws(() => efEncAdn({}, '00'), /too short/);
assert.throws(() => efEncAdn({ number: '+' }, ADN_1), /digits/);
assert.throws(() => efEncAdn({ number: '12a' }, ADN_1), /digits/);
assert.throws(() => efEncAdn({ number: '1'.repeat(21) }, ADN_1), /too long/);
});
test('EF.SPN and the ADN family share the encoder registry with the PLMN list', () => {
assert.strictEqual(efFindEncoder('EF.SPN', null).fid, '6f46');
assert.strictEqual(efFindEncoder('ef.adn', null).fid, '6f3a');
assert.strictEqual(efFindEncoder('ADF.USIM/EF.MSISDN', null).name, 'EF.MSISDN');
assert.strictEqual(efFindEncoder('EF.ALIAS', '6f60').enc, efEncPlmnList);
assert.strictEqual(efFindEncoder('EF.NOPE', 'abcd'), null);
assert.strictEqual(efHasEncoder('EF.ADN', null), true);
assert.strictEqual(efHasEncoder('EF.IMSI', null), false);
// one shared schema for the ADN family and one for the three PLMN files
assert.strictEqual(efFindEncoder('EF.ADN', null).fields, efFindEncoder('EF.BDN', null).fields);
assert.strictEqual(efFindEncoder('EF.PLMNwAcT', null).list, efFindEncoder('EF.HPLMNwAcT', null).list);
});
test('encoder validation errors go through t() when the runtime provides it', () => {
globalThis.t = k => (k === 'Select at least one access technology' ? 'Выберите технологию' : k);
try {
assert.throws(() => efActEnc(''), /Выберите технологию/);
} finally {
delete globalThis.t;
}
});
// EF.PNN (TS 31.102 4.2.58): a record is a list of Network name TLVs. These
// are pySim's reference records (the 43/45 values decoded by efPnnText).
const PNN_1 = '430a82d432bbbc7eb75de432450a82d432bbbc7eb75de432ffffffff';
const PNN_2 = '430a82c596b34cbfbfe5eb39ffffffffffffffffffffffffffffffffffff';
test('EF.PNN round-trips the reference vectors byte-for-byte', () => {
assert.deepStrictEqual(efDecPnn(efBytes(PNN_1)), { full: 'Telekom.de', short: 'Telekom.de' });
assert.deepStrictEqual(efDecPnn(efBytes(PNN_2)), { full: 'E-Networks' });
const entry = efFindEncoder('EF.PNN', null);
assert.strictEqual(entry.fid, '6fc5');
assert.strictEqual(entry.items.addLabel, 'Add TLV');
for (const hex of [PNN_1, PNN_2]) {
const items = efDecItemsFromHex(entry, hex);
assert.strictEqual(efEncPnn(items, hex), hex.toUpperCase());
}
// a known name tag is decoded to its text, not kept raw
assert.deepStrictEqual(efDecItemsFromHex(entry, PNN_1), [
{ tag: '43', text: 'Telekom.de', orig: '430A82D432BBBC7EB75DE432' },
{ tag: '45', text: 'Telekom.de', orig: '450A82D432BBBC7EB75DE432' },
]);
});
test('EF.PNN encodes a non-GSM7 name as UCS-2 behind 0x90', () => {
const entry = efFindEncoder('EF.PNN', null);
const out = efEncPnn([{ tag: '43', text: 'Сеть' }], PNN_1);
assert.strictEqual(out.slice(0, 4), '4309'); // 1 + 4 UCS-2 chars
assert.strictEqual(out.slice(4, 6), '90');
assert.deepStrictEqual(efDecItemsFromHex(entry, out), [{ tag: '43', text: 'Сеть', orig: '430990042104350442044C' }]);
// the empty rest is FF padding up to the record length
assert.strictEqual(out.length, PNN_1.length);
assert.match(out.slice((2 + 9) * 2), /^F+$/);
});
test('EF.PNN re-encodes an edited name and keeps the other TLVs', () => {
const entry = efFindEncoder('EF.PNN', null);
const items = efDecItemsFromHex(entry, PNN_1).map(i =>
i.tag === '43' ? { tag: i.tag, text: 'NewNet' } : i);
const out = efEncPnn(items, PNN_1);
assert.ok(out.includes('450A82D432BBBC7EB75DE432'), 'the 45 TLV is unchanged');
assert.deepStrictEqual(efDecItemsFromHex(entry, out), [
{ tag: '43', text: 'NewNet', orig: '430786CEF2DD59A603' },
{ tag: '45', text: 'Telekom.de', orig: '450A82D432BBBC7EB75DE432' },
]);
assert.strictEqual(out.length, PNN_1.length);
});
test('EF.PNN adds and removes TLVs', () => {
const entry = efFindEncoder('EF.PNN', null);
const withInfo = efEncPnn([{ tag: '43', text: 'E-Networks' }, { tag: '80', hex: '00410042' }], PNN_2);
assert.deepStrictEqual(efDecItemsFromHex(entry, withInfo), [
{ tag: '43', text: 'E-Networks', orig: '430A82C596B34CBFBFE5EB39' },
{ tag: '80', hex: '00410042', orig: '800400410042' },
]);
const onlyFull = efEncPnn([{ tag: '43', text: 'E-Networks' }], PNN_2);
assert.deepStrictEqual(efDecItemsFromHex(entry, onlyFull), [{ tag: '43', text: 'E-Networks', orig: '430A82C596B34CBFBFE5EB39' }]);
});
test('EF.PNN encodes tag 80 as raw bytes and preserves an unknown tag verbatim', () => {
const entry = efFindEncoder('EF.PNN', null);
const out = efEncPnn([{ tag: '80', hex: '00410042' }], 'FF'.repeat(16));
assert.strictEqual(out.slice(0, 12), '800400410042');
assert.deepStrictEqual(efDecItemsFromHex(entry, out), [{ tag: '80', hex: '00410042', orig: '800400410042' }]);
// an unknown tag parsed from the card is read-only and re-emitted as-is
const hex = '430a82d432bbbc7eb75de4325a02aabb' + 'ff'.repeat(14);
const items = efDecItemsFromHex(entry, hex);
assert.deepStrictEqual(items, [
{ tag: '43', text: 'Telekom.de', orig: '430A82D432BBBC7EB75DE432' },
{ tag: '5A', hex: 'AABB', raw: true, orig: '5A02AABB' },
]);
assert.strictEqual(efEncPnn(items, hex), hex.toUpperCase());
});
test('EF.PNN refuses duplicate names, bad hex and an overflowing record', () => {
assert.throws(() => efEncPnn([{ tag: '43', text: 'A' }, { tag: '43', text: 'B' }], PNN_1), /Only one full name/);
assert.throws(() => efEncPnn([{ tag: '45', text: 'A' }, { tag: '45', text: 'B' }], PNN_1), /Only one full name/);
assert.throws(() => efEncPnn([{ tag: '80', hex: '0G' }], PNN_1), /even number of hex/);
assert.throws(() => efEncPnn([{ tag: '80', hex: '0' }], PNN_1), /even number of hex/);
assert.throws(() => efEncPnn([{ tag: '4', hex: '00' }], PNN_1), /one hex byte/);
assert.throws(() => efEncPnn([{ tag: '43', text: 'X'.repeat(40) }], 'FF'.repeat(10)), /too short for these items/);
assert.throws(() => efEncPnn([{ tag: '80', hex: '00'.repeat(20) }], 'FF'.repeat(10)), /too short for these items/);
// an empty name omits its TLV (nothing but FF padding)
assert.strictEqual(efEncPnn([{ tag: '43', text: '' }], 'FF'.repeat(8)), 'FF'.repeat(8));
});
test('EF.PNN writes spare 0 when the packed text fills the last octet', () => {
const entry = efFindEncoder('EF.PNN', null);
// 8 septets = 56 bits = 7 octets exactly -> coding octet 0x80
const out = efEncPnn([{ tag: '43', text: 'Vodafone' }], 'FF'.repeat(16));
assert.strictEqual(out.slice(0, 4), '4308');
assert.strictEqual(out.slice(4, 6), '80');
assert.deepStrictEqual(efDecItemsFromHex(entry, out), [{ tag: '43', text: 'Vodafone', orig: '430880D637396C7EBBCB' }]);
});
// EF.BDN (TS 31.102 4.2.44) is the X+15 layout: alpha, len, TON/NPI, BCD(10),
// CCP, EXT4, then the EF.CMI comparison-method pointer.
const BDN = '54455354' + '06' + '91' + '2143658709' + 'FF'.repeat(7) + '03';
test('EF.BDN round-trips the X+15 layout and preserves the CMI pointer', () => {
const bytes = efBytes(BDN);
assert.strictEqual(bytes.length, 19);
const dec = efDecAdn(bytes, 15);
assert.deepStrictEqual(dec, { name: 'TEST', number: '+1234567890', cmi: 3 });
assert.strictEqual(efEncAdn(dec, BDN, 15), BDN);
// the registry's BDN entry applies the 15-byte layout
const entry = efFindEncoder('EF.BDN', null);
assert.strictEqual(entry.fid, '6f4d');
assert.strictEqual(entry.enc(dec, BDN), BDN);
// editing the number keeps the alpha and the trailing CMI pointer
const edited = entry.enc({ name: 'TEST', number: '+79001234567' }, BDN);
assert.strictEqual(edited.slice(0, 8), '54455354');
assert.strictEqual(edited.slice(-2), '03');
const back = efDecAdn(efBytes(edited), 15);
assert.strictEqual(back.name, 'TEST');
assert.strictEqual(back.number, '+79001234567');
assert.strictEqual(back.cmi, 3);
assert.strictEqual(back.ext1, undefined);
});
test('the ADN family stays on the 14-byte layout', () => {
// the default is the old 14-byte layout: the registry entry and the
// 14-byte round trip are unchanged
assert.strictEqual(efFindEncoder('EF.ADN', null).enc(efDecAdn(efBytes(ADN_1)), ADN_1), ADN_1.toUpperCase());
assert.strictEqual(efEncAdn(efDecAdn(efBytes(ADN_1)), ADN_1), ADN_1.toUpperCase());
assert.notDeepStrictEqual(efDecAdn(efBytes(ADN_1)), efDecAdn(efBytes(ADN_1), 15));
// a BDN record decoded with the default layout is not the BDN result; a
// number EDIT through the 14-byte layout is refused (its EXT slot is the
// BDN's EF.CMI pointer), while the BDN layout writes it
assert.notDeepStrictEqual(efDecAdn(efBytes(BDN)), efDecAdn(efBytes(BDN), 15));
assert.throws(() => efEncAdn({ name: 'TEST', number: '+7900' }, BDN), /EF\.EXT1/);
assert.ok(efEncAdn({ name: 'TEST', number: '+7900' }, BDN, 15));
});
// A control-string/extended-BCD dialling number (SSC): the decoder only sees
// the digit fragment, so an untouched number must not be rebuilt from it.
const SSC_ADN = '54455354' + 'FF'.repeat(12) + '04' + 'FF' + 'BA01B0' + 'FF'.repeat(7) + 'FF' + 'FF';
test('F1: an untouched SSC number block is copied verbatim', () => {
const hex = SSC_ADN;
assert.strictEqual(efBytes(hex).length, 30);
assert.deepStrictEqual(efDecAdn(efBytes(hex)), { name: 'TEST', number: '100' });
// name-only edit: the [length, TON/NPI, 10 BCD/SSC] block is untouched
const out = efEncAdn({ name: 'NEW', number: '100' }, hex);
assert.strictEqual(out.slice(0, 6), '4E4557');
assert.strictEqual(out.slice(32, 56), hex.toUpperCase().slice(32, 56));
// no edit at all: byte-identical
assert.strictEqual(efEncAdn(efDecAdn(efBytes(hex)), hex), hex.toUpperCase());
// an all-FF record stays all-FF (the old encoder wrote a 00 length byte)
assert.strictEqual(efEncAdn({}, 'FF'.repeat(64)), 'FF'.repeat(64));
});
test('F4: an untouched all-FF SPN stays all-FF', () => {
const ff = 'FF'.repeat(17);
// the model exposes the FF display bits (both set), not a blank object
assert.deepStrictEqual(efDecSpn(efBytes(ff)), { show_in_hplmn: true, hide_in_oplmn: true });
assert.strictEqual(efEncSpn(efDecSpn(efBytes(ff)), ff), ff);
});
test('F5: unchanged PLMN rows keep unmodelled ACT bits and codings', () => {
// 0x8C00: UTRAN + NG-RAN + the Rel-18 satellite bit the form does not model
const rec = '62F2108C00';
const dec = efDecPlmnWact(efBytes(rec));
assert.strictEqual(dec.plmns[0].access_tech, 'NG-RAN, UTRAN');
assert.strictEqual(efEncPlmnList(dec.plmns, rec), rec);
// editing the MCC keeps the original ACT bytes (not 0x8800)
assert.strictEqual(efEncPlmnList([{ mcc: '250', mnc: '01', access_tech: 'NG-RAN, UTRAN' }], rec),
'52F010' + '8C00');
// equal codings are not canonicalised either
assert.strictEqual(efEncPlmnList(efDecPlmnWact(efBytes('62F210008C')).plmns, '62F210008C'), '62F210008C');
assert.strictEqual(efEncPlmnList(efDecPlmnWact(efBytes('62F2107000')).plmns, '62F2107000'), '62F2107000');
// a changed ACT is encoded canonically
assert.strictEqual(efEncPlmnList([{ mcc: '262', mnc: '01', access_tech: 'NG-RAN' }], rec), '62F2100800');
});
test('F9: an emptied full name is refused while other TLVs remain', () => {
assert.throws(() => efEncPnn([{ tag: '43', text: '' }, { tag: '45', text: 'Short' }], PNN_1),
/full name \(43\) is required/);
assert.throws(() => efEncPnn([{ tag: '43', text: '' }, { tag: '80', hex: '00' }], PNN_1),
/full name/);
// the empty omission is only for a record that ends up with no items
assert.strictEqual(efEncPnn([{ tag: '43', text: '' }], 'FF'.repeat(8)), 'FF'.repeat(8));
});
test('F10: a TLV value longer than 255 bytes is refused', () => {
const big = '00'.repeat(256);
assert.throws(() => efEncPnn([{ tag: '80', hex: big }], 'FF'.repeat(40)), /longer than 255/);
assert.throws(() => efEncPnn([{ tag: '5A', hex: big }], 'FF'.repeat(40)), /longer than 255/);
});
test('F14: the encoder messages go through t() (unknown technology, too many rows)', () => {
globalThis.t = k => (k === 'Unknown access technology' ? 'U'
: (k === 'Too many entries for this file' ? 'T' : k));
try {
assert.throws(() => efActEnc('BOGUS'), /^Error: U: BOGUS$/);
assert.throws(() => efEncPlmnList([
{ mcc: '262', mnc: '01', access_tech: 'GSM' },
{ mcc: '228', mnc: '06', access_tech: 'GSM' },
], '62F2100084'), /^Error: T \(1 max\)$/);
} finally {
delete globalThis.t;
}
});
test('unchanged content re-encodes byte-identically (all fixtures)', () => {
const decode = (name, hex) => {
const bytes = efBytes(hex);
if (name === 'EF.SPN') return efDecSpn(bytes);
if (name === 'EF.PLMNwAcT') return efDecPlmnWact(bytes).plmns;
if (name === 'EF.BDN') return efDecAdn(bytes, 15);
if (name === 'EF.ADN') return efDecAdn(bytes);
if (name === 'EF.PNN') return efDecItemsFromHex(efFindEncoder('EF.PNN', null), hex);
throw new Error('no decoder for ' + name);
};
const cases = [
{ name: 'EF.SPN', fid: '6f46', hex: SPN_ORIG },
{ name: 'EF.SPN', fid: '6f46', hex: 'FF'.repeat(17) },
{ name: 'EF.PLMNwAcT', fid: '6f60', hex: PLMN_ORIG },
{ name: 'EF.PLMNwAcT', fid: '6f60', hex: PLMN_ORIG + '22F8608000' },
{ name: 'EF.PLMNwAcT', fid: '6f60', hex: '62F2108C00' },
{ name: 'EF.PLMNwAcT', fid: '6f60', hex: '62F210008C' },
{ name: 'EF.PLMNwAcT', fid: '6f60', hex: '62F2107000' },
{ name: 'EF.ADN', fid: '6f3a', hex: ADN_1 },
{ name: 'EF.ADN', fid: '6f3a', hex: ADN_2 },
{ name: 'EF.ADN', fid: '6f3a', hex: SSC_ADN },
{ name: 'EF.ADN', fid: '6f3a', hex: 'FF'.repeat(64) },
{ name: 'EF.BDN', fid: '6f4d', hex: BDN },
{ name: 'EF.BDN', fid: '6f4d', hex: 'FF'.repeat(38) },
{ name: 'EF.PNN', fid: '6fc5', hex: PNN_1 },
{ name: 'EF.PNN', fid: '6fc5', hex: PNN_2 },
{ name: 'EF.PNN', fid: '6fc5', hex: '800400410042' + 'FF'.repeat(16) },
{ name: 'EF.PNN', fid: '6fc5', hex: '430a82d432bbbc7eb75de4325a02aabb' + 'ff'.repeat(14) },
];
for (const c of cases) {
const entry = efFindEncoder(c.name, c.fid);
assert.strictEqual(entry.enc(decode(c.name, c.hex), c.hex), c.hex.toUpperCase(),
c.name + ' ' + c.hex);
}
});
test('F2: low-byte packed, AddCI and UCS-2-padded names round-trip unchanged', () => {
const entry = efFindEncoder('EF.PNN', null);
// 0x82 spare 2, packed 41 21 = 'AB' (the old all-low heuristic read 'A!')
assert.strictEqual(efPnnText(efBytes('824121')), 'AB');
const records = [
'4303824121' + 'FF'.repeat(28), // low-byte packed 'AB'
'43038A4121' + 'FF'.repeat(28), // the same with the AddCI bit
'43079000410042FFFF' + 'FF'.repeat(20), // UCS-2 'AB' with FF padding
];
for (const hex of records) {
const items = efDecItemsFromHex(entry, hex);
assert.strictEqual(efEncPnn(items, hex), hex, hex);
}
});
test('F2: editing one PNN name leaves the other TLV byte-identical', () => {
const entry = efFindEncoder('EF.PNN', null);
// 43 carries the AddCI-bit packing; 45 is a plain packed name
const hex = '43038A4121' + '4503824222' + 'FF'.repeat(24);
const items = efDecItemsFromHex(entry, hex);
assert.deepStrictEqual(items.map(i => i.text), ['AB', 'BD']);
const edited = items.map(i => i.tag === '43' ? { tag: i.tag, text: 'Z', orig: i.orig } : i);
const out = efEncPnn(edited, hex);
assert.ok(out.includes('4503824222'), 'the 45 TLV is unchanged');
assert.strictEqual(efDecItemsFromHex(entry, out)[1].text, 'BD');
});
test('F8: the ADN-family entries name their extension EF and use it in refusals', () => {
const names = ['EF.ADN', 'EF.FDN', 'EF.SDN', 'EF.MSISDN', 'EF.LND', 'EF.BDN'];
assert.deepStrictEqual(names.map(n => efFindEncoder(n, null).ext),
['EXT1', 'EXT2', 'EXT3', 'EXT5', 'EXT1', 'EXT4']);
// a non-FF extension slot refuses a changed number with its own EF name
const fdn = ADN_1.slice(0, -2) + '03'; // EXT2 set
assert.throws(() => efFindEncoder('EF.FDN', null).enc(
{ name: 'B HA 1 Sic', number: '6082658002' }, fdn), /EF\.EXT2/);
const sdn = ADN_1.slice(0, -2) + '03';
assert.throws(() => efFindEncoder('EF.SDN', null).enc(
{ name: 'B HA 1 Sic', number: '6082658002' }, sdn), /EF\.EXT3/);
const msisdn = ADN_1.slice(0, -2) + '03';
assert.throws(() => efFindEncoder('EF.MSISDN', null).enc(
{ name: 'B HA 1 Sic', number: '6082658002' }, msisdn), /EF\.EXT5/);
// BDN: the slot before the CMI pointer is EXT4
const bdnExt4 = '54455354' + '06' + '91' + '2143658709' + 'FF'.repeat(6) + '04' + '03';
assert.throws(() => efEncAdn({ name: 'TEST', number: '+79001234567' }, bdnExt4, 15, 'EXT4'), /EF\.EXT4/);
// the unchanged number still copies the extension slots verbatim
assert.strictEqual(efEncAdn(efDecAdn(efBytes(fdn)), fdn, 14, 'EXT2'), fdn.toUpperCase());
});
+655
View File
@@ -0,0 +1,655 @@
const { test } = require('node:test');
const assert = require('node:assert');
const fs = require('node:fs');
const path = require('node:path');
const html = fs.readFileSync(path.join(__dirname, '..', 'index.html'), 'utf8');
function extractFunc(src, name) {
const re = new RegExp('(?:async\\s+)?function\\s+' + name + '\\s*\\([^)]*\\)\\s*\\{');
const m = re.exec(src);
if (!m) throw new Error('function ' + name + ' not found');
let i = m.index + m[0].length - 1;
let depth = 0;
for (; i < src.length; i++) {
if (src[i] === '{') depth++;
else if (src[i] === '}') {
depth--;
if (depth === 0) break;
}
}
return src.slice(m.index, i + 1);
}
function extractConst(src, name) {
const m = new RegExp('const ' + name + ' = ').exec(src);
if (!m) throw new Error('const ' + name + ' not found');
const begin = m.index + m[0].length;
const opener = src[begin];
const closer = opener === '[' ? ']' : (opener === '{' ? '}' : null);
if (!closer) throw new Error('unsupported const ' + name);
let depth = 0, i = begin;
for (; i < src.length; i++) {
if (src[i] === opener) depth++;
else if (src[i] === closer) {
depth--;
if (depth === 0) { i++; break; }
}
}
return 'var ' + name + ' = ' + src.slice(begin, i) + ';\n';
}
const FNS = [
'efBytes', 'efHex', 'efAllFf', 'efRstripFf', 'efUcs2Be', 'efGsm7Octets',
'efAnnexA', 'efBcdAddress', 'efPlmn', 'efPlmnAct', 'efDecSpn', 'efDecPlmnWact',
'efDecAdn', 'efFindDecoder', 'efDecodeBytes', 'efIsRawOnly', 'efTlv',
'efPnnText', 'efDecItemsFromHex',
'efGsm7PlainText', 'efEncTextBytes', 'efPlmnEnc', 'efActEnc', 'efEncSpn',
'efEncPlmnList', 'efPlmnRowOrig', 'efEncAdn', 'efEncAdnExt', 'efPnnNameBytes', 'efPnnHexBytes', 'efPnnOrigTlv', 'efEncPnn',
'efFindEncoder', 'efHasEncoder', 'efEncError',
'efDecModelValues', 'efDecEditModel', 'efActTechList', 'efDecFieldHtml',
'efDecRowHtml', 'efDecGridHtml', 'efDecItemRowHtml', 'efDecItemsHtml',
'efDecRecordsHtml', 'efDecEditHtml',
'efDecCollectValues', 'efDecCollectRows', 'efDecCollectItems',
'pysimFsDecRowCheck', 'pysimFsDecItemTag', 'pysimFsDecAddItem',
'pysimFsDecRemoveItem', 'pysimFsFindNode',
'pysimFsHasDecoder', 'pysimFsHasEncoder', 'pysimFsDecSave', 'pysimFsDecEdit',
'pysimFsPillsEnabled', 'pysimFsSyncContentVisibility', 'gsm7Decode',
'gsm7TextToSeptets', 'gsm7Encode',
];
let code = 'var pysimFsSelected = null;\nvar pysimFsTreeRoot = null;\nvar pysimFsDecModel = null;\n'
+ 'var pysimFsDecEditMode = false;\nvar pysimFsEditMode = false;\n';
for (const f of FNS) code += extractFunc(html, f) + '\n';
for (const c of ['GSM7_ALPHABET', 'GSM7_EXT_MAP', 'EF_ACT_TECHS', 'EF_SPN_FIELDS',
'EF_ADN_FIELDS', 'EF_PLMN_LIST', 'EF_PNN_ITEMS', 'EF_ENCODERS']) {
code += extractConst(html, c);
}
eval(code);
// The decoder entries these files resolve to (the full EF_DECODERS registry is
// pinned by ef_decode.test.js; only the model's lookup path is exercised here).
var EF_DECODERS = [
{ name: 'EF.SPN', fid: '6f46', fn: efDecSpn },
{ name: 'EF.PLMNwAcT', fid: '6f60', fn: efDecPlmnWact },
{ name: 'EF.OPLMNwAcT', fid: '6f61', fn: efDecPlmnWact },
{ name: 'EF.HPLMNwAcT', fid: '6f62', fn: efDecPlmnWact },
{ name: 'EF.ADN', fid: '6f3a', fn: efDecAdn },
{ name: 'EF.FDN', fid: '6f3b', fn: efDecAdn },
{ name: 'EF.SDN', fid: '6f49', fn: efDecAdn },
{ name: 'EF.MSISDN', fid: '6f40', fn: efDecAdn },
{ name: 'EF.LND', fid: '6f44', fn: efDecAdn },
{ name: 'EF.BDN', fid: '6f4d', fn: b => efDecAdn(b, 15) },
];
globalThis.t = k => k;
globalThis.esc = s => String(s);
const SPN_ORIG = '0147534d2d52204348ffffffffffffffff';
const ADN_1 = '42204841203120536963FFFFFFFFFFFF06810628560810FFFFFFFFFFFFFF';
const ADN_FF = 'FF'.repeat(64);
const PLMN_ORIG = '62F210008422F8608000';
test('efFindEncoder / efHasEncoder resolve by name and FID, like the decoders', () => {
assert.strictEqual(efFindEncoder('EF.SPN', null).fid, '6f46');
assert.strictEqual(efFindEncoder('ef.plmnwact', null).name, 'EF.PLMNwAcT');
assert.strictEqual(efFindEncoder('MF/7F10/6F3A/EF.ADN', null).name, 'EF.ADN');
assert.strictEqual(efFindEncoder('EF.ALIAS', '6f40').name, 'EF.MSISDN');
assert.strictEqual(efFindEncoder('EF.IMSI', '6f07'), null);
assert.strictEqual(efFindEncoder(null, null), null);
assert.strictEqual(efHasEncoder('EF.SDN', null), true);
assert.strictEqual(efHasEncoder('EF.IMSI', null), false);
});
test('the model is a TR value set for SPN / PLMNwAcT and record rows for ADN', () => {
const spn = efDecEditModel('EF.SPN', null, { kind: 'transparent', data: SPN_ORIG });
assert.strictEqual(spn.mode, 'tr');
assert.strictEqual(spn.entry.name, 'EF.SPN');
assert.deepStrictEqual(spn.values, { show_in_hplmn: true, hide_in_oplmn: false, name: 'GSM-R CH' });
// an all-FF SPN exposes its FF display bits (both set, no name), so an
// untouched save keeps the file byte-identical
const emptySpn = efDecEditModel('EF.SPN', '6f46', { kind: 'transparent', data: 'FF'.repeat(17) });
assert.deepStrictEqual(emptySpn.values, { show_in_hplmn: true, hide_in_oplmn: true });
const plmn = efDecEditModel('EF.PLMNwAcT', null, { kind: 'transparent', data: PLMN_ORIG });
assert.strictEqual(plmn.values.plmns.length, 2);
assert.strictEqual(plmn.values.plmns[0].mcc, '262');
const adn = efDecEditModel('EF.ADN', null, { kind: 'record', records: [
{ num: 1, data: ADN_1 },
{ num: 2, data: ADN_FF },
] });
assert.strictEqual(adn.mode, 'records');
assert.strictEqual(adn.records.length, 2);
assert.strictEqual(adn.records[0].num, 1);
assert.strictEqual(adn.records[0].orig, ADN_1);
assert.strictEqual(adn.records[0].values.name, 'B HA 1 Sic');
assert.strictEqual(adn.records[0].values.number, '6082658001');
assert.deepStrictEqual(adn.records[1].values, {});
assert.strictEqual(adn.records[1].orig, ADN_FF);
// no encoder -> no model, even when a decoder exists
assert.strictEqual(efDecEditModel('EF.IMSI', null, { kind: 'transparent', data: '082982608200002080' }), null);
assert.strictEqual(efDecEditModel('EF.NOPE', 'abcd', { kind: 'transparent', data: '00' }), null);
});
test('the TR form is a field grid carrying data-key inputs and the original hex', () => {
const model = efDecEditModel('EF.SPN', null, { kind: 'transparent', data: SPN_ORIG });
const out = efDecEditHtml(model, SPN_ORIG);
assert.match(out, /class="pysim-dec-edit"/);
assert.match(out, /data-mode="tr"/);
assert.ok(out.includes('data-orig="' + SPN_ORIG + '"'));
assert.match(out, /data-key="show_in_hplmn" checked/);
assert.match(out, /data-key="hide_in_oplmn"/);
assert.match(out, /data-key="name" value="GSM-R CH"/);
assert.strictEqual((out.match(/class="pysim-dec-item /g) || []).length, 3);
assert.doesNotMatch(out, /pysim-fs-record-cb/);
// the form's booleans/labels go through t() and the labels are the spec
// bit semantics (TS 31.102 4.2.12)
assert.match(out, /Display the network name in HPLMN \/ an SPDI PLMN/);
assert.match(out, /Hide the service provider name outside HPLMN \/ the SPDI PLMNs/);
});
test('the TR list form is a row editor with per-row remove and an Add button', () => {
const model = efDecEditModel('EF.PLMNwAcT', null, { kind: 'transparent', data: PLMN_ORIG });
const out = efDecEditHtml(model, PLMN_ORIG);
assert.match(out, /class="pysim-dec-edit"/);
assert.strictEqual((out.match(/class="pysim-dec-row /g) || []).length, 2);
assert.strictEqual((out.match(/class="pysim-dec-remove/g) || []).length, 2);
assert.ok(out.includes('✕'));
assert.match(out, /onclick="pysimFsDecAddRow\(\)"/);
assert.match(out, />Add network</);
assert.match(out, /data-key="mcc" value="262"/);
assert.match(out, /data-key="mnc" value="01"/);
assert.strictEqual((out.match(/data-tech="GSM" checked/g) || []).length, 1);
assert.strictEqual((out.match(/data-tech="UTRAN" checked/g) || []).length, 1);
assert.match(out, /data-tech="NG-RAN"/);
});
test('the record form is one block per record with a hidden selection checkbox', () => {
const model = efDecEditModel('EF.ADN', null, { kind: 'record', records: [
{ num: 1, data: ADN_1 },
{ num: 2, data: ADN_FF },
] });
const out = efDecEditHtml(model);
assert.match(out, /data-mode="records"/);
assert.strictEqual((out.match(/class="pysim-dec-rec /g) || []).length, 2);
assert.match(out, /data-rec="1" data-orig="4220/);
assert.match(out, /data-rec="2" data-orig="FFFF/);
assert.strictEqual((out.match(/class="pysim-fs-record-cb shrink-0 hidden"/g) || []).length, 2);
assert.match(out, /Record 1/);
assert.match(out, /Record 2 — empty/);
assert.match(out, /data-key="name" value="B HA 1 Sic"/);
assert.match(out, /data-key="number" value="6082658001"/);
// every input auto-checks its record block
assert.match(out, /oninput="pysimFsDecRowCheck\(this\)"/);
});
function fakeRoot(inputs, techs) {
return {
querySelector: sel => {
const m = /^\[data-key="(.+)"\]$/.exec(sel);
return (m && inputs[m[1]]) || null;
},
querySelectorAll: sel => sel === '[data-tech]' ? (techs || []) : [],
};
}
test('efDecCollectValues reads the DOM inputs back into the decoder shape', () => {
const spn = efDecCollectValues(fakeRoot({
show_in_hplmn: { checked: true },
hide_in_oplmn: { checked: false },
name: { value: 'GSM-R CH' },
}), EF_SPN_FIELDS);
assert.deepStrictEqual(spn, { show_in_hplmn: true, hide_in_oplmn: false, name: 'GSM-R CH' });
// a missing boolean reads false, a missing text field ''
assert.deepStrictEqual(efDecCollectValues(fakeRoot({}), EF_ADN_FIELDS), { name: '', number: '' });
const row = efDecCollectValues(fakeRoot({
mcc: { value: '262' },
mnc: { value: '01' },
}, [
{ checked: true, getAttribute: () => 'GSM' },
{ checked: true, getAttribute: () => 'UTRAN' },
{ checked: false, getAttribute: () => 'NG-RAN' },
]), EF_PLMN_LIST.itemFields);
assert.deepStrictEqual(row, { mcc: '262', mnc: '01', access_tech: 'GSM, UTRAN' });
assert.deepStrictEqual(efDecCollectValues(null, EF_ADN_FIELDS), {});
});
test('efDecCollectRows reads the PLMN row editor back into the decoder shape', () => {
const rows = [
fakeRoot({ mcc: { value: '262' }, mnc: { value: '01' } }, [{ checked: true, getAttribute: () => 'GSM' }]),
fakeRoot({ mcc: { value: '228' }, mnc: { value: '06' } }, [{ checked: true, getAttribute: () => 'UTRAN' }]),
];
const out = efDecCollectRows({ querySelectorAll: sel => sel === '.pysim-dec-row' ? rows : [] }, EF_PLMN_LIST.itemFields);
assert.deepStrictEqual(out, [
{ mcc: '262', mnc: '01', access_tech: 'GSM' },
{ mcc: '228', mnc: '06', access_tech: 'UTRAN' },
]);
assert.deepStrictEqual(efDecCollectRows(null, EF_PLMN_LIST.itemFields), []);
});
test('changing a record input checks its block', () => {
const cb = { checked: false };
const rec = { querySelector: sel => sel === '.pysim-fs-record-cb' ? cb : null };
const el = { closest: sel => sel === '.pysim-dec-rec' ? rec : null };
pysimFsDecRowCheck(el);
assert.strictEqual(cb.checked, true);
pysimFsDecRowCheck(null);
assert.strictEqual(cb.checked, true);
});
test('pysimFsHasEncoder resolves the selected file by name or FID', () => {
pysimFsTreeRoot = { name: 'MF', children: [{ name: 'EF.SPN', fid: '6f46' }, { name: 'EF.ALIAS', fid: '6f3a' }] };
pysimFsSelected = 'EF.SPN';
assert.strictEqual(pysimFsHasEncoder(), true);
pysimFsSelected = 'EF.ALIAS';
assert.strictEqual(pysimFsHasEncoder(), true);
pysimFsSelected = '6F46';
assert.strictEqual(pysimFsHasEncoder(), true);
pysimFsSelected = 'EF.IMSI';
assert.strictEqual(pysimFsHasEncoder(), false);
pysimFsSelected = null;
assert.strictEqual(pysimFsHasEncoder(), false);
});
test('the Edit decoded button is encoder-gated next to Edit raw', () => {
const btn = /<button[^>]*id="pysim-fs-edit-dec-btn"[^>]*>/.exec(html);
assert.ok(btn, 'Edit decoded button');
assert.ok(btn[0].includes('data-needs="card"'));
assert.ok(btn[0].includes('data-needs-encoder'));
assert.ok(btn[0].includes('onclick="pysimFsDecEdit()"'));
assert.match(html, /data-l10n="Edit decoded">Edit decoded</);
assert.ok(html.indexOf('id="pysim-fs-edit-btn"') < html.indexOf('id="pysim-fs-edit-dec-btn"'));
const avail = extractFunc(html, 'pysimApplyAvailability');
assert.ok(avail.includes("hasAttribute('data-needs-encoder')"));
assert.ok(avail.includes("t('No decoded editor for this file')"));
});
test('the edit flow reads raw, renders the model and saves only checked records', () => {
const edit = extractFunc(html, 'pysimFsDecEdit');
assert.ok(edit.includes("body.mode = 'raw'"));
assert.ok(edit.includes('efDecEditModel'));
assert.ok(edit.includes('efDecEditHtml'));
assert.ok(edit.includes('pysimFsDecEditMode = true'));
assert.ok(edit.includes("querySelectorAll('.pysim-fs-record-cb')"));
const save = extractFunc(html, 'pysimFsSave');
assert.ok(save.includes('pysimFsDecEditMode'));
assert.ok(save.includes('pysimFsDecSave'));
const decSave = extractFunc(html, 'pysimFsDecSave');
assert.ok(decSave.includes("pysimFetch('/api/write'"));
assert.ok(decSave.includes("querySelectorAll('.pysim-dec-rec')"));
assert.ok(decSave.includes("querySelector('.pysim-fs-record-cb')"));
assert.ok(decSave.includes('if (cb && !cb.checked) continue;'), 'only checked records are written');
assert.ok(decSave.includes('body.record_nr = parseInt(num)'));
assert.ok(decSave.includes('entry.enc('));
assert.ok(decSave.includes('efDecCollectRows(root, entry.list.itemFields)'));
assert.ok(decSave.includes("getAttribute('data-orig')"));
assert.ok(decSave.includes('_pysimArrCache = null'));
const add = extractFunc(html, 'pysimFsDecAddRow');
assert.ok(add.includes('efDecRowHtml'));
const remove = extractFunc(html, 'pysimFsDecRemoveRow');
assert.ok(remove.includes("closest('.pysim-dec-row')"));
});
function fakeRecordBlock(num, orig, inputs, checked) {
return {
querySelector: sel => {
if (sel === '.pysim-fs-record-cb') return { checked: checked };
const m = /^\[data-key="(.+)"\]$/.exec(sel);
return (m && inputs[m[1]]) || null;
},
querySelectorAll: () => [],
getAttribute: k => k === 'data-rec' ? String(num) : (k === 'data-orig' ? orig : null),
};
}
function saveStubs(result) {
const bodies = [];
globalThis.pysimFetch = async (path, body) => {
bodies.push({ path: path, body: body });
return typeof result === 'function' ? result(path, body) : result;
};
globalThis.pysimFsSelectBody = () => ({ name: 'EF.ADN' });
globalThis.pysimFsShowError = (el, sw, err) => { el.textContent = 'SW: ' + sw + ' — ' + err; };
let cancelled = 0;
globalThis.pysimFsCancel = () => { cancelled++; };
return { bodies: bodies, cancelled: () => cancelled };
}
test('the decoded save writes a TR list as one re-encoded /api/write', async () => {
const entry = efFindEncoder('EF.PLMNwAcT', null);
pysimFsDecModel = { entry: entry, mode: 'tr' };
const rows = [
fakeRoot({ mcc: { value: '262' }, mnc: { value: '01' } }, [{ checked: true, getAttribute: () => 'GSM' }]),
fakeRoot({ mcc: { value: '228' }, mnc: { value: '06' } }, [{ checked: true, getAttribute: () => 'UTRAN' }]),
];
const root = {
getAttribute: k => k === 'data-orig' ? PLMN_ORIG : null,
querySelectorAll: sel => sel === '.pysim-dec-row' ? rows : [],
};
const out = { querySelector: sel => sel === '.pysim-dec-edit' ? root : null, querySelectorAll: () => [] };
const stubs = saveStubs({ success: true, sw: '9000' });
const status = { textContent: '' };
await pysimFsDecSave(null, 'EF.PLMNwAcT', status, out, false);
assert.strictEqual(stubs.bodies.length, 1);
assert.strictEqual(stubs.bodies[0].path, '/api/write');
assert.strictEqual(stubs.bodies[0].body.data, PLMN_ORIG);
assert.strictEqual(stubs.bodies[0].body.record_nr, undefined);
assert.strictEqual(status.textContent, 'SW: 9000 OK');
assert.strictEqual(stubs.cancelled(), 1);
});
test('the decoded save writes only the checked ADN records, with record_nr', async () => {
const entry = efFindEncoder('EF.ADN', null);
pysimFsDecModel = { entry: entry, mode: 'records' };
const blocks = [
fakeRecordBlock(1, ADN_1, { name: { value: 'Ann' }, number: { value: '123' } }, true),
fakeRecordBlock(2, ADN_FF, { name: { value: '' }, number: { value: '' } }, false),
];
const out = { querySelector: () => null, querySelectorAll: sel => sel === '.pysim-dec-rec' ? blocks : [] };
const stubs = saveStubs({ success: true, sw: '9000' });
const status = { textContent: '' };
await pysimFsDecSave(null, 'EF.ADN', status, out, false);
assert.strictEqual(stubs.bodies.length, 1);
assert.strictEqual(stubs.bodies[0].body.record_nr, 1);
assert.strictEqual(stubs.bodies[0].body.data, efEncAdn({ name: 'Ann', number: '123' }, ADN_1));
assert.strictEqual(status.textContent, 'SW: 9000 OK');
assert.strictEqual(stubs.cancelled(), 1);
});
test('no checked record means no write; a failed write stops at that record', async () => {
const entry = efFindEncoder('EF.ADN', null);
pysimFsDecModel = { entry: entry, mode: 'records' };
const off = [fakeRecordBlock(1, ADN_1, { name: { value: 'Ann' }, number: { value: '1' } }, false)];
const out = { querySelector: () => null, querySelectorAll: sel => sel === '.pysim-dec-rec' ? off : [] };
const stubs = saveStubs({ success: true, sw: '9000' });
const status = { textContent: '' };
await pysimFsDecSave(null, 'EF.ADN', status, out, false);
assert.strictEqual(stubs.bodies.length, 0);
assert.match(status.textContent, /No records selected/);
assert.strictEqual(stubs.cancelled(), 0);
const on = [fakeRecordBlock(1, ADN_1, { name: { value: 'Ann' }, number: { value: '1' } }, true)];
const out2 = { querySelector: () => null, querySelectorAll: sel => sel === '.pysim-dec-rec' ? on : [] };
const stubs2 = saveStubs(() => ({ success: false, sw: '6982', error: 'Security status not satisfied' }));
const status2 = { textContent: '' };
await pysimFsDecSave(null, 'EF.ADN', status2, out2, false);
assert.strictEqual(stubs2.bodies.length, 1);
assert.match(status2.textContent, /6982/);
assert.match(status2.textContent, /record 1/);
assert.strictEqual(stubs2.cancelled(), 0);
});
// EF.PNN: the fourth shape - a record is a list of TLVs.
const PNN_1 = '430a82d432bbbc7eb75de432450a82d432bbbc7eb75de432ffffffff';
test('the model builds per-record item lists for an items encoder', () => {
const model = efDecEditModel('EF.PNN', '6fc5', { kind: 'record', records: [
{ num: 1, data: PNN_1 },
{ num: 2, data: ADN_FF },
] });
assert.strictEqual(model.mode, 'records');
assert.deepStrictEqual(model.records[0].items, [
{ tag: '43', text: 'Telekom.de', orig: '430A82D432BBBC7EB75DE432' },
{ tag: '45', text: 'Telekom.de', orig: '450A82D432BBBC7EB75DE432' },
]);
assert.strictEqual(model.records[0].values, undefined);
assert.deepStrictEqual(model.records[1].items, []);
});
test('the items form renders a tag select, the value input, remove and Add TLV per record', () => {
const model = efDecEditModel('EF.PNN', '6fc5', { kind: 'record', records: [
{ num: 1, data: PNN_1 },
{ num: 2, data: ADN_FF },
] });
const out = efDecEditHtml(model);
assert.strictEqual((out.match(/class="pysim-dec-item-row /g) || []).length, 2);
assert.strictEqual((out.match(/class="pysim-dec-items /g) || []).length, 2);
assert.strictEqual((out.match(/data-itag/g) || []).length, 2);
assert.strictEqual((out.match(/class="pysim-dec-remove /g) || []).length, 2);
assert.strictEqual((out.match(/class="pysim-dec-add /g) || []).length, 2);
assert.match(out, /data-tag="43"/);
assert.match(out, /data-tag="45"/);
assert.match(out, /data-ikey="text" value="Telekom.de"/);
assert.match(out, /<option value="43" data-kind="name" selected>43 Full name<\/option>/);
assert.match(out, />Add TLV</);
// the empty record still gets its own Add TLV button
assert.match(out, /Record 2 — empty/);
});
test('an unknown tag in an items record stays read-only without a remove button', () => {
const hex = '430a82d432bbbc7eb75de4325a02aabb' + 'ff'.repeat(14);
const model = efDecEditModel('EF.PNN', '6fc5', { kind: 'record', records: [{ num: 1, data: hex }] });
const out = efDecEditHtml(model);
assert.match(out, /data-tag="5A"/);
assert.strictEqual((out.match(/data-itag/g) || []).length, 1, 'the raw row has no tag select');
assert.match(out, /data-ikey="hex" value="AABB" readonly/);
assert.strictEqual((out.match(/class="pysim-dec-remove /g) || []).length, 1);
});
function fakeItemRow(tag, kind, value) {
return {
getAttribute: k => k === 'data-tag' ? tag : null,
querySelector: sel => sel === '[data-ikey]' ? { getAttribute: () => kind, value: value } : null,
};
}
test('efDecCollectItems reads the TLV rows in DOM order', () => {
const rows = [
fakeItemRow('43', 'text', 'Name'),
fakeItemRow('80', 'hex', '00410042'),
fakeItemRow('5A', 'hex', 'AABB'),
];
const root = { querySelectorAll: sel => sel === '.pysim-dec-item-row' ? rows : [] };
assert.deepStrictEqual(efDecCollectItems(root), [
{ tag: '43', text: 'Name' },
{ tag: '80', hex: '00410042' },
{ tag: '5A', hex: 'AABB' },
]);
assert.deepStrictEqual(efDecCollectItems(null), []);
});
test('the decoded save encodes item records through the entry encoder', async () => {
const entry = efFindEncoder('EF.PNN', null);
pysimFsDecModel = { entry: entry, mode: 'records' };
const rows = [
fakeItemRow('43', 'text', 'Telekom.de'),
fakeItemRow('80', 'hex', '00410042'),
];
const block = {
querySelector: sel => sel === '.pysim-fs-record-cb' ? { checked: true } : null,
querySelectorAll: sel => sel === '.pysim-dec-item-row' ? rows : [],
getAttribute: k => k === 'data-rec' ? '1' : (k === 'data-orig' ? PNN_1 : null),
};
const out = { querySelector: () => null, querySelectorAll: sel => sel === '.pysim-dec-rec' ? [block] : [] };
const stubs = saveStubs({ success: true, sw: '9000' });
const status = { textContent: '' };
await pysimFsDecSave(null, 'EF.PNN', status, out, false);
assert.strictEqual(stubs.bodies.length, 1);
assert.strictEqual(stubs.bodies[0].body.record_nr, 1);
assert.strictEqual(stubs.bodies[0].body.data,
efEncPnn([{ tag: '43', text: 'Telekom.de' }, { tag: '80', hex: '00410042' }], PNN_1));
assert.strictEqual(status.textContent, 'SW: 9000 OK');
assert.strictEqual(stubs.cancelled(), 1);
});
test('the item add/remove/tag handlers are wired to the record', () => {
const add = extractFunc(html, 'pysimFsDecAddItem');
assert.ok(add.includes("querySelector('.pysim-dec-items')"));
assert.ok(add.includes('efDecItemRowHtml'));
assert.ok(add.includes('pysimFsDecRowCheck'));
const rem = extractFunc(html, 'pysimFsDecRemoveItem');
assert.ok(rem.includes("closest('.pysim-dec-item-row')"));
assert.ok(rem.includes('pysimFsDecRowCheck'));
const tag = extractFunc(html, 'pysimFsDecItemTag');
assert.ok(tag.includes("setAttribute('data-tag'"));
assert.ok(tag.includes("setAttribute('data-ikey'"));
});
test('the BDN model decodes the X+15 layout and keeps the CMI pointer out of the form', () => {
const BDN = '54455354' + '06' + '91' + '2143658709' + 'FF'.repeat(7) + '03';
const model = efDecEditModel('EF.BDN', '6f4d', { kind: 'record', records: [{ num: 1, data: BDN }] });
assert.deepStrictEqual(model.records[0].values, { name: 'TEST', number: '+1234567890', cmi: 3 });
const out = efDecEditHtml(model);
assert.match(out, /data-key="name" value="TEST"/);
assert.match(out, /data-key="number" value="\+1234567890"/);
assert.doesNotMatch(out, /cmi/);
// the save path re-encodes with the 15-byte layout (the CMI byte survives)
const entry = efFindEncoder('EF.BDN', null);
assert.strictEqual(entry.enc({ name: 'TEST', number: '+1234567890' }, BDN), BDN);
});
// A control-string/extended-BCD ADN record (the decoder only sees '100').
const SSC_ADN = '54455354' + 'FF'.repeat(12) + '04' + 'FF' + 'BA01B0' + 'FF'.repeat(7) + 'FF' + 'FF';
function adnBlock(num, orig, name, number, checked) {
const inputs = { name: { value: name }, number: { value: number } };
return {
querySelector: sel => {
if (sel === '.pysim-fs-record-cb') return { checked: checked };
const m = /^\[data-key="(.+)"\]$/.exec(sel);
return m ? inputs[m[1]] : null;
},
querySelectorAll: () => [],
getAttribute: k => k === 'data-rec' ? String(num) : (k === 'data-orig' ? orig : null),
};
}
test('F1: the save path writes an untouched SSC record byte-identically', async () => {
const entry = efFindEncoder('EF.ADN', null);
pysimFsDecModel = { entry: entry, mode: 'records' };
const out = { querySelector: () => null,
querySelectorAll: sel => sel === '.pysim-dec-rec' ? [adnBlock(1, SSC_ADN, 'TEST', '100', true)] : [] };
const stubs = saveStubs({ success: true, sw: '9000' });
const status = { textContent: '' };
await pysimFsDecSave(null, 'EF.ADN', status, out, false);
assert.strictEqual(stubs.bodies.length, 1);
assert.strictEqual(stubs.bodies[0].body.record_nr, 1);
assert.strictEqual(stubs.bodies[0].body.data, SSC_ADN);
});
test('F3: a later record that cannot be encoded writes nothing', async () => {
const entry = efFindEncoder('EF.ADN', null);
pysimFsDecModel = { entry: entry, mode: 'records' };
// record 2 has a non-FF EXT1 and an edited number -> efEncAdn refuses
const ext1 = ADN_1.slice(0, -2) + '03';
const blocks = [
adnBlock(1, ADN_1, 'B HA 1 Sic', '6082658001', true),
adnBlock(2, ext1, 'B HA 1 Sic', '6082658002', true),
];
const out = { querySelector: () => null, querySelectorAll: sel => sel === '.pysim-dec-rec' ? blocks : [] };
const stubs = saveStubs({ success: true, sw: '9000' });
const status = { textContent: '' };
await pysimFsDecSave(null, 'EF.ADN', status, out, false);
assert.strictEqual(stubs.bodies.length, 0, 'nothing may be written');
assert.match(status.textContent, /record 2/);
assert.match(status.textContent, /EF\.EXT1/);
assert.strictEqual(stubs.cancelled(), 0, 'the form stays open');
});
test('F3: a failed write names the record and how many were written', async () => {
const entry = efFindEncoder('EF.ADN', null);
pysimFsDecModel = { entry: entry, mode: 'records' };
const blocks = [
adnBlock(1, ADN_1, 'B HA 1 Sic', '6082658001', true),
adnBlock(2, ADN_1, 'B HA 1 Sic', '6082658001', true),
];
const out = { querySelector: () => null, querySelectorAll: sel => sel === '.pysim-dec-rec' ? blocks : [] };
let calls = 0;
const stubs = saveStubs(() => (++calls === 1
? { success: true, sw: '9000' }
: { success: false, sw: '6982', error: 'Security status not satisfied' }));
const status = { textContent: '' };
await pysimFsDecSave(null, 'EF.ADN', status, out, false);
assert.strictEqual(stubs.bodies.length, 2);
assert.match(status.textContent, /record 2/);
assert.match(status.textContent, /1 of 2 records written/);
assert.strictEqual(stubs.cancelled(), 0, 'the form stays open');
});
test('F6: a missing decoded model refuses to write', async () => {
pysimFsDecModel = null;
const stubs = saveStubs({ success: true, sw: '9000' });
const status = { textContent: '' };
await pysimFsDecSave(null, 'EF.ADN', status, { querySelector: () => null, querySelectorAll: () => [] }, false);
assert.strictEqual(stubs.bodies.length, 0);
assert.match(status.textContent, /No decoded editor/);
});
test('F2: untouched PNN items keep their TLV bytes through the DOM and the save', async () => {
const entry = efFindEncoder('EF.PNN', null);
const model = efDecEditModel('EF.PNN', '6fc5', { kind: 'record', records: [{ num: 1, data: PNN_1 }] });
const form = efDecEditHtml(model);
assert.ok(form.includes('data-iorig="430A82D432BBBC7EB75DE432"'));
assert.ok(form.includes('data-iorig="450A82D432BBBC7EB75DE432"'));
// the row carries its original TLV into the collection
const row = {
getAttribute: k => k === 'data-tag' ? '43' : (k === 'data-iorig' ? '430A82D432BBBC7EB75DE432' : null),
querySelector: sel => sel === '[data-ikey]' ? { getAttribute: () => 'text', value: 'Telekom.de' } : null,
};
assert.deepStrictEqual(
efDecCollectItems({ querySelectorAll: sel => sel === '.pysim-dec-item-row' ? [row] : [] }),
[{ tag: '43', text: 'Telekom.de', orig: '430A82D432BBBC7EB75DE432' }]);
// an untouched save is byte-identical
pysimFsDecModel = { entry: entry, mode: 'records' };
const rows = efDecItemsFromHex(entry, PNN_1).map(it => ({
getAttribute: k => k === 'data-tag' ? it.tag : (k === 'data-iorig' ? it.orig : null),
querySelector: sel => sel === '[data-ikey]' ? {
getAttribute: () => (it.text !== undefined ? 'text' : 'hex'),
value: it.text !== undefined ? it.text : it.hex,
} : null,
}));
const block = {
querySelector: sel => sel === '.pysim-fs-record-cb' ? { checked: true } : null,
querySelectorAll: sel => sel === '.pysim-dec-item-row' ? rows : [],
getAttribute: k => k === 'data-rec' ? '1' : (k === 'data-orig' ? PNN_1 : null),
};
const out = { querySelector: () => null, querySelectorAll: sel => sel === '.pysim-dec-rec' ? [block] : [] };
const stubs = saveStubs({ success: true, sw: '9000' });
const status = { textContent: '' };
await pysimFsDecSave(null, 'EF.PNN', status, out, false);
assert.strictEqual(stubs.bodies[0].body.data, PNN_1.toUpperCase());
});
test('F12: a selection change during the read abandons the decoded edit', async () => {
pysimFsSelected = 'EF.SPN';
pysimFsTreeRoot = { name: 'MF', children: [{ name: 'EF.SPN', fid: '6f46' }] };
const fakeEl = () => {
const classes = new Set();
return {
innerHTML: '', textContent: '',
classList: {
add: (...c) => c.forEach(x => classes.add(x)),
remove: (...c) => c.forEach(x => classes.delete(x)),
toggle: (c, on) => { if (on) classes.add(c); else classes.delete(c); },
contains: c => classes.has(c),
},
};
};
const content = fakeEl();
const status = fakeEl();
const buttons = {
'pysim-fs-content': content, 'pysim-fs-status': status,
'pysim-fs-edit-btn': fakeEl(), 'pysim-fs-edit-dec-btn': fakeEl(),
'pysim-fs-save-btn': fakeEl(), 'pysim-fs-cancel-btn': fakeEl(),
};
globalThis.document = {
getElementById: id => buttons[id] || null,
querySelectorAll: () => [],
};
globalThis.t = k => k;
globalThis.esc = s => String(s);
let release = null;
globalThis.pysimFetch = () => new Promise(res => { release = () => res({ success: true, sw: '9000', data: SPN_ORIG }); });
globalThis.pysimFsSelectBody = () => ({});
globalThis.pysimFsShowError = () => {};
let resets = 0;
globalThis.pysimFsResetEdit = () => { resets++; };
const pending = pysimFsDecEdit();
pysimFsSelected = 'EF.ADN';
release();
await pending;
assert.strictEqual(resets, 1);
assert.strictEqual(content.innerHTML, '', 'the old file must not render over the new selection');
});
+2
View File
@@ -24,6 +24,8 @@ function extractFunc(src, name) {
let code = 'var pysimFsDecodedMode = false;\n'
+ 'var pysimFsEditMode = false;\n'
+ 'var pysimFsEditData = null;\n'
+ 'var pysimFsDecEditMode = false;\n'
+ 'var pysimFsDecModel = null;\n'
+ 'var pysimFsSelected = null;\n'
+ 'var pysimFsTreeRoot = null;\n';
for (const fn of ['pysimFsSetMode', 'pysimFsPillsEnabled', 'pysimFsEdit',