- gen-market 产出 <id>-<version>.zip(STORE)+ bundle/bundleSize/bundleSha256/files[];保留单文件兼容 - 安装器:下载→整包校验→解包→逐文件 sha256→路径白名单→体积/数量上限→.new→.old→rename→清理(失败回滚) - storage-file 增 renameDir/existsDir;卸载目录插件保留 plugin-data - 新增 scripts/plugin-zip.js 与 plugin-bundle / plugin-install 两组测试(含覆盖升级与拒绝路径)
137 lines
7.3 KiB
JavaScript
137 lines
7.3 KiB
JavaScript
'use strict'
|
|
// 目录插件安装(M3)端到端测试:真实 zip → 假 FS → 断言落盘/索引/失败回滚
|
|
//
|
|
// 不依赖 Tauri:用 fs 临时目录实现 IWBStorageFile.plugins 的同名契约,
|
|
// 用 vm 加载真实的 src/storage-zip.js 与 src/runtime-plugin-market.js。
|
|
const assert = require('assert')
|
|
const fs = require('fs')
|
|
const os = require('os')
|
|
const path = require('path')
|
|
const vm = require('vm')
|
|
|
|
const root = path.resolve(__dirname, '..')
|
|
const { zipStore } = require(path.join(root, 'scripts', 'plugin-zip'))
|
|
const work = fs.mkdtempSync(path.join(os.tmpdir(), 'iwb-install-'))
|
|
const pluginsDir = path.join(work, 'plugins')
|
|
|
|
function p (rel) { return path.join(pluginsDir, rel) }
|
|
const fakePlugins = {
|
|
async manifest () {
|
|
try { return JSON.parse(fs.readFileSync(p('index.json'), 'utf8')) } catch (e) { return [] }
|
|
},
|
|
async read (f) { return fs.readFileSync(p(f), 'utf8') },
|
|
async write (f, text) { fs.mkdirSync(path.dirname(p(f)), { recursive: true }); fs.writeFileSync(p(f), text) },
|
|
async remove (f) { try { fs.unlinkSync(p(f)) } catch (e) { /* ignore */ } },
|
|
async mkdir (rel) { fs.mkdirSync(p(rel), { recursive: true }) },
|
|
async writeBinary (rel, bytes) { fs.mkdirSync(path.dirname(p(rel)), { recursive: true }); fs.writeFileSync(p(rel), Buffer.from(bytes)) },
|
|
async removeDir (rel) { fs.rmSync(p(rel), { recursive: true, force: true }) },
|
|
async renameDir (a, b) { fs.renameSync(p(a), p(b)) },
|
|
async existsDir (rel) { return fs.existsSync(p(rel)) },
|
|
async listDirs () { return [] },
|
|
}
|
|
|
|
const mainCode = 'global.__demoLoaded = 1\n'
|
|
const manifest = { id: 'demo', name: '演示', version: '1.0.0', apiVersion: 1, main: 'main.js' }
|
|
function buildZip (over) {
|
|
const man = Object.assign({}, manifest, over || {})
|
|
const main = (over && over.__main) || mainCode
|
|
return zipStore([
|
|
{ name: 'manifest.json', data: JSON.stringify(man) },
|
|
{ name: 'main.js', data: main },
|
|
])
|
|
}
|
|
const sha = (buf) => require('crypto').createHash('sha256').update(buf).digest('hex')
|
|
;(async () => {
|
|
// 真实解包在下面由 install 走异步路径;这里改用同步手工解析避免测试辅助复杂化
|
|
const sb = { TextDecoder }
|
|
vm.runInNewContext(fs.readFileSync(path.join(root, 'src', 'storage-zip.js'), 'utf8'), sb)
|
|
const Z = sb.IWBStorageZip
|
|
async function contents (buf) {
|
|
const ab = buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength)
|
|
const parsed = Z.wZipParseEntries(ab)
|
|
const out = {}
|
|
for (const e of parsed.entries) out[e.name] = Buffer.from(await Z.wZipReadEntry(parsed, e)).toString('utf8')
|
|
return out
|
|
}
|
|
|
|
const zip1 = buildZip()
|
|
const files1 = await contents(zip1)
|
|
const item = {
|
|
id: 'demo', version: '1.0.0', bundle: 'plugins/demo-1.0.0.zip',
|
|
bundleSha256: sha(zip1),
|
|
files: Object.keys(files1).map((n) => ({ path: n, sha256: sha(Buffer.from(files1[n])), size: Buffer.byteLength(files1[n]) })),
|
|
}
|
|
const sandbox = {
|
|
crypto: require('crypto').webcrypto, TextDecoder, TextEncoder,
|
|
IWBStorageFile: { plugins: fakePlugins },
|
|
IWBStorageLocal: { getStore: () => ({ get: () => null, set: () => true }) },
|
|
fetch: async (url) => {
|
|
if (String(url).indexOf('index.json') >= 0) return { ok: true, json: async () => ({ plugins: [item] }) }
|
|
return { ok: true, arrayBuffer: async () => zip1.buffer.slice(zip1.byteOffset, zip1.byteOffset + zip1.byteLength) }
|
|
},
|
|
}
|
|
sandbox.__TAURI__ = null
|
|
sandbox.global = sandbox
|
|
vm.runInNewContext(fs.readFileSync(path.join(root, 'src', 'storage-zip.js'), 'utf8'), sandbox)
|
|
vm.runInNewContext(fs.readFileSync(path.join(root, 'src', 'runtime-plugin-market.js'), 'utf8'), sandbox)
|
|
const M = sandbox.IWBRuntimePluginMarket
|
|
|
|
const r1 = await M.install('demo')
|
|
assert.ok(r1.ok, '首次安装应成功:' + JSON.stringify(r1))
|
|
assert.strictEqual(fs.readFileSync(p('demo/main.js'), 'utf8'), mainCode, 'main.js 应落盘且内容一致')
|
|
const idx1 = JSON.parse(fs.readFileSync(p('index.json'), 'utf8'))
|
|
const e1 = idx1.find((x) => x.id === 'demo')
|
|
assert.ok(e1 && e1.dir === 'demo' && e1.main === 'main.js', '索引应写入目录条目')
|
|
assert.strictEqual(e1.files['main.js'], sha(Buffer.from(mainCode)), '索引应记录逐文件 sha256')
|
|
|
|
// 覆盖安装(升级):旧目录必须被替换,且不残留 .old/.new
|
|
const zip2 = buildZip({ __main: 'global.__demoLoaded = 2\n' })
|
|
const files2 = await contents(zip2)
|
|
const item2 = Object.assign({}, item, {
|
|
version: '1.0.1', bundleSha256: sha(zip2),
|
|
files: Object.keys(files2).map((n) => ({ path: n, sha256: sha(Buffer.from(files2[n])), size: Buffer.byteLength(files2[n]) })),
|
|
})
|
|
sandbox.fetch = async (url) => {
|
|
if (String(url).indexOf('index.json') >= 0) return { ok: true, json: async () => ({ plugins: [item2] }) }
|
|
return { ok: true, arrayBuffer: async () => zip2.buffer.slice(zip2.byteOffset, zip2.byteOffset + zip2.byteLength) }
|
|
}
|
|
const r2 = await M.install('demo')
|
|
assert.ok(r2.ok, '覆盖安装应成功:' + JSON.stringify(r2))
|
|
assert.strictEqual(fs.readFileSync(p('demo/main.js'), 'utf8'), 'global.__demoLoaded = 2\n', '升级后应是新内容')
|
|
assert.ok(!fs.existsSync(p('demo.old')), '不应残留 .old')
|
|
assert.ok(!fs.existsSync(p('demo.new')), '不应残留 .new')
|
|
|
|
// 逐文件 sha256 不符 → 拒绝,且旧版本不受影响
|
|
const bad = Object.assign({}, item2, { files: [{ path: 'manifest.json', sha256: 'deadbeef', size: 1 }] })
|
|
sandbox.fetch = async (url) => {
|
|
if (String(url).indexOf('index.json') >= 0) return { ok: true, json: async () => ({ plugins: [bad] }) }
|
|
return { ok: true, arrayBuffer: async () => zip2.buffer.slice(zip2.byteOffset, zip2.byteOffset + zip2.byteLength) }
|
|
}
|
|
const r3 = await M.install('demo')
|
|
assert.ok(!r3.ok && /校验失败/.test(r3.error), '逐文件 sha256 不符必须拒绝:' + JSON.stringify(r3))
|
|
assert.strictEqual(fs.readFileSync(p('demo/main.js'), 'utf8'), 'global.__demoLoaded = 2\n', '拒绝安装不得破坏已装版本')
|
|
|
|
// manifest id 不一致 → 拒绝
|
|
const wrongId = buildZip({ id: 'other' })
|
|
const wrongFiles = await contents(wrongId)
|
|
const item4 = Object.assign({}, item2, {
|
|
bundleSha256: sha(wrongId),
|
|
files: Object.keys(wrongFiles).map((n) => ({ path: n, sha256: sha(Buffer.from(wrongFiles[n])), size: Buffer.byteLength(wrongFiles[n]) })),
|
|
})
|
|
sandbox.fetch = async (url) => {
|
|
if (String(url).indexOf('index.json') >= 0) return { ok: true, json: async () => ({ plugins: [item4] }) }
|
|
return { ok: true, arrayBuffer: async () => wrongId.buffer.slice(wrongId.byteOffset, wrongId.byteOffset + wrongId.byteLength) }
|
|
}
|
|
const r4 = await M.install('demo')
|
|
assert.ok(!r4.ok && /id 与市场条目不一致/.test(r4.error), 'manifest id 不一致必须拒绝:' + JSON.stringify(r4))
|
|
|
|
// 卸载:目录删除,索引移除
|
|
const r5 = await M.uninstall('demo')
|
|
assert.ok(r5.ok, '卸载应成功')
|
|
assert.ok(!fs.existsSync(p('demo')), '卸载应删除插件目录')
|
|
assert.strictEqual((JSON.parse(fs.readFileSync(p('index.json'), 'utf8')) || []).filter((x) => x.id === 'demo').length, 0, '索引条目应移除')
|
|
|
|
fs.rmSync(work, { recursive: true, force: true })
|
|
console.log('plugin-install checks passed(首装 / 覆盖升级 / 逐文件校验拒绝 / id 校验 / 卸载)')
|
|
})().catch((e) => { try { fs.rmSync(work, { recursive: true, force: true }) } catch (e2) { /* ignore */ } console.error(e); process.exit(1) })
|