142 lines
7.2 KiB
JavaScript
142 lines
7.2 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import fs from 'node:fs';
|
|
import vm from 'node:vm';
|
|
import { execFileSync } from 'node:child_process';
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
const base = new URL('../public/', import.meta.url);
|
|
const html = fs.readFileSync(new URL('cheerpj-native-loader-test.html', base), 'utf8');
|
|
const js = fs.readFileSync(new URL('cheerpj-native-loader-test.js', base), 'utf8');
|
|
const native = fs.readFileSync(new URL('cheerpj/natives/wwprobe.js', base), 'utf8');
|
|
const java = fs.readFileSync(new URL('../../hmi-docs/cheerpj/NativeLoaderSmoke.java', base), 'utf8');
|
|
const jarFile = fileURLToPath(new URL('cheerpj/jaas-native-loader-smoke.jar', base));
|
|
const bridgeJar = fileURLToPath(new URL('cheerpj/jaas-identity-bridge.jar', base));
|
|
|
|
assert.ok(fs.statSync(jarFile).size > 1500);
|
|
assert.ok(fs.statSync(bridgeJar).size > 1000);
|
|
assert.match(html, /id="modes"/);
|
|
assert.match(html, /id="runtime-links"/);
|
|
assert.match(html, /id="copy-report"/);
|
|
assert.doesNotMatch(html, /<script src="https:\/\/cjrtnc/); // Only one selected CheerpJ runtime is loaded.
|
|
assert.match(js, /version:\s*11/);
|
|
assert.match(js, /java\.library\.path=/);
|
|
assert.match(js, /cheerpjRunMain/);
|
|
assert.ok(js.includes("params.get('runtime') === '4.3'"));
|
|
assert.match(js, /System\.loadLibrary\/UnixSystem ist dadurch NICHT repariert/);
|
|
assert.match(native, /Java_com_widgetworkbench_cheerpj_NativeLoaderSmoke_ping/);
|
|
assert.match(java, /System\.loadLibrary\(mode\)/);
|
|
assert.match(java, /"direct"/);
|
|
assert.match(java, /"bridge"/);
|
|
assert.match(java, /"unix"/);
|
|
assert.match(java, /"environment"/);
|
|
assert.match(java, /"sysprop"/);
|
|
assert.match(java, /Unsafe/);
|
|
assert.doesNotMatch(java, /System\.out\.println\([^\n]*\+\s*user\s*\)/);
|
|
for(const entry of [
|
|
{jar:jarFile, clazz:'com.widgetworkbench.cheerpj.NativeLoaderSmoke'},
|
|
{jar:bridgeJar, clazz:'com.widgetworkbench.cheerpj.JaasIdentityBridge'},
|
|
]) {
|
|
const javap = execFileSync('javap', ['-verbose', '-cp', entry.jar, entry.clazz], {encoding:'utf8'});
|
|
assert.match(javap, /major version: 55/); // Java 11 classfile
|
|
}
|
|
|
|
// Contract tests, not real browser/JVM. Verify the Java-11 runtime settings and
|
|
// that no direct natives handler is supplied to System.loadLibrary tests.
|
|
async function launch(mode, { runtime='5.0', path='normal', rangeStatus=206, javaExit=0,
|
|
moduleMime='text/javascript', dynamicLoader=false, blockedLoader=false }={}) {
|
|
const output = { textContent: '' };
|
|
const calls = [];
|
|
const wwprobeNativeHandlers = { Java_com_widgetworkbench_cheerpj_NativeLoaderSmoke_ping() { return 42; } };
|
|
const content = js
|
|
.replace(/import wwprobeNativeHandlers from '\.\/cheerpj\/natives\/wwprobe\.js';/,
|
|
'const wwprobeNativeHandlers=globalThis.__nativeHandlers;')
|
|
.replace('run().catch(error => {', 'globalThis.__runPromise=run().catch(error => {');
|
|
let scriptRequested = null;
|
|
const context = {
|
|
__nativeHandlers: wwprobeNativeHandlers,
|
|
navigator: {clipboard:{writeText:async()=>{}}},
|
|
document: {
|
|
getElementById: id => id==='result' ? output : null,
|
|
createElement: name => {
|
|
assert.equal(name, 'script');
|
|
scriptRequested = {src:'',onload:null,onerror:null};
|
|
return scriptRequested;
|
|
},
|
|
head: {appendChild: script=>{
|
|
calls.push(['script',script.src]);
|
|
if(blockedLoader) script.onerror();
|
|
else {
|
|
context.cheerpjInit = async opts=>{calls.push(['init', opts]);};
|
|
script.onload();
|
|
}
|
|
}},
|
|
},
|
|
location: {href: 'https://company.example/backend/hmi/cheerpj-native-loader-test.html?mode=' + mode + '&runtime=' + runtime + '&path=' + path},
|
|
URL,
|
|
console: {info() {}, error() {}},
|
|
fetch: async (url, opts) => {
|
|
calls.push(['fetch', url, opts]);
|
|
const isJar = url.endsWith('.jar');
|
|
const status = isJar ? rangeStatus : 200;
|
|
return {
|
|
ok: status >= 200 && status < 300, status,
|
|
headers: {get: name => {
|
|
if (name==='content-type') return isJar?'application/java-archive':moduleMime;
|
|
if (name==='content-range') return isJar && status===206 ? 'bytes 0-0/4821' : null;
|
|
if (name==='accept-ranges') return isJar ? 'bytes' : null;
|
|
return null;
|
|
}},
|
|
text: async () => 'export default { async Java_com_widgetworkbench_cheerpj_NativeLoaderSmoke_ping(){} }',
|
|
};
|
|
},
|
|
cheerpjRunMain: async (...args) => {calls.push(['main', args]); return javaExit;},
|
|
};
|
|
if(!dynamicLoader) context.cheerpjInit = async opts => {calls.push(['init', opts]);};
|
|
vm.runInNewContext(content, context, {filename:'cheerpj-native-loader-test.js'});
|
|
await context.__runPromise;
|
|
return { calls, output:output.textContent, scriptRequested };
|
|
}
|
|
|
|
for (const mode of ['environment','sysprop','direct','wwprobe','jaas','unix','bridge']) {
|
|
const {calls, output} = await launch(mode);
|
|
const init = calls.find(c => c[0] === 'init')?.[1];
|
|
const main = calls.find(c => c[0] === 'main')?.[1];
|
|
assert.equal(init.version, 11, mode);
|
|
assert.equal(main[2], mode, mode);
|
|
assert.equal(main[0], 'com.widgetworkbench.cheerpj.NativeLoaderSmoke');
|
|
assert.ok(Array.from(init.javaProperties).includes('java.library.path=/app/backend/hmi/cheerpj/natives'));
|
|
assert.match(output, /TESTERGEBNIS: PASS/);
|
|
if (mode === 'direct') {
|
|
assert.equal(typeof init.natives.Java_com_widgetworkbench_cheerpj_NativeLoaderSmoke_ping, 'function');
|
|
assert.match(output, /Direct JNI funktioniert/);
|
|
} else {
|
|
assert.equal(init.natives, undefined, 'Direct registration must only be used in direct mode.');
|
|
}
|
|
if (mode === 'bridge') {
|
|
assert.match(main[1], /jaas-native-loader-smoke\.jar:.*jaas-identity-bridge\.jar/);
|
|
assert.match(output, /Java-Bridge funktioniert OHNE UnixSystem-Konstruktor/);
|
|
} else {
|
|
assert.doesNotMatch(main[1], /jaas-identity-bridge\.jar/);
|
|
}
|
|
assert.equal(calls.filter(c=>c[0]==='fetch' && c[1].endsWith('.jar')).length, mode==='bridge'?2:1);
|
|
}
|
|
|
|
// Optional CheerpJ 4.3 comparison is still running Java 11 (not Java 17).
|
|
const versionCompare = await launch('wwprobe', {runtime:'4.3', dynamicLoader:true});
|
|
assert.equal(versionCompare.calls.find(c=>c[0]==='script')[1], 'https://cjrtnc.leaningtech.com/4.3/loader.js');
|
|
assert.equal(versionCompare.calls.find(c=>c[0]==='init')[1].version, 11);
|
|
const failedExit = await launch('wwprobe', {javaExit:31});
|
|
assert.match(failedExit.output,/TESTERGEBNIS: FAIL/);
|
|
const missingRange = await launch('bridge', {rangeStatus:200});
|
|
assert.match(missingRange.output,/TESTABBRUCH: Java-JAR unterstützt keinen HTTP-Range-Abruf/);
|
|
assert.ok(!missingRange.calls.some(c=>c[0]==='init'));
|
|
const invalidMime = await launch('wwprobe', {moduleMime:'text/html'});
|
|
assert.match(invalidMime.output,/TESTABBRUCH: JS-Native-Modul fehlt/);
|
|
const missingPath = await launch('wwprobe', {path:'missing'});
|
|
assert.ok(Array.from(missingPath.calls.find(c=>c[0]==='init')[1].javaProperties)
|
|
.some(s=>s.endsWith('/absichtlich-nicht-vorhanden')));
|
|
const blockedCdn = await launch('environment',{dynamicLoader:true,blockedLoader:true});
|
|
assert.match(blockedCdn.output,/TESTABBRUCH: CheerpJ-Loader nicht erreichbar/);
|
|
assert.ok(!blockedCdn.calls.some(c=>c[0]==='init'));
|
|
console.log('CheerpJ V5.10.22 Java11 diagnostics: 7 modes, 4.3 vs 5.0, classfile55, JNI isolation, Range, MIME, and error paths validated.');
|