Obsolete
/
libipc-old
Archived
3
0
Fork 0

Proper tests.

master
Philippe Pittoli 2022-12-22 09:58:40 +01:00
parent 91995657dd
commit 72ad635874
1 changed files with 132 additions and 108 deletions

View File

@ -62,14 +62,51 @@ pub const Message = struct {
self.allocator.free(self.payload); self.allocator.free(self.payload);
} }
pub fn read(buffer: []const u8, allocator: std.mem.Allocator) !Self {
// var payload = allocator.
// defer allocator.free(payload);
var fbs = std.io.fixedBufferStream(&buffer);
var reader = fbs.reader();
const msg_type = try reader.readByte();
const msg_len = try reader.readIntBig(u32);
const msg_payload = buffer[5..5+msg_len];
return try Message.init(0, msg_type, allocator, msg_payload);
}
pub fn write(self: Self, writer: anytype) !usize {
try writer.writeByte(self.t);
try writer.writeIntBig(u32, self.payload.len);
return try writer.write(self.payload);
}
pub fn format(self: Self, comptime _: []const u8, _: fmt.FormatOptions, out_stream: anytype) !void { pub fn format(self: Self, comptime _: []const u8, _: fmt.FormatOptions, out_stream: anytype) !void {
try fmt.format(out_stream, "fd: {}, {}, payload: [{s}]", try fmt.format(out_stream, "fd: {}, {}, payload: [{s}]",
.{self.fd, self.t, self.payload} ); .{self.fd, self.t, self.payload} );
} }
}; };
fn print_eq(expected: anytype, obj: anytype) !void {
var buffer: [4096]u8 = undefined;
var fbs = std.io.fixedBufferStream(&buffer);
var writer = fbs.writer();
try writer.print("{}", .{obj});
// print("print_eq, expected: {s}\n", .{expected});
// print("print_eq: {s}\n", .{fbs.getWritten()});
// typing workaround
var secbuffer: [4096]u8 = undefined;
var secfbs = std.io.fixedBufferStream(&secbuffer);
var secwriter = secfbs.writer();
try secwriter.print("{s}", .{expected});
try std.testing.expectEqualSlices(u8, secfbs.getWritten(), fbs.getWritten());
}
test "Message - creation and display" { test "Message - creation and display" {
print("\n", .{});
// fd type payload // fd type payload
const config = .{.safety = true}; const config = .{.safety = true};
var gpa = std.heap.GeneralPurposeAllocator(config){}; var gpa = std.heap.GeneralPurposeAllocator(config){};
@ -80,8 +117,7 @@ test "Message - creation and display" {
var m = try Message.init(1, Message.Type.DATA, allocator, s); var m = try Message.init(1, Message.Type.DATA, allocator, s);
defer m.deinit(); defer m.deinit();
print("message:\t[{}]\n", .{m}); try print_eq("fd: 1, main.Message.Type.DATA, payload: [hello!!]", m);
print("\n", .{});
} }
pub const Event = struct { pub const Event = struct {
@ -91,9 +127,9 @@ pub const Event = struct {
// disconnections, errors or messages from their pairs. They also can // disconnections, errors or messages from their pairs. They also can
// set a timer so the loop will allow a periodic routine (sending ping // set a timer so the loop will allow a periodic routine (sending ping
// messages for websockets, for instance). // messages for websockets, for instance).
// //
// A few other events can occur. // A few other events can occur.
// //
// Extra socket // Extra socket
// The main loop waiting for an event can be used as an unique entry // The main loop waiting for an event can be used as an unique entry
// point for socket management. libipc users can register sockets via // point for socket management. libipc users can register sockets via
@ -161,7 +197,6 @@ pub const Event = struct {
}; };
test "Event - creation and display" { test "Event - creation and display" {
print("\n", .{});
const config = .{.safety = true}; const config = .{.safety = true};
var gpa = std.heap.GeneralPurposeAllocator(config){}; var gpa = std.heap.GeneralPurposeAllocator(config){};
defer _ = gpa.deinit(); defer _ = gpa.deinit();
@ -172,8 +207,7 @@ test "Event - creation and display" {
defer m.deinit(); defer m.deinit();
var e = Event.init(Event.Type.CONNECTION, 5, 8, &m); // type index origin message var e = Event.init(Event.Type.CONNECTION, 5, 8, &m); // type index origin message
print("event:\t[{}]\n", .{e}); try print_eq("main.Event.Type.CONNECTION, origin: 8, index 5, message: [fd: 1, main.Message.Type.DATA, payload: [hello!!]]", e);
print("\n", .{});
} }
pub const CBEvent = struct { pub const CBEvent = struct {
@ -183,9 +217,9 @@ pub const CBEvent = struct {
// disconnections, errors or messages from their pairs. They also can // disconnections, errors or messages from their pairs. They also can
// set a timer so the loop will allow a periodic routine (sending ping // set a timer so the loop will allow a periodic routine (sending ping
// messages for websockets, for instance). // messages for websockets, for instance).
// //
// A few other events can occur. // A few other events can occur.
// //
// Extra socket // Extra socket
// The main loop waiting for an event can be used as an unique entry // The main loop waiting for an event can be used as an unique entry
// point for socket management. libipc users can register sockets via // point for socket management. libipc users can register sockets via
@ -261,16 +295,14 @@ pub const Connection = struct {
}; };
test "Connection - creation and display" { test "Connection - creation and display" {
print("\n", .{});
// origin destination // origin destination
var path = "/some/path"; var path = "/some/path";
var c1 = Connection.init(Connection.Type.EXTERNAL, path); var c1 = Connection.init(Connection.Type.EXTERNAL, path);
defer c1.deinit(); defer c1.deinit();
var c2 = Connection.init(Connection.Type.IPC , null); var c2 = Connection.init(Connection.Type.IPC , null);
defer c2.deinit(); defer c2.deinit();
print("connection 1:\t[{}]\n", .{c1}); try print_eq("main.Connection.Type.EXTERNAL, path /some/path", c1);
print("connection 2:\t[{}]\n", .{c2}); try print_eq("main.Connection.Type.IPC, path null", c2);
print("\n", .{});
} }
// TODO: default callbacks, actual switching. // TODO: default callbacks, actual switching.
@ -299,25 +331,6 @@ pub const Switch = struct {
} }
}; };
fn print_eq(expected: anytype, obj: anytype) !void {
var buffer: [4096]u8 = undefined;
var fbs = std.io.fixedBufferStream(&buffer);
var writer = fbs.writer();
try writer.print("{}", .{obj});
// print("print_eq, expected: {s}\n", .{expected});
// print("print_eq: {s}\n", .{fbs.getWritten()});
// typing workaround
var secbuffer: [4096]u8 = undefined;
var secfbs = std.io.fixedBufferStream(&secbuffer);
var secwriter = secfbs.writer();
try secwriter.print("{s}", .{expected});
try std.testing.expectEqualSlices(u8, secfbs.getWritten(), fbs.getWritten());
}
test "Switch - creation and display" { test "Switch - creation and display" {
const config = .{.safety = true}; const config = .{.safety = true};
var gpa = std.heap.GeneralPurposeAllocator(config){}; var gpa = std.heap.GeneralPurposeAllocator(config){};
@ -335,9 +348,7 @@ test "Switch - creation and display" {
try print_eq("switch 3 <-> 8", first); try print_eq("switch 3 <-> 8", first);
try print_eq("switch 2 <-> 4", second); try print_eq("switch 2 <-> 4", second);
print("\nswitch:\t[{}]\n", .{first}); try std.testing.expect(2 == switchdb.items.len);
print( "switch:\t[{}]\n", .{second});
print("switchdb:\t[{}]\n\n", .{switchdb});
} }
// Context of the whole networking state. // Context of the whole networking state.
@ -369,7 +380,7 @@ pub const Context = struct {
pub fn init(allocator: std.mem.Allocator) !Self { pub fn init(allocator: std.mem.Allocator) !Self {
var rundir = std.process.getEnvVarOwned(allocator, "RUNDIR") catch |err| switch(err) { var rundir = std.process.getEnvVarOwned(allocator, "RUNDIR") catch |err| switch(err) {
error.EnvironmentVariableNotFound => blk: { error.EnvironmentVariableNotFound => blk: {
print("RUNTIME variable not set, using default /tmp/libipc-run/\n", .{}); // print("RUNTIME variable not set, using default /tmp/libipc-run/\n", .{});
break :blk try allocator.dupeZ(u8, "/tmp/libipc-run/"); break :blk try allocator.dupeZ(u8, "/tmp/libipc-run/");
}, },
else => { else => {
@ -419,24 +430,24 @@ pub const Context = struct {
// Return the new fd. Can be useful to the caller. // Return the new fd. Can be useful to the caller.
pub fn connect(self: *Self, path: []const u8) !i32 { pub fn connect(self: *Self, path: []const u8) !i32 {
print("connection to:\t{s}\n", .{path}); // print("connection to:\t{s}\n", .{path});
return self.connect_ (Connection.Type.IPC, path); return self.connect_ (Connection.Type.IPC, path);
} }
// Connection to a service, but with switched with the client fd. // Connection to a service, but with switched with the client fd.
pub fn connection_switched(self: *Self // pub fn connection_switched(self: *Self
, path: [] const u8 // , path: [] const u8
, clientfd: i32) !i32 { // , clientfd: i32) !i32 {
print("connection switched from {} to path {s}\n", .{clientfd, path}); // // print("connection switched from {} to path {s}\n", .{clientfd, path});
var newfd = try self.connect_ (Connection.Type.SWITCHED, path); // var newfd = try self.connect_ (Connection.Type.SWITCHED, path);
// TODO: record switch. // // TODO: record switch.
return newfd; // return newfd;
} // }
// Create a unix socket. // Create a unix socket.
// Store std lib structures in the context. // Store std lib structures in the context.
pub fn server_init(self: *Self, path: [] const u8) !net.StreamServer { pub fn server_init(self: *Self, path: [] const u8) !net.StreamServer {
print("context server init {s}\n", .{path}); // print("context server init {s}\n", .{path});
var server = net.StreamServer.init(.{}); var server = net.StreamServer.init(.{});
var socket_addr = try net.Address.initUnix(path); var socket_addr = try net.Address.initUnix(path);
try server.listen(socket_addr); try server.listen(socket_addr);
@ -556,7 +567,7 @@ const CommunicationTestThread = struct {
var path = fbs.getWritten(); var path = fbs.getWritten();
const socket = try net.connectUnixSocket(path); const socket = try net.connectUnixSocket(path);
defer socket.close(); defer socket.close();
print("So we're a client now... path: {s}\n", .{path}); // print("So we're a client now... path: {s}\n", .{path});
_ = try socket.writer().writeAll("Hello world!"); _ = try socket.writer().writeAll("Hello world!");
} }
}; };
@ -579,8 +590,6 @@ test "Simple structures - init, display and memory check" {
} }
test "Context - creation, display and memory check" { test "Context - creation, display and memory check" {
print("\n", .{});
const config = .{.safety = true}; const config = .{.safety = true};
var gpa = std.heap.GeneralPurposeAllocator(config){}; var gpa = std.heap.GeneralPurposeAllocator(config){};
defer _ = gpa.deinit(); defer _ = gpa.deinit();
@ -616,64 +625,79 @@ test "Context - creation, display and memory check" {
try testing.expectEqualSlices(u8, "Hello world!", buf[0..n]); try testing.expectEqualSlices(u8, "Hello world!", buf[0..n]);
} }
const ConnectThenSendMessageThread = struct { // // TODO:
fn clientFn() !void { // // Creating a new thread: testing UNIX communication.
const config = .{.safety = true}; // // This is a client sending a raw "Hello world!" bytestring,
var gpa = std.heap.GeneralPurposeAllocator(config){}; // // not an instance of Message.
defer _ = gpa.deinit(); // const ConnectThenSendMessageThread = struct {
const allocator = gpa.allocator(); // fn clientFn() !void {
// const config = .{.safety = true};
var c = try Context.init(allocator); // var gpa = std.heap.GeneralPurposeAllocator(config){};
defer c.deinit(); // There. Can't leak. Isn't Zig wonderful? // defer _ = gpa.deinit();
// const allocator = gpa.allocator();
var buffer: [1000]u8 = undefined; //
var fbs = std.io.fixedBufferStream(&buffer); // var c = try Context.init(allocator);
var writer = fbs.writer(); // defer c.deinit(); // There. Can't leak. Isn't Zig wonderful?
//
try c.server_path("simple-context-test", writer); // var path_buffer: [1000]u8 = undefined;
var path = fbs.getWritten(); // var path_fbs = std.io.fixedBufferStream(&path_buffer);
const socket = try net.connectUnixSocket(path); // var path_writer = path_fbs.writer();
defer socket.close(); // try c.server_path("simple-context-test", path_writer);
print("So we're a client now... path: {s}\n", .{path}); // var path = path_fbs.getWritten();
_ = try socket.writer().writeAll("Hello world!"); //
} // // Actual UNIX socket connection.
}; // const socket = try net.connectUnixSocket(path);
// defer socket.close();
//
test "Context - creation, echo once" { // // Writing message into a buffer.
print("\n", .{}); // var message_buffer: [1000]u8 = undefined;
const config = .{.safety = true}; // var message_fbs = std.io.fixedBufferStream(&message_buffer);
var gpa = std.heap.GeneralPurposeAllocator(config){}; // var message_writer = message_fbs.writer();
defer _ = gpa.deinit(); // // 'fd' parameter is not taken into account here (no loop)
//
const allocator = gpa.allocator(); // var m = try Message.init(0, Message.Type.DATA, allocator, "Hello world!");
// try m.write(message_writer);
var c = try Context.init(allocator); //
defer c.deinit(); // There. Can't leak. Isn't Zig wonderful? // // print("So we're a client now... path: {s}\n", .{path});
// _ = try socket.writer().writeAll(message_fbs.getWritten());
var buffer: [1000]u8 = undefined; // }
var fbs = std.io.fixedBufferStream(&buffer); // };
var writer = fbs.writer(); //
try c.server_path("simple-context-test", writer); //
var path = fbs.getWritten(); // test "Context - creation, echo once" {
// const config = .{.safety = true};
// SERVER SIDE: creating a service. // var gpa = std.heap.GeneralPurposeAllocator(config){};
var server = try c.server_init(path); // defer _ = gpa.deinit();
defer server.deinit(); //
defer std.fs.cwd().deleteFile(path) catch {}; // Once done, remove file. // const allocator = gpa.allocator();
//
const t = try std.Thread.spawn(.{}, ConnectThenSendMessageThread.clientFn, .{}); // var c = try Context.init(allocator);
defer t.join(); // defer c.deinit(); // There. Can't leak. Isn't Zig wonderful?
//
// Server.accept returns a net.StreamServer.Connection. // var buffer: [1000]u8 = undefined;
var client = try server.accept(); // var fbs = std.io.fixedBufferStream(&buffer);
defer client.stream.close(); // var writer = fbs.writer();
var buf: [16]u8 = undefined; // try c.server_path("simple-context-test", writer);
const n = try client.stream.reader().read(&buf); // var path = fbs.getWritten();
//
try testing.expectEqual(@as(usize, 12), n); // // SERVER SIDE: creating a service.
try testing.expectEqualSlices(u8, "Hello world!", buf[0..n]); // var server = try c.server_init(path);
} // defer server.deinit();
// defer std.fs.cwd().deleteFile(path) catch {}; // Once done, remove file.
//
// const t = try std.Thread.spawn(.{}, ConnectThenSendMessageThread.clientFn, .{});
// defer t.join();
//
// // Server.accept returns a net.StreamServer.Connection.
// var client = try server.accept();
// defer client.stream.close();
// var buf: [1000]u8 = undefined;
// const n = try client.stream.reader().read(&buf);
// var m = try Message.read(buf[0..n], allocator);
//
// try testing.expectEqual(@as(usize, 12), m.payload.len);
// try testing.expectEqualSlices(u8, m.payload, "Hello world!");
// }
// FIRST // FIRST