cb40662e4e
Every preset TAR entry carries `keyset` (int 1-15) - the keyset every
operation addressed to that TAR uses: mandatory for any non-zero MSL
(the counters are keyset-linked), optional for MSL 00 (allowed: some
cards reject the no-security 00/00 KIc/KID form). The store fills it
automatically when the preset has a single keyset, the validation
refuses an entry naming an undefined keyset, and the schema is 6 (a v5
file loads leniently; editing it forces the fix).
The keyset selectors are gone: the SCP80 TAR field became a select of
the preset's entries (applying the TAR's keyset to the form and seeding
the SPI1 from its MSL); the RAM operations use the preset's ISD role
entry (the PWA sends only the preset id, the ISD TAR, SPI and the
counter override - the server resolves the keyset and the keys:
presets.tar_keyset, _preset_tar_keyset, _resolve_operation_keyset,
_op_keyset, used by send-ota's APDU path, ram-install, ram-install-app
and cap-compat); the counter probe and the test runner's scp80 steps
default to their TAR's keyset (an explicit kvn stays an override). A
security TAR without a keyset refuses the operation ("set it in the
Cards tab") - never a silent keyless send - and the start buttons wait
for a usable operation-TAR keyset.
The TAR probe checks the preset's TAR list only (the standard
TAR_PROBE_TARS list and the custom-TAR localStorage are gone; a `tars`
list selects a subset, a TAR outside the preset is refused), each row
using its own MSL and default keyset. The Cards editor's TAR table
gained a Keyset column (the only keyset preselected, an amber marker
when a security MSL lacks one).
Docs: help EN/RU, api.md's tar-probe contract, both AGENTS. Tests: the
store (the field, the auto-fill, the schema, the string forms), the
server resolution (send-ota + the refusal, the probe's per-row keysets
and refusals), the PWA (the TAR select, the RAM ISD resolution, the
keyset-free requests) - several fixtures updated for the new schema.
891 frontend + 885 Python; sw cache simple-v434.
355 lines
14 KiB
JavaScript
355 lines
14 KiB
JavaScript
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 extractConst(src, name) {
|
|
const m = new RegExp('const\\s+' + name + '\\s*=\\s*([^;]+);').exec(src);
|
|
if (!m) throw new Error('const ' + name + ' not found');
|
|
return 'var ' + name + ' = ' + m[1] + ';\n';
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
let code = 'var _pysimServerAvailable = null;\nvar _pysimCardEquipped = false;\nvar _pysimEquipping = false;\nvar _pysimCardIccid = null;\nvar _pysimHeaderIccid = undefined;\nvar _pysimHeaderScp80 = undefined;\nvar _pysimHeaderScp81 = undefined;\n';
|
|
code += extractFunc(html, 'pysimAvailabilityState') + '\n';
|
|
code += extractFunc(html, 'pysimControlDisabled') + '\n';
|
|
code += extractFunc(html, 'pysimNeedsHint') + '\n';
|
|
code += extractFunc(html, 'pysimApplyAvailability') + '\n';
|
|
code += extractFunc(html, 'pysimUpdateCardActionLabels') + '\n';
|
|
code += extractFunc(html, 'pysimUpdateStateIndicator') + '\n';
|
|
code += extractFunc(html, 'pysimUpdateIccidIndicator') + '\n';
|
|
code += extractFunc(html, 'pysimUpdatePresetIndicator') + '\n';
|
|
code += extractFunc(html, 'pysimUpdatePresetIndicators') + '\n';
|
|
code += extractConst(html, 'CARDS_TAR_DEFAULTS') + extractConst(html, 'CARDS_TAR_ROLES') + extractConst(html, 'CARDS_MSL_DEFAULT');
|
|
code += extractFunc(html, 'cardsTarList') + '\n';
|
|
code += extractFunc(html, 'cardsScp80Complete') + '\n';
|
|
code += extractFunc(html, 'cardsScp81Complete') + '\n';
|
|
code += extractFunc(html, 'cardsMatchedPreset') + '\n';
|
|
code += extractFunc(html, 'spKeysetStartReady') + '\n';
|
|
code += extractFunc(html, 'spKeysetGuard') + '\n';
|
|
code += extractFunc(html, 'ramKeysetGuard') + '\n';
|
|
code += extractFunc(html, 'spTarKeysetGuard') + '\n';
|
|
code += extractFunc(html, 'cardsTarEntry') + '\n';
|
|
code += extractFunc(html, 'cardsRoleTar') + '\n';
|
|
code += extractFunc(html, 'cardsRoleDefault') + '\n';
|
|
code += extractFunc(html, 'spPresetIdx') + '\n';
|
|
code += extractFunc(html, 'ramPresetIdx') + '\n';
|
|
code += extractFunc(html, 'tarPresetIdx') + '\n';
|
|
code += extractFunc(html, 'spKeysetCheck') + '\n';
|
|
code += extractFunc(html, 'spKeysetFor') + '\n';
|
|
code += extractFunc(html, 'spKeysetList') + '\n';
|
|
code += extractFunc(html, 'spKeysetKvnOf') + '\n';
|
|
code += 'globalThis.t = s => s;\n';
|
|
code += 'globalThis.testRunActive = () => false;\n';
|
|
eval(code);
|
|
|
|
function fakeEl() {
|
|
const classes = new Set();
|
|
return {
|
|
classes,
|
|
attrs: {},
|
|
classList: {
|
|
add: (...cs) => cs.forEach(c => classes.add(c)),
|
|
remove: (...cs) => cs.forEach(c => classes.delete(c)),
|
|
contains: c => classes.has(c),
|
|
},
|
|
setAttribute(k, v) { this.attrs[k] = v; },
|
|
getAttribute(k) { return this.attrs[k]; },
|
|
hasAttribute(k) { return k in this.attrs; },
|
|
removeAttribute(k) { delete this.attrs[k]; },
|
|
};
|
|
}
|
|
|
|
function setup() {
|
|
const els = {
|
|
'state-indicator': fakeEl(),
|
|
'state-indicator-dot': fakeEl(),
|
|
'state-indicator-img': fakeEl(),
|
|
'state-indicator-iccid': fakeEl(),
|
|
'state-indicator-scp80': fakeEl(),
|
|
'state-indicator-scp81': fakeEl(),
|
|
};
|
|
els['state-indicator-img'].src = '';
|
|
globalThis.document = { getElementById: id => els[id] || null, querySelectorAll: () => [] };
|
|
_pysimServerAvailable = null;
|
|
_pysimCardEquipped = false;
|
|
_pysimEquipping = false;
|
|
_pysimCardIccid = null;
|
|
_pysimHeaderIccid = undefined;
|
|
_pysimHeaderScp80 = undefined;
|
|
_pysimHeaderScp81 = undefined;
|
|
globalThis.cards = [];
|
|
globalThis.cardsFindByIccid = () => -1;
|
|
return els;
|
|
}
|
|
|
|
test('unprobed server shows a gray dot and a Connecting title', () => {
|
|
const els = setup();
|
|
pysimUpdateStateIndicator();
|
|
const { 'state-indicator': wrap, 'state-indicator-dot': dot, 'state-indicator-img': img } = els;
|
|
assert.ok(dot.classes.has('text-gray-400'));
|
|
assert.ok(!dot.classes.has('hidden'));
|
|
assert.ok(img.classes.has('hidden'));
|
|
assert.strictEqual(wrap.attrs.title, 'Connecting...');
|
|
assert.strictEqual(dot.attrs.title, 'Connecting...');
|
|
});
|
|
|
|
test('unreachable server shows a red dot', () => {
|
|
const els = setup();
|
|
_pysimServerAvailable = false;
|
|
pysimUpdateStateIndicator();
|
|
const { 'state-indicator': wrap, 'state-indicator-dot': dot, 'state-indicator-img': img } = els;
|
|
assert.ok(dot.classes.has('text-red-500'));
|
|
assert.ok(!dot.classes.has('text-gray-400'));
|
|
assert.ok(img.classes.has('hidden'));
|
|
assert.strictEqual(wrap.attrs.title, 'No server connection');
|
|
assert.strictEqual(dot.attrs.title, 'No server connection');
|
|
});
|
|
|
|
test('server up without a card shows nosim.svg', () => {
|
|
const els = setup();
|
|
_pysimServerAvailable = true;
|
|
pysimUpdateStateIndicator();
|
|
const { 'state-indicator': wrap, 'state-indicator-dot': dot, 'state-indicator-img': img } = els;
|
|
assert.ok(dot.classes.has('hidden'));
|
|
assert.ok(!img.classes.has('hidden'));
|
|
assert.strictEqual(img.src, 'nosim.svg');
|
|
assert.strictEqual(wrap.attrs.title, 'Server connected, no card equipped');
|
|
assert.strictEqual(dot.attrs.title, undefined);
|
|
});
|
|
|
|
test('equipped card shows sim.svg', () => {
|
|
const els = setup();
|
|
_pysimServerAvailable = true;
|
|
_pysimCardEquipped = true;
|
|
pysimUpdateStateIndicator();
|
|
const { 'state-indicator': wrap, 'state-indicator-img': img } = els;
|
|
assert.strictEqual(img.src, 'sim.svg');
|
|
assert.strictEqual(wrap.attrs.title, 'Card equipped');
|
|
});
|
|
|
|
test('equipping shows the animated sim_anim.svg', () => {
|
|
const els = setup();
|
|
_pysimServerAvailable = true;
|
|
_pysimEquipping = true;
|
|
pysimUpdateStateIndicator();
|
|
const { 'state-indicator': wrap, 'state-indicator-img': img } = els;
|
|
assert.strictEqual(img.src, 'sim_anim.svg');
|
|
assert.strictEqual(wrap.attrs.title, 'Card inserted — initializing...');
|
|
});
|
|
|
|
test('dot color transitions do not accumulate', () => {
|
|
const els = setup();
|
|
_pysimServerAvailable = false;
|
|
pysimUpdateStateIndicator();
|
|
_pysimServerAvailable = null;
|
|
pysimUpdateStateIndicator();
|
|
const dot = els['state-indicator-dot'];
|
|
assert.ok(dot.classes.has('text-gray-400'));
|
|
assert.ok(!dot.classes.has('text-red-500'));
|
|
});
|
|
|
|
test('indicator markup carries the dot and image elements', () => {
|
|
assert.match(html, /id="state-indicator-dot"/);
|
|
assert.match(html, /id="state-indicator-img"[^>]*src="nosim\.svg"/);
|
|
});
|
|
|
|
test('header prints the equipped card ICCID next to the card image', () => {
|
|
const els = setup();
|
|
pysimUpdateIccidIndicator('89701450001700031958');
|
|
const el = els['state-indicator-iccid'];
|
|
assert.strictEqual(el.textContent, '89701450001700031958');
|
|
assert.ok(!el.classes.has('hidden'));
|
|
// no card session -> hidden again
|
|
pysimUpdateIccidIndicator(null);
|
|
assert.strictEqual(el.textContent, '');
|
|
assert.ok(el.classes.has('hidden'));
|
|
// markup order: image, ICCID, ADM badge, SCP80, SCP81
|
|
assert.ok(html.indexOf('id="state-indicator-img"') < html.indexOf('id="state-indicator-iccid"'));
|
|
assert.ok(html.indexOf('id="state-indicator-iccid"') < html.indexOf('id="state-indicator-adm"'));
|
|
assert.ok(html.indexOf('id="state-indicator-adm"') < html.indexOf('id="state-indicator-scp80"'));
|
|
assert.ok(html.indexOf('id="state-indicator-scp80"') < html.indexOf('id="state-indicator-scp81"'));
|
|
});
|
|
|
|
test('SCP80/SCP81 markers follow the matching preset completeness', () => {
|
|
const els = setup();
|
|
globalThis.cards = [{ name: 'C', kic: '15', kid: '15',
|
|
cntr: '0000000001', kicKey: 'AA', kidKey: 'BB', pskIdentity: 'id', pskKey: 'KEY',
|
|
tars: [{ role: 'isd', tar: '000000', msl: '16' },
|
|
{ role: 'uiccRfm', tar: 'B00000', msl: '16' },
|
|
{ role: 'usimRfm', tar: 'B00001', msl: '16' }] }];
|
|
globalThis.cardsFindByIccid = () => 0;
|
|
_pysimCardIccid = '89701450001700031958';
|
|
pysimUpdatePresetIndicators();
|
|
const e80 = els['state-indicator-scp80'];
|
|
const e81 = els['state-indicator-scp81'];
|
|
assert.strictEqual(e80.textContent, 'SCP80');
|
|
assert.strictEqual(e81.textContent, 'SCP81');
|
|
assert.ok(!e80.classes.has('hidden'));
|
|
assert.ok(!e81.classes.has('hidden'));
|
|
assert.strictEqual(e80.attrs.title, 'SCP80 preset complete');
|
|
assert.strictEqual(e81.attrs.title, 'SCP81 preset complete');
|
|
// SCP80 incomplete (missing key) -> only the SCP81 marker stays
|
|
globalThis.cards[0].kicKey = '';
|
|
pysimUpdatePresetIndicators();
|
|
assert.ok(e80.classes.has('hidden'));
|
|
assert.strictEqual(e80.textContent, '');
|
|
assert.strictEqual(e80.attrs.title, undefined);
|
|
assert.ok(!e81.classes.has('hidden'));
|
|
// no matching preset -> both hidden
|
|
globalThis.cardsFindByIccid = () => -1;
|
|
pysimUpdatePresetIndicators();
|
|
assert.ok(e81.classes.has('hidden'));
|
|
// explicit null ICCID (no card / server lost) hides them
|
|
globalThis.cardsFindByIccid = () => 0;
|
|
pysimUpdatePresetIndicators(null);
|
|
assert.ok(e80.classes.has('hidden'));
|
|
assert.ok(e81.classes.has('hidden'));
|
|
});
|
|
|
|
test('card-iccid controls need an equipped card with a readable ICCID', () => {
|
|
const check = (state, iccid) => {
|
|
const el = fakeEl();
|
|
el.setAttribute('data-needs', 'card-iccid');
|
|
globalThis.document = { querySelectorAll: () => [el], getElementById: () => null };
|
|
_pysimServerAvailable = state !== 'server-down';
|
|
_pysimCardEquipped = state === 'card';
|
|
_pysimCardIccid = iccid;
|
|
pysimApplyAvailability();
|
|
return el;
|
|
};
|
|
// no card -> disabled
|
|
let el = check('no-card', null);
|
|
assert.strictEqual(el.disabled, true);
|
|
assert.strictEqual(el.attrs.title, 'Insert and equip a card');
|
|
// equipped but unreadable ICCID -> disabled with its own hint
|
|
el = check('card', null);
|
|
assert.strictEqual(el.disabled, true);
|
|
assert.strictEqual(el.attrs.title, 'Card equipped but its ICCID is not readable');
|
|
// equipped with an ICCID -> enabled
|
|
el = check('card', '8970119000004600098');
|
|
assert.strictEqual(el.disabled, false);
|
|
assert.strictEqual(el.attrs.title, undefined);
|
|
// server down -> disabled
|
|
el = check('server-down', '8970119000004600098');
|
|
assert.strictEqual(el.disabled, true);
|
|
});
|
|
|
|
test('Read decoded needs a client-side decoder for the selected file', () => {
|
|
const run = (hasDecoder, cardEquipped) => {
|
|
const el = fakeEl();
|
|
el.setAttribute('data-needs', 'card');
|
|
el.setAttribute('data-needs-decoder', '');
|
|
globalThis.document = { querySelectorAll: () => [el], getElementById: () => null };
|
|
_pysimServerAvailable = true;
|
|
_pysimCardEquipped = cardEquipped;
|
|
globalThis.pysimFsHasDecoder = () => hasDecoder;
|
|
pysimApplyAvailability();
|
|
return el;
|
|
};
|
|
// decoder present, card equipped -> enabled, no tooltip
|
|
let el = run(true, true);
|
|
assert.strictEqual(el.disabled, false);
|
|
assert.strictEqual(el.attrs.title, undefined);
|
|
// no decoder -> disabled with its own hint
|
|
el = run(false, true);
|
|
assert.strictEqual(el.disabled, true);
|
|
assert.strictEqual(el.attrs.title, 'No decoder for this file');
|
|
// card missing and no decoder -> the card hint wins
|
|
el = run(false, false);
|
|
assert.strictEqual(el.disabled, true);
|
|
assert.strictEqual(el.attrs.title, 'Insert and equip a card');
|
|
});
|
|
|
|
test('indicator image stays within the 32px header row budget', () => {
|
|
const m = /id="state-indicator-img"[^>]*style="width:(\d+)px;height:(\d+)px"/.exec(html);
|
|
assert.ok(m, 'inline image size not found');
|
|
assert.strictEqual(m[1], m[2]);
|
|
const size = Number(m[1]);
|
|
assert.ok(size >= 24 && size <= 32, 'size ' + size + 'px would change the header height');
|
|
});
|
|
|
|
test('header badges are bordered chips with even spacing', () => {
|
|
for (const id of ['state-indicator-adm', 'state-indicator-scp80', 'state-indicator-scp81']) {
|
|
const m = new RegExp('id="' + id + '"[^>]*class="([^"]*)"').exec(html);
|
|
assert.ok(m, id + ' markup not found');
|
|
assert.match(m[1], /\bborder\b/);
|
|
assert.match(m[1], /\brounded\b/);
|
|
}
|
|
assert.match(html, /id="state-indicator-scp80"[^>]*class="[^"]*ml-1/);
|
|
assert.match(html, /id="state-indicator-scp81"[^>]*class="[^"]*ml-1/);
|
|
});
|
|
|
|
test('card action labels carry the equipped ICCID', () => {
|
|
setup();
|
|
const plain = fakeEl();
|
|
const colon = fakeEl();
|
|
colon.attrs['data-iccid-sep'] = ': ';
|
|
globalThis.document.querySelectorAll = sel => sel === '.profiler-iccid-label' ? [plain, colon] : [];
|
|
_pysimCardIccid = '89701450001700031958';
|
|
pysimUpdateCardActionLabels();
|
|
assert.strictEqual(plain.textContent, ' 89701450001700031958');
|
|
assert.strictEqual(colon.textContent, ': 89701450001700031958');
|
|
_pysimCardIccid = null;
|
|
pysimUpdateCardActionLabels();
|
|
assert.strictEqual(plain.textContent, '');
|
|
assert.strictEqual(colon.textContent, '');
|
|
});
|
|
|
|
test('the start buttons wait for a preset and a keyset defined in it', () => {
|
|
const run = (presetIdx, kic, kid) => {
|
|
const el = fakeEl();
|
|
el.setAttribute('data-needs', 'card');
|
|
el.setAttribute('data-needs-keyset', 'sp');
|
|
const els = {
|
|
'sp-card-sel': { value: presetIdx },
|
|
'sp-kic-hex': { value: kic },
|
|
'sp-kid-hex': { value: kid },
|
|
};
|
|
globalThis.document = { querySelectorAll: () => [el], getElementById: id => els[id] || null };
|
|
globalThis.cards = [{ name: 'EP', keysets: [
|
|
{ kic: '15', kid: '15', kicKey: 'AA', kidKey: 'BB', cntr: '0000000001' }] }];
|
|
_pysimServerAvailable = true;
|
|
_pysimCardEquipped = true;
|
|
pysimApplyAvailability();
|
|
return el;
|
|
};
|
|
// no preset selected -> disabled with the hint
|
|
let el = run('', '15', '15');
|
|
assert.strictEqual(el.disabled, true);
|
|
assert.strictEqual(el.attrs.title, 'Select a card preset and its keyset');
|
|
// a keyset number the preset does not define -> disabled
|
|
el = run('0', '35', '35');
|
|
assert.strictEqual(el.disabled, true);
|
|
// a keyset of the preset -> enabled, no tooltip
|
|
el = run('0', '15', '15');
|
|
assert.strictEqual(el.disabled, false);
|
|
assert.strictEqual(el.attrs.title, undefined);
|
|
});
|
|
|
|
test('the SCP80/RAM/TAR start buttons carry the keyset gate', () => {
|
|
const sp = /<button[^>]*id="sp-send-btn"[^>]*>/.exec(html)[0];
|
|
assert.match(sp, /data-needs-keyset="sp"/);
|
|
const ram = /<button[^>]*onclick="ramExecute\(\)"[^>]*>/.exec(html)[0];
|
|
assert.match(ram, /data-needs-keyset="ram"/);
|
|
const tar = /<button[^>]*onclick="tarExecute\(\)"[^>]*>/.exec(html)[0];
|
|
assert.match(tar, /data-needs-keyset="tar"/);
|
|
});
|