test(api): update mocks with new interface

This commit is contained in:
Hugo Pointcheval 2023-04-04 23:24:20 +02:00
parent e47004e2d0
commit 108c394a25
Signed by: hugo
GPG Key ID: 3AAC487E131E00BC
2 changed files with 153 additions and 117 deletions

View File

@ -33,14 +33,14 @@ dependencies:
dev_dependencies: dev_dependencies:
flutter_test: { sdk: flutter } flutter_test: { sdk: flutter }
mockito: ^5.3.2 mockito: ^5.4.0
plugin_platform_interface: ^2.1.3 plugin_platform_interface: ^2.1.4
wyatt_analysis: wyatt_analysis:
hosted: hosted:
url: https://git.wyatt-studio.fr/api/packages/Wyatt-FOSS/pub/ url: https://git.wyatt-studio.fr/api/packages/Wyatt-FOSS/pub/
name: wyatt_analysis name: wyatt_analysis
version: 2.4.0 version: 2.4.1
flutter: flutter:
plugin: plugin:

View File

@ -6,7 +6,7 @@
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:native_crypto_platform_interface/native_crypto_platform_interface.dart'; import 'package:native_crypto_platform_interface/native_crypto_platform_interface_gen.dart';
class MockNativeCryptoAPI implements NativeCryptoAPI { class MockNativeCryptoAPI implements NativeCryptoAPI {
static Uint8List? Function(int length)? generateSecureRandomFn; static Uint8List? Function(int length)? generateSecureRandomFn;
@ -41,6 +41,13 @@ class MockNativeCryptoAPI implements NativeCryptoAPI {
String algorithm, String algorithm,
)? encryptFn; )? encryptFn;
static Uint8List? Function(
Uint8List plainText,
Uint8List key,
Uint8List iv,
String algorithm,
)? encryptWithIVFn;
static bool? Function( static bool? Function(
String plainTextPath, String plainTextPath,
String cipherTextPath, String cipherTextPath,
@ -48,12 +55,13 @@ class MockNativeCryptoAPI implements NativeCryptoAPI {
String algorithm, String algorithm,
)? encryptFileFn; )? encryptFileFn;
static Uint8List? Function( static bool? Function(
Uint8List plainText, String plainTextPath,
String cipherTextPath,
Uint8List key, Uint8List key,
Uint8List iv, Uint8List iv,
String algorithm, String algorithm,
)? encryptWithIVFn; )? encryptFileWithIVFn;
static Uint8List? Function( static Uint8List? Function(
Uint8List password, Uint8List password,
@ -64,130 +72,158 @@ class MockNativeCryptoAPI implements NativeCryptoAPI {
)? pbkdf2Fn; )? pbkdf2Fn;
@override @override
Future<DecryptResponse> decrypt(DecryptRequest argRequest) async => Future<Uint8List?> decrypt(
decryptFn != null Uint8List argCiphertext,
? DecryptResponse( Uint8List argKey,
plainText: decryptFn!( CipherAlgorithm argAlgorithm,
argRequest.cipherText!, ) async {
argRequest.key!, if (decryptFn != null) {
argRequest.algorithm!, return decryptFn!(argCiphertext, argKey, argAlgorithm.toString());
), } else {
) return Uint8List.fromList([1, 2, 3]);
: DecryptResponse( }
plainText: Uint8List.fromList([1, 2, 3]), }
);
@override
@override Future<bool?> decryptFile(
Future<DecryptFileResponse> decryptFile( String argCiphertextpath,
DecryptFileRequest argRequest, String argPlaintextpath,
) async => Uint8List argKey,
decryptFileFn != null CipherAlgorithm argAlgorithm,
? DecryptFileResponse( ) async {
success: decryptFileFn!( if (decryptFileFn != null) {
argRequest.cipherTextPath!, return decryptFileFn!(
argRequest.plainTextPath!, argCiphertextpath,
argRequest.key!, argPlaintextpath,
argRequest.algorithm!, argKey,
), argAlgorithm.toString(),
) );
: DecryptFileResponse(success: true); } else {
return Future.value(true);
@override }
Future<EncryptResponse> encrypt(EncryptRequest argRequest) async => }
encryptFn != null
? EncryptResponse( @override
cipherText: encryptFn!( Future<Uint8List?> encrypt(
argRequest.plainText!, Uint8List argPlaintext,
argRequest.key!, Uint8List argKey,
argRequest.algorithm!, CipherAlgorithm argAlgorithm,
), ) async {
) if (encryptFn != null) {
: EncryptResponse( return encryptFn!(argPlaintext, argKey, argAlgorithm.toString());
cipherText: Uint8List.fromList([1, 2, 3]), } else {
); return Uint8List.fromList([1, 2, 3]);
}
@override }
Future<EncryptFileResponse> encryptFile(
EncryptFileRequest argRequest, @override
) async => Future<bool?> encryptFile(
encryptFileFn != null String argPlaintextpath,
? EncryptFileResponse( String argCiphertextpath,
success: encryptFileFn!( Uint8List argKey,
argRequest.plainTextPath!, CipherAlgorithm argAlgorithm,
argRequest.cipherTextPath!, ) async {
argRequest.key!, if (encryptFileFn != null) {
argRequest.algorithm!, return encryptFileFn!(
), argPlaintextpath,
) argCiphertextpath,
: EncryptFileResponse(success: true); argKey,
argAlgorithm.toString(),
@override );
Future<EncryptResponse> encryptWithIV( } else {
EncryptWithIVRequest argRequest, return Future.value(true);
) async => }
encryptWithIVFn != null }
? EncryptResponse(
cipherText: encryptWithIVFn!( @override
argRequest.plainText!, Future<bool?> encryptFileWithIV(
argRequest.key!, String argPlaintextpath,
argRequest.iv!, String argCiphertextpath,
argRequest.algorithm!, Uint8List argIv,
), Uint8List argKey,
) CipherAlgorithm argAlgorithm,
: EncryptResponse( ) async {
cipherText: Uint8List.fromList([1, 2, 3]), if (encryptFileWithIVFn != null) {
); return encryptFileWithIVFn!(
argPlaintextpath,
@override argCiphertextpath,
Future<GenerateSecureRandomResponse> generateSecureRandom( argKey,
GenerateSecureRandomRequest argRequest, argIv,
) async => argAlgorithm.toString(),
generateSecureRandomFn != null );
? GenerateSecureRandomResponse( } else {
random: generateSecureRandomFn!(argRequest.length!), return Future.value(true);
) }
: GenerateSecureRandomResponse( }
random: Uint8List.fromList([1, 2, 3]),
); @override
Future<Uint8List?> encryptWithIV(
@override Uint8List argPlaintext,
Future<HashResponse> hash(HashRequest argRequest) async => hashFn != null Uint8List argIv,
? HashResponse( Uint8List argKey,
hash: hashFn!( CipherAlgorithm argAlgorithm,
argRequest.data!, ) async {
argRequest.algorithm!, if (encryptWithIVFn != null) {
), return encryptWithIVFn!(
) argPlaintext,
: HashResponse( argKey,
hash: Uint8List.fromList([1, 2, 3]), argIv,
); argAlgorithm.toString(),
);
@override } else {
Future<HmacResponse> hmac(HmacRequest argRequest) async => hmacFn != null return Future.value(Uint8List.fromList([1, 2, 3]));
? HmacResponse( }
hmac: hmacFn!( }
argRequest.data!,
argRequest.key!, @override
argRequest.algorithm!, Future<Uint8List?> generateSecureRandom(int argLength) {
), if (generateSecureRandomFn != null) {
) return Future.value(generateSecureRandomFn!(argLength));
: HmacResponse( } else {
hmac: Uint8List.fromList([1, 2, 3]), return Future.value(Uint8List.fromList([1, 2, 3]));
); }
}
@override
Future<Pbkdf2Response> pbkdf2(Pbkdf2Request argRequest) async => @override
pbkdf2Fn != null Future<Uint8List?> hash(Uint8List argData, HashAlgorithm argAlgorithm) {
? Pbkdf2Response( if (hashFn != null) {
key: pbkdf2Fn!( return Future.value(hashFn!(argData, argAlgorithm.toString()));
argRequest.password!, } else {
argRequest.salt!, return Future.value(Uint8List.fromList([1, 2, 3]));
argRequest.iterations!, }
argRequest.length!, }
argRequest.hashAlgorithm!,
), @override
) Future<Uint8List?> hmac(
: Pbkdf2Response( Uint8List argData,
key: Uint8List.fromList([1, 2, 3]), Uint8List argKey,
); HashAlgorithm argAlgorithm,
) {
if (hmacFn != null) {
return Future.value(hmacFn!(argData, argKey, argAlgorithm.toString()));
} else {
return Future.value(Uint8List.fromList([1, 2, 3]));
}
}
@override
Future<Uint8List?> pbkdf2(
Uint8List argPassword,
Uint8List argSalt,
int argLength,
int argIterations,
HashAlgorithm argAlgorithm,
) {
if (pbkdf2Fn != null) {
return Future.value(pbkdf2Fn!(
argPassword,
argSalt,
argIterations,
argLength,
argAlgorithm.toString(),
),);
} else {
return Future.value(Uint8List.fromList([1, 2, 3]));
}
}
} }