arblant
firefox-vpn-client / app / test / core_api_test.dart
core_api_test.dart169 lines · 5.5 KB
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
import 'dart:convert';
import 'dart:io';

import 'package:flutter_test/flutter_test.dart';
import 'package:firefox_vpn/core/core_api.dart';

void main() {
  group('CoreApi', () {
    late HttpServer server;
    late CoreApi api;
    final seen = <String, Object?>{};

    setUp(() async {
      seen.clear();
      // A local fixture: unknown requests fail and nothing leaves loopback.
      server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0);
      server.listen((req) async {
        final body = await utf8.decoder.bind(req).join();
        seen['token'] = req.headers.value('X-Local-Token');
        seen['type'] = req.headers.contentType?.mimeType;
        seen['body'] = body;
        // As the core's guard: a state change must be JSON.
        final json = req.headers.contentType?.mimeType == 'application/json';
        final (status, out) = switch ((req.method, req.uri.path)) {
          (final m, _) when m != 'GET' && !json => (
            415,
            {'error': 'requests must be JSON'},
          ),
          ('GET', '/api/status') => (
            200,
            {
              'state': {'phase': 'running', 'message': 'ok'},
              'system_proxy': 'set',
              'accounts': {'total': 2, 'ready': 1},
            },
          ),
          ('POST', '/api/system-proxy') => (200, {'mode': 'clear'}),
          ('DELETE', '/api/accounts/a') => (200, {}),
          ('DELETE', '/api/signin/s') => (200, {}),
          ('POST', '/api/test') => (409, {'error': 'the proxy is not running'}),
          _ => (404, {'error': 'not found'}),
        };
        req.response
          ..statusCode = status
          ..headers.contentType = ContentType.json
          ..write(jsonEncode(out));
        await req.response.close();
      });
      api = CoreApi(
        CoreEndpoint(
          Uri.parse('http://127.0.0.1:${server.port}'),
          'secret-token',
        ),
      );
    });

    tearDown(() async {
      api.close();
      await server.close(force: true);
    });

    test('sends the local token and parses the status', () async {
      final s = await api.status();
      expect(seen['token'], 'secret-token');
      expect(s.systemProxySet, isTrue);
      expect(s.readyAccounts, 1);
      expect(s.totalAccounts, 2);
    });

    test('sends JSON with a DELETE too, which the core requires', () async {
      await api.removeAccount('a');
      expect(seen['type'], 'application/json');
      await api.cancelSignIn('s');
      expect(seen['type'], 'application/json');
    });

    test('posts JSON', () async {
      await api.setSystemProxy('clear');
      expect(seen['type'], 'application/json');
      expect(jsonDecode(seen['body'] as String), {'mode': 'clear'});
    });

    test('turns error answers into CoreApiException', () async {
      await expectLater(
        api.test(),
        throwsA(
          isA<CoreApiException>()
              .having((e) => e.statusCode, 'status', 409)
              .having((e) => e.message, 'message', 'the proxy is not running'),
        ),
      );
    });

    test('reports a missing core as CoreUnavailable', () async {
      await server.close(force: true);
      await expectLater(api.status(), throwsA(isA<CoreUnavailable>()));
    });
  });

  group('CoreLocator', () {
    late Directory dir;

    setUp(() => dir = Directory.systemTemp.createTempSync('core_locator_'));
    tearDown(() => dir.deleteSync(recursive: true));

    test('reads the token and the dashboard address', () async {
      File('${dir.path}/local-api.token').writeAsStringSync('abc\n');
      File('${dir.path}/config.json').writeAsStringSync(
        jsonEncode({
          'dashboard': {'listen': '0.0.0.0:9191'},
        }),
      );
      final e = await CoreLocator.locate(dir.path);
      expect(e.token, 'abc');
      expect(e.baseUri.toString(), 'http://127.0.0.1:9191');
    });

    test('defaults the address without a config', () async {
      File('${dir.path}/local-api.token').writeAsStringSync('abc');
      final e = await CoreLocator.locate(dir.path);
      expect(e.baseUri.toString(), 'http://127.0.0.1:10820');
    });

    test('explains a missing token', () async {
      await expectLater(
        CoreLocator.locate(dir.path),
        throwsA(isA<CoreUnavailable>()),
      );
    });

    test('takes --data-dir from the arguments', () {
      expect(CoreLocator.dataDir(['--data-dir=C:/x/data']), 'C:/x/data');
    });

    test('keeps the data in one place for every version', () {
      String dir({
        bool portable = false,
        String os = 'windows',
        Map<String, String>? env,
      }) => CoreLocator.dataDirFor(
        args: const [],
        exeDir: r'D:\dist\Firefox-VPN-1.3',
        portable: portable,
        env: env ?? {'LOCALAPPDATA': r'C:\Users\u\AppData\Local'},
        os: os,
      );
      // Windows: always beside the app.
      expect(dir(), r'D:\dist\Firefox-VPN-1.3\user-data');
      expect(dir(portable: true), r'D:\dist\Firefox-VPN-1.3\user-data');
      expect(
        dir(os: 'macos', env: {'HOME': '/Users/u'}),
        '/Users/u/Library/Application Support/Firefox VPN Client',
      );
      expect(
        dir(os: 'linux', env: {'HOME': '/home/u'}),
        '/home/u/.local/share/firefox-vpn-client',
      );
    });

    test('maps listen addresses to loopback URLs', () {
      expect(CoreLocator.listenUri('[::1]:9090').host, '::1');
      expect(
        CoreLocator.listenUri(':8080').toString(),
        'http://127.0.0.1:8080',
      );
      expect(CoreLocator.listenUri('[::]:8080').host, '127.0.0.1');
    });
  });
}