Files
SHiNE-server/shine-UI/tests/registration-cc0-header.test.mjs
T

40 lines
2.3 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 = file => fs.readFileSync(new URL(file, import.meta.url), 'utf8');
const auth = read('../js/services/auth-service.js');
// Исполняем именно production-сериализатор, а не дублируем алгоритм тестовым кодом.
const makeBytesFunction = auth.split('function makeHeaderBodyBytes(')[1]?.split('\nfunction makeUserParamBodyBytes(')[0];
assert.ok(makeBytesFunction);
const context = {
HEADER_DEFAULT_LICENSE: auth.match(/export const HEADER_DEFAULT_LICENSE = '([^']+)'/)[1],
utf8Bytes: value => new TextEncoder().encode(value),
int8Byte: value => Uint8Array.of(value),
concatBytes: (...parts) => Uint8Array.from(parts.flatMap(part => Array.from(part))),
Uint8Array,
};
const makeHeaderBodyBytes = vm.runInNewContext(`function makeHeaderBodyBytes(${makeBytesFunction}\nmakeHeaderBodyBytes`, context);
test('CORE/CONTENT HEADER 1 contains identity, arbitrary license and initial key', () => {
const key = Uint8Array.from({ length: 32 }, (_, n) => n + 1);
const result = makeHeaderBodyBytes({ login: 'Alice@shine', initialBlockchainKey32: key });
const login = new TextEncoder().encode('Alice@shine');
assert.equal(new TextDecoder().decode(result.slice(0,5)), 'SHiNE');
assert.equal(result[5], login.length);
assert.deepEqual(Array.from(result.slice(6, 6+login.length)), Array.from(login));
const identityOffset = 6 + login.length;
assert.equal(result[identityOffset], 1);
assert.deepEqual(Array.from(result.slice(identityOffset + 1, identityOffset + 3)), [0, 0]);
const license = context.HEADER_DEFAULT_LICENSE;
const licenseBytes = new TextEncoder().encode(license);
assert.deepEqual(Array.from(result.slice(identityOffset + 3, identityOffset + 5)), [licenseBytes.length >> 8, licenseBytes.length & 255]);
assert.equal(new TextDecoder().decode(result.slice(identityOffset + 5, identityOffset + 5 + licenseBytes.length)), license);
assert.deepEqual(Array.from(result.slice(identityOffset + 5 + licenseBytes.length)), Array.from(key));
assert.equal(result.length, 5 + 1 + login.length + 1 + 2 + 2 + licenseBytes.length + 32);
assert.match(auth, /msgSubType: MSG_SUBTYPE_TECH_HEADER,\s*msgVersion: HEADER_VERSION/);
assert.match(auth, /export const HEADER_VERSION = 1/);
});