dsh-fishpal 0.9.5 → 0.9.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +54 -0
- package/README.md +29 -3
- package/_run-selftest-v07.mjs +36 -0
- package/_run-selftest.mjs +23 -0
- package/_selftest-budget.mjs +241 -0
- package/_selftest-client.mjs +141 -0
- package/_selftest-config-pack.mjs +128 -0
- package/_selftest-guard.mjs +156 -0
- package/_selftest-invite.mjs +132 -0
- package/_selftest-localserver.mjs +245 -0
- package/_selftest-loop.mjs +316 -0
- package/_selftest-memory-adapter.mjs +190 -0
- package/_selftest-pair.mjs +231 -0
- package/_selftest-privacy.mjs +151 -0
- package/_selftest-v07.mjs +460 -0
- package/_selftest.mjs +490 -0
- package/lib/adapter.js +10 -3
- package/lib/index.js +78 -3
- package/lib/memory-bridge.js +347 -0
- package/lib/ui.js +5 -1
- package/package.json +15 -1
- package/tests/run.mjs +5 -0
|
@@ -0,0 +1,460 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 0.7.0 新模块自测:unpair / recall / backup / router / friends
|
|
3
|
+
*
|
|
4
|
+
* ★ 跑法必须走 `_run-selftest-v07.mjs`(先设 FISHPAL_HOME 再动态 import)。
|
|
5
|
+
* 直接 `node _selftest-v07.mjs` 会写到真实的 ~/.fishpal 里去。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import assert from 'node:assert/strict';
|
|
9
|
+
import fs from 'node:fs';
|
|
10
|
+
import os from 'node:os';
|
|
11
|
+
import path from 'node:path';
|
|
12
|
+
|
|
13
|
+
const HOME = process.env.FISHPAL_HOME;
|
|
14
|
+
if (!HOME) {
|
|
15
|
+
console.error('✗ FISHPAL_HOME 没设——不许直接跑,走 _run-selftest-v07.mjs');
|
|
16
|
+
process.exit(1);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
let pass = 0;
|
|
20
|
+
let fail = 0;
|
|
21
|
+
const failures = [];
|
|
22
|
+
|
|
23
|
+
function ok(name, fn) {
|
|
24
|
+
try {
|
|
25
|
+
fn();
|
|
26
|
+
pass += 1;
|
|
27
|
+
console.log(` ✓ ${name}`);
|
|
28
|
+
} catch (e) {
|
|
29
|
+
fail += 1;
|
|
30
|
+
failures.push({ name, error: e?.message ?? String(e) });
|
|
31
|
+
console.log(` ✗ ${name}\n ${e?.message ?? e}`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async function okAsync(name, fn) {
|
|
36
|
+
try {
|
|
37
|
+
await fn();
|
|
38
|
+
pass += 1;
|
|
39
|
+
console.log(` ✓ ${name}`);
|
|
40
|
+
} catch (e) {
|
|
41
|
+
fail += 1;
|
|
42
|
+
failures.push({ name, error: e?.message ?? String(e) });
|
|
43
|
+
console.log(` ✗ ${name}\n ${e?.message ?? e}`);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function resetHome() {
|
|
48
|
+
fs.rmSync(HOME, { recursive: true, force: true });
|
|
49
|
+
fs.mkdirSync(HOME, { recursive: true });
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function forceIdentity(fp = 'fp_' + 'a'.repeat(32)) {
|
|
53
|
+
fs.writeFileSync(
|
|
54
|
+
path.join(HOME, 'identity.json'),
|
|
55
|
+
JSON.stringify({
|
|
56
|
+
version: 1,
|
|
57
|
+
id: fp,
|
|
58
|
+
createdAt: new Date().toISOString(),
|
|
59
|
+
signing: { publicKey: 'x', privateKey: 'y' },
|
|
60
|
+
exchange: { publicKey: 'x', privateKey: 'y' },
|
|
61
|
+
}),
|
|
62
|
+
);
|
|
63
|
+
return fp;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function forceConfig(over = {}) {
|
|
67
|
+
const cfg = {
|
|
68
|
+
version: 2,
|
|
69
|
+
me: 'fish',
|
|
70
|
+
meFp: 'fp_' + 'a'.repeat(32),
|
|
71
|
+
peerFp: 'fp_' + 'b'.repeat(32),
|
|
72
|
+
peerSigningPub: 'peer-sign-pub',
|
|
73
|
+
peerExchangePub: 'peer-exch-pub',
|
|
74
|
+
peer: '朋友',
|
|
75
|
+
...over,
|
|
76
|
+
};
|
|
77
|
+
fs.writeFileSync(path.join(HOME, 'config.json'), JSON.stringify(cfg, null, 2));
|
|
78
|
+
return cfg;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function forceFriends(peers = {}) {
|
|
82
|
+
fs.writeFileSync(path.join(HOME, 'friends.json'), JSON.stringify({ version: 1, peers }, null, 2));
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// ── 动态 import(必须在 HOME 设好之后)────────────────────────────────────
|
|
86
|
+
const router = await import('./lib/router.js');
|
|
87
|
+
const friends = await import('./lib/friends.js');
|
|
88
|
+
const recall = await import('./lib/recall.js');
|
|
89
|
+
const backup = await import('./lib/backup.js');
|
|
90
|
+
const unpair = await import('./lib/unpair.js');
|
|
91
|
+
|
|
92
|
+
console.log('\n=== thread_id 路由(规格 6.2 / 6.3)===');
|
|
93
|
+
|
|
94
|
+
resetHome();
|
|
95
|
+
ok('没带 thread_id → 进 bg 会话', () => {
|
|
96
|
+
const r = router.routeInbound({ type: 'chat', thread_id: null });
|
|
97
|
+
assert.equal(r.route, router.ROUTE.BG);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
ok('登记 thread 后 → 投到那个会话', () => {
|
|
101
|
+
router.registerThread('task-1', { sessionId: 'session-abc' });
|
|
102
|
+
const r = router.routeInbound({ type: 'chat', thread_id: 'task-1' });
|
|
103
|
+
assert.equal(r.route, router.ROUTE.THREAD);
|
|
104
|
+
assert.equal(r.thread.sessionId, 'session-abc');
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
ok('thread_id 本机不认识 → unroutable(不唤醒)', () => {
|
|
108
|
+
const r = router.routeInbound({ type: 'chat', thread_id: 'never-seen' });
|
|
109
|
+
assert.equal(r.route, router.ROUTE.UNROUTABLE);
|
|
110
|
+
assert.match(r.reason, /不认识/);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
ok('answer 带未知 thread_id → unroutable(规格 361 明确)', () => {
|
|
114
|
+
const r = router.routeInbound({ type: 'answer', thread_id: 'ghost', reply_to: 'x' });
|
|
115
|
+
assert.equal(r.route, router.ROUTE.UNROUTABLE);
|
|
116
|
+
assert.match(r.reason, /answer/);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
ok('thread 归档后 → unroutable', () => {
|
|
120
|
+
router.registerThread('task-2', { sessionId: 'session-x' });
|
|
121
|
+
router.markThread('task-2', router.THREAD_STATE.ARCHIVED);
|
|
122
|
+
const r = router.routeInbound({ type: 'chat', thread_id: 'task-2' });
|
|
123
|
+
assert.equal(r.route, router.ROUTE.UNROUTABLE);
|
|
124
|
+
assert.match(r.reason, /归档/);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
ok('thread 销毁后 → unroutable', () => {
|
|
128
|
+
router.registerThread('task-3', { sessionId: 'session-y' });
|
|
129
|
+
router.markThread('task-3', router.THREAD_STATE.DESTROYED);
|
|
130
|
+
const r = router.routeInbound({ type: 'chat', thread_id: 'task-3' });
|
|
131
|
+
assert.equal(r.route, router.ROUTE.UNROUTABLE);
|
|
132
|
+
assert.match(r.reason, /销毁/);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
ok('thread 没登记会话 id → unroutable(不往黑洞投)', () => {
|
|
136
|
+
router.registerThread('task-4', { sessionId: null });
|
|
137
|
+
const r = router.routeInbound({ type: 'chat', thread_id: 'task-4' });
|
|
138
|
+
assert.equal(r.route, router.ROUTE.UNROUTABLE);
|
|
139
|
+
assert.match(r.reason, /没登记会话/);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
ok('unroutable 落盘 + 能读回来', () => {
|
|
143
|
+
router.noteUnroutable({ id: 'm1', from: 'fp_b', type: 'answer', thread_id: 'ghost', text: '正文' }, '测试落盘');
|
|
144
|
+
const rows = router.listUnroutable();
|
|
145
|
+
assert.ok(rows.length >= 1);
|
|
146
|
+
assert.equal(rows[rows.length - 1].id, 'm1');
|
|
147
|
+
assert.equal(rows[rows.length - 1].reason, '测试落盘');
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
ok('clearUnroutable 清空', () => {
|
|
151
|
+
router.clearUnroutable();
|
|
152
|
+
assert.equal(router.listUnroutable().length, 0);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
console.log('\n=== 好友抽屉写操作 ===');
|
|
156
|
+
|
|
157
|
+
resetHome();
|
|
158
|
+
forceFriends({});
|
|
159
|
+
ok('改备注名', () => {
|
|
160
|
+
const r = friends.updateFriend('fp_' + 'b'.repeat(32), { alias: '老王' });
|
|
161
|
+
assert.ok(r.ok);
|
|
162
|
+
assert.deepEqual(r.changed, ['alias']);
|
|
163
|
+
assert.equal(friends.readFriend('fp_' + 'b'.repeat(32)).alias, '老王');
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
ok('置顶 / 静音 / 软删 / 拉黑 四个独立开关', () => {
|
|
167
|
+
const fp = 'fp_' + 'b'.repeat(32);
|
|
168
|
+
friends.updateFriend(fp, { pinned: true, muted: true });
|
|
169
|
+
let f = friends.readFriend(fp);
|
|
170
|
+
assert.equal(f.pinned, true);
|
|
171
|
+
assert.equal(f.muted, true);
|
|
172
|
+
friends.updateFriend(fp, { hidden: true });
|
|
173
|
+
assert.equal(friends.listFriends().length, 0, '软删后列表里看不到');
|
|
174
|
+
assert.equal(friends.listFriends({ includeHidden: true }).length, 1, '带 includeHidden 还能看到');
|
|
175
|
+
friends.updateFriend(fp, { hidden: false });
|
|
176
|
+
assert.equal(friends.listFriends().length, 1);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
ok('信任级别只认 stranger / trusted', () => {
|
|
180
|
+
const fp = 'fp_' + 'b'.repeat(32);
|
|
181
|
+
const bad = friends.updateFriend(fp, { trustLevel: 'god' });
|
|
182
|
+
assert.equal(bad.ok, false);
|
|
183
|
+
const good = friends.updateFriend(fp, { trustLevel: 'trusted' });
|
|
184
|
+
assert.ok(good.ok);
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
ok('未知字段拒绝(不许乱写)', () => {
|
|
188
|
+
const r = friends.updateFriend('fp_' + 'b'.repeat(32), { hacked: true });
|
|
189
|
+
assert.equal(r.ok, false);
|
|
190
|
+
assert.match(r.error, /不能改/);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
ok('好友独立隐私清单:数组、上限 200 条', () => {
|
|
194
|
+
const fp = 'fp_' + 'b'.repeat(32);
|
|
195
|
+
assert.ok(friends.updateFriend(fp, { privacyList: ['住址', '电话'] }).ok);
|
|
196
|
+
assert.deepEqual(friends.readFriend(fp).privacyList, ['住址', '电话']);
|
|
197
|
+
const many = Array.from({ length: 201 }, (_, i) => 'w' + i);
|
|
198
|
+
assert.equal(friends.updateFriend(fp, { privacyList: many }).ok, false);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
ok('软删保留备注和隐私清单(不是删数据)', () => {
|
|
202
|
+
const fp = 'fp_' + 'b'.repeat(32);
|
|
203
|
+
friends.updateFriend(fp, { alias: '留着的', hidden: true });
|
|
204
|
+
const f = friends.readFriend(fp);
|
|
205
|
+
assert.equal(f.alias, '留着的');
|
|
206
|
+
assert.deepEqual(f.privacyList, ['住址', '电话']);
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
ok('purge 才真删', () => {
|
|
210
|
+
const fp = 'fp_' + 'b'.repeat(32);
|
|
211
|
+
assert.ok(friends.purge(fp).ok);
|
|
212
|
+
assert.equal(friends.readFriend(fp).alias, '');
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
console.log('\n=== 安全解绑(规格 3.5)===');
|
|
216
|
+
|
|
217
|
+
resetHome();
|
|
218
|
+
forceIdentity();
|
|
219
|
+
forceConfig();
|
|
220
|
+
forceFriends({ ['fp_' + 'b'.repeat(32)]: { trustLevel: 'trusted', alias: '朋友', privacyList: ['x'] } });
|
|
221
|
+
|
|
222
|
+
ok('主动解绑:config 三字段清空、好友降回 stranger', () => {
|
|
223
|
+
const r = unpair.finishUnpair({ reason: '不聊了' });
|
|
224
|
+
assert.ok(r.ok);
|
|
225
|
+
const cfg = JSON.parse(fs.readFileSync(path.join(HOME, 'config.json'), 'utf8'));
|
|
226
|
+
assert.equal(cfg.peerFp, null);
|
|
227
|
+
assert.equal(cfg.peerSigningPub, null);
|
|
228
|
+
assert.equal(cfg.peerExchangePub, null);
|
|
229
|
+
assert.ok(cfg.unpairedAt, '要记解绑时间');
|
|
230
|
+
assert.equal(r.trustReset, true);
|
|
231
|
+
const f = friends.readFriend('fp_' + 'b'.repeat(32));
|
|
232
|
+
assert.equal(f.trustLevel, 'stranger');
|
|
233
|
+
assert.equal(f.paired, false);
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
ok('解绑保留历史/备注/隐私清单(只切断信任)', () => {
|
|
237
|
+
const f = friends.readFriend('fp_' + 'b'.repeat(32));
|
|
238
|
+
assert.equal(f.alias, '朋友');
|
|
239
|
+
assert.deepEqual(f.privacyList, ['x']);
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
ok('解绑后 mailbox 一条不少', () => {
|
|
243
|
+
const mb = path.join(HOME, 'mailbox.jsonl');
|
|
244
|
+
fs.writeFileSync(mb, JSON.stringify({ direction: 'in', message: { id: 'old-1', text: '老消息' } }) + '\n');
|
|
245
|
+
unpair.finishUnpair({ reason: '再解一次' });
|
|
246
|
+
const lines = fs.readFileSync(mb, 'utf8').split('\n').filter(Boolean);
|
|
247
|
+
assert.equal(lines.length, 1, '历史必须留着');
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
ok('重复解绑:没配对时明确报错', () => {
|
|
251
|
+
const r = unpair.finishUnpair();
|
|
252
|
+
assert.equal(r.ok, false);
|
|
253
|
+
assert.match(r.error, /没配对/);
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
resetHome();
|
|
257
|
+
forceIdentity();
|
|
258
|
+
forceConfig();
|
|
259
|
+
forceFriends({ ['fp_' + 'b'.repeat(32)]: { trustLevel: 'trusted' } });
|
|
260
|
+
|
|
261
|
+
ok('收到对方的 unpair:来源必须匹配当前配对', () => {
|
|
262
|
+
const r = unpair.handleInboundUnpair({ type: 'unpair', from: 'fp_' + 'c'.repeat(32), text: '解绑' });
|
|
263
|
+
assert.equal(r.ok, false);
|
|
264
|
+
assert.match(r.error, /不是当前配对对象/);
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
ok('收到对方的 unpair:来源匹配才处理', () => {
|
|
268
|
+
const r = unpair.handleInboundUnpair({ type: 'unpair', from: 'fp_' + 'b'.repeat(32), text: '解绑' });
|
|
269
|
+
assert.ok(r.ok);
|
|
270
|
+
assert.equal(r.unpairedBy, 'peer');
|
|
271
|
+
const cfg = JSON.parse(fs.readFileSync(path.join(HOME, 'config.json'), 'utf8'));
|
|
272
|
+
assert.equal(cfg.peerFp, null);
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
console.log('\n=== 消息撤回(规格 5.7)===');
|
|
276
|
+
|
|
277
|
+
resetHome();
|
|
278
|
+
function seedMailbox(rows) {
|
|
279
|
+
fs.writeFileSync(path.join(HOME, 'mailbox.jsonl'), rows.map((r) => JSON.stringify(r)).join('\n') + '\n');
|
|
280
|
+
}
|
|
281
|
+
function readMailbox() {
|
|
282
|
+
return fs
|
|
283
|
+
.readFileSync(path.join(HOME, 'mailbox.jsonl'), 'utf8')
|
|
284
|
+
.split('\n')
|
|
285
|
+
.filter(Boolean)
|
|
286
|
+
.map((l) => JSON.parse(l));
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
seedMailbox([
|
|
290
|
+
{ direction: 'out', message: { id: 'out-1', text: '我发的' } },
|
|
291
|
+
{ direction: 'in', message: { id: 'in-unread', text: '对方发的还没读' } },
|
|
292
|
+
{ direction: 'in', message: { id: 'in-read', text: '对方发的已读' }, readAt: '2026-11-27T12:00:00Z' },
|
|
293
|
+
]);
|
|
294
|
+
|
|
295
|
+
ok('只能撤自己发出去的', () => {
|
|
296
|
+
assert.equal(recall.canRecall('in-unread').ok, false);
|
|
297
|
+
assert.match(recall.canRecall('in-unread').detail, /自己发/);
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
ok('自己发的可以撤', () => {
|
|
301
|
+
assert.ok(recall.canRecall('out-1').ok);
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
ok('不存在的消息撤不了', () => {
|
|
305
|
+
assert.equal(recall.canRecall('ghost').ok, false);
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
ok('对方未读 → 撤成功,文本换成占位', () => {
|
|
309
|
+
const r = recall.applyInboundRecall({ type: 'recall', reply_to: 'in-unread' });
|
|
310
|
+
assert.ok(r.ok);
|
|
311
|
+
assert.equal(r.result, recall.RECALL_RESULT.RECALLED);
|
|
312
|
+
const mb = readMailbox();
|
|
313
|
+
const row = mb.find((x) => x.message.id === 'in-unread');
|
|
314
|
+
assert.equal(row.message.text, recall.RECALLED_TEXT);
|
|
315
|
+
assert.equal(row.recalled, true);
|
|
316
|
+
assert.equal(row.originalText, '对方发的还没读', '原文要留底');
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
ok('对方已读 → 拒绝撤回(这条是硬线)', () => {
|
|
320
|
+
const r = recall.applyInboundRecall({ type: 'recall', reply_to: 'in-read' });
|
|
321
|
+
assert.ok(r.ok);
|
|
322
|
+
assert.equal(r.result, recall.RECALL_RESULT.REFUSED);
|
|
323
|
+
const mb = readMailbox();
|
|
324
|
+
const row = mb.find((x) => x.message.id === 'in-read');
|
|
325
|
+
assert.equal(row.message.text, '对方发的已读', '已读的不许改');
|
|
326
|
+
assert.equal(row.recalled, undefined);
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
ok('找不到那条 → missing(不报错)', () => {
|
|
330
|
+
const r = recall.applyInboundRecall({ type: 'recall', reply_to: 'nope' });
|
|
331
|
+
assert.ok(r.ok);
|
|
332
|
+
assert.equal(r.result, recall.RECALL_RESULT.MISSING);
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
ok('撤回回执:撤成功要标记', () => {
|
|
336
|
+
recall.markRecallPending('out-1');
|
|
337
|
+
assert.ok(recall.canRecall('out-1').ok === false, '等回执期间不许再发');
|
|
338
|
+
recall.applyRecallReceipt({ type: 'recall', reply_to: 'out-1', text: 'recalled' });
|
|
339
|
+
const mb = readMailbox();
|
|
340
|
+
const row = mb.find((x) => x.message.id === 'out-1');
|
|
341
|
+
assert.equal(row.recalled, true);
|
|
342
|
+
assert.equal(row.recallResult, 'recalled');
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
ok('已撤过的不能再撤(要赶在改 mailbox 之前查)', () => {
|
|
346
|
+
const r = recall.canRecall('out-1');
|
|
347
|
+
assert.equal(r.ok, false);
|
|
348
|
+
assert.equal(r.code, 'already_recalled');
|
|
349
|
+
assert.match(r.detail, /已经撤过/);
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
ok('撤回回执:refused 不标撤回', () => {
|
|
353
|
+
seedMailbox([{ direction: 'out', message: { id: 'out-2', text: '第二条' } }]);
|
|
354
|
+
recall.markRecallPending('out-2');
|
|
355
|
+
recall.applyRecallReceipt({ type: 'recall', reply_to: 'out-2', text: 'refused' });
|
|
356
|
+
const row = readMailbox()[0];
|
|
357
|
+
assert.equal(row.recalled, undefined);
|
|
358
|
+
assert.equal(row.recallResult, 'refused');
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
console.log('\n=== 密钥导出 / 导入(规格 3.6)===');
|
|
362
|
+
|
|
363
|
+
resetHome();
|
|
364
|
+
const realFp = forceIdentity();
|
|
365
|
+
|
|
366
|
+
await okAsync('密码太短拒绝导出', async () => {
|
|
367
|
+
const r = backup.exportBackup({ password: '123' });
|
|
368
|
+
assert.equal(r.ok, false);
|
|
369
|
+
assert.match(r.error, /至少 8 位/);
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
await okAsync('常见弱密码拒绝', async () => {
|
|
373
|
+
assert.equal(backup.exportBackup({ password: '12345678' }).ok, false);
|
|
374
|
+
assert.equal(backup.exportBackup({ password: 'password123' }).ok, false);
|
|
375
|
+
assert.equal(backup.exportBackup({ password: 'aaaaaaaa' }).ok, false);
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
let goodBackup = null;
|
|
379
|
+
await okAsync('正常导出:结构齐全 + 有 sha256', async () => {
|
|
380
|
+
const r = backup.exportBackup({ password: 'a-good-password-9x' });
|
|
381
|
+
assert.ok(r.ok, r.error);
|
|
382
|
+
goodBackup = r.backup;
|
|
383
|
+
assert.equal(goodBackup.format, backup.BACKUP_FORMAT);
|
|
384
|
+
assert.equal(goodBackup.fp, realFp);
|
|
385
|
+
assert.ok(goodBackup.sha256);
|
|
386
|
+
assert.ok(goodBackup.ciphertext);
|
|
387
|
+
// 私钥不许以明文出现在备份里
|
|
388
|
+
assert.equal(JSON.stringify(goodBackup).includes('"y"'), false, '私钥不能在包里明文');
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
ok('导入前校验结构:正常包通过', () => {
|
|
392
|
+
const v = backup.verifyBackupShape(goodBackup);
|
|
393
|
+
assert.ok(v.ok, v.error);
|
|
394
|
+
assert.equal(v.fp, realFp);
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
ok('篡改密文 → SHA256 对不上,拒绝', () => {
|
|
398
|
+
const tampered = { ...goodBackup, ciphertext: 'AAAA' + goodBackup.ciphertext.slice(4) };
|
|
399
|
+
const v = backup.verifyBackupShape(tampered);
|
|
400
|
+
assert.equal(v.ok, false);
|
|
401
|
+
assert.match(v.error, /SHA256/);
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
ok('篡改 sha256 字段本身也拦不住——但密文被改时 GCM 还会再拦一道', () => {
|
|
405
|
+
// 改 sha256 后 shape 会过(因为重算对不上),但解密 GCM 认证会失败
|
|
406
|
+
const trick = { ...goodBackup, ciphertext: 'ZZZZ' };
|
|
407
|
+
const v = backup.verifyBackupShape(trick);
|
|
408
|
+
assert.equal(v.ok, false, 'SHA256 先拦');
|
|
409
|
+
});
|
|
410
|
+
|
|
411
|
+
ok('不是 fishpal 备份的拒绝', () => {
|
|
412
|
+
assert.equal(backup.verifyBackupShape({ format: 'other' }).ok, false);
|
|
413
|
+
assert.equal(backup.verifyBackupShape(null).ok, false);
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
ok('kdf 参数太弱的拒绝(防降级攻击)', () => {
|
|
417
|
+
const weak = { ...goodBackup, kdf: { ...goodBackup.kdf, n: 1024 } };
|
|
418
|
+
const v = backup.verifyBackupShape(weak);
|
|
419
|
+
assert.equal(v.ok, false);
|
|
420
|
+
assert.match(v.error, /kdf/);
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
ok('密码对了能解开,拿到完整身份', () => {
|
|
424
|
+
const r = backup.decryptBackup(goodBackup, 'a-good-password-9x');
|
|
425
|
+
assert.ok(r.ok, r.error);
|
|
426
|
+
assert.equal(r.identity.id, realFp);
|
|
427
|
+
assert.ok(r.identity.signing.privateKey);
|
|
428
|
+
assert.ok(r.identity.exchange.privateKey);
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
ok('密码错了解不开(且不说破是密码错还是包坏)', () => {
|
|
432
|
+
const r = backup.decryptBackup(goodBackup, 'wrong-password-here');
|
|
433
|
+
assert.equal(r.ok, false);
|
|
434
|
+
assert.match(r.error, /密码不对,或者包被改过/);
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
ok('导入落盘前先备份旧身份', () => {
|
|
438
|
+
const before = fs.readFileSync(path.join(HOME, 'identity.json'), 'utf8');
|
|
439
|
+
const r = backup.installIdentity({ id: 'fp_' + 'z'.repeat(32), signing: { privateKey: 'n' }, exchange: { privateKey: 'm' } });
|
|
440
|
+
assert.ok(r.ok);
|
|
441
|
+
assert.ok(r.backedUpOldPath, '要留旧身份备份');
|
|
442
|
+
assert.equal(fs.readFileSync(r.backedUpOldPath, 'utf8'), before);
|
|
443
|
+
const now = JSON.parse(fs.readFileSync(path.join(HOME, 'identity.json'), 'utf8'));
|
|
444
|
+
assert.equal(now.id, 'fp_' + 'z'.repeat(32));
|
|
445
|
+
});
|
|
446
|
+
|
|
447
|
+
ok('installIdentity 同步更新 config.meFp', () => {
|
|
448
|
+
forceConfig({ meFp: 'fp_' + 'a'.repeat(32) });
|
|
449
|
+
backup.installIdentity({ id: 'fp_' + 'q'.repeat(32), signing: { privateKey: 'n' }, exchange: { privateKey: 'm' } });
|
|
450
|
+
const cfg = JSON.parse(fs.readFileSync(path.join(HOME, 'config.json'), 'utf8'));
|
|
451
|
+
assert.equal(cfg.meFp, 'fp_' + 'q'.repeat(32));
|
|
452
|
+
});
|
|
453
|
+
|
|
454
|
+
console.log('\n────────────────────────────');
|
|
455
|
+
console.log(`结果:${pass} 通过,${fail} 失败`);
|
|
456
|
+
if (fail) {
|
|
457
|
+
console.log('\n失败明细:');
|
|
458
|
+
for (const f of failures) console.log(` · ${f.name}\n ${f.error}`);
|
|
459
|
+
process.exitCode = 1;
|
|
460
|
+
}
|