From 6d53567657a74c5afcb6d4308e2450e5205f46bf Mon Sep 17 00:00:00 2001 From: Alex Li Date: Fri, 11 Oct 2024 14:35:23 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Integrate=20tests=20as=20much=20as?= =?UTF-8?q?=20possible?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test/agent_dart_base_test.dart | 8 +- .../test/authentication/authentication.dart | 3 + packages/agent_dart_base/test/candid/idl.dart | 2 +- .../test/identity/delegation.dart | 25 +++--- .../test/identity/identity.dart | 6 ++ .../agent_dart_base/test/identity/p256.dart | 2 - .../test/identity/schnorr.dart | 3 - .../agent_dart_base/test/principal/index.dart | 9 ++- packages/agent_dart_base/test/test_utils.dart | 77 ++++--------------- .../test/utils/utils_test.dart | 2 + .../agent_dart_base/test/wallet/index.dart | 11 +++ 11 files changed, 59 insertions(+), 89 deletions(-) create mode 100644 packages/agent_dart_base/test/wallet/index.dart diff --git a/packages/agent_dart_base/test/agent_dart_base_test.dart b/packages/agent_dart_base/test/agent_dart_base_test.dart index f4bec40d..45015b85 100644 --- a/packages/agent_dart_base/test/agent_dart_base_test.dart +++ b/packages/agent_dart_base/test/agent_dart_base_test.dart @@ -1,12 +1,11 @@ +import './test_utils.dart'; import 'agent/agent.dart' as agent; import 'authentication/authentication.dart' as auth; import 'candid/idl.dart' as candid; import 'identity/identity.dart' as identity; import 'principal/index.dart' as principal; -import 'test_utils.dart'; import 'utils/utils_test.dart' as utils; -import 'wallet/pem.dart' as pem; -import 'wallet/signer.dart' as signer; +import 'wallet/index.dart' as wallet; void main() async { ffiInit(); @@ -16,6 +15,5 @@ void main() async { identity.main(); principal.main(); utils.main(); - signer.main(); - pem.main(); + wallet.main(); } diff --git a/packages/agent_dart_base/test/authentication/authentication.dart b/packages/agent_dart_base/test/authentication/authentication.dart index 0dbdffc3..915e32ec 100644 --- a/packages/agent_dart_base/test/authentication/authentication.dart +++ b/packages/agent_dart_base/test/authentication/authentication.dart @@ -3,6 +3,8 @@ import 'dart:typed_data'; import 'package:agent_dart_base/agent_dart_base.dart'; import 'package:test/test.dart'; +import '../test_utils.dart'; + Future createIdentity(int seed) async { final seed1 = List.filled(32, 0); seed1[0] = seed; @@ -10,6 +12,7 @@ Future createIdentity(int seed) async { } void main() { + ffiInit(); authenticationTest(); } diff --git a/packages/agent_dart_base/test/candid/idl.dart b/packages/agent_dart_base/test/candid/idl.dart index c3f3a5a9..e37245a5 100644 --- a/packages/agent_dart_base/test/candid/idl.dart +++ b/packages/agent_dart_base/test/candid/idl.dart @@ -4,9 +4,9 @@ import 'package:agent_dart_base/agent_dart_base.dart'; import 'package:test/test.dart'; import '../test_utils.dart'; -// ignore: library_prefixes void main() { + ffiInit(); idlTest(); } diff --git a/packages/agent_dart_base/test/identity/delegation.dart b/packages/agent_dart_base/test/identity/delegation.dart index ac2301c8..c9e31a82 100644 --- a/packages/agent_dart_base/test/identity/delegation.dart +++ b/packages/agent_dart_base/test/identity/delegation.dart @@ -4,23 +4,20 @@ import 'dart:typed_data'; import 'package:agent_dart_base/agent_dart_base.dart'; import 'package:test/test.dart'; -Future createIdentity(int seed) { - final s = Uint8List.fromList([seed, ...List.filled(31, 0)]); - return Ed25519KeyIdentity.generate(s); -} - -// BinaryBlob h(String text) { -// return blobFromHex(text.codeUnits.); -// } void main() { delegationTest(); } +Future _createIdentity(int seed) { + final s = Uint8List.fromList([seed, ...List.filled(31, 0)]); + return Ed25519KeyIdentity.generate(s); +} + void delegationTest() { test('delegation signs with proper keys (3)', () async { - final root = await createIdentity(2); - final middle = await createIdentity(1); - final bottom = await createIdentity(0); + final root = await _createIdentity(2); + final middle = await _createIdentity(1); + final bottom = await _createIdentity(0); final rootToMiddle = await DelegationChain.create( root, @@ -66,9 +63,9 @@ void delegationTest() { expect(middleToBottom.toJson(), result); }); test('DelegationChain can be serialized to and from JSON', () async { - final root = await createIdentity(2); - final middle = await createIdentity(1); - final bottom = await createIdentity(0); + final root = await _createIdentity(2); + final middle = await _createIdentity(1); + final bottom = await _createIdentity(0); final rootToMiddle = await DelegationChain.create( root, diff --git a/packages/agent_dart_base/test/identity/identity.dart b/packages/agent_dart_base/test/identity/identity.dart index 2209e69a..1560ea1a 100644 --- a/packages/agent_dart_base/test/identity/identity.dart +++ b/packages/agent_dart_base/test/identity/identity.dart @@ -1,9 +1,15 @@ +import '../test_utils.dart'; import 'delegation.dart' as delegation; import 'ed25519.dart' as ed25519; +import 'p256.dart' as p256; +import 'schnorr.dart' as schnorr; import 'secp256k1.dart' as secp256k1; void main() { + ffiInit(); delegation.main(); ed25519.main(); + p256.main(); + schnorr.main(); secp256k1.main(); } diff --git a/packages/agent_dart_base/test/identity/p256.dart b/packages/agent_dart_base/test/identity/p256.dart index caf8e54c..86a6cc0f 100644 --- a/packages/agent_dart_base/test/identity/p256.dart +++ b/packages/agent_dart_base/test/identity/p256.dart @@ -3,8 +3,6 @@ import 'package:test/test.dart'; import '../test_utils.dart'; void main() { - ffiInit(); - // matchFFI(); p256Test(); } diff --git a/packages/agent_dart_base/test/identity/schnorr.dart b/packages/agent_dart_base/test/identity/schnorr.dart index 609d33f6..11bb3d65 100644 --- a/packages/agent_dart_base/test/identity/schnorr.dart +++ b/packages/agent_dart_base/test/identity/schnorr.dart @@ -2,11 +2,8 @@ import 'dart:typed_data'; import 'package:agent_dart_base/agent_dart_base.dart'; import 'package:test/test.dart'; -import '../test_utils.dart'; void main() { - ffiInit(); - // matchFFI(); schnorrTest(); } diff --git a/packages/agent_dart_base/test/principal/index.dart b/packages/agent_dart_base/test/principal/index.dart index d79b3f82..bd6d80a8 100644 --- a/packages/agent_dart_base/test/principal/index.dart +++ b/packages/agent_dart_base/test/principal/index.dart @@ -1,10 +1,11 @@ +import '../test_utils.dart'; import 'principal.dart' as principal; import 'utils/base32.dart' as base32; -// ignore: library_prefixes -import 'utils/get_crc.dart' as getCrc; +import 'utils/get_crc.dart' as get_crc; void main() { - base32.main(); - getCrc.main(); + ffiInit(); principal.main(); + base32.main(); + get_crc.main(); } diff --git a/packages/agent_dart_base/test/test_utils.dart b/packages/agent_dart_base/test/test_utils.dart index 3dbd422b..40d3cf45 100644 --- a/packages/agent_dart_base/test/test_utils.dart +++ b/packages/agent_dart_base/test/test_utils.dart @@ -3,6 +3,23 @@ import 'dart:ffi'; import 'package:agent_dart_ffi/agent_dart_ffi.dart'; import 'package:test/test.dart'; +Future ffiInit() async { + // ignore: invalid_use_of_internal_member + if (AgentDart.instance.initialized) { + return; + } + final [os, arch] = Abi.current().toString().split('_'); + final libName = switch ((os, arch)) { + ('macos', _) || ('linux', 'arm64') => 'libagent_dart.dylib', + ('linux', _) => 'libagent_dart.so', + ('windows', _) => 'agent_dart.dll', + _ => throw UnsupportedError('$os $arch is not a supported platform.'), + }; + return AgentDart.init( + externalLibrary: ExternalLibrary.open('../../target/debug/$libName'), + ); +} + const isAssertionError = TypeMatcher(); Matcher assertionThrowsContains(String str) { @@ -25,63 +42,3 @@ class ErrorMessageMatcher extends TypeMatcher { bool matches(Object? item, Map matchState) => item is T && (_message == null || (item as dynamic).message == _message); } - -Future ffiInit() { - final [os, arch] = Abi.current().toString().split('_'); - final libName = switch ((os, arch)) { - ('macos', _) || ('linux', 'arm64') => 'libagent_dart.dylib', - ('linux', _) => 'libagent_dart.so', - ('windows', _) => 'agent_dart.dll', - _ => throw UnsupportedError('$os $arch is not a supported platform.'), - }; - return AgentDart.init( - externalLibrary: ExternalLibrary.open('../../target/debug/$libName'), - ); -} - -// void matchFFI() { -// final architech = Abi.current().toString(); -// final arr = architech.split('_'); -// final os = arr[0]; -// final arc = arr[1]; -// const dyLib = 'libagent_dart.dylib'; -// const dySo = 'libagent_dart.so'; -// const dyDll = 'agent_dart.dll'; -// final String lib; -// switch (os) { -// case 'macos': -// { -// if (arc == 'arm64') { -// lib = '../../platform-build/dylib/aarch64-apple-darwin/$dyLib'; -// break; -// } else { -// lib = '../../platform-build/dylib/x86_64-apple-darwin/$dyLib'; -// break; -// } -// } -// case 'linux': -// { -// if (arc == 'arm64') { -// lib = '../../platform-build/dylib/aarch64-unknown-linux-gnu/$dyLib'; -// break; -// } else { -// lib = '../../platform-build/dylib/x86_64-unknown-linux-gnu/$dySo'; -// break; -// } -// } -// case 'windows': -// { -// if (arc == 'arm64') { -// lib = '../../platform-build/dylib/aarch64-pc-windows-msvc/$dyDll'; -// break; -// } else { -// lib = '../../platform-build/dylib/x86_64-pc-windows-msvc/$dyDll'; -// break; -// } -// } -// default: -// throw 'Unsupported OS: $os'; -// } -// print(lib); -// AgentDartFFI.setImpl(AgentDartImpl(DynamicLibrary.open(lib))); -// } diff --git a/packages/agent_dart_base/test/utils/utils_test.dart b/packages/agent_dart_base/test/utils/utils_test.dart index 884b6c3b..67120fbe 100644 --- a/packages/agent_dart_base/test/utils/utils_test.dart +++ b/packages/agent_dart_base/test/utils/utils_test.dart @@ -1,9 +1,11 @@ import 'package:agent_dart_base/agent_dart_base.dart'; import 'package:test/test.dart'; +import '../test_utils.dart'; import 'is.dart' as is_test; void main() { + ffiInit(); is_test.main(); test('Hex string to u8', () { diff --git a/packages/agent_dart_base/test/wallet/index.dart b/packages/agent_dart_base/test/wallet/index.dart new file mode 100644 index 00000000..55668b5d --- /dev/null +++ b/packages/agent_dart_base/test/wallet/index.dart @@ -0,0 +1,11 @@ +import '../test_utils.dart'; +import 'aes.dart' as aes; +import 'pem.dart' as pem; +import 'signer.dart' as signer; + +void main() { + ffiInit(); + aes.main(); + pem.main(); + signer.main(); +}