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

72 lines
3.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 = 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,
HEADER_LICENSE_FIELD_CC0: 'license:CC0-1.0',
});
test('client writes HEADER v2 as SHiNE → login → literal license → key', () => {
const key = Uint8Array.from({ length: 32 }, (_, i) => i + 1);
const login = 'Alice@shine';
const body = encode({ login, initialBlockchainKey32: key });
const loginBytes = utf8(login);
const expected = Uint8Array.from([
...utf8('SHiNE'), loginBytes.length, ...loginBytes,
...utf8('license:CC0-1.0'), ...key,
]);
assert.deepEqual(Array.from(body), Array.from(expected));
assert.match(auth, /msgSubType: MSG_SUBTYPE_TECH_HEADER,\s*msgVersion: HEADER_VERSION_CC0/);
assert.match(auth, /export const HEADER_VERSION_CC0 = 2/);
});
test('inspector decodes both v1 and v2 and rejects invalid v2 license/order', () => {
const stripped = inspector.replace(/^import .+;\s*$/gm, '').replace(/^export /gm, '');
const inspect = vm.runInNewContext(`${stripped}\nparseShineFrame`, {
TextDecoder, DataView, Uint8Array, BigInt,
bytesToBase64Url: bytes => Buffer.from(bytes).toString('base64url'),
Buffer,
});
const key = Uint8Array.from({ length: 32 }, (_, i) => i + 1);
const login = utf8('Alice@shine');
const prefix = Uint8Array.from([...utf8('SHiNE'), login.length, ...login]);
const bodyV1 = Uint8Array.from([...prefix, ...key]);
const bodyV2 = Uint8Array.from([...prefix, ...utf8('license:CC0-1.0'), ...key]);
const frameFor = (version, body) => {
const frame = new Uint8Array(88 + body.length);
const view = new DataView(frame.buffer);
view.setUint16(0, 1, false); // frameCode
view.setInt32(66, frame.length, false);
view.setInt32(70, 0, false); // blockNumber
view.setBigInt64(74, 0n, false);
view.setUint16(82, 0, false); // type TECH
view.setUint16(84, 0, false); // subtype HEADER
view.setUint16(86, version, false);
frame.set(body, 88);
return frame;
};
const old = inspect(frameFor(1, bodyV1));
assert.equal(old.body.kind, 'header');
assert.equal(old.body.license, null);
const current = inspect(frameFor(2, bodyV2));
assert.equal(current.body.kind, 'header');
assert.equal(current.body.license, 'CC0-1.0');
assert.equal(current.body.initialBlockchainKey, Buffer.from(key).toString('base64url'));
assert.throws(() => inspect(frameFor(2, Uint8Array.from([...prefix, ...key, ...utf8('license:CC0-1.0')]))));
const invalid = bodyV2.slice();
invalid[prefix.length + 8] = 'X'.charCodeAt(0);
assert.throws(() => inspect(frameFor(2, invalid)));
assert.throws(() => inspect(frameFor(2, new Uint8Array(0))));
});