crash_reporting_test.dart 12 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
// @dart = 2.8

7
import 'dart:convert';
8

9
import 'package:file/file.dart';
10 11
import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/io.dart';
12 13
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/os.dart';
14
import 'package:flutter_tools/src/base/platform.dart';
15
import 'package:flutter_tools/src/doctor.dart';
16
import 'package:flutter_tools/src/project.dart';
17 18 19
import 'package:flutter_tools/src/reporting/reporting.dart';
import 'package:http/http.dart';
import 'package:http/testing.dart';
20

21 22
import '../src/common.dart';
import '../src/context.dart';
23
import '../src/fake_http_client.dart';
24 25

void main() {
26
  BufferLogger logger;
27
  FileSystem fs;
28
  TestUsage testUsage;
29 30 31 32 33
  Platform platform;
  OperatingSystemUtils operatingSystemUtils;

  setUp(() async {
    logger = BufferLogger.test();
34
    fs = MemoryFileSystem.test();
35
    testUsage = TestUsage();
36 37 38

    platform = FakePlatform(environment: <String, String>{}, operatingSystem: 'linux');
    operatingSystemUtils = OperatingSystemUtils(
39
      fileSystem: fs,
40 41 42 43
      logger: logger,
      platform: platform,
      processManager: FakeProcessManager.any(),
    );
44

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
    MockCrashReportSender.sendCalls = 0;
  });

  Future<void> verifyCrashReportSent(RequestInfo crashInfo, {
    int crashes = 1,
  }) async {
    // Verify that we sent the crash report.
    expect(crashInfo.method, 'POST');
    expect(crashInfo.uri, Uri(
      scheme: 'https',
      host: 'clients2.google.com',
      port: 443,
      path: '/cr/report',
      queryParameters: <String, String>{
        'product': 'Flutter_Tools',
        'version': 'test-version',
      },
    ));
63
    expect(crashInfo.fields['uuid'], testUsage.clientId);
64 65 66 67 68 69 70 71 72 73 74 75 76
    expect(crashInfo.fields['product'], 'Flutter_Tools');
    expect(crashInfo.fields['version'], 'test-version');
    expect(crashInfo.fields['osName'], 'linux');
    expect(crashInfo.fields['osVersion'], 'Linux');
    expect(crashInfo.fields['type'], 'DartError');
    expect(crashInfo.fields['error_runtime_type'], 'StateError');
    expect(crashInfo.fields['error_message'], 'Bad state: Test bad state error');
    expect(crashInfo.fields['comments'], 'crash');

    expect(logger.traceText, contains('Sending crash report to Google.'));
    expect(logger.traceText, contains('Crash report sent (report ID: test-report-id)'));
  }

77 78 79 80 81
  testWithoutContext('CrashReporter.informUser provides basic instructions', () async {
    final CrashReporter crashReporter = CrashReporter(
      fileSystem: fs,
      logger: logger,
      flutterProjectFactory: FlutterProjectFactory(fileSystem: fs, logger: logger),
82
      client: FakeHttpClient.any(),
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    );

    final File file = fs.file('flutter_00.log');

    await crashReporter.informUser(
      CrashDetails(
        command: 'arg1 arg2 arg3',
        error: Exception('Dummy exception'),
        stackTrace: StackTrace.current,
        doctorText: 'Fake doctor text'),
      file,
    );

    expect(logger.errorText, contains('A crash report has been written to ${file.path}.'));
    expect(logger.statusText, contains('https://github.com/flutter/flutter/issues/new'));
  });

100
  testWithoutContext('suppress analytics', () async {
101
    testUsage.suppressAnalytics = true;
102 103 104

    final CrashReportSender crashReportSender = CrashReportSender(
      client: CrashingCrashReportSender(const SocketException('no internets')),
105
      usage: testUsage,
106 107 108 109 110 111 112 113 114 115 116 117 118 119
      platform: platform,
      logger: logger,
      operatingSystemUtils: operatingSystemUtils,
    );

    await crashReportSender.sendReport(
      error: StateError('Test bad state error'),
      stackTrace: null,
      getFlutterVersion: () => 'test-version',
      command: 'crash',
    );

    expect(logger.traceText, isEmpty);
  });
120

121 122
  group('allow analytics', () {
    setUp(() async {
123
      testUsage.suppressAnalytics = false;
124 125
    });

126
    testWithoutContext('should send crash reports', () async {
127
      final RequestInfo requestInfo = RequestInfo();
128

129 130
      final CrashReportSender crashReportSender = CrashReportSender(
        client: MockCrashReportSender(requestInfo),
131
        usage: testUsage,
132 133 134 135 136 137 138 139 140 141
        platform: platform,
        logger: logger,
        operatingSystemUtils: operatingSystemUtils,
      );

      await crashReportSender.sendReport(
        error: StateError('Test bad state error'),
        stackTrace: null,
        getFlutterVersion: () => 'test-version',
        command: 'crash',
142 143
      );

144 145
      await verifyCrashReportSent(requestInfo);
    });
146

147 148 149
    testWithoutContext('should print an explanatory message when there is a SocketException', () async {
      final CrashReportSender crashReportSender = CrashReportSender(
        client: CrashingCrashReportSender(const SocketException('no internets')),
150
        usage: testUsage,
151 152 153 154
        platform: platform,
        logger: logger,
        operatingSystemUtils: operatingSystemUtils,
      );
155

156 157 158 159 160 161
      await crashReportSender.sendReport(
        error: StateError('Test bad state error'),
        stackTrace: null,
        getFlutterVersion: () => 'test-version',
        command: 'crash',
      );
162

163
      expect(logger.errorText, contains('Failed to send crash report due to a network error'));
164 165
    });

166 167 168
    testWithoutContext('should print an explanatory message when there is an HttpException', () async {
      final CrashReportSender crashReportSender = CrashReportSender(
        client: CrashingCrashReportSender(const HttpException('no internets')),
169
        usage: testUsage,
170 171 172 173
        platform: platform,
        logger: logger,
        operatingSystemUtils: operatingSystemUtils,
      );
174

175 176 177 178 179 180
      await crashReportSender.sendReport(
        error: StateError('Test bad state error'),
        stackTrace: null,
        getFlutterVersion: () => 'test-version',
        command: 'crash',
      );
181

182
      expect(logger.errorText, contains('Failed to send crash report due to a network error'));
183 184
    });

185 186 187
    testWithoutContext('should print an explanatory message when there is a ClientException', () async {
      final CrashReportSender crashReportSender = CrashReportSender(
        client: CrashingCrashReportSender(const HttpException('no internets')),
188
        usage: testUsage,
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
        platform: platform,
        logger: logger,
        operatingSystemUtils: operatingSystemUtils,
      );

      await crashReportSender.sendReport(
        error: ClientException('Test bad state error'),
        stackTrace: null,
        getFlutterVersion: () => 'test-version',
        command: 'crash',
      );

      expect(logger.errorText, contains('Failed to send crash report due to a network error'));
    });

204
    testWithoutContext('should send only one crash report when sent many times', () async {
205 206
      final RequestInfo requestInfo = RequestInfo();

207 208
      final CrashReportSender crashReportSender = CrashReportSender(
        client: MockCrashReportSender(requestInfo),
209
        usage: testUsage,
210 211 212 213
        platform: platform,
        logger: logger,
        operatingSystemUtils: operatingSystemUtils,
      );
214

215 216 217 218 219 220
      await crashReportSender.sendReport(
        error: StateError('Test bad state error'),
        stackTrace: null,
        getFlutterVersion: () => 'test-version',
        command: 'crash',
      );
221

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
      await crashReportSender.sendReport(
        error: StateError('Test bad state error'),
        stackTrace: null,
        getFlutterVersion: () => 'test-version',
        command: 'crash',
      );

      await crashReportSender.sendReport(
        error: StateError('Test bad state error'),
        stackTrace: null,
        getFlutterVersion: () => 'test-version',
        command: 'crash',
      );

      await crashReportSender.sendReport(
        error: StateError('Test bad state error'),
        stackTrace: null,
        getFlutterVersion: () => 'test-version',
        command: 'crash',
      );
242 243 244 245 246

      expect(MockCrashReportSender.sendCalls, 1);
      await verifyCrashReportSent(requestInfo, crashes: 4);
    });

247
    testWithoutContext('should not send a crash report if on a user-branch', () async {
248 249 250
      String method;
      Uri uri;

251
      final MockClient mockClient = MockClient((Request request) async {
252 253 254 255 256 257 258
        method = request.method;
        uri = request.url;

        return Response(
          'test-report-id',
          200,
        );
259
      });
260

261 262
      final CrashReportSender crashReportSender = CrashReportSender(
        client: mockClient,
263
        usage: testUsage,
264 265 266
        platform: platform,
        logger: logger,
        operatingSystemUtils: operatingSystemUtils,
267 268
      );

269 270 271 272 273 274
      await crashReportSender.sendReport(
        error: StateError('Test bad state error'),
        stackTrace: null,
        getFlutterVersion: () => '[user-branch]/v1.2.3',
        command: 'crash',
      );
275 276 277 278 279

      // Verify that the report wasn't sent
      expect(method, null);
      expect(uri, null);

280
      expect(logger.traceText, isNot(contains('Crash report sent')));
281 282
    });

283
    testWithoutContext('can override base URL', () async {
284
      Uri uri;
285
      final MockClient mockClient = MockClient((Request request) async {
286
        uri = request.url;
287
        return Response('test-report-id', 200);
288 289 290 291 292 293 294 295 296 297
      });

      final Platform environmentPlatform = FakePlatform(
        operatingSystem: 'linux',
        environment: <String, String>{
          'HOME': '/',
          'FLUTTER_CRASH_SERVER_BASE_URL': 'https://localhost:12345/fake_server',
        },
        script: Uri(scheme: 'data'),
      );
298

299 300
      final CrashReportSender crashReportSender = CrashReportSender(
        client: mockClient,
301
        usage: testUsage,
302 303 304
        platform: environmentPlatform,
        logger: logger,
        operatingSystemUtils: operatingSystemUtils,
305 306
      );

307 308 309 310 311 312
      await crashReportSender.sendReport(
        error: StateError('Test bad state error'),
        stackTrace: null,
        getFlutterVersion: () => 'test-version',
        command: 'crash',
      );
313 314 315

      // Verify that we sent the crash report.
      expect(uri, isNotNull);
316
      expect(uri, Uri(
317 318 319 320 321 322
        scheme: 'https',
        host: 'localhost',
        port: 12345,
        path: '/fake_server',
        queryParameters: <String, String>{
          'product': 'Flutter_Tools',
323
          'version': 'test-version',
324 325 326
        },
      ));
    });
327 328 329
  });
}

330 331 332 333 334 335 336 337
class RequestInfo {
  String method;
  Uri uri;
  Map<String, String> fields;
}

class MockCrashReportSender extends MockClient {
  MockCrashReportSender(RequestInfo crashInfo) : super((Request request) async {
338 339 340 341 342 343 344 345 346 347 348
    MockCrashReportSender.sendCalls++;
    crashInfo.method = request.method;
    crashInfo.uri = request.url;

    // A very ad-hoc multipart request parser. Good enough for this test.
    String boundary = request.headers['Content-Type'];
    boundary = boundary.substring(boundary.indexOf('boundary=') + 9);
    crashInfo.fields = Map<String, String>.fromIterable(
      utf8.decode(request.bodyBytes)
        .split('--$boundary')
        .map<List<String>>((String part) {
349 350 351 352 353 354 355 356
        final Match nameMatch = RegExp(r'name="(.*)"').firstMatch(part);
        if (nameMatch == null) {
          return null;
        }
        final String name = nameMatch[1];
        final String value = part.split('\n').skip(2).join('\n').trim();
        return <String>[name, value];
      })
357 358
        .where((List<String> pair) => pair != null),
      key: (dynamic key) {
359
        final List<String> pair = key as List<String>;
360 361 362
        return pair[0];
      },
      value: (dynamic value) {
363
        final List<String> pair = value as List<String>;
364 365 366 367 368 369 370 371 372
        return pair[1];
      },
    );

    return Response(
      'test-report-id',
      200,
    );
  });
373 374

  static int sendCalls = 0;
375 376
}

377
class CrashingCrashReportSender extends MockClient {
378
  CrashingCrashReportSender(Object exception) : super((Request request) async {
379 380 381 382
    throw exception;
  });
}

383 384 385 386 387 388 389 390 391
/// A DoctorValidatorsProvider that overrides the default validators without
/// overriding the doctor.
class FakeDoctorValidatorsProvider implements DoctorValidatorsProvider {
  @override
  List<DoctorValidator> get validators => <DoctorValidator>[];

  @override
  List<Workflow> get workflows => <Workflow>[];
}