test(platform): add tests for platform and method channel

This commit is contained in:
Hugo Pointcheval 2022-05-25 23:31:01 +02:00
parent 9aa4eeb567
commit 81335dc350
Signed by: hugo
GPG Key ID: A9E8E9615379254F
3 changed files with 346 additions and 2 deletions

View File

@ -9,15 +9,17 @@ environment:
dependencies:
flutter:
sdk: flutter
plugin_platform_interface: ^2.1.2
dev_dependencies:
flutter_test:
sdk: flutter
mockito: ^5.2.0
wyatt_analysis:
git:
url: https://git.wyatt-studio.fr/Wyatt-FOSS/wyatt-packages
ref: wyatt_analysis-v2.1.0
path: packages/wyatt_analysis
path: packages/wyatt_analysis

View File

@ -0,0 +1,176 @@
// Author: Hugo Pointcheval
// Email: git@pcl.ovh
// -----
// File: method_channel_native_crypto_test.dart
// Created Date: 25/05/2022 22:47:41
// Last Modified: 25/05/2022 23:22:44
// -----
// Copyright (c) 2022
import 'dart:typed_data';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:native_crypto_platform_interface/src/method_channel/method_channel_native_crypto.dart';
void main() {
TestWidgetsFlutterBinding
.ensureInitialized(); // Required for setMockMethodCallHandler
group('$MethodChannelNativeCrypto', () {
const MethodChannel channel =
MethodChannel('plugins.hugop.cl/native_crypto');
final List<MethodCall> log = <MethodCall>[];
final MethodChannelNativeCrypto nativeCrypto = MethodChannelNativeCrypto();
TestDefaultBinaryMessengerBinding.instance?.defaultBinaryMessenger
.setMockMethodCallHandler(channel, (MethodCall call) async {
log.add(call);
return null;
});
// Run after each test.
tearDown(log.clear);
test('digest', () async {
await nativeCrypto.digest(Uint8List(0), 'sha256');
expect(
log,
<Matcher>[
isMethodCall(
'digest',
arguments: <String, dynamic>{
'data': Uint8List(0),
'algorithm': 'sha256',
},
),
],
);
});
test('generateSecretKey', () async {
await nativeCrypto.generateSecretKey(256);
expect(
log,
<Matcher>[
isMethodCall(
'generateSecretKey',
arguments: <String, dynamic>{
'bitsCount': 256,
},
),
],
);
});
test('pbkdf2', () async {
await nativeCrypto.pbkdf2(
'password',
'salt',
32,
10000,
'sha256',
);
expect(
log,
<Matcher>[
isMethodCall(
'pbkdf2',
arguments: <String, dynamic>{
'password': 'password',
'salt': 'salt',
'keyBytesCount': 32,
'iterations': 10000,
'algorithm': 'sha256',
},
),
],
);
});
test('encryptAsList', () async {
await nativeCrypto.encryptAsList(
Uint8List(0),
Uint8List(0),
'aes',
);
expect(
log,
<Matcher>[
isMethodCall(
'encryptAsList',
arguments: <String, dynamic>{
'data': Uint8List(0),
'key': Uint8List(0),
'algorithm': 'aes',
},
),
],
);
});
test('decryptAsList', () async {
await nativeCrypto.decryptAsList(
[Uint8List(0)],
Uint8List(0),
'aes',
);
expect(
log,
<Matcher>[
isMethodCall(
'decryptAsList',
arguments: <String, dynamic>{
'data': [Uint8List(0)],
'key': Uint8List(0),
'algorithm': 'aes',
},
),
],
);
});
test('encrypt', () async {
await nativeCrypto.encrypt(
Uint8List(0),
Uint8List(0),
'aes',
);
expect(
log,
<Matcher>[
isMethodCall(
'encrypt',
arguments: <String, dynamic>{
'data': Uint8List(0),
'key': Uint8List(0),
'algorithm': 'aes',
},
),
],
);
});
test('decrypt', () async {
await nativeCrypto.decrypt(
Uint8List(0),
Uint8List(0),
'aes',
);
expect(
log,
<Matcher>[
isMethodCall(
'decrypt',
arguments: <String, dynamic>{
'data': Uint8List(0),
'key': Uint8List(0),
'algorithm': 'aes',
},
),
],
);
});
});
}

View File

@ -0,0 +1,166 @@
// Author: Hugo Pointcheval
// Email: git@pcl.ovh
// -----
// File: native_crypto_platform_test.dart
// Created Date: 25/05/2022 21:43:25
// Last Modified: 25/05/2022 23:26:18
// -----
// Copyright (c) 2022
import 'dart:typed_data';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:native_crypto_platform_interface/src/platform_interface/native_crypto_platform.dart';
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
void main() {
late ExtendsNativeCryptoPlatform nativeCryptoPlatform;
group('$NativeCryptoPlatform', () {
setUpAll(() {
nativeCryptoPlatform = ExtendsNativeCryptoPlatform();
});
test('Constructor', () {
expect(nativeCryptoPlatform, isA<NativeCryptoPlatform>());
expect(nativeCryptoPlatform, isA<PlatformInterface>());
});
test('get.instance', () {
expect(
NativeCryptoPlatform.instance,
isA<NativeCryptoPlatform>(),
);
});
test('set.instance', () {
NativeCryptoPlatform.instance = ExtendsNativeCryptoPlatform();
expect(
NativeCryptoPlatform.instance,
isA<NativeCryptoPlatform>(),
);
});
test('Cannot be implemented with `implements`', () {
expect(
() {
NativeCryptoPlatform.instance = ImplementsNativeCryptoPlatform();
},
throwsA(isInstanceOf<AssertionError>()),
);
});
test('Can be mocked with `implements`', () {
final MockNativeCryptoPlatform mock = MockNativeCryptoPlatform();
NativeCryptoPlatform.instance = mock;
});
test('Can be extended', () {
NativeCryptoPlatform.instance = ExtendsNativeCryptoPlatform();
});
test('throws if .digest() not implemented', () async {
await expectLater(
() => nativeCryptoPlatform.digest(Uint8List(0), 'sha256'),
throwsA(
isA<UnimplementedError>().having(
(e) => e.message,
'message',
'digest is not implemented',
),
),
);
});
test('throws if .generateSecretKey() not implemented', () async {
await expectLater(
() => nativeCryptoPlatform.generateSecretKey(256),
throwsA(
isA<UnimplementedError>().having(
(e) => e.message,
'message',
'generateSecretKey is not implemented',
),
),
);
});
test('throws if .pbkdf2() not implemented', () async {
await expectLater(
() => nativeCryptoPlatform.pbkdf2('password', 'salt', 0, 0, 'sha256'),
throwsA(
isA<UnimplementedError>().having(
(e) => e.message,
'message',
'pbkdf2 is not implemented',
),
),
);
});
test('throws if .encryptAsList() not implemented', () async {
await expectLater(
() => nativeCryptoPlatform.encryptAsList(
Uint8List(0),
Uint8List(0),
'aes',
),
throwsA(
isA<UnimplementedError>().having(
(e) => e.message,
'message',
'encryptAsList is not implemented',
),
),
);
});
test('throws if .decryptAsList() not implemented', () async {
await expectLater(
() => nativeCryptoPlatform
.decryptAsList([Uint8List(0)], Uint8List(0), 'aes'),
throwsA(
isA<UnimplementedError>().having(
(e) => e.message,
'message',
'decryptAsList is not implemented',
),
),
);
});
test('throws if .encrypt() not implemented', () async {
await expectLater(
() => nativeCryptoPlatform.encrypt(Uint8List(0), Uint8List(0), 'aes'),
throwsA(
isA<UnimplementedError>().having(
(e) => e.message,
'message',
'encrypt is not implemented',
),
),
);
});
test('throws if .decrypt() not implemented', () async {
await expectLater(
() => nativeCryptoPlatform.decrypt(Uint8List(0), Uint8List(0), 'aes'),
throwsA(
isA<UnimplementedError>().having(
(e) => e.message,
'message',
'decrypt is not implemented',
),
),
);
});
});
}
class ExtendsNativeCryptoPlatform extends NativeCryptoPlatform {}
class ImplementsNativeCryptoPlatform extends Mock
implements NativeCryptoPlatform {}
class MockNativeCryptoPlatform extends Mock
with MockPlatformInterfaceMixin
implements NativeCryptoPlatform {}