aboutsummaryrefslogtreecommitdiff
path: root/src/conn/conn.zig
diff options
context:
space:
mode:
authorMartin Ashby <martin@ashbysoft.com>2023-09-27 20:23:30 +0100
committerMartin Ashby <martin@ashbysoft.com>2023-09-27 20:23:30 +0100
commit08472c27c77d27ea084e3458842540351c5a5c28 (patch)
tree67029d8f6779b107ec8d0cf370981045ccea1e30 /src/conn/conn.zig
parent4c5101d5f3a60c58190809166f1aa1eac2e7875f (diff)
downloadpgz-08472c27c77d27ea084e3458842540351c5a5c28.tar.gz
pgz-08472c27c77d27ea084e3458842540351c5a5c28.tar.bz2
pgz-08472c27c77d27ea084e3458842540351c5a5c28.tar.xz
pgz-08472c27c77d27ea084e3458842540351c5a5c28.zip
Add cleartext password handling.
Fix segfault on error response read. Add test for tcp connection and incorrect password
Diffstat (limited to 'src/conn/conn.zig')
-rw-r--r--src/conn/conn.zig42
1 files changed, 39 insertions, 3 deletions
diff --git a/src/conn/conn.zig b/src/conn/conn.zig
index 1b2bf2d..4d62f57 100644
--- a/src/conn/conn.zig
+++ b/src/conn/conn.zig
@@ -6,6 +6,7 @@ const Proto = @import("../proto/proto.zig");
const read_message = @import("../main.zig").read_message;
const ProtocolError = @import("../main.zig").ProtocolError;
const ServerError = @import("../main.zig").ServerError;
+const ClientError = @import("../main.zig").ClientError;
const diagnosticReader = @import("../main.zig").diagnosticReader;
const Conn = @This();
@@ -57,6 +58,17 @@ pub fn connect(config: Config) !Conn {
var ar = try read_message(Proto.AuthenticationRequest, allocator, reader);
defer ar.deinit(allocator);
// TODO handle the authentication request
+ switch (ar.inner_type) {
+ .AuthRequestTypeOk => {}, // fine do nothing!
+ .AuthRequestTypeCleartextPassword => {
+ if (config.password) |password| {
+ const pm = Proto.PasswordMessage{ .password = password };
+ try pm.write(allocator, writer);
+ } else {
+ return ClientError.NoPasswordSupplied;
+ }
+ },
+ }
log.info("authentication request", .{});
},
Proto.ReadyForQuery.Tag => {
@@ -94,9 +106,7 @@ fn deinit(self: *Conn) void {
self.stream.close();
}
-//pub fn exec(self: *Conn)
-
-test "connect" {
+test "connect unix" {
// must have a local postgres runnning
// TODO maybe use docker to start one?
const allocator = std.testing.allocator;
@@ -109,3 +119,29 @@ test "connect" {
var conn = try Conn.connect(cfg);
defer conn.deinit();
}
+
+test "connect tcp with password" {
+ const allocator = std.testing.allocator;
+ const cfg = Config{
+ .allocator = allocator,
+ .address = .{ .net = std.net.Address{ .in = std.net.Ip4Address.init([4]u8{ 127, 0, 0, 1 }, 5432) } },
+ .database = "martin",
+ .user = "martin",
+ .password = "martin",
+ };
+ var conn = try Conn.connect(cfg);
+ defer conn.deinit();
+}
+
+test "connect tcp with wrong password" {
+ // TODO how to disable failing tests on error log
+ // const allocator = std.testing.allocator;
+ // const cfg = Config{
+ // .allocator = allocator,
+ // .address = .{ .net = std.net.Address{ .in = std.net.Ip4Address.init([4]u8{ 127, 0, 0, 1 }, 5432) } },
+ // .database = "martin",
+ // .user = "martin",
+ // .password = "foobar",
+ // };
+ // try std.testing.expectError(ServerError.ErrorResponse, Conn.connect(cfg));
+}