From 580bd66689e043d6c1bc6033bf4bf75dbd5e927a Mon Sep 17 00:00:00 2001 From: mochalins <117967760+mochalins@users.noreply.github.com> Date: Mon, 16 Sep 2024 17:29:48 +0900 Subject: [PATCH] test: linux backend reading/writing from port --- src/backend/linux.zig | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/src/backend/linux.zig b/src/backend/linux.zig index ae336a8..fabd65d 100644 --- a/src/backend/linux.zig +++ b/src/backend/linux.zig @@ -150,20 +150,30 @@ test { @cInclude("unistd.h"); }); - const master = c.posix_openpt(c.O_RDWR); - if (master < 0) - return error.MasterPseudoTerminalSetupError; - defer _ = c.close(master); + var master: Port = .{ + .name = "/dev/ptmx", + .file = null, + }; + try master.open(); + defer master.close(); - if (c.grantpt(master) < 0 or c.unlockpt(master) < 0) + if (c.grantpt(master.file.?.handle) < 0 or + c.unlockpt(master.file.?.handle) < 0) return error.MasterPseudoTerminalSetupError; - const slave_name = c.ptsname(master) orelse + const slave_name = c.ptsname(master.file.?.handle) orelse return error.SlavePseudoTerminalSetupError; const slave_name_len = std.mem.len(slave_name); if (slave_name_len == 0) return error.SlavePseudoTerminalSetupError; + try master.configure(.{ + .baud_rate = .B115200, + }); + + const master_writer = master.writer() orelse + return error.InvalidMasterWriter; + var port: Port = .{ .name = slave_name[0..slave_name_len], .file = null, @@ -171,5 +181,17 @@ test { try port.open(); defer port.close(); + try port.configure(.{ + .baud_rate = .B115200, + }); + + const reader = port.reader() orelse return error.InvalidPortReader; + try std.testing.expectEqual(false, try port.poll()); + try std.testing.expectEqual(12, try master_writer.write("test message")); + try std.testing.expectEqual(true, try port.poll()); + + var buffer: [16]u8 = undefined; + try std.testing.expectEqual(12, try reader.read(&buffer)); + try std.testing.expectEqualSlices(u8, "test message", buffer[0..12]); }