Skip to content

Commit

Permalink
✅ Integrate tests as much as possible
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexV525 committed Oct 11, 2024
1 parent bd94ab0 commit be41886
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 89 deletions.
8 changes: 3 additions & 5 deletions packages/agent_dart_base/test/agent_dart_base_test.dart
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -16,6 +15,5 @@ void main() async {
identity.main();
principal.main();
utils.main();
signer.main();
pem.main();
wallet.main();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ import 'dart:typed_data';
import 'package:agent_dart_base/agent_dart_base.dart';
import 'package:test/test.dart';

import '../test_utils.dart';

Future<SignIdentity> createIdentity(int seed) async {
final seed1 = List.filled(32, 0);
seed1[0] = seed;
return Ed25519KeyIdentity.generate(Uint8List.fromList(seed1));
}

void main() {
ffiInit();
authenticationTest();
}

Expand Down
2 changes: 1 addition & 1 deletion packages/agent_dart_base/test/candid/idl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
25 changes: 11 additions & 14 deletions packages/agent_dart_base/test/identity/delegation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,20 @@ import 'dart:typed_data';
import 'package:agent_dart_base/agent_dart_base.dart';
import 'package:test/test.dart';

Future<SignIdentity> 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<SignIdentity> _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,
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions packages/agent_dart_base/test/identity/identity.dart
Original file line number Diff line number Diff line change
@@ -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();
}
2 changes: 0 additions & 2 deletions packages/agent_dart_base/test/identity/p256.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import 'package:test/test.dart';
import '../test_utils.dart';

void main() {
ffiInit();
// matchFFI();
p256Test();
}

Expand Down
3 changes: 0 additions & 3 deletions packages/agent_dart_base/test/identity/schnorr.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
9 changes: 5 additions & 4 deletions packages/agent_dart_base/test/principal/index.dart
Original file line number Diff line number Diff line change
@@ -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();
}
77 changes: 17 additions & 60 deletions packages/agent_dart_base/test/test_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@ import 'dart:ffi';
import 'package:agent_dart_ffi/agent_dart_ffi.dart';
import 'package:test/test.dart';

Future<void> 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<AssertionError>();

Matcher assertionThrowsContains(String str) {
Expand All @@ -25,63 +42,3 @@ class ErrorMessageMatcher<T extends Error> extends TypeMatcher<T> {
bool matches(Object? item, Map matchState) =>
item is T && (_message == null || (item as dynamic).message == _message);
}

Future<void> 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)));
// }
2 changes: 2 additions & 0 deletions packages/agent_dart_base/test/utils/utils_test.dart
Original file line number Diff line number Diff line change
@@ -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', () {
Expand Down
11 changes: 11 additions & 0 deletions packages/agent_dart_base/test/wallet/index.dart
Original file line number Diff line number Diff line change
@@ -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();
}

0 comments on commit be41886

Please sign in to comment.