protocol_discovery_test.dart 8.63 KB
Newer Older
1 2 3 4 5 6
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

7
import 'package:flutter_tools/src/device.dart';
8
import 'package:flutter_tools/src/protocol_discovery.dart';
9

10 11 12
import '../src/common.dart';
import '../src/context.dart';
import '../src/mocks.dart';
13

14
void main() {
15
  group('service_protocol discovery', () {
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
    MockDeviceLogReader logReader;
    ProtocolDiscovery discoverer;

    group('no port forwarding', () {
      /// Performs test set-up functionality that must be performed as part of
      /// the `test()` pass and not part of the `setUp()` pass.
      ///
      /// This exists to make sure we're not creating an error that tries to
      /// cross an error-zone boundary. Our use of `testUsingContext()` runs the
      /// test code inside an error zone, but the `setUp()` code is not run in
      /// any zone. This creates the potential for errors that try to cross
      /// error-zone boundaries, which are considered uncaught.
      ///
      /// This also exists for cases where our initialization requires access to
      /// a `Context` object, which is only set up inside the zone.
      ///
Ian Hickson's avatar
Ian Hickson committed
32 33
      /// These issues do not pertain to real code and are a test-only concern,
      /// because in real code, the zone is set up in `main()`.
34 35 36
      ///
      /// See also: [runZoned]
      void initialize() {
37 38
        logReader = MockDeviceLogReader();
        discoverer = ProtocolDiscovery.observatory(logReader);
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
      }

      tearDown(() {
        discoverer.cancel();
        logReader.dispose();
      });

      testUsingContext('returns non-null uri future', () async {
        initialize();
        expect(discoverer.uri, isNotNull);
      });

      testUsingContext('discovers uri if logs already produced output', () async {
        initialize();
        logReader.addLine('HELLO WORLD');
        logReader.addLine('Observatory listening on http://127.0.0.1:9999');
        final Uri uri = await discoverer.uri;
        expect(uri.port, 9999);
        expect('$uri', 'http://127.0.0.1:9999');
      });

      testUsingContext('discovers uri if logs not yet produced output', () async {
        initialize();
        final Future<Uri> uriFuture = discoverer.uri;
        logReader.addLine('Observatory listening on http://127.0.0.1:3333');
        final Uri uri = await uriFuture;
        expect(uri.port, 3333);
        expect('$uri', 'http://127.0.0.1:3333');
      });

69
      testUsingContext('discovers uri with Ascii Esc code', () async {
70
        initialize();
71
        logReader.addLine('Observatory listening on http://127.0.0.1:3333\x1b[');
72 73 74 75 76
        final Uri uri = await discoverer.uri;
        expect(uri.port, 3333);
        expect('$uri', 'http://127.0.0.1:3333');
      });

77 78 79 80 81 82 83 84 85 86 87 88 89 90
      testUsingContext('uri throws if logs produce bad line', () async {
        initialize();
        Timer.run(() {
          logReader.addLine('Observatory listening on http://127.0.0.1:apple');
        });
        expect(discoverer.uri, throwsA(isFormatException));
      });

      testUsingContext('uri waits for correct log line', () async {
        initialize();
        final Future<Uri> uriFuture = discoverer.uri;
        logReader.addLine('Observatory not listening...');
        final Uri timeoutUri = Uri.parse('http://timeout');
        final Uri actualUri = await uriFuture.timeout(
91 92 93
          const Duration(milliseconds: 100),
          onTimeout: () => timeoutUri,
        );
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
        expect(actualUri, timeoutUri);
      });

      testUsingContext('discovers uri if log line contains Android prefix', () async {
        initialize();
        logReader.addLine('I/flutter : Observatory listening on http://127.0.0.1:52584');
        final Uri uri = await discoverer.uri;
        expect(uri.port, 52584);
        expect('$uri', 'http://127.0.0.1:52584');
      });

      testUsingContext('discovers uri if log line contains auth key', () async {
        initialize();
        final Future<Uri> uriFuture = discoverer.uri;
        logReader.addLine('I/flutter : Observatory listening on http://127.0.0.1:54804/PTwjm8Ii8qg=/');
        final Uri uri = await uriFuture;
        expect(uri.port, 54804);
        expect('$uri', 'http://127.0.0.1:54804/PTwjm8Ii8qg=/');
      });

      testUsingContext('discovers uri if log line contains non-localhost', () async {
        initialize();
        final Future<Uri> uriFuture = discoverer.uri;
117
        logReader.addLine('I/flutter : Observatory listening on http://127.0.0.1:54804/PTwjm8Ii8qg=/');
118 119
        final Uri uri = await uriFuture;
        expect(uri.port, 54804);
120
        expect('$uri', 'http://127.0.0.1:54804/PTwjm8Ii8qg=/');
121
      });
122
    });
123

124 125
    group('port forwarding', () {
      testUsingContext('default port', () async {
126 127
        final MockDeviceLogReader logReader = MockDeviceLogReader();
        final ProtocolDiscovery discoverer = ProtocolDiscovery.observatory(
128
          logReader,
129
          portForwarder: MockPortForwarder(99),
130
        );
131 132 133 134 135

        // Get next port future.
        final Future<Uri> nextUri = discoverer.uri;
        logReader.addLine('I/flutter : Observatory listening on http://127.0.0.1:54804/PTwjm8Ii8qg=/');
        final Uri uri = await nextUri;
136 137
        expect(uri.port, 99);
        expect('$uri', 'http://127.0.0.1:99/PTwjm8Ii8qg=/');
138

139
        await discoverer.cancel();
140 141 142 143
        logReader.dispose();
      });

      testUsingContext('specified port', () async {
144 145
        final MockDeviceLogReader logReader = MockDeviceLogReader();
        final ProtocolDiscovery discoverer = ProtocolDiscovery.observatory(
146
          logReader,
147
          portForwarder: MockPortForwarder(99),
148 149
          hostPort: 1243,
        );
150 151 152 153 154 155 156 157

        // Get next port future.
        final Future<Uri> nextUri = discoverer.uri;
        logReader.addLine('I/flutter : Observatory listening on http://127.0.0.1:54804/PTwjm8Ii8qg=/');
        final Uri uri = await nextUri;
        expect(uri.port, 1243);
        expect('$uri', 'http://127.0.0.1:1243/PTwjm8Ii8qg=/');

158
        await discoverer.cancel();
159 160 161
        logReader.dispose();
      });

162
      testUsingContext('specified port zero', () async {
163 164
        final MockDeviceLogReader logReader = MockDeviceLogReader();
        final ProtocolDiscovery discoverer = ProtocolDiscovery.observatory(
165
          logReader,
166
          portForwarder: MockPortForwarder(99),
167 168 169 170 171 172 173 174 175 176
          hostPort: 0,
        );

        // Get next port future.
        final Future<Uri> nextUri = discoverer.uri;
        logReader.addLine('I/flutter : Observatory listening on http://127.0.0.1:54804/PTwjm8Ii8qg=/');
        final Uri uri = await nextUri;
        expect(uri.port, 99);
        expect('$uri', 'http://127.0.0.1:99/PTwjm8Ii8qg=/');

177
        await discoverer.cancel();
178 179 180
        logReader.dispose();
      });

181
      testUsingContext('ipv6', () async {
182 183
        final MockDeviceLogReader logReader = MockDeviceLogReader();
        final ProtocolDiscovery discoverer = ProtocolDiscovery.observatory(
184
          logReader,
185
          portForwarder: MockPortForwarder(99),
186 187 188
          hostPort: 54777,
          ipv6: true,
        );
189

190 191 192 193 194 195 196
        // Get next port future.
        final Future<Uri> nextUri = discoverer.uri;
        logReader.addLine('I/flutter : Observatory listening on http://127.0.0.1:54804/PTwjm8Ii8qg=/');
        final Uri uri = await nextUri;
        expect(uri.port, 54777);
        expect('$uri', 'http://[::1]:54777/PTwjm8Ii8qg=/');

197
        await discoverer.cancel();
198 199
        logReader.dispose();
      });
200 201

      testUsingContext('ipv6 with Ascii Escape code', () async {
202 203
        final MockDeviceLogReader logReader = MockDeviceLogReader();
        final ProtocolDiscovery discoverer = ProtocolDiscovery.observatory(
204
          logReader,
205
          portForwarder: MockPortForwarder(99),
206 207 208 209 210 211 212 213 214 215 216
          hostPort: 54777,
          ipv6: true,
        );

        // Get next port future.
        final Future<Uri> nextUri = discoverer.uri;
        logReader.addLine('I/flutter : Observatory listening on http://[::1]:54777/PTwjm8Ii8qg=/\x1b[');
        final Uri uri = await nextUri;
        expect(uri.port, 54777);
        expect('$uri', 'http://[::1]:54777/PTwjm8Ii8qg=/');

217
        await discoverer.cancel();
218 219
        logReader.dispose();
      });
220
    });
221 222
  });
}
223 224 225 226

class MockPortForwarder extends DevicePortForwarder {
  MockPortForwarder([this.availablePort]);

227 228
  final int availablePort;

229
  @override
230
  Future<int> forward(int devicePort, { int hostPort }) async {
231
    hostPort ??= 0;
232
    if (hostPort == 0) {
233
      return availablePort;
234
    }
235 236
    return hostPort;
  }
237 238 239 240 241

  @override
  List<ForwardedPort> get forwardedPorts => throw 'not implemented';

  @override
242
  Future<void> unforward(ForwardedPort forwardedPort) {
243 244 245
    throw 'not implemented';
  }
}