2022-10-04 13:12:54 +03:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2022 Yubico.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-05-12 16:35:14 +03:00
|
|
|
import 'package:flutter/material.dart';
|
2023-11-14 12:48:01 +03:00
|
|
|
import 'package:flutter/services.dart' show rootBundle;
|
2022-07-21 16:42:47 +03:00
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
2022-09-13 18:13:06 +03:00
|
|
|
import 'package:freezed_annotation/freezed_annotation.dart';
|
2022-09-12 17:00:08 +03:00
|
|
|
import 'package:yubico_authenticator/app/views/keys.dart' as app_keys;
|
2022-10-03 16:34:06 +03:00
|
|
|
import 'package:yubico_authenticator/app/views/keys.dart';
|
2022-05-12 16:35:14 +03:00
|
|
|
import 'package:yubico_authenticator/core/state.dart';
|
2022-10-03 16:34:06 +03:00
|
|
|
import 'package:yubico_authenticator/management/views/keys.dart';
|
2022-05-12 16:35:14 +03:00
|
|
|
|
2022-09-13 13:32:13 +03:00
|
|
|
import 'android/util.dart' as android_test_util;
|
|
|
|
import 'desktop/util.dart' as desktop_test_util;
|
2022-07-21 16:42:47 +03:00
|
|
|
|
2023-11-14 18:44:14 +03:00
|
|
|
const shortWaitMs = 200;
|
2023-06-30 16:32:10 +03:00
|
|
|
const longWaitMs = 500;
|
2023-11-14 18:44:14 +03:00
|
|
|
const ultraLongWaitMs = 5000;
|
2022-09-12 07:34:49 +03:00
|
|
|
|
2022-09-12 17:00:08 +03:00
|
|
|
/// information about YubiKey as seen by the app
|
|
|
|
String? yubiKeyName;
|
|
|
|
String? yubiKeyFirmware;
|
|
|
|
String? yubiKeySerialNumber;
|
2022-09-13 10:52:48 +03:00
|
|
|
bool collectedYubiKeyInformation = false;
|
2022-09-12 07:34:49 +03:00
|
|
|
|
2022-09-12 17:00:08 +03:00
|
|
|
extension AppWidgetTester on WidgetTester {
|
2022-09-12 07:34:49 +03:00
|
|
|
Future<void> shortWait() async {
|
|
|
|
await pump(const Duration(milliseconds: shortWaitMs));
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> longWait() async {
|
|
|
|
await pump(const Duration(milliseconds: longWaitMs));
|
|
|
|
}
|
|
|
|
|
2023-11-14 18:44:14 +03:00
|
|
|
Future<void> ultraLongWait() async {
|
|
|
|
await pump(const Duration(milliseconds: ultraLongWaitMs));
|
|
|
|
}
|
|
|
|
|
2022-09-13 18:13:06 +03:00
|
|
|
/// waits up to [timeOutSec] seconds evaluating whether [Finder] f is
|
|
|
|
/// visible
|
|
|
|
Future<Finder> waitForFinder(Finder f, [int timeOutSec = 20]) async {
|
|
|
|
int delayMs = 500;
|
|
|
|
int elapsedTime = 0;
|
|
|
|
|
|
|
|
var evaluated = f.evaluate();
|
|
|
|
while (evaluated.isEmpty && elapsedTime < timeOutSec * 1000) {
|
|
|
|
await pump(Duration(milliseconds: delayMs));
|
|
|
|
elapsedTime += delayMs;
|
|
|
|
evaluated = f.evaluate();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (evaluated.isEmpty) {
|
2023-11-23 17:33:15 +03:00
|
|
|
testLog(false,
|
|
|
|
'Found 0 ${f.describeMatch(Plurality.zero)} in $timeOutSec seconds.');
|
2022-09-13 18:13:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2023-01-11 12:04:15 +03:00
|
|
|
Finder findActionIconButton() {
|
|
|
|
return find.byKey(actionsIconButtonKey).hitTestable();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> tapActionIconButton() async {
|
|
|
|
await tap(findActionIconButton());
|
|
|
|
await pump(const Duration(milliseconds: 500));
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> tapTopLeftCorner() async {
|
|
|
|
await tapAt(const Offset(0, 0));
|
|
|
|
await longWait();
|
|
|
|
}
|
|
|
|
|
2022-10-03 16:34:06 +03:00
|
|
|
/// Drawer helpers
|
|
|
|
bool hasDrawer() => scaffoldGlobalKey.currentState!.hasDrawer;
|
|
|
|
|
|
|
|
/// Open drawer
|
|
|
|
Future<void> openDrawer() async {
|
|
|
|
if (hasDrawer()) {
|
|
|
|
scaffoldGlobalKey.currentState!.openDrawer();
|
|
|
|
await pump(const Duration(milliseconds: 500));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Close drawer
|
|
|
|
Future<void> closeDrawer() async {
|
|
|
|
if (hasDrawer()) {
|
|
|
|
scaffoldGlobalKey.currentState!.closeDrawer();
|
|
|
|
await pump(const Duration(milliseconds: 500));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Is drawer opened?
|
|
|
|
/// If there is no drawer say it is open (all items are available)
|
|
|
|
bool isDrawerOpened() =>
|
|
|
|
hasDrawer() == false || scaffoldGlobalKey.currentState!.isDrawerOpen;
|
|
|
|
|
2023-11-24 14:18:30 +03:00
|
|
|
/// Tap a app button in the drawer
|
|
|
|
/// If the drawer is closed, it is opened first
|
|
|
|
Future<void> tapAppDrawerButton(Key appKey) async {
|
|
|
|
if (hasDrawer() && !isDrawerOpened()) {
|
|
|
|
await openDrawer();
|
|
|
|
}
|
|
|
|
|
|
|
|
var appButtonFinder = find.byKey(appKey).hitTestable();
|
|
|
|
await tap(appButtonFinder);
|
|
|
|
await longWait();
|
|
|
|
}
|
|
|
|
|
2022-10-03 16:34:06 +03:00
|
|
|
/// Management screen
|
|
|
|
Future<void> openManagementScreen() async {
|
|
|
|
if (!isDrawerOpened()) {
|
|
|
|
await openDrawer();
|
|
|
|
}
|
|
|
|
|
2023-07-03 12:27:25 +03:00
|
|
|
await tap(find.byKey(managementAppDrawer).hitTestable());
|
2022-10-03 16:34:06 +03:00
|
|
|
await pump(const Duration(milliseconds: 500));
|
|
|
|
|
|
|
|
expect(find.byKey(screenKey), findsOneWidget);
|
|
|
|
}
|
|
|
|
|
2023-11-14 12:48:01 +03:00
|
|
|
/// Retrieve a list of test approved serial numbers.
|
|
|
|
///
|
2023-11-17 15:32:27 +03:00
|
|
|
/// There are two ways how to provide approved serial numbers:
|
|
|
|
///
|
|
|
|
/// 1. Serial numbers defined in test resource file
|
2023-11-14 12:48:01 +03:00
|
|
|
/// To add testing keys add comma separated serial numbers to a file
|
|
|
|
/// `approved_serial_numbers.csv` in `integration_test/test_res/resources/`.
|
|
|
|
/// This file is bundled only during test runs and is explicitly ignored from
|
|
|
|
/// version control.
|
2023-11-17 15:32:27 +03:00
|
|
|
///
|
|
|
|
/// 2. Serial numbers passed through build environment
|
|
|
|
/// YA_TEST_APPROVED_KEY_SN should contain comma separated list of
|
|
|
|
/// YubiKey serial numbers which are approved for tests
|
|
|
|
/// To pass the variable to the test use:
|
|
|
|
/// flutter --dart-define=YA_TEST_APPROVED_KEY_SN=SN1,SN2,...,SNn test t
|
2023-11-14 12:48:01 +03:00
|
|
|
Future<List<String>> getApprovedSerialNumbers() async {
|
|
|
|
const approvedKeysResource = 'approved_serial_numbers.csv';
|
|
|
|
String approved = '';
|
|
|
|
|
2023-11-13 18:50:20 +03:00
|
|
|
const envVar = String.fromEnvironment('YA_TEST_APPROVED_KEY_SN');
|
|
|
|
|
2023-11-14 12:48:01 +03:00
|
|
|
try {
|
|
|
|
approved = await rootBundle.loadString(
|
|
|
|
'packages/test_res/resources/$approvedKeysResource',
|
|
|
|
);
|
|
|
|
} catch (_) {
|
|
|
|
testLog(false, 'Failed to read $approvedKeysResource');
|
|
|
|
}
|
|
|
|
|
2023-11-17 15:32:27 +03:00
|
|
|
return (approved + (approved.isEmpty ? ',' : '') + envVar)
|
|
|
|
.split(',')
|
|
|
|
.map((e) => e.trim())
|
|
|
|
.toList(growable: false);
|
2023-11-14 12:48:01 +03:00
|
|
|
}
|
2023-11-13 18:50:20 +03:00
|
|
|
|
2023-11-14 12:48:01 +03:00
|
|
|
Future<void> startUp([Map<dynamic, dynamic> startUpParams = const {}]) async {
|
2022-09-13 10:52:48 +03:00
|
|
|
var result = isAndroid == true
|
2022-09-13 13:32:13 +03:00
|
|
|
? await android_test_util.startUp(this, startUpParams)
|
|
|
|
: await desktop_test_util.startUp(this, startUpParams);
|
2022-09-13 10:52:48 +03:00
|
|
|
|
2022-09-13 13:32:13 +03:00
|
|
|
await collectYubiKeyInformation();
|
2022-09-12 17:00:08 +03:00
|
|
|
|
2023-11-14 16:03:47 +03:00
|
|
|
if (yubiKeySerialNumber == null) {
|
|
|
|
fail('No YubiKey connected');
|
|
|
|
}
|
|
|
|
|
2023-11-17 15:32:27 +03:00
|
|
|
final approvedSerialNumbers = await getApprovedSerialNumbers();
|
|
|
|
|
2023-11-13 18:50:20 +03:00
|
|
|
if (!approvedSerialNumbers.contains(yubiKeySerialNumber)) {
|
2023-11-14 16:03:47 +03:00
|
|
|
fail('YubiKey with S/N $yubiKeySerialNumber is not approved for '
|
|
|
|
'integration tests.\nUse --dart-define='
|
|
|
|
'YA_TEST_APPROVED_KEY_SN=$yubiKeySerialNumber test '
|
|
|
|
'parameter to approve it.');
|
2022-09-11 12:05:00 +03:00
|
|
|
}
|
2022-09-13 10:52:48 +03:00
|
|
|
|
|
|
|
return result;
|
2022-07-22 14:29:32 +03:00
|
|
|
}
|
2022-09-12 07:34:49 +03:00
|
|
|
|
|
|
|
void testLog(bool quiet, String message) {
|
|
|
|
if (!quiet) {
|
|
|
|
printToConsole(message);
|
|
|
|
}
|
|
|
|
}
|
2022-09-12 17:00:08 +03:00
|
|
|
|
|
|
|
/// get key information
|
2022-09-13 13:32:13 +03:00
|
|
|
Future<void> collectYubiKeyInformation() async {
|
2022-09-13 10:52:48 +03:00
|
|
|
if (collectedYubiKeyInformation) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-03 12:27:25 +03:00
|
|
|
await openDrawer();
|
2022-09-12 17:00:08 +03:00
|
|
|
|
|
|
|
var deviceInfo = find.byKey(app_keys.deviceInfoListTile);
|
|
|
|
if (deviceInfo.evaluate().isNotEmpty) {
|
2023-07-03 12:27:25 +03:00
|
|
|
ListTile lt = find
|
|
|
|
.descendant(of: deviceInfo, matching: find.byType(ListTile))
|
|
|
|
.evaluate()
|
|
|
|
.single
|
|
|
|
.widget as ListTile;
|
2023-11-13 18:50:20 +03:00
|
|
|
|
2022-09-12 17:00:08 +03:00
|
|
|
yubiKeyName = (lt.title as Text).data;
|
|
|
|
var subtitle = (lt.subtitle as Text?)?.data;
|
|
|
|
|
|
|
|
if (subtitle != null) {
|
2023-07-03 12:27:25 +03:00
|
|
|
RegExpMatch? match =
|
|
|
|
RegExp(r'S/N: (\d.*) F/W: (\d\.\d\.\d)').firstMatch(subtitle);
|
2022-09-12 17:00:08 +03:00
|
|
|
if (match != null) {
|
2023-01-11 12:04:15 +03:00
|
|
|
yubiKeySerialNumber = match.group(1);
|
|
|
|
yubiKeyFirmware = match.group(2);
|
2022-09-12 17:00:08 +03:00
|
|
|
} else {
|
2023-01-11 12:04:15 +03:00
|
|
|
match = RegExp(r'F/W: (\d\.\d\.\d)').firstMatch(subtitle);
|
2022-09-12 17:00:08 +03:00
|
|
|
if (match != null) {
|
2023-01-11 12:04:15 +03:00
|
|
|
yubiKeyFirmware = match.group(1);
|
2022-09-12 17:00:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-13 10:52:48 +03:00
|
|
|
// close the opened menu
|
2023-07-03 12:27:25 +03:00
|
|
|
await closeDrawer();
|
2022-09-13 10:52:48 +03:00
|
|
|
|
2023-11-14 16:03:47 +03:00
|
|
|
if (yubiKeySerialNumber != null) {
|
|
|
|
testLog(false,
|
|
|
|
'Connected YubiKey: $yubiKeySerialNumber/$yubiKeyFirmware - $yubiKeyName');
|
|
|
|
}
|
2022-09-13 10:52:48 +03:00
|
|
|
collectedYubiKeyInformation = true;
|
2022-09-12 17:00:08 +03:00
|
|
|
}
|
2022-07-21 16:42:47 +03:00
|
|
|
}
|
2022-09-13 13:32:13 +03:00
|
|
|
|
2022-09-13 18:13:06 +03:00
|
|
|
@isTest
|
2022-09-13 13:32:13 +03:00
|
|
|
void appTest(
|
|
|
|
String description,
|
|
|
|
WidgetTesterCallback callback, {
|
|
|
|
bool? skip,
|
|
|
|
Map startUpParams = const {},
|
|
|
|
}) {
|
2023-11-24 14:18:30 +03:00
|
|
|
testWidgets(description, skip: skip, (WidgetTester tester) async {
|
2022-09-13 13:32:13 +03:00
|
|
|
await tester.startUp(startUpParams);
|
|
|
|
await callback(tester);
|
|
|
|
});
|
|
|
|
}
|