Skip to content

All your computers and your phone/iPhone, working as one desk. Screens, apps, files, clipboard, calls, texts, notifications, mouse and keyboard, shared between Mac, Windows, Linux , Android, Web and ios / ipados . Free and open source. No account, no cloud, and no shared network needed.

3.9 KB
Raw Blame
// TwinDesk — Copyright (C) 2026 inukaasith. GPL-3.0, see LICENSE.
// Apple's developer disk image from a Mac of the mesh (shared/ios-ddi.js): a Mac with Xcode sends its
// copy over the session in parts, the other computer keeps it (the same bytes, the same folders) and
// uses it from then on; a Mac without one says so; a path out of the folder spoils the whole copy.
// Run with: node --test shared/test/ios-ddi.test.js
'use strict';

const assert = require('assert');
const fs = require('fs');
const os = require('os');
const path = require('path');
const { test } = require('node:test');

const P = require('../protocol.js');
const { IosDdi } = require('../ios-ddi.js');

const T = P.T;

function mesh() {
  const nodes = {};
  const make = (id, os_) => {
    const handlers = new Map();
    const bus = {
      on: (t, fn) => handlers.set(t, fn),
      send: (to, m) => { const h = nodes[to] && nodes[to].handlers.get(m.t); if (h) setImmediate(() => h(JSON.parse(JSON.stringify(m)), id)); return true; },
    };
    const sessions = { list: () => Object.keys(nodes).filter((x) => x !== id).map((x) => ({ id: x, connected: true, peerOs: nodes[x].os, transport: 'ip' })) };
    nodes[id] = { handlers, os: os_ };
    return { bus, sessions };
  };
  return { make, nodes };
}

const tmp = () => fs.mkdtempSync(path.join(os.tmpdir(), 'td-ddi-'));

test('a Mac with Xcode sends its copy; it is kept, as it is, and used from then on', async () => {
  const xcode = tmp();
  fs.mkdirSync(path.join(xcode, 'Firmware'));
  fs.writeFileSync(path.join(xcode, 'BuildManifest.plist'), 'manifest');
  fs.writeFileSync(path.join(xcode, 'big.dmg'), Buffer.alloc(200 * 1024, 7)); // several parts
  fs.writeFileSync(path.join(xcode, 'Firmware', 'big.dmg.trustcache'), 'tc');
  const { make } = mesh();
  const macHub = make('mac', P.OS.MAC);
  const linHub = make('nitro', P.OS.LINUX);
  const keep = path.join(tmp(), 'ios-ddi');
  const lin = new IosDdi({ hub: linHub, dir: keep, xcode: () => '' });
  // eslint-disable-next-line no-new
  new IosDdi({ hub: macHub, dir: tmp(), xcode: () => xcode });
  lin._fromMirror = async () => assert.fail('not the mirror');
  const dir = await lin.ensure();
  assert.strictEqual(dir, keep);
  assert.strictEqual(fs.readFileSync(path.join(keep, 'BuildManifest.plist'), 'utf8'), 'manifest');
  assert.deepStrictEqual(fs.readFileSync(path.join(keep, 'big.dmg')), Buffer.alloc(200 * 1024, 7));
  assert.strictEqual(fs.readFileSync(path.join(keep, 'Firmware', 'big.dmg.trustcache'), 'utf8'), 'tc');
  lin._macs = () => assert.fail('asked again');
  assert.strictEqual(await lin.ensure(), keep, 'kept: not asked again');
});

test('a Mac without Xcode says so; then the mirror', async () => {
  const { make } = mesh();
  const macHub = make('mac', P.OS.MAC);
  const linHub = make('nitro', P.OS.LINUX);
  // eslint-disable-next-line no-new
  new IosDdi({ hub: macHub, dir: tmp(), xcode: () => '' });
  const lin = new IosDdi({ hub: linHub, dir: path.join(tmp(), 'ios-ddi'), xcode: () => '' });
  let mirror = 0;
  lin._fromMirror = async () => { mirror++; return false; };
  assert.strictEqual(await lin.ensure(), '');
  assert.strictEqual(mirror, 1);
});

test('a part with a path out of the folder spoils the copy', async () => {
  const { make, nodes } = mesh();
  make('mac', P.OS.MAC);
  const linHub = make('nitro', P.OS.LINUX);
  const keep = path.join(tmp(), 'ios-ddi');
  const lin = new IosDdi({ hub: linHub, dir: keep, xcode: () => '' });
  lin._fromMirror = async () => false;
  nodes.mac.handlers.set(T.IOS_DDI_WANT, (m) => {
    const h = nodes.nitro.handlers;
    h.get(T.IOS_DDI_PART)({ id: m.id, path: '../escape', off: 0, data: Buffer.from('x').toString('base64') }, 'mac');
    h.get(T.IOS_DDI_DONE)({ id: m.id, files: [{ path: '../escape', size: 1 }] }, 'mac');
  });
  assert.strictEqual(await lin.ensure(), '');
  assert.ok(!fs.existsSync(path.join(path.dirname(keep), 'escape')));
  assert.ok(!fs.existsSync(keep));
});