From 57dd045cefd4ead384dad95394abbea619b657a2 Mon Sep 17 00:00:00 2001 From: Zachary Anderson Date: Tue, 14 Apr 2020 09:42:26 -0700 Subject: [PATCH] [flutter_tools] Handle empty gzip file on Windows (#54679) * [flutter_tools] Handle empty gzip file on Windows * Update packages/flutter_tools/test/general.shard/base/os_test.dart Co-Authored-By: Jonah Williams * Update packages/flutter_tools/test/general.shard/base/os_test.dart Co-Authored-By: Jonah Williams * Update packages/flutter_tools/test/general.shard/base/os_test.dart Co-Authored-By: Jonah Williams Co-authored-by: Jonah Williams --- packages/flutter_tools/lib/src/base/os.dart | 2 + .../test/general.shard/base/os_test.dart | 57 +++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/packages/flutter_tools/lib/src/base/os.dart b/packages/flutter_tools/lib/src/base/os.dart index 12b34fe3eaa..f4e8399bc61 100644 --- a/packages/flutter_tools/lib/src/base/os.dart +++ b/packages/flutter_tools/lib/src/base/os.dart @@ -362,6 +362,8 @@ class _WindowsUtils extends OperatingSystemUtils { return false; } on ArchiveException catch (_) { return false; + } on RangeError catch (_) { + return false; } return true; } diff --git a/packages/flutter_tools/test/general.shard/base/os_test.dart b/packages/flutter_tools/test/general.shard/base/os_test.dart index b7ca1f26789..3914f81b144 100644 --- a/packages/flutter_tools/test/general.shard/base/os_test.dart +++ b/packages/flutter_tools/test/general.shard/base/os_test.dart @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'dart:typed_data'; + import 'package:file/file.dart'; import 'package:file/memory.dart'; import 'package:flutter_tools/src/base/io.dart'; @@ -87,9 +89,64 @@ void main() { }); }); + group('gzip on Windows:', () { + testWithoutContext('verifyGzip returns false on a FileSystemException', () { + final MockFileSystem fileSystem = MockFileSystem(); + final MockFile mockFile = MockFile(); + when(fileSystem.file(any)).thenReturn(mockFile); + when(mockFile.readAsBytesSync()).thenThrow( + const FileSystemException('error'), + ); + final OperatingSystemUtils osUtils = OperatingSystemUtils( + fileSystem: fileSystem, + logger: BufferLogger.test(), + platform: FakePlatform(operatingSystem: 'windows'), + processManager: mockProcessManager, + ); + + expect(osUtils.verifyGzip(mockFile), isFalse); + }); + + testWithoutContext('verifyGzip returns false on an ArchiveException', () { + final MockFileSystem fileSystem = MockFileSystem(); + final MockFile mockFile = MockFile(); + when(fileSystem.file(any)).thenReturn(mockFile); + when(mockFile.readAsBytesSync()).thenReturn(Uint8List.fromList([ + // Anything other than the magic header: 0x1f, 0x8b. + 0x01, + 0x02, + ])); + final OperatingSystemUtils osUtils = OperatingSystemUtils( + fileSystem: fileSystem, + logger: BufferLogger.test(), + platform: FakePlatform(operatingSystem: 'windows'), + processManager: mockProcessManager, + ); + + expect(osUtils.verifyGzip(mockFile), isFalse); + }); + + testWithoutContext('verifyGzip returns false on an empty file', () { + final MockFileSystem fileSystem = MockFileSystem(); + final MockFile mockFile = MockFile(); + when(fileSystem.file(any)).thenReturn(mockFile); + when(mockFile.readAsBytesSync()).thenReturn(Uint8List(0)); + final OperatingSystemUtils osUtils = OperatingSystemUtils( + fileSystem: fileSystem, + logger: BufferLogger.test(), + platform: FakePlatform(operatingSystem: 'windows'), + processManager: mockProcessManager, + ); + + expect(osUtils.verifyGzip(mockFile), isFalse); + }); + }); + testWithoutContext('stream compression level', () { expect(OperatingSystemUtils.gzipLevel1.level, equals(1)); }); } class MockProcessManager extends Mock implements ProcessManager {} +class MockFileSystem extends Mock implements FileSystem {} +class MockFile extends Mock implements File {}