Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

15.3 KB
Raw Blame
import 'dart:convert';
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:harness/api/api_client.dart';
import 'package:harness/auth/auth_session.dart';
import 'package:harness/core/config.dart';
import 'package:harness/ws/daemon_pipe_record.dart' show defaultDaemonDataDir;
import 'package:harness/ws/local_cli_discovery.dart';
import 'package:harness/ws/local_daemon_transport.dart';
import 'package:harness/ws/ws_conn.dart';

const _computerId = '0123456789abcdef0123456789abcdef';

/// "No socket can exist" is the macOS and Linux data folder too long for one. Windows has no such
/// case: every local route goes over the daemon's proven pipe, and the credential is never shown to
/// a port (`LocalDaemonTransport.tcpHeaders`, CLI `localCredentialHeaders`, audit W1).
final Object _portCredentialSkip = Platform.isWindows
    ? 'macOS and Linux only: Windows reaches the daemon over its pipe and never sends the credential to a port'
    : false;

/// A stand-in daemon: `/api/status`, `/api/auth/me` and the local WebSocket,
/// on whatever [HttpServer] it is given, counting what reached it.
class _Daemon {
  _Daemon(this.server, this.name) {
    server.listen((request) async {
      hits.add(request.uri.path);
      credentials.add(request.headers.value(daemonCredentialHeader));
      if (WebSocketTransformer.isUpgradeRequest(request)) {
        final ws = await WebSocketTransformer.upgrade(request);
        ws.listen((data) {
          final frame = jsonDecode(data as String) as Map<String, dynamic>;
          if (frame['type'] == 'machine_select') {
            selects++;
            ws.add(jsonEncode({'type': 'connected', 'payload': frame['payload']}));
          }
        });
        return;
      }
      request.response.headers.contentType = ContentType.json;
      if (request.uri.path == '/api/status') {
        request.response.write(
          jsonEncode({
            'computerId': _computerId,
            'localWs': {
              'path': '/api/local-ws',
              'protocolVersion': 1,
              'terminalProtocolVersion': 3,
              'e2ee': false,
            },
          }),
        );
      } else {
        request.response.write(
          jsonEncode({
            'success': true,
            'data': {'via': name},
          }),
        );
      }
      await request.response.close();
    });
  }

  final HttpServer server;
  final String name;
  final List<String> hits = [];
  final List<String?> credentials = [];
  int selects = 0;

  static Future<_Daemon> tcp() async =>
      _Daemon(await HttpServer.bind(InternetAddress.loopbackIPv4, 0), 'tcp');

  static Future<_Daemon> unix(String path) async => _Daemon(
    await HttpServer.bind(InternetAddress(path, type: InternetAddressType.unix), 0),
    'unix',
  );

  Future<void> close() => server.close(force: true);
}

void main() {
  late Directory scratch;
  late String socketPath;
  late File identityFile;
  final daemons = <_Daemon>[];

  setUp(() async {
    // Socket paths are capped near 104 bytes; the default temp dir is longer.
    scratch = await (Platform.isWindows
            ? Directory.systemTemp
            : Directory('/tmp'))
        .createTemp('hsock-');
    socketPath = '${scratch.path}/daemon.sock';
    identityFile = File('${scratch.path}/computer-id')
      ..writeAsStringSync(_computerId);
  });

  tearDown(() async {
    for (final daemon in daemons.reversed) {
      await daemon.close();
    }
    daemons.clear();
    if (await scratch.exists()) await scratch.delete(recursive: true);
  });

  Future<_Daemon> start(Future<_Daemon> daemon) async {
    final started = await daemon;
    daemons.add(started);
    return started;
  }

  AppConfig configFor(_Daemon tcp) => AppConfig(
    apiBaseUrl: 'http://unused.invalid',
    localCliBaseUrl: 'http://127.0.0.1:${tcp.server.port}',
  );

  group('defaultDaemonSocketPath', () {
    test('uses USERPROFILE when a Windows shell also sets HOME', () {
      expect(
        defaultDaemonDataDir(
          environment: {'USERPROFILE': r'C:\Users\me', 'HOME': '/c/Users/me'},
          isWindows: true,
        ),
        r'C:\Users\me\.harness\cli\data',
      );
    });

    test('is the socket for the daemon\'s port in the CLI data directory', () {
      expect(
        defaultDaemonSocketPath(18473, environment: {'HOME': '/Users/me'}, isWindows: false),
        '/Users/me/.harness/cli/data/daemon-18473.sock',
      );
      // Another port, another daemon, another socket.
      expect(
        defaultDaemonSocketPath(
          18474,
          environment: {'HOME': '/Users/me', 'ADAPTER_DATA_DIR': '/srv/h'},
          isWindows: false,
        ),
        '/srv/h/daemon-18474.sock',
      );
      expect(defaultDaemonSocketPath(18473, environment: {'HOME': '/Users/me'}, isWindows: true), isNull);
      // Too long for a socket: under the home folder, by the folder's hash, the
      // same hash as the CLI's (pinned for `/x` in cli localSocket.spec.ts).
      expect(
        defaultDaemonSocketPath(
          18473,
          environment: {'HOME': '/h', 'ADAPTER_DATA_DIR': '/${'x' * 100}'},
          isWindows: false,
        ),
        matches(RegExp(r'^/h/\.harness/s/[0-9a-f]{16}/daemon-18473\.sock$')),
      );
      expect(
        defaultDaemonSocketPath(18473, environment: {'HOME': '/${'x' * 100}'}, isWindows: false),
        isNull,
      );
    });

    test('a long folder\'s socket matches the CLI\'s hash', () {
      // sha256('/' + 'x' * 100)[0..16], as cli/src/lib/localSocket.ts computes it.
      expect(
        defaultDaemonSocketPath(
          1,
          environment: {'HOME': '/h', 'ADAPTER_DATA_DIR': '/${'x' * 100}'},
          isWindows: false,
        ),
        '/h/.harness/s/e50e7828cbafcbee/daemon-1.sock',
      );
    });

    test('a long folder with no HOME still has a socket to fail on, so the port is not allowed', () {
      final path = defaultDaemonSocketPath(
        1,
        environment: {'ADAPTER_DATA_DIR': '/${'x' * 100}'},
        isWindows: false,
      );
      expect(path, isNotNull);
      expect(File(path!).existsSync(), isFalse);
    });

    test('is never looked for under flutter test', () {
      expect(
        LocalDaemonTransport.detect(Uri.parse('http://127.0.0.1:18473')).candidate,
        isNull,
      );
    });
  });

  group('local credential', () {
    final credential = 'A' * 42 + '_';

    test('is the hook-credential file in the daemon data directory', () {
      final env = {'ADAPTER_DATA_DIR': scratch.path};
      expect(readDaemonCredential(environment: env), isNull);
      File('${scratch.path}/hook-credential').writeAsStringSync('$credential\n');
      expect(readDaemonCredential(environment: env), credential);
      // Anything that is not a credential the CLI made is not sent.
      File('${scratch.path}/hook-credential').writeAsStringSync('short');
      expect(readDaemonCredential(environment: env), isNull);
    });

    test('is never read under flutter test', () {
      expect(
        LocalDaemonTransport.detect(Uri.parse('http://127.0.0.1:18473')).tcpHeaders(),
        isEmpty,
      );
    });

    test('never goes down the socket, and never to the port once it is gone', () async {
      final tcp = await start(_Daemon.tcp());
      final unix = await start(_Daemon.unix(socketPath));
      final transport = LocalDaemonTransport(
        candidate: socketPath,
        credential: () => credential,
      )..useSocket();
      final api = ApiClient(
        config: configFor(tcp),
        session: AuthSession(),
        localTransport: transport,
      );

      expect(await api.me(), {'via': 'unix'});
      expect(unix.credentials, [null]);

      await unix.close();
      daemons.remove(unix);
      if (File(socketPath).existsSync()) File(socketPath).deleteSync();
      // The port's listener stands for another account that took it (audit H1).
      await expectLater(api.me(), throwsA(anything));
      expect(tcp.hits, isEmpty);
      expect(tcp.credentials, isEmpty);
    });

    test('goes with REST over the port where no socket can exist', () async {
      final tcp = await start(_Daemon.tcp());
      final transport = LocalDaemonTransport(credential: () => credential);
      final api = ApiClient(
        config: configFor(tcp),
        session: AuthSession(),
        localTransport: transport,
      );

      expect(await api.me(), {'via': 'tcp'});
      expect(tcp.credentials.last, credential);
    }, skip: _portCredentialSkip);

    test('is not offered to a port while a socket can exist', () {
      final transport = LocalDaemonTransport(
        candidate: socketPath,
        credential: () => credential,
      );
      expect(transport.portAllowed, isFalse);
      expect(transport.tcpHeaders(), isEmpty);
    });

    test('goes with the local WebSocket over the port where no socket can exist', () async {
      final tcp = await start(_Daemon.tcp());
      final transport = LocalDaemonTransport(credential: () => credential);
      final conn = WsConn(
        wsBaseUrl: 'wss://unused.example',
        autonomousEnv: 'prod',
        machineId: 'm1',
        accessTokenProvider: (_, _) async => '',
        onAuthFailure: (_) {},
        onEvent: (_) {},
        onStatus: (_) {},
        transportKind: WsTransportKind.localPlaintext,
        localWsUri: Uri.parse('ws://127.0.0.1:${tcp.server.port}/api/local-ws'),
        localTransport: transport,
      );
      addTearDown(conn.close);
      await conn.connect();
      for (var i = 0; i < 100 && tcp.selects == 0; i++) {
        await Future<void>.delayed(const Duration(milliseconds: 10));
      }

      expect(tcp.selects, 1);
      expect(tcp.credentials, [credential]);
    }, skip: _portCredentialSkip);
  });

  group('discovery', () {
    test('asks the socket first and keeps the port untouched', () async {
      final tcp = await start(_Daemon.tcp());
      await start(_Daemon.unix(socketPath));
      final transport = LocalDaemonTransport(candidate: socketPath);
      final endpoint = await LocalCliDiscovery(
        config: configFor(tcp),
        identity: LocalMachineIdentity(computerIdFile: identityFile),
        transport: transport,
      ).discover();

      expect(endpoint?.socketPath, socketPath);
      expect(transport.socketPath, socketPath);
      // The WebSocket keeps its loopback name, so the pool's key is unchanged.
      expect(endpoint?.wsUri.toString(), 'ws://127.0.0.1:${tcp.server.port}/api/local-ws');
      expect(tcp.hits, isEmpty);
    });

    test('finds no daemon, and asks no port, when the socket is gone', () async {
      // The port's listener stands for another account: after a reboot it holds the
      // port until this user logs in, and could answer anything (audit H1).
      final tcp = await start(_Daemon.tcp());
      final transport = LocalDaemonTransport(candidate: socketPath)..useSocket();
      final endpoint = await LocalCliDiscovery(
        config: configFor(tcp),
        identity: LocalMachineIdentity(computerIdFile: identityFile),
        transport: transport,
      ).discover();

      expect(endpoint, isNull);
      expect(transport.socketPath, isNull);
      expect(tcp.hits, isEmpty);
    });

    test('asks the port where no socket can exist', () async {
      final tcp = await start(_Daemon.tcp());
      final transport = LocalDaemonTransport();
      final endpoint = await LocalCliDiscovery(
        config: configFor(tcp),
        identity: LocalMachineIdentity(computerIdFile: identityFile),
        transport: transport,
      ).discover();

      expect(endpoint, isNotNull);
      expect(endpoint!.socketPath, isNull);
      expect(tcp.hits, ['/api/status']);
    });
  });

  group('REST', () {
    test('goes over the socket, and fails as a daemon that is down once it is gone', () async {
      final tcp = await start(_Daemon.tcp());
      final unix = await start(_Daemon.unix(socketPath));
      final transport = LocalDaemonTransport(candidate: socketPath)..useSocket();
      final api = ApiClient(
        config: configFor(tcp),
        session: AuthSession(),
        localTransport: transport,
      );

      expect(await api.me(), {'via': 'unix'});
      expect(unix.hits, ['/api/auth/me']);

      await unix.close();
      daemons.remove(unix);
      // Closing the server removes its socket file; make sure nothing is left to dial.
      if (File(socketPath).existsSync()) File(socketPath).deleteSync();
      await expectLater(api.me(), throwsA(anything));
      expect(tcp.hits, isEmpty);
      expect(transport.socketPath, isNull);
    });

    test('never sends a request for another address down the socket', () async {
      final tcp = await start(_Daemon.tcp());
      final other = await start(_Daemon.tcp());
      final unix = await start(_Daemon.unix(socketPath));
      final transport = LocalDaemonTransport(candidate: socketPath)..useSocket();
      final dio = Dio()
        ..httpClientAdapter = LocalDaemonHttpAdapter(
          transport,
          daemonBase: Uri.parse('http://127.0.0.1:${tcp.server.port}'),
        );

      final response = await dio.get<Map<String, dynamic>>(
        'http://127.0.0.1:${other.server.port}/api/elsewhere',
      );
      expect(response.data?['data'], {'via': 'tcp'});
      expect(other.hits, ['/api/elsewhere']);
      expect(unix.hits, isEmpty);
    });
  });

  group('local WebSocket', () {
    WsConn connFor(_Daemon tcp, LocalDaemonTransport transport) => WsConn(
      wsBaseUrl: 'wss://unused.example',
      autonomousEnv: 'prod',
      machineId: 'm1',
      accessTokenProvider: (_, _) async => '',
      onAuthFailure: (_) {},
      onEvent: (_) {},
      onStatus: (_) {},
      transportKind: WsTransportKind.localPlaintext,
      localWsUri: Uri.parse('ws://127.0.0.1:${tcp.server.port}/api/local-ws'),
      localTransport: transport,
    );

    Future<void> until(bool Function() done) async {
      for (var i = 0; i < 100 && !done(); i++) {
        await Future<void>.delayed(const Duration(milliseconds: 10));
      }
    }

    test('dials the socket when discovery chose it', () async {
      final tcp = await start(_Daemon.tcp());
      final unix = await start(_Daemon.unix(socketPath));
      final conn = connFor(tcp, LocalDaemonTransport(candidate: socketPath)..useSocket());
      addTearDown(conn.close);
      await conn.connect();
      await until(() => unix.selects > 0);

      expect(unix.selects, 1);
      expect(tcp.selects, 0);
      expect(unix.hits, ['/api/local-ws']);
    });

    test('goes back to the socket once it is there again', () async {
      // A dial during a daemon restart fails and drops the transport to the
      // port; the next connect must still find the socket the new daemon made.
      final tcp = await start(_Daemon.tcp());
      final unix = await start(_Daemon.unix(socketPath));
      final transport = LocalDaemonTransport(candidate: socketPath)..useTcp();
      final conn = connFor(tcp, transport);
      addTearDown(conn.close);
      await conn.connect();
      await until(() => unix.selects > 0);

      expect(unix.selects, 1);
      expect(tcp.selects, 0);
    });

    test('never dials the port when the socket is not there', () async {
      final tcp = await start(_Daemon.tcp());
      final transport = LocalDaemonTransport(candidate: socketPath)..useSocket();
      final conn = connFor(tcp, transport);
      addTearDown(conn.close);
      await conn.connect();
      await Future<void>.delayed(const Duration(milliseconds: 200));

      expect(tcp.selects, 0);
      expect(transport.socketPath, isNull);
    });
  });
}