diff --git a/packages/native_crypto_platform_interface/pubspec.yaml b/packages/native_crypto_platform_interface/pubspec.yaml index 8bd0ca3..57593de 100644 --- a/packages/native_crypto_platform_interface/pubspec.yaml +++ b/packages/native_crypto_platform_interface/pubspec.yaml @@ -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 \ No newline at end of file + path: packages/wyatt_analysis diff --git a/packages/native_crypto_platform_interface/test/method_channel/method_channel_native_crypto_test.dart b/packages/native_crypto_platform_interface/test/method_channel/method_channel_native_crypto_test.dart new file mode 100644 index 0000000..5f59f97 --- /dev/null +++ b/packages/native_crypto_platform_interface/test/method_channel/method_channel_native_crypto_test.dart @@ -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 log = []; + 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, + [ + isMethodCall( + 'digest', + arguments: { + 'data': Uint8List(0), + 'algorithm': 'sha256', + }, + ), + ], + ); + }); + + test('generateSecretKey', () async { + await nativeCrypto.generateSecretKey(256); + expect( + log, + [ + isMethodCall( + 'generateSecretKey', + arguments: { + 'bitsCount': 256, + }, + ), + ], + ); + }); + + test('pbkdf2', () async { + await nativeCrypto.pbkdf2( + 'password', + 'salt', + 32, + 10000, + 'sha256', + ); + expect( + log, + [ + isMethodCall( + 'pbkdf2', + arguments: { + 'password': 'password', + 'salt': 'salt', + 'keyBytesCount': 32, + 'iterations': 10000, + 'algorithm': 'sha256', + }, + ), + ], + ); + }); + + test('encryptAsList', () async { + await nativeCrypto.encryptAsList( + Uint8List(0), + Uint8List(0), + 'aes', + ); + expect( + log, + [ + isMethodCall( + 'encryptAsList', + arguments: { + 'data': Uint8List(0), + 'key': Uint8List(0), + 'algorithm': 'aes', + }, + ), + ], + ); + }); + + test('decryptAsList', () async { + await nativeCrypto.decryptAsList( + [Uint8List(0)], + Uint8List(0), + 'aes', + ); + expect( + log, + [ + isMethodCall( + 'decryptAsList', + arguments: { + 'data': [Uint8List(0)], + 'key': Uint8List(0), + 'algorithm': 'aes', + }, + ), + ], + ); + }); + + test('encrypt', () async { + await nativeCrypto.encrypt( + Uint8List(0), + Uint8List(0), + 'aes', + ); + expect( + log, + [ + isMethodCall( + 'encrypt', + arguments: { + 'data': Uint8List(0), + 'key': Uint8List(0), + 'algorithm': 'aes', + }, + ), + ], + ); + }); + + test('decrypt', () async { + await nativeCrypto.decrypt( + Uint8List(0), + Uint8List(0), + 'aes', + ); + expect( + log, + [ + isMethodCall( + 'decrypt', + arguments: { + 'data': Uint8List(0), + 'key': Uint8List(0), + 'algorithm': 'aes', + }, + ), + ], + ); + }); + + }); +} diff --git a/packages/native_crypto_platform_interface/test/platform_interface/native_crypto_platform_test.dart b/packages/native_crypto_platform_interface/test/platform_interface/native_crypto_platform_test.dart new file mode 100644 index 0000000..acd0f9d --- /dev/null +++ b/packages/native_crypto_platform_interface/test/platform_interface/native_crypto_platform_test.dart @@ -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()); + expect(nativeCryptoPlatform, isA()); + }); + + test('get.instance', () { + expect( + NativeCryptoPlatform.instance, + isA(), + ); + }); + test('set.instance', () { + NativeCryptoPlatform.instance = ExtendsNativeCryptoPlatform(); + expect( + NativeCryptoPlatform.instance, + isA(), + ); + }); + + test('Cannot be implemented with `implements`', () { + expect( + () { + NativeCryptoPlatform.instance = ImplementsNativeCryptoPlatform(); + }, + throwsA(isInstanceOf()), + ); + }); + + 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().having( + (e) => e.message, + 'message', + 'digest is not implemented', + ), + ), + ); + }); + + test('throws if .generateSecretKey() not implemented', () async { + await expectLater( + () => nativeCryptoPlatform.generateSecretKey(256), + throwsA( + isA().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().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().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().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().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().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 {}