feat: Test writing and reading

This commit is contained in:
PapaTutuWawa 2022-11-08 12:24:54 +01:00
parent 4ef73546aa
commit 2b11fb8157
3 changed files with 65 additions and 7 deletions

View File

@ -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"} {"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"}

View File

@ -1,21 +1,25 @@
import 'dart:convert';
import 'dart:io'; import 'dart:io';
import 'dart:typed_data';
import 'package:moxxmpp_socket/src/ssl.dart'; import 'package:moxxmpp_socket/src/ssl.dart';
void main(List<String> argv) async { void main(List<String> argv) async {
if (argv.length != 2) { if (argv.length < 2) {
print('Usage: test_wrong_host.dart server-addr host-name'); print('Usage: test_wrong_host.dart server-addr host-name');
exit(1); exit(1);
} }
final server = argv[0]; final server = argv[0];
final hostname = argv[1]; final hostname = argv[1];
final port = argv.length == 3 ? argv[2] : '5223';
final ctx = MbedSockCtx('/etc/ssl/certs/'); final ctx = MbedSockCtx('/etc/ssl/certs/');
final sock = MbedSock(ctx); final sock = MbedSock(ctx);
print('Connecting...'); print('Connecting to $server:$port while indicating $hostname...');
final done = sock.connectSecure( final done = sock.connectSecure(
server, server,
'5223', port,
alpn: 'xmpp-client', alpn: 'xmpp-client',
hostname: hostname, hostname: hostname,
); );
@ -23,6 +27,22 @@ void main(List<String> argv) async {
print('Success? $done'); print('Success? $done');
print('Secure? ${sock.isSecure()}'); print('Secure? ${sock.isSecure()}');
final write = sock.write(
"<?xml version='1.0'?><stream:stream to='$hostname' version='1.0' xml:lang='en' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'>"
);
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(); sock.free();
ctx.free(); ctx.free();
print('OKAY'); print('OKAY');

View File

@ -1,5 +1,6 @@
import 'dart:io'; import 'dart:convert';
import 'dart:ffi'; import 'dart:ffi';
import 'dart:io';
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:ffi/ffi.dart'; import 'package:ffi/ffi.dart';
import 'package:path/path.dart' as path; import 'package:path/path.dart' as path;
@ -26,9 +27,13 @@ class MbedSockCtx {
class MbedSock { class MbedSock {
late Pointer<libmbedsock.mbedsock> sock; late Pointer<libmbedsock.mbedsock> sock;
late Pointer<Uint8> _readBuf;
late Pointer<Uint8> _writeBuf;
MbedSock(MbedSockCtx ctx) { MbedSock(MbedSockCtx ctx) {
sock = lib.mbedsock_new_ex(ctx.ctx); sock = lib.mbedsock_new_ex(ctx.ctx);
_readBuf = malloc.call<Uint8>(2048);
_writeBuf = malloc.call<Uint8>(2048);
} }
bool connect(String host, int port) { bool connect(String host, int port) {
@ -76,11 +81,44 @@ class MbedSock {
return lib.mbedsock_is_secure(sock) == 1; return lib.mbedsock_is_secure(sock) == 1;
} }
void write(String data) { int write(String data) {
//lib.mbedsock_write(sock, data, data.length); final rawData = utf8.encode(data);
// 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() { void free() {
lib.mbedsock_free_ex(sock); lib.mbedsock_free_ex(sock);
malloc.free(_readBuf);
malloc.free(_writeBuf);
} }
} }