SHA256
70 lines
3.2 KiB
JavaScript
70 lines
3.2 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import fs from 'node:fs';
|
|
import vm from 'node:vm';
|
|
|
|
const read = path => fs.readFileSync(new URL(path, import.meta.url), 'utf8');
|
|
const auth = read('../js/services/auth-service.js');
|
|
const inspector = read('../js/services/blockchain-entry-inspector.js');
|
|
const source = auth.split('function makeHeaderBodyBytes(')[1]?.split('\nfunction makeUserParamBodyBytes(')[0];
|
|
assert.ok(source, 'production makeHeaderBodyBytes must exist');
|
|
const utf8 = value => new TextEncoder().encode(value);
|
|
const encode = vm.runInNewContext(`function makeHeaderBodyBytes(${source}\nmakeHeaderBodyBytes`, {
|
|
utf8Bytes: utf8,
|
|
int8Byte: value => Uint8Array.of(value),
|
|
concatBytes: (...parts) => Uint8Array.from(parts.flatMap(part => Array.from(part))),
|
|
Uint8Array,
|
|
});
|
|
|
|
function inspectorParser() {
|
|
const stripped = inspector.replace(/^import .+;\s*$/gm, '').replace(/^export /gm, '');
|
|
return vm.runInNewContext(`${stripped}\nparseShineFrame`, {
|
|
TextDecoder, DataView, Uint8Array, BigInt,
|
|
bytesToBase64Url: bytes => Buffer.from(bytes).toString('base64url'), Buffer,
|
|
});
|
|
}
|
|
|
|
function headerFrame(body, version = 1) {
|
|
const frame = new Uint8Array(88 + body.length);
|
|
const view = new DataView(frame.buffer);
|
|
view.setUint16(0, 1, false);
|
|
view.setInt32(66, frame.length, false);
|
|
view.setInt32(70, 0, false);
|
|
view.setBigInt64(74, 0n, false);
|
|
view.setUint16(82, 0, false);
|
|
view.setUint16(84, 0, false);
|
|
view.setUint16(86, version, false);
|
|
frame.set(body, 88);
|
|
return frame;
|
|
}
|
|
|
|
test('client writes only HEADER 1.0: identity, arbitrary license, then initial key', () => {
|
|
const key = Uint8Array.from({ length: 32 }, (_, i) => i + 1);
|
|
const login = 'Alice@shine';
|
|
const license = 'Лицензия 🌞';
|
|
const previous = 'Bob@free,Older';
|
|
const body = encode({ login, initialBlockchainKey32: key, accountType: 1, previousAccounts: ['Bob@free', 'Older'], license });
|
|
const expected = Uint8Array.from([
|
|
...utf8('SHiNE'), utf8(login).length, ...utf8(login),
|
|
1, 0, utf8(previous).length, ...utf8(previous), utf8(license).length, ...utf8(license), ...key,
|
|
]);
|
|
assert.deepEqual(Array.from(body), Array.from(expected));
|
|
assert.match(auth, /export const HEADER_VERSION = 1/);
|
|
});
|
|
|
|
test('inspector accepts only HEADER 1.0 and validates its identity and license', () => {
|
|
const inspect = inspectorParser();
|
|
const key = Uint8Array.from({ length: 32 }, (_, i) => i + 1);
|
|
for (const license of ['', 'CC-BY-NC-4.0', 'Лицензия 🌞', 'x'.repeat(255)]) {
|
|
const body = encode({ login: 'Alice@shine', initialBlockchainKey32: key, accountType: 1, previousAccounts: ['Bob@free'], license });
|
|
const parsed = inspect(headerFrame(body)).body;
|
|
assert.equal(parsed.accountType, 1);
|
|
assert.deepEqual(Array.from(parsed.previousAccounts), ['Bob@free']);
|
|
assert.equal(parsed.license, license);
|
|
assert.throws(() => inspect(headerFrame(body.slice(0, -1))));
|
|
assert.equal(inspect(headerFrame(body, 2)).body.kind, 'unsupported_version');
|
|
}
|
|
assert.throws(() => encode({ login: 'Alice@shine', initialBlockchainKey32: key, license: 'x'.repeat(256) }));
|
|
assert.throws(() => encode({ login: 'Alice@shine', initialBlockchainKey32: key, accountType: 0, previousAccounts: ['Bob@free'] }));
|
|
});
|