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<DecryptFileResponse> decryptFile( Future<bool?> decryptFile(
DecryptFileRequest argRequest, String argCiphertextpath,
) async => String argPlaintextpath,
decryptFileFn != null Uint8List argKey,
? DecryptFileResponse( CipherAlgorithm argAlgorithm,
success: decryptFileFn!( ) async {
argRequest.cipherTextPath!, if (decryptFileFn != null) {
argRequest.plainTextPath!, return decryptFileFn!(
argRequest.key!, argCiphertextpath,
argRequest.algorithm!, argPlaintextpath,
), argKey,
) argAlgorithm.toString(),
: DecryptFileResponse(success: true); );
} else {
return Future.value(true);
}
}
@override @override
Future<EncryptResponse> encrypt(EncryptRequest argRequest) async => Future<Uint8List?> encrypt(
encryptFn != null Uint8List argPlaintext,
? EncryptResponse( Uint8List argKey,
cipherText: encryptFn!( CipherAlgorithm argAlgorithm,
argRequest.plainText!, ) async {
argRequest.key!, if (encryptFn != null) {
argRequest.algorithm!, return encryptFn!(argPlaintext, argKey, argAlgorithm.toString());
), } else {
) return Uint8List.fromList([1, 2, 3]);
: EncryptResponse( }
cipherText: Uint8List.fromList([1, 2, 3]), }
);
@override @override
Future<EncryptFileResponse> encryptFile( Future<bool?> encryptFile(
EncryptFileRequest argRequest, String argPlaintextpath,
) async => String argCiphertextpath,
encryptFileFn != null Uint8List argKey,
? EncryptFileResponse( CipherAlgorithm argAlgorithm,
success: encryptFileFn!( ) async {
argRequest.plainTextPath!, if (encryptFileFn != null) {
argRequest.cipherTextPath!, return encryptFileFn!(
argRequest.key!, argPlaintextpath,
argRequest.algorithm!, argCiphertextpath,
), argKey,
) argAlgorithm.toString(),
: EncryptFileResponse(success: true); );
} else {
return Future.value(true);
}
}
@override @override
Future<EncryptResponse> encryptWithIV( Future<bool?> encryptFileWithIV(
EncryptWithIVRequest argRequest, String argPlaintextpath,
) async => String argCiphertextpath,
encryptWithIVFn != null Uint8List argIv,
? EncryptResponse( Uint8List argKey,
cipherText: encryptWithIVFn!( CipherAlgorithm argAlgorithm,
argRequest.plainText!, ) async {
argRequest.key!, if (encryptFileWithIVFn != null) {
argRequest.iv!, return encryptFileWithIVFn!(
argRequest.algorithm!, argPlaintextpath,
), argCiphertextpath,
) argKey,
: EncryptResponse( argIv,
cipherText: Uint8List.fromList([1, 2, 3]), argAlgorithm.toString(),
); );
} else {
return Future.value(true);
}
}
@override @override
Future<GenerateSecureRandomResponse> generateSecureRandom( Future<Uint8List?> encryptWithIV(
GenerateSecureRandomRequest argRequest, Uint8List argPlaintext,
) async => Uint8List argIv,
generateSecureRandomFn != null Uint8List argKey,
? GenerateSecureRandomResponse( CipherAlgorithm argAlgorithm,
random: generateSecureRandomFn!(argRequest.length!), ) async {
) if (encryptWithIVFn != null) {
: GenerateSecureRandomResponse( return encryptWithIVFn!(
random: Uint8List.fromList([1, 2, 3]), argPlaintext,
); argKey,
argIv,
argAlgorithm.toString(),
);
} else {
return Future.value(Uint8List.fromList([1, 2, 3]));
}
}
@override @override
Future<HashResponse> hash(HashRequest argRequest) async => hashFn != null Future<Uint8List?> generateSecureRandom(int argLength) {
? HashResponse( if (generateSecureRandomFn != null) {
hash: hashFn!( return Future.value(generateSecureRandomFn!(argLength));
argRequest.data!, } else {
argRequest.algorithm!, return Future.value(Uint8List.fromList([1, 2, 3]));
), }
) }
: HashResponse(
hash: Uint8List.fromList([1, 2, 3]),
);
@override @override
Future<HmacResponse> hmac(HmacRequest argRequest) async => hmacFn != null Future<Uint8List?> hash(Uint8List argData, HashAlgorithm argAlgorithm) {
? HmacResponse( if (hashFn != null) {
hmac: hmacFn!( return Future.value(hashFn!(argData, argAlgorithm.toString()));
argRequest.data!, } else {
argRequest.key!, return Future.value(Uint8List.fromList([1, 2, 3]));
argRequest.algorithm!, }
), }
)
: HmacResponse(
hmac: Uint8List.fromList([1, 2, 3]),
);
@override @override
Future<Pbkdf2Response> pbkdf2(Pbkdf2Request argRequest) async => Future<Uint8List?> hmac(
pbkdf2Fn != null Uint8List argData,
? Pbkdf2Response( Uint8List argKey,
key: pbkdf2Fn!( HashAlgorithm argAlgorithm,
argRequest.password!, ) {
argRequest.salt!, if (hmacFn != null) {
argRequest.iterations!, return Future.value(hmacFn!(argData, argKey, argAlgorithm.toString()));
argRequest.length!, } else {
argRequest.hashAlgorithm!, return Future.value(Uint8List.fromList([1, 2, 3]));
), }
) }
: Pbkdf2Response(
key: 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]));
}
}
} }