flutter_flutter/dev/bots/test/codesign_test.dart
Kate Lovett 9d96df2364
Modernize framework lints (#179089)
WIP

Commits separated as follows:
- Update lints in analysis_options files
- Run `dart fix --apply`
- Clean up leftover analysis issues 
- Run `dart format .` in the right places.

Local analysis and testing passes. Checking CI now.

Part of https://github.com/flutter/flutter/issues/178827
- Adoption of flutter_lints in examples/api coming in a separate change
(cc @loic-sharma)

## Pre-launch Checklist

- [ ] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [ ] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [ ] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [ ] I signed the [CLA].
- [ ] I listed at least one issue that this PR fixes in the description
above.
- [ ] I updated/added relevant documentation (doc comments with `///`).
- [ ] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [ ] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [ ] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

**Note**: The Flutter team is currently trialing the use of [Gemini Code
Assist for
GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code).
Comments from the `gemini-code-assist` bot should not be taken as
authoritative feedback from the Flutter team. If you find its comments
useful you can update your code accordingly, but if you are unsure or
disagree with the feedback, please feel free to wait for a Flutter team
member's review for guidance on which automated comments should be
addressed.

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
2025-11-26 01:10:39 +00:00

286 lines
10 KiB
Dart

// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@TestOn('mac-os')
library;
import '../../../packages/flutter_tools/test/src/fake_process_manager.dart';
import '../suite_runners/run_verify_binaries_codesigned_tests.dart';
import './common.dart';
void main() async {
const flutterRoot = '/a/b/c';
final List<String> allExpectedFiles =
binariesWithEntitlements(flutterRoot) + binariesWithoutEntitlements(flutterRoot);
final String allFilesStdout = allExpectedFiles.join('\n');
final List<String> allExpectedXcframeworks = signedXcframeworks(flutterRoot);
final String allXcframeworksStdout = allExpectedXcframeworks.join('\n');
final List<String> withEntitlements = binariesWithEntitlements(flutterRoot);
group('verifyExist', () {
test('Not all files found', () async {
final ProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
const FakeCommand(
command: <String>['find', '/a/b/c/bin/cache', '-type', 'f'],
stdout: '/a/b/c/bin/cache/artifacts/engine/android-arm-profile/darwin-x64/gen_snapshot',
),
const FakeCommand(
command: <String>[
'file',
'--mime-type',
'-b',
'/a/b/c/bin/cache/artifacts/engine/android-arm-profile/darwin-x64/gen_snapshot',
],
stdout: 'application/x-mach-binary',
),
]);
expect(
() async => verifyExist(flutterRoot, processManager: processManager),
throwsExceptionWith('Did not find all expected binaries!'),
);
});
test('All files found', () async {
final commandList = <FakeCommand>[];
final findCmd = FakeCommand(
command: const <String>['find', '$flutterRoot/bin/cache', '-type', 'f'],
stdout: allFilesStdout,
);
commandList.add(findCmd);
for (final expectedFile in allExpectedFiles) {
commandList.add(
FakeCommand(
command: <String>['file', '--mime-type', '-b', expectedFile],
stdout: 'application/x-mach-binary',
),
);
}
final ProcessManager processManager = FakeProcessManager.list(commandList);
await expectLater(verifyExist('/a/b/c', processManager: processManager), completes);
});
});
group('find paths', () {
test('All binary files found', () async {
final commandList = <FakeCommand>[];
final findCmd = FakeCommand(
command: const <String>['find', '$flutterRoot/bin/cache', '-type', 'f'],
stdout: allFilesStdout,
);
commandList.add(findCmd);
for (final expectedFile in allExpectedFiles) {
commandList.add(
FakeCommand(
command: <String>['file', '--mime-type', '-b', expectedFile],
stdout: 'application/x-mach-binary',
),
);
}
final ProcessManager processManager = FakeProcessManager.list(commandList);
final List<String> foundFiles = await findBinaryPaths(
'$flutterRoot/bin/cache',
processManager: processManager,
);
expect(foundFiles, allExpectedFiles);
});
test('Empty file list', () async {
final commandList = <FakeCommand>[];
const findCmd = FakeCommand(
command: <String>['find', '$flutterRoot/bin/cache', '-type', 'f'],
);
commandList.add(findCmd);
final ProcessManager processManager = FakeProcessManager.list(commandList);
final List<String> foundFiles = await findBinaryPaths(
'$flutterRoot/bin/cache',
processManager: processManager,
);
expect(foundFiles, <String>[]);
});
test('All xcframeworks files found', () async {
final commandList = <FakeCommand>[
FakeCommand(
command: const <String>[
'find',
'$flutterRoot/bin/cache',
'-type',
'd',
'-name',
'*xcframework',
],
stdout: allXcframeworksStdout,
),
];
final ProcessManager processManager = FakeProcessManager.list(commandList);
final List<String> foundFiles = await findXcframeworksPaths(
'$flutterRoot/bin/cache',
processManager: processManager,
);
expect(foundFiles, allExpectedXcframeworks);
});
group('isBinary', () {
test('isTrue', () async {
final commandList = <FakeCommand>[];
const fileToCheck = '/a/b/c/one.zip';
const findCmd = FakeCommand(
command: <String>['file', '--mime-type', '-b', fileToCheck],
stdout: 'application/x-mach-binary',
);
commandList.add(findCmd);
final ProcessManager processManager = FakeProcessManager.list(commandList);
final bool result = await isBinary(fileToCheck, processManager: processManager);
expect(result, isTrue);
});
test('isFalse', () async {
final commandList = <FakeCommand>[];
const fileToCheck = '/a/b/c/one.zip';
const findCmd = FakeCommand(
command: <String>['file', '--mime-type', '-b', fileToCheck],
stdout: 'text/xml',
);
commandList.add(findCmd);
final ProcessManager processManager = FakeProcessManager.list(commandList);
final bool result = await isBinary(fileToCheck, processManager: processManager);
expect(result, isFalse);
});
});
group('hasExpectedEntitlements', () {
test('expected entitlements', () async {
final commandList = <FakeCommand>[];
const fileToCheck = '/a/b/c/one.zip';
const codesignCmd = FakeCommand(
command: <String>['codesign', '--display', '--entitlements', ':-', fileToCheck],
);
commandList.add(codesignCmd);
final ProcessManager processManager = FakeProcessManager.list(commandList);
final bool result = await hasExpectedEntitlements(
fileToCheck,
flutterRoot,
processManager: processManager,
);
expect(result, isTrue);
});
test('unexpected entitlements', () async {
final commandList = <FakeCommand>[];
const fileToCheck = '/a/b/c/one.zip';
const codesignCmd = FakeCommand(
command: <String>['codesign', '--display', '--entitlements', ':-', fileToCheck],
exitCode: 1,
);
commandList.add(codesignCmd);
final ProcessManager processManager = FakeProcessManager.list(commandList);
final bool result = await hasExpectedEntitlements(
fileToCheck,
flutterRoot,
processManager: processManager,
);
expect(result, isFalse);
});
});
});
group('verifySignatures', () {
test('succeeds if every binary is codesigned and has correct entitlements', () async {
final commandList = <FakeCommand>[];
final findCmd = FakeCommand(
command: const <String>['find', '$flutterRoot/bin/cache', '-type', 'f'],
stdout: allFilesStdout,
);
commandList.add(findCmd);
for (final expectedFile in allExpectedFiles) {
commandList.add(
FakeCommand(
command: <String>['file', '--mime-type', '-b', expectedFile],
stdout: 'application/x-mach-binary',
),
);
}
commandList.add(
FakeCommand(
command: const <String>[
'find',
'$flutterRoot/bin/cache',
'-type',
'd',
'-name',
'*xcframework',
],
stdout: allXcframeworksStdout,
),
);
for (final expectedFile in allExpectedFiles) {
commandList.add(FakeCommand(command: <String>['codesign', '-vvv', expectedFile]));
if (withEntitlements.contains(expectedFile)) {
commandList.add(
FakeCommand(
command: <String>['codesign', '--display', '--entitlements', ':-', expectedFile],
stdout: expectedEntitlements.join('\n'),
),
);
}
}
for (final expectedXcframework in allExpectedXcframeworks) {
commandList.add(FakeCommand(command: <String>['codesign', '-vvv', expectedXcframework]));
}
final ProcessManager processManager = FakeProcessManager.list(commandList);
await expectLater(verifySignatures(flutterRoot, processManager: processManager), completes);
});
test('fails if binaries do not have the right entitlements', () async {
final commandList = <FakeCommand>[];
final findCmd = FakeCommand(
command: const <String>['find', '$flutterRoot/bin/cache', '-type', 'f'],
stdout: allFilesStdout,
);
commandList.add(findCmd);
for (final expectedFile in allExpectedFiles) {
commandList.add(
FakeCommand(
command: <String>['file', '--mime-type', '-b', expectedFile],
stdout: 'application/x-mach-binary',
),
);
}
commandList.add(
FakeCommand(
command: const <String>[
'find',
'$flutterRoot/bin/cache',
'-type',
'd',
'-name',
'*xcframework',
],
stdout: allXcframeworksStdout,
),
);
for (final expectedFile in allExpectedFiles) {
commandList.add(FakeCommand(command: <String>['codesign', '-vvv', expectedFile]));
if (withEntitlements.contains(expectedFile)) {
commandList.add(
FakeCommand(
command: <String>['codesign', '--display', '--entitlements', ':-', expectedFile],
),
);
}
}
for (final expectedXcframework in allExpectedXcframeworks) {
commandList.add(FakeCommand(command: <String>['codesign', '-vvv', expectedXcframework]));
}
final ProcessManager processManager = FakeProcessManager.list(commandList);
expect(
() async => verifySignatures(flutterRoot, processManager: processManager),
throwsExceptionWith('Test failed because files found with the wrong entitlements'),
);
});
});
}