From 2b11fb8157047d9602b79c1d8d86efed336f5b6e Mon Sep 17 00:00:00 2001 From: "Alexander \"PapaTutuWawa" Date: Tue, 8 Nov 2022 12:24:54 +0100 Subject: [PATCH] feat: Test writing and reading --- moxxmpp_socket/.flutter-plugins-dependencies | 2 +- .../integration_test/test_wrong_host.dart | 26 +++++++++-- moxxmpp_socket/lib/src/ssl.dart | 44 +++++++++++++++++-- 3 files changed, 65 insertions(+), 7 deletions(-) diff --git a/moxxmpp_socket/.flutter-plugins-dependencies b/moxxmpp_socket/.flutter-plugins-dependencies index 22cea49..d783f46 100644 --- a/moxxmpp_socket/.flutter-plugins-dependencies +++ b/moxxmpp_socket/.flutter-plugins-dependencies @@ -1 +1 @@ -{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"integration_test","path":"/nix/store/8gcfk0g1lg8gccd9kv3rzj910w9pz1kj-flutter-3.3.3-unwrapped/packages/integration_test/","native_build":true,"dependencies":[]}],"android":[{"name":"integration_test","path":"/nix/store/8gcfk0g1lg8gccd9kv3rzj910w9pz1kj-flutter-3.3.3-unwrapped/packages/integration_test/","native_build":true,"dependencies":[]},{"name":"moxdns_android","path":"/home/alexander/.pub-cache/hosted/git.polynom.me%47api%47packages%47Moxxy%47pub%47/moxdns_android-0.1.4/","native_build":true,"dependencies":[]}],"macos":[],"linux":[{"name":"moxdns_linux","path":"/home/alexander/.pub-cache/hosted/git.polynom.me%47api%47packages%47Moxxy%47pub%47/moxdns_linux-0.1.4/","native_build":true,"dependencies":[]}],"windows":[],"web":[]},"dependencyGraph":[{"name":"integration_test","dependencies":[]},{"name":"moxdns","dependencies":["moxdns_android","moxdns_linux"]},{"name":"moxdns_android","dependencies":["moxdns"]},{"name":"moxdns_linux","dependencies":["moxdns"]}],"date_created":"2022-11-07 22:04:34.759562","version":"3.3.3"} \ No newline at end of file +{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"integration_test","path":"/nix/store/8gcfk0g1lg8gccd9kv3rzj910w9pz1kj-flutter-3.3.3-unwrapped/packages/integration_test/","native_build":true,"dependencies":[]}],"android":[{"name":"integration_test","path":"/nix/store/8gcfk0g1lg8gccd9kv3rzj910w9pz1kj-flutter-3.3.3-unwrapped/packages/integration_test/","native_build":true,"dependencies":[]},{"name":"moxdns_android","path":"/home/alexander/.pub-cache/hosted/git.polynom.me%47api%47packages%47Moxxy%47pub%47/moxdns_android-0.1.4/","native_build":true,"dependencies":[]}],"macos":[],"linux":[{"name":"moxdns_linux","path":"/home/alexander/.pub-cache/hosted/git.polynom.me%47api%47packages%47Moxxy%47pub%47/moxdns_linux-0.1.4/","native_build":true,"dependencies":[]}],"windows":[],"web":[]},"dependencyGraph":[{"name":"integration_test","dependencies":[]},{"name":"moxdns","dependencies":["moxdns_android","moxdns_linux"]},{"name":"moxdns_android","dependencies":["moxdns"]},{"name":"moxdns_linux","dependencies":["moxdns"]}],"date_created":"2022-11-08 12:16:15.896518","version":"3.3.3"} \ No newline at end of file diff --git a/moxxmpp_socket/integration_test/test_wrong_host.dart b/moxxmpp_socket/integration_test/test_wrong_host.dart index 2e3f153..b7f6d5c 100644 --- a/moxxmpp_socket/integration_test/test_wrong_host.dart +++ b/moxxmpp_socket/integration_test/test_wrong_host.dart @@ -1,21 +1,25 @@ +import 'dart:convert'; import 'dart:io'; +import 'dart:typed_data'; import 'package:moxxmpp_socket/src/ssl.dart'; void main(List argv) async { - if (argv.length != 2) { + if (argv.length < 2) { print('Usage: test_wrong_host.dart server-addr host-name'); exit(1); } final server = argv[0]; final hostname = argv[1]; + final port = argv.length == 3 ? argv[2] : '5223'; + final ctx = MbedSockCtx('/etc/ssl/certs/'); final sock = MbedSock(ctx); - print('Connecting...'); + print('Connecting to $server:$port while indicating $hostname...'); final done = sock.connectSecure( server, - '5223', + port, alpn: 'xmpp-client', hostname: hostname, ); @@ -23,6 +27,22 @@ void main(List argv) async { print('Success? $done'); print('Secure? ${sock.isSecure()}'); + final write = sock.write( + "" + ); + print('Write: $write'); + + Uint8List? read = Uint8List(0); + do { + read = sock.read(); + if (read != null) { + final str = utf8.decode(read); + print('Read: $str'); + } else { + print('Read: Null'); + } + } while (read != null); + sock.free(); ctx.free(); print('OKAY'); diff --git a/moxxmpp_socket/lib/src/ssl.dart b/moxxmpp_socket/lib/src/ssl.dart index 6ae5e7e..4a2425b 100644 --- a/moxxmpp_socket/lib/src/ssl.dart +++ b/moxxmpp_socket/lib/src/ssl.dart @@ -1,5 +1,6 @@ -import 'dart:io'; +import 'dart:convert'; import 'dart:ffi'; +import 'dart:io'; import 'dart:typed_data'; import 'package:ffi/ffi.dart'; import 'package:path/path.dart' as path; @@ -26,9 +27,13 @@ class MbedSockCtx { class MbedSock { late Pointer sock; + late Pointer _readBuf; + late Pointer _writeBuf; MbedSock(MbedSockCtx ctx) { sock = lib.mbedsock_new_ex(ctx.ctx); + _readBuf = malloc.call(2048); + _writeBuf = malloc.call(2048); } bool connect(String host, int port) { @@ -75,12 +80,45 @@ class MbedSock { bool isSecure() { return lib.mbedsock_is_secure(sock) == 1; } + + int write(String data) { + final rawData = utf8.encode(data); - void write(String data) { - //lib.mbedsock_write(sock, data, data.length); + // TODO: Buffer the write + assert(rawData.length <= 2048); + + _writeBuf.asTypedList(2048).setAll(0, rawData); + return lib.mbedsock_write( + sock, + _writeBuf, + rawData.length, + ); + } + + Uint8List? read() { + final result = lib.mbedsock_read( + sock, + _readBuf, + 2048, + ); + + // TODO: Buffer the read + assert(result <= 2048); + + if (result < 0) { + print('Socket error'); + return null; + } else if (result == 0) { + print('Socket closed'); + return null; + } else { + return _readBuf.asTypedList(result) as Uint8List; + } } void free() { lib.mbedsock_free_ex(sock); + malloc.free(_readBuf); + malloc.free(_writeBuf); } }