From 08472c27c77d27ea084e3458842540351c5a5c28 Mon Sep 17 00:00:00 2001 From: Martin Ashby Date: Wed, 27 Sep 2023 20:23:30 +0100 Subject: Add cleartext password handling. Fix segfault on error response read. Add test for tcp connection and incorrect password --- src/proto/error_response.zig | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) (limited to 'src/proto/error_response.zig') diff --git a/src/proto/error_response.zig b/src/proto/error_response.zig index 2aafe8d..dc75053 100644 --- a/src/proto/error_response.zig +++ b/src/proto/error_response.zig @@ -28,12 +28,18 @@ routine: ?[]const u8 = null, unknown_fields: HMByteString, pub fn read(allocator: std.mem.Allocator, b: []const u8) !ErrorResponse { - var res: ErrorResponse = undefined; - res.unknown_fields = HMByteString.init(allocator); - res.buf = try allocator.dupe(u8, b); - errdefer allocator.free(res.buf.?); + var res = ErrorResponse{ + .severity = "", + .code = "", + .message = "", + .unknown_fields = HMByteString.init(allocator), + .buf = try allocator.dupe(u8, b), + }; + errdefer res.deinit(allocator); var it = std.mem.splitScalar(u8, res.buf.?, 0); - var setSev = false; var setCode = false; var setMsg = false; + var setSev = false; + var setCode = false; + var setMsg = false; while (it.next()) |next| { if (next.len < 1) break; switch (next[0]) { @@ -97,7 +103,7 @@ pub fn read(allocator: std.mem.Allocator, b: []const u8) !ErrorResponse { }, else => { try res.unknown_fields.put(next[0], next[1..]); - } + }, } } if (!(setSev and setCode and setMsg)) return ProtocolError.MissingField; @@ -112,11 +118,10 @@ pub fn write(self: ErrorResponse, allocator: std.mem.Allocator, stream_writer: a try writer.writeIntBig(u32, 0); // Length placeholder. try write_field_nt('S', self, "severity", writer); - if (self.severity_unlocalized) |severity_unlocalized| { - try write_nt('V', severity_unlocalized, writer); - } + if (self.severity_unlocalized) |severity_unlocalized| try write_nt('V', severity_unlocalized, writer); try write_field_nt('C', self, "code", writer); try write_field_nt('M', self, "message", writer); + if (self.detail) |detail| try write_nt('D', detail, writer); // TODO rest of the fields // replace the length and write it to the actual stream @@ -137,6 +142,20 @@ pub fn deinit(self: *ErrorResponse, allocator: std.mem.Allocator) void { if (self.buf != null) allocator.free(self.buf.?); } +pub fn format(self: ErrorResponse, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { + _ = options; + _ = fmt; + try writer.writeAll("ErrorResponse severity ["); + try writer.writeAll(self.severity); + try writer.writeAll("] "); + try writer.writeAll("code ["); + try writer.writeAll(self.code); + try writer.writeAll("] "); + try writer.writeAll("message ["); + try writer.writeAll(self.message); + try writer.writeAll("]"); +} + test "round trip" { const allocator = std.testing.allocator; var sm = ErrorResponse{ @@ -144,6 +163,7 @@ test "round trip" { .severity_unlocalized = "foo_unlocal", .code = "bar", .message = "baz", + .detail = "bang, and that's the end", .unknown_fields = HMByteString.init(allocator), }; defer sm.deinit(allocator); @@ -167,4 +187,5 @@ test "round trip" { try std.testing.expectEqualStrings("foo_unlocal", sm2.severity_unlocalized.?); try std.testing.expectEqualStrings("bar", sm2.code); try std.testing.expectEqualStrings("baz", sm2.message); + try std.testing.expectEqualStrings("bang, and that's the end", sm2.detail.?); } -- cgit v1.2.3-ZIG