aboutsummaryrefslogtreecommitdiffstats
path: root/packages/meshbay-hub/tests/harness/network_change_harness.mjs
blob: e0201125a480e8844b06975f4ac878457b7386c3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Run the SHIPPED transport through a change of network, and see whether the
// connection follows.
//
// A phone moving between mobile data and the Wi-Fi the node is on keeps a
// connection whose candidate pair runs over the network it left. Reported on
// the Android application: the group stopped answering and the application had
// to be killed. transport.js is executed whole; what is modelled is the
// environment: a clock, the window's events, `navigator.connection`, a peer
// connection that opens its channel after the answer, a hub that answers the
// offer and the token (or does not, while the device is offline), and a node
// that answers the handshake and pings until it is cut off.
//
// Usage: node network_change_harness.mjs <transport sources> <json>
//   {"scenario": "change-dead" | "change-alive" | "backoff" | "ice-dead" | "ice-alive"
//                | "events" | "closed"}
import { readFileSync } from 'fs';
import { delimiter } from 'path';

const transportSrc = process.argv[2].split(delimiter)
  .map((p) => readFileSync(p, 'utf8')).join('\n');
const { scenario } = JSON.parse(process.argv[3] || '{}');

for (const level of ['log', 'warn', 'error', 'info', 'debug']) {
  console[level] = (...args) => process.stderr.write(args.join(' ') + '\n');
}
process.on('unhandledRejection', () => {});

// ── The clock ────────────────────────────────────────────────────────────────

let now = 0;
let nextId = 1;
let timers = [];
globalThis.setTimeout = (fn, ms) => {
  const t = { at: now + (ms || 0), fn, id: nextId++ };
  timers.push(t);
  return t.id;
};
globalThis.setInterval = (fn, ms) => {
  const t = { at: now + ms, fn, id: nextId++, every: ms };
  timers.push(t);
  return t.id;
};
globalThis.clearTimeout = (id) => { timers = timers.filter((t) => t.id !== id); };
globalThis.clearInterval = globalThis.clearTimeout;
Date.now = () => now;
globalThis.performance = { now: () => now };
const flush = async () => { for (let i = 0; i < 50; i++) await Promise.resolve(); };
async function run(until) {
  await flush();
  while (timers.length) {
    const due = timers.reduce((a, b) => (b.at < a.at ? b : a));
    if (due.at > until) break;
    if (due.every) due.at += due.every;
    else timers = timers.filter((t) => t !== due);
    now = Math.max(now, due.every ? due.at - due.every : due.at);
    due.fn();
    await flush();
  }
  now = until;
}

// ── The page around the transport ────────────────────────────────────────────

function eventTarget(obj) {
  const listeners = new Map();
  obj.addEventListener = (type, fn) => {
    if (!listeners.has(type)) listeners.set(type, new Set());
    listeners.get(type).add(fn);
  };
  obj.removeEventListener = (type, fn) => { listeners.get(type)?.delete(fn); };
  obj.dispatch = (type) => { for (const fn of [...(listeners.get(type) || [])]) fn({ type }); };
  obj.listening = () => [...listeners.values()].reduce((n, s) => n + s.size, 0);
  return obj;
}

globalThis.window = globalThis;
eventTarget(globalThis);
const connection = eventTarget({ type: 'cellular', effectiveType: '4g' });
Object.defineProperty(globalThis, 'navigator',
  { value: { userAgent: 'harness', connection }, configurable: true });
globalThis.location = { hash: '' };
globalThis.document = {
  addEventListener() {}, removeEventListener() {}, visibilityState: 'visible',
};
globalThis.localStorage = { getItem: () => null, setItem() {}, removeItem() {} };

const b64 = (u8) => Buffer.from(u8).toString('base64');
window.MeshBayCrypto = {
  b64encode: (u8) => b64(u8),
  b64decode: (s) => new Uint8Array(Buffer.from(String(s), 'base64')),
  verifyNodeSignature: async () => true,
  challengeTranscript: () => new Uint8Array(1),
  webrtcBinding: () => new Uint8Array(1),
  handshakeProof: async () => new Uint8Array([7]),
  handshakeTranscript: () => new Uint8Array(1),
  constantTimeEqual: () => true,
  openGroup: async () => window.__codec.msgpack_encode({ chat_epoch: 1 }),
};

const SDP = 'v=0\r\na=fingerprint:sha-256 AA:BB\r\n';
let offline = false;
globalThis.fetch = async (url) => {
  if (offline) throw new TypeError('Failed to fetch');
  return {
    ok: true, status: 200, headers: { get: () => null },
    json: async () => (url.endsWith('/mnp-token') ? { mnp_token: 't' } : { sdp: SDP }),
  };
};

// ── The node ─────────────────────────────────────────────────────────────────

let pings = 0;
const pcs = [];

class NodeSession {
  constructor(channel) { this.channel = channel; this.cut = false; }
  reply(obj, ms) {
    setTimeout(() => {
      if (this.cut || this.channel.readyState !== 'open') return;
      const body = window.__codec.msgpack_encode(obj);
      const frame = new Uint8Array(4 + body.byteLength);
      new DataView(frame.buffer).setUint32(0, body.byteLength, false);
      frame.set(body, 4);
      this.channel.onmessage({ data: frame.buffer });
    }, ms);
  }
  receive(frame) {
    if (this.cut) return;
    const msg = window.__codec.msgpack_decode(frame.slice(4));
    const req_id = msg.req_id;
    switch (msg.type) {
      case 'handshake':
        return this.reply({ type: 'handshake_challenge', v: '99.0', v_min: '0.0',
          nonce: b64(new Uint8Array(32)), node_pk: 'pk', sig: 's', req_id }, 50);
      case 'handshake_response':
        return this.reply({ type: 'handshake_ack', proof: 'AA==', node_pk: 'pk', sig: 's',
          nonce: 'n', ct: 'c', req_id }, 50);
      case 'device_hello':
        return this.reply({ type: 'device_hello_ack', req_id }, 50);
      case 'ping':
        pings += 1;
        return this.reply({ type: 'pong', token: msg.token, req_id }, 30);
      default:
        return undefined;
    }
  }
}

class FakeChannel {
  constructor() { this.readyState = 'connecting'; this.binaryType = 'arraybuffer'; }
  send(frame) {
    if (this.readyState !== 'open') throw new Error('InvalidStateError');
    this.session.receive(frame);
  }
  close() {
    if (this.readyState === 'closed') return;
    this.readyState = 'closed';
    if (this.onclose) this.onclose({});
  }
}

class FakePC {
  constructor() {
    this.connectionState = 'new';
    this.iceConnectionState = 'new';
    this.iceGatheringState = 'complete';
    pcs.push(this);
  }
  createDataChannel() { this.channel = new FakeChannel(); return this.channel; }
  async createOffer() { return { type: 'offer', sdp: SDP }; }
  async setLocalDescription(d) { this.localDescription = d; }
  async getStats() { return new Map(); }
  async setRemoteDescription() {
    setTimeout(() => {
      if (this.connectionState === 'closed') return;
      this.channel.session = new NodeSession(this.channel);
      this.channel.readyState = 'open';
      this.connectionState = 'connected';
      this.iceConnectionState = 'connected';
      if (this.channel.onopen) this.channel.onopen();
    }, 100);
  }
  close() {
    this.connectionState = 'closed';
    if (this.channel) this.channel.close();
  }
}
globalThis.RTCPeerConnection = FakePC;

// ── The shipped transport ────────────────────────────────────────────────────

new Function(`${transportSrc}\nwindow.__codec = { msgpack_encode, msgpack_decode };`)();
const Transport = window.MeshBayTransport;
// What the transport's own module listens to, before any connection exists.
const moduleListeners = window.listening() + connection.listening();
const out = {};

if (scenario === 'events') {
  // The helper alone: which events count as another network, and how many.
  let calls = 0;
  const off = Transport.onNetworkChange(() => { calls += 1; });
  const at = [];
  const step = async (fire) => { fire(); await run(now + 5000); at.push(calls); };
  await step(() => connection.dispatch('change'));                  // a bandwidth estimate
  await step(() => { connection.type = 'wifi'; connection.dispatch('change'); });
  await step(() => window.dispatch('online'));
  await step(() => window.dispatch('meshbay-default-network'));
  await step(() => {                                                 // one change, three events
    connection.type = 'cellular'; connection.dispatch('change');
    window.dispatch('online');
    window.dispatch('meshbay-default-network');
  });
  off();
  await step(() => window.dispatch('online'));
  out.calls = at;
  out.leftListening = window.listening() + connection.listening() - moduleListeners;
} else {
  const main = new Transport('https://hub.invalid', 'token');
  let reconnectedAt = null;
  main.addReconnectListener(() => { if (reconnectedAt === null) reconnectedAt = now; });
  let connected = false;
  const identity = { pkEdB64: 'ed', signAs: async () => 'sig', native: false };
  main.connect('node', 'token', 'group', new Uint8Array(32), identity, null, 'u', 'uid',
               undefined, undefined, '', 'pk')
    .then(() => { connected = true; }, (err) => { out.connectError = err.message; });
  await run(5000);
  out.connected = connected;

  const first = pcs[0];
  if (scenario === 'change-dead' || scenario === 'change-alive') {
    // Mobile data to Wi-Fi. The pair in use ran over mobile data; when it is
    // gone, the node hears nothing more on it.
    if (scenario === 'change-dead') first.channel.session.cut = true;
    connection.type = 'wifi';
    connection.dispatch('change');
    await run(25000);
  } else if (scenario === 'ice-dead' || scenario === 'ice-alive') {
    if (scenario === 'ice-dead') first.channel.session.cut = true;
    first.iceConnectionState = 'disconnected';
    first.oniceconnectionstatechange();
    await run(25000);
  } else if (scenario === 'backoff') {
    // The connection fails while the device has no network, and every attempt
    // fails with it until the backoff has grown long. Then Wi-Fi comes back.
    offline = true;
    first.channel.session.cut = true;
    first.connectionState = 'failed';
    first.onconnectionstatechange();
    await run(40000);
    offline = false;
    connection.type = 'wifi';
    connection.dispatch('change');
    out.backAt = now;
    await run(80000);
  } else if (scenario === 'closed') {
    main.close();
    connection.type = 'wifi';
    connection.dispatch('change');
    await run(25000);
    out.leftListening = window.listening() + connection.listening() - moduleListeners;
  }
  out.reconnectedAt = reconnectedAt;
  out.connections = pcs.length;
  out.pings = pings;
}

process.stdout.write(JSON.stringify(out));