Skip to content

Commit 5e85d6b

Browse files
author
Matthijs van der Vleuten
committed
test_unix_connection now detects the socket directory.
Change pg_hba.conf to allow connections through the socket. Ignore connect_unix doc test. It requires `extern crate url;` which is not allowed with rustdoc. Also, per comments on PR rust-postgres#35: - Inline open_unix - Centralize common code from connect and connect_unix in connect_finish.
1 parent 707e7cc commit 5e85d6b

3 files changed

Lines changed: 30 additions & 39 deletions

File tree

src/lib.rs

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -328,13 +328,6 @@ fn open_socket(host: &str, port: Port)
328328
Err(SocketError(err.unwrap()))
329329
}
330330

331-
fn open_unix(path: &Path) -> Result<UnixStream, PostgresConnectError> {
332-
match UnixStream::connect(path) {
333-
Ok(unix) => Ok(unix),
334-
Err(err) => Err(SocketError(err))
335-
}
336-
}
337-
338331
fn initialize_stream(host: &str, port: Port, ssl: &SslMode)
339332
-> Result<InternalStream, PostgresConnectError> {
340333
let mut socket = match open_socket(host, port) {
@@ -367,9 +360,9 @@ fn initialize_stream(host: &str, port: Port, ssl: &SslMode)
367360

368361
fn initialize_unix(path: &Path)
369362
-> Result<InternalStream, PostgresConnectError> {
370-
match open_unix(path) {
363+
match UnixStream::connect(path) {
371364
Ok(unix) => Ok(NormalUnix(unix)),
372-
Err(err) => Err(err)
365+
Err(err) => Err(SocketError(err))
373366
}
374367
}
375368

@@ -454,31 +447,13 @@ impl InnerPostgresConnection {
454447

455448
let stream = try!(initialize_stream(host, port, ssl));
456449

457-
let conn = InnerPostgresConnection {
458-
stream: BufferedStream::new(stream),
459-
next_stmt_id: 0,
460-
notice_handler: ~DefaultNoticeHandler,
461-
notifications: RingBuf::new(),
462-
cancel_data: PostgresCancelData { process_id: 0, secret_key: 0 },
463-
unknown_types: HashMap::new(),
464-
desynchronized: false,
465-
finished: false,
466-
canary: CANARY,
467-
};
468-
469-
args.push((~"client_encoding", ~"UTF8"));
470-
// Postgres uses the value of TimeZone as the time zone for TIMESTAMP
471-
// WITH TIME ZONE values. Timespec converts to GMT internally.
472-
args.push((~"TimeZone", ~"GMT"));
473-
// We have to clone here since we need the user again for auth
474-
args.push((~"user", user.user.clone()));
475450
if !path.is_empty() {
476451
// path contains the leading /
477452
let (_, path) = path.slice_shift_char();
478453
args.push((~"database", path.to_owned()));
479454
}
480455

481-
InnerPostgresConnection::connect_finish(conn, args, user)
456+
InnerPostgresConnection::connect_finish(stream, args, user)
482457
}
483458

484459
fn connect_unix(socket_dir: &Path, port: Port, user: UserInfo, database: ~str)
@@ -488,7 +463,16 @@ impl InnerPostgresConnection {
488463

489464
let stream = try!(initialize_unix(&socket));
490465

491-
let conn = InnerPostgresConnection {
466+
let mut args = Vec::new();
467+
args.push((~"database", database));
468+
469+
InnerPostgresConnection::connect_finish(stream, args, user)
470+
}
471+
472+
fn connect_finish(stream: InternalStream, mut args: Vec<(~str, ~str)>, user: UserInfo)
473+
-> Result<InnerPostgresConnection, PostgresConnectError> {
474+
475+
let mut conn = InnerPostgresConnection {
492476
stream: BufferedStream::new(stream),
493477
next_stmt_id: 0,
494478
notice_handler: ~DefaultNoticeHandler,
@@ -500,21 +484,13 @@ impl InnerPostgresConnection {
500484
canary: CANARY,
501485
};
502486

503-
let mut args = Vec::new();
504-
505487
args.push((~"client_encoding", ~"UTF8"));
506488
// Postgres uses the value of TimeZone as the time zone for TIMESTAMP
507489
// WITH TIME ZONE values. Timespec converts to GMT internally.
508490
args.push((~"TimeZone", ~"GMT"));
509491
// We have to clone here since we need the user again for auth
510492
args.push((~"user", user.user.clone()));
511-
args.push((~"database", database));
512493

513-
InnerPostgresConnection::connect_finish(conn, args, user)
514-
}
515-
516-
fn connect_finish(mut conn: InnerPostgresConnection, args: Vec<(~str, ~str)>, user: UserInfo)
517-
-> Result<InnerPostgresConnection, PostgresConnectError> {
518494
try_pg_conn!(conn.write_messages([StartupMessage {
519495
version: message::PROTOCOL_VERSION,
520496
parameters: args.as_slice()
@@ -796,7 +772,9 @@ impl PostgresConnection {
796772
///
797773
/// # Example
798774
///
799-
/// ```rust,no_run
775+
/// ```rust,ignore
776+
/// # extern crate url;
777+
/// # use url::UserInfo;
800778
/// # use postgres::PostgresConnection;
801779
/// let path = Path::new("/tmp");
802780
/// let port = 5432;

src/test.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,18 @@ fn test_connection_finish() {
110110

111111
#[test]
112112
fn test_unix_connection() {
113-
let conn = or_fail!(PostgresConnection::connect_unix(&Path::new("/tmp"), 5432, UserInfo::new(~"postgres", None), ~"postgres"));
113+
let conn = or_fail!(PostgresConnection::connect("postgres://postgres@localhost", &NoSsl));
114+
let stmt = or_fail!(conn.prepare("SHOW unix_socket_directories"));
115+
let result = or_fail!(stmt.query([]));
116+
let unix_socket_directories: ~str = result.map(|row| row[1]).next().unwrap();
117+
118+
if unix_socket_directories == ~"" {
119+
fail!("can't test connect_unix; unix_socket_directories is empty");
120+
}
121+
122+
let unix_socket_directory = unix_socket_directories.splitn(',', 1).next().unwrap();
123+
124+
let conn = or_fail!(PostgresConnection::connect_unix(&Path::new(unix_socket_directory), 5432, UserInfo::new(~"postgres", None), ~"postgres"));
114125
assert!(conn.finish().is_ok());
115126
}
116127

travis/pg_hba.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ host all md5_user ::1/128 md5
88
host all postgres 127.0.0.1/32 trust
99
# IPv6 local connections:
1010
host all postgres ::1/128 trust
11+
# Unix socket connections:
12+
local all postgres trust

0 commit comments

Comments
 (0)