pg_walstream 0.6.3

PostgreSQL logical replication protocol library - parse and handle PostgreSQL WAL streaming messages
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
//! PostgreSQL authentication handler.
//!
//! Supports cleartext, MD5, and SCRAM-SHA-256 authentication.

use super::wire;
use crate::error::ReplicationError;
use bytes::BytesMut;
use tokio::io::{AsyncRead, AsyncWrite};

/// Handle the authentication exchange after the startup message.
///
/// Reads AuthenticationXxx messages until AuthenticationOk (type 0) is received,
/// or returns an error if authentication fails.
///
/// `tls_server_end_point` is the SHA-256 hash of the server's TLS certificate
/// (DER-encoded), used for SCRAM-SHA-256 channel binding. Pass `None` for
/// plain-text connections.
pub async fn authenticate<S: AsyncRead + AsyncWrite + Unpin>(
    stream: &mut S,
    buf: &mut BytesMut,
    user: &str,
    password: Option<&str>,
    tls_server_end_point: Option<Vec<u8>>,
) -> Result<(), ReplicationError> {
    loop {
        let msg = wire::read_message(stream, buf).await?;
        if msg.is_empty() {
            return Err(ReplicationError::protocol(
                "Empty message during authentication".to_string(),
            ));
        }

        match msg[0] {
            b'R' => {
                // Authentication message
                if msg.len() < 9 {
                    return Err(ReplicationError::protocol(
                        "Authentication message too short".to_string(),
                    ));
                }
                let auth_type = i32::from_be_bytes(msg[5..9].try_into().unwrap());

                match auth_type {
                    0 => {
                        // AuthenticationOk
                        tracing::debug!("Authentication successful");
                        return Ok(());
                    }
                    3 => {
                        // AuthenticationCleartextPassword
                        if tls_server_end_point.is_none() {
                            tracing::warn!(
                                "Server requested cleartext password over unencrypted connection"
                            );
                        }
                        let pw = password.ok_or_else(|| {
                            ReplicationError::authentication(
                                "Server requires password but none provided".to_string(),
                            )
                        })?;
                        let pw_msg = wire::build_password_message(pw);
                        wire::write_all(stream, &pw_msg).await?;
                        wire::flush(stream).await?;
                    }
                    5 => {
                        // AuthenticationMD5Password
                        let pw = password.ok_or_else(|| {
                            ReplicationError::authentication(
                                "Server requires password but none provided".to_string(),
                            )
                        })?;
                        if msg.len() < 13 {
                            return Err(ReplicationError::protocol(
                                "MD5 auth message too short (missing salt)".to_string(),
                            ));
                        }
                        let salt = &msg[9..13];
                        let hashed = md5_password(user, pw, salt);
                        let pw_msg = wire::build_password_message(&hashed);
                        wire::write_all(stream, &pw_msg).await?;
                        wire::flush(stream).await?;
                    }
                    10 => {
                        // AuthenticationSASL — SCRAM-SHA-256
                        handle_scram_sha256(stream, buf, &msg, password, &tls_server_end_point)
                            .await?;
                        // SCRAM handler consumed AuthenticationOk internally
                        return Ok(());
                    }
                    _ => {
                        return Err(ReplicationError::authentication(format!(
                            "Unsupported authentication type: {auth_type}"
                        )));
                    }
                }
            }
            b'E' => {
                // ErrorResponse during auth
                let fields = super::error::parse_error_fields(&msg[5..]);
                return Err(ReplicationError::authentication(format!(
                    "Authentication failed: {}",
                    fields
                )));
            }
            _ => {
                // Unexpected message during auth phase — skip
                tracing::warn!(
                    "Unexpected message type '{}' during authentication",
                    msg[0] as char
                );
            }
        }
    }
}

/// Compute an MD5 password hash as PostgreSQL expects:
/// `"md5" + md5(md5(password + user) + salt)`
fn md5_password(user: &str, password: &str, salt: &[u8]) -> String {
    // Step 1: md5(password + user)
    let mut ctx = super::md5::Context::new();
    ctx.consume(password.as_bytes());
    ctx.consume(user.as_bytes());
    let inner_hash = ctx.finalize();
    let inner = super::md5::hex_digest(&inner_hash);

    // Step 2: md5(inner_hex + salt)
    let mut ctx = super::md5::Context::new();
    ctx.consume(inner.as_bytes());
    ctx.consume(salt);
    let outer_hash = ctx.finalize();
    let outer = super::md5::hex_digest(&outer_hash);

    format!("md5{outer}")
}

/// Handle SCRAM-SHA-256 authentication using `postgres_protocol`.
///
/// When `tls_server_end_point` is `Some`, the SCRAM exchange uses channel
/// binding (`tls-server-end-point`) if the server advertises
/// `SCRAM-SHA-256-PLUS`. This binds the TLS session to the authentication
/// exchange, preventing MITM attacks.
async fn handle_scram_sha256<S: AsyncRead + AsyncWrite + Unpin>(
    stream: &mut S,
    buf: &mut BytesMut,
    initial_msg: &[u8],
    password: Option<&str>,
    tls_server_end_point: &Option<Vec<u8>>,
) -> Result<(), ReplicationError> {
    use postgres_protocol::authentication::sasl;

    let pw = password.ok_or_else(|| {
        ReplicationError::authentication(
            "Server requires password for SCRAM-SHA-256 but none provided".to_string(),
        )
    })?;

    // Parse the SASL mechanism list from the initial message.
    // After the 4-byte auth type, the payload contains null-terminated mechanism names
    // followed by an extra null terminator.
    let mechanisms_payload = &initial_msg[9..];
    let mut has_scram_sha256 = false;
    let mut has_scram_sha256_plus = false;
    let mut pos = 0;
    while pos < mechanisms_payload.len() {
        if mechanisms_payload[pos] == 0 {
            break;
        }
        let end = mechanisms_payload[pos..]
            .iter()
            .position(|&b| b == 0)
            .unwrap_or(mechanisms_payload.len() - pos);
        let mechanism = std::str::from_utf8(&mechanisms_payload[pos..pos + end]).unwrap_or("");
        match mechanism {
            "SCRAM-SHA-256" => has_scram_sha256 = true,
            "SCRAM-SHA-256-PLUS" => has_scram_sha256_plus = true,
            _ => {}
        }
        pos += end + 1;
    }

    if !has_scram_sha256 && !has_scram_sha256_plus {
        return Err(ReplicationError::authentication(
            "Server does not support SCRAM-SHA-256".to_string(),
        ));
    }

    // Determine channel binding mode:
    // - If the server offers SCRAM-SHA-256-PLUS and we have TLS channel binding data,
    //   use `tls-server-end-point` channel binding for MITM protection.
    // - If we have TLS but the server doesn't offer PLUS, use `unrequested` to signal
    //   we could have done channel binding (allows the server to detect downgrade).
    // - If no TLS, use `unsupported`.
    let (channel_binding, use_plus) = match tls_server_end_point {
        Some(ref data) if has_scram_sha256_plus => (
            sasl::ChannelBinding::tls_server_end_point(data.clone()),
            true,
        ),
        Some(_) => (sasl::ChannelBinding::unrequested(), false),
        None => (sasl::ChannelBinding::unsupported(), false),
    };

    let mechanism_name = if use_plus {
        sasl::SCRAM_SHA_256_PLUS
    } else {
        sasl::SCRAM_SHA_256
    };

    // Create the SCRAM client
    let mut scram = sasl::ScramSha256::new(pw.as_bytes(), channel_binding);

    // Step 1: Send SASLInitialResponse using postgres-protocol's canonical framing
    let mut sasl_init = BytesMut::new();
    postgres_protocol::message::frontend::sasl_initial_response(
        mechanism_name,
        scram.message(),
        &mut sasl_init,
    )
    .map_err(|e| {
        ReplicationError::authentication(format!("Failed to build SASL initial response: {e}"))
    })?;

    wire::write_all(stream, &sasl_init).await?;
    wire::flush(stream).await?;

    // Step 2: Receive AuthenticationSASLContinue (type 11)
    let msg2 = wire::read_message(stream, buf).await?;
    if msg2[0] == b'E' {
        let fields = super::error::parse_error_fields(&msg2[5..]);
        return Err(ReplicationError::authentication(format!(
            "SCRAM authentication failed: {}",
            fields
        )));
    }
    if msg2[0] != b'R' || msg2.len() < 9 {
        return Err(ReplicationError::protocol(
            "Expected AuthenticationSASLContinue".to_string(),
        ));
    }
    let auth_type2 = i32::from_be_bytes(msg2[5..9].try_into().unwrap());
    if auth_type2 != 11 {
        return Err(ReplicationError::protocol(format!(
            "Expected SASL continue (11), got {auth_type2}"
        )));
    }

    // Process server-first message
    let server_first = &msg2[9..];
    scram.update(server_first).map_err(|e| {
        ReplicationError::authentication(format!("SCRAM server-first processing failed: {e}"))
    })?;

    // Step 3: Send SASLResponse (client-final) using postgres-protocol's canonical framing
    let mut sasl_resp = BytesMut::new();
    postgres_protocol::message::frontend::sasl_response(scram.message(), &mut sasl_resp).map_err(
        |e| ReplicationError::authentication(format!("Failed to build SASL response: {e}")),
    )?;

    wire::write_all(stream, &sasl_resp).await?;
    wire::flush(stream).await?;

    // Step 4: Receive AuthenticationSASLFinal (type 12)
    let msg3 = wire::read_message(stream, buf).await?;
    if msg3[0] == b'E' {
        let fields = super::error::parse_error_fields(&msg3[5..]);
        return Err(ReplicationError::authentication(format!(
            "SCRAM authentication failed: {}",
            fields
        )));
    }
    if msg3[0] != b'R' || msg3.len() < 9 {
        return Err(ReplicationError::protocol(
            "Expected AuthenticationSASLFinal".to_string(),
        ));
    }
    let auth_type3 = i32::from_be_bytes(msg3[5..9].try_into().unwrap());
    if auth_type3 != 12 {
        return Err(ReplicationError::protocol(format!(
            "Expected SASL final (12), got {auth_type3}"
        )));
    }

    // Verify server signature
    let server_final = &msg3[9..];
    scram.finish(server_final).map_err(|e| {
        ReplicationError::authentication(format!("SCRAM server signature verification failed: {e}"))
    })?;

    // Step 5: AuthenticationOk (type 0) should follow
    let msg4 = wire::read_message(stream, buf).await?;
    if msg4[0] == b'R' && msg4.len() >= 9 {
        let auth_type4 = i32::from_be_bytes(msg4[5..9].try_into().unwrap());
        if auth_type4 != 0 {
            return Err(ReplicationError::protocol(format!(
                "Expected AuthenticationOk (0) after SCRAM, got {auth_type4}"
            )));
        }
    } else if msg4[0] == b'E' {
        let fields = super::error::parse_error_fields(&msg4[5..]);
        return Err(ReplicationError::authentication(format!(
            "Authentication failed after SCRAM: {}",
            fields
        )));
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use tokio::io::{AsyncReadExt, AsyncWriteExt};

    #[test]
    fn test_md5_password() {
        // Known test vector: user="md5", password="md5", salt=[0x01, 0x02, 0x03, 0x04]
        let result = md5_password("md5", "md5", &[0x01, 0x02, 0x03, 0x04]);
        assert!(result.starts_with("md5"));
        assert_eq!(result.len(), 35); // "md5" + 32 hex chars
    }

    #[test]
    fn test_md5_password_known() {
        // PostgreSQL MD5 hash for user=test, password=test, salt=[0,0,0,0]
        let result = md5_password("test", "test", &[0, 0, 0, 0]);
        assert!(result.starts_with("md5"));
    }

    // === Helper: build auth messages ===

    /// Build an AuthenticationOk message: R + len(8) + type(0)
    fn build_auth_ok() -> Vec<u8> {
        let mut msg = vec![b'R'];
        msg.extend_from_slice(&8i32.to_be_bytes());
        msg.extend_from_slice(&0i32.to_be_bytes());
        msg
    }

    /// Build an AuthenticationCleartextPassword message: R + len(8) + type(3)
    fn build_auth_cleartext() -> Vec<u8> {
        let mut msg = vec![b'R'];
        msg.extend_from_slice(&8i32.to_be_bytes());
        msg.extend_from_slice(&3i32.to_be_bytes());
        msg
    }

    /// Build an AuthenticationMD5Password message: R + len(12) + type(5) + salt(4)
    fn build_auth_md5(salt: &[u8; 4]) -> Vec<u8> {
        let mut msg = vec![b'R'];
        msg.extend_from_slice(&12i32.to_be_bytes());
        msg.extend_from_slice(&5i32.to_be_bytes());
        msg.extend_from_slice(salt);
        msg
    }

    /// Build an ErrorResponse message
    fn build_error_response(severity: &str, code: &str, message: &str) -> Vec<u8> {
        let mut payload = Vec::new();
        payload.push(b'S');
        payload.extend_from_slice(severity.as_bytes());
        payload.push(0);
        payload.push(b'C');
        payload.extend_from_slice(code.as_bytes());
        payload.push(0);
        payload.push(b'M');
        payload.extend_from_slice(message.as_bytes());
        payload.push(0);
        payload.push(0); // terminator

        let mut msg = vec![b'E'];
        let len = (4 + payload.len()) as i32;
        msg.extend_from_slice(&len.to_be_bytes());
        msg.extend_from_slice(&payload);
        msg
    }

    // === authenticate mock stream tests ===

    #[tokio::test]
    async fn test_auth_ok_immediately() {
        let (mut client, mut server) = tokio::io::duplex(8192);

        tokio::spawn(async move {
            server.write_all(&build_auth_ok()).await.unwrap();
            server.flush().await.unwrap();
        });

        let mut buf = BytesMut::new();
        let result = authenticate(&mut client, &mut buf, "user", Some("pass"), None).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_auth_cleartext_then_ok() {
        let (mut client, mut server) = tokio::io::duplex(8192);

        tokio::spawn(async move {
            // Send cleartext password challenge
            server.write_all(&build_auth_cleartext()).await.unwrap();
            server.flush().await.unwrap();

            // Read and discard the password message from client
            let mut discard = vec![0u8; 1024];
            let _ = AsyncReadExt::read(&mut server, &mut discard).await;

            // Send AuthenticationOk
            server.write_all(&build_auth_ok()).await.unwrap();
            server.flush().await.unwrap();
        });

        let mut buf = BytesMut::new();
        let result = authenticate(&mut client, &mut buf, "user", Some("secret"), None).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_auth_md5_then_ok() {
        let (mut client, mut server) = tokio::io::duplex(8192);
        let salt = [0x01, 0x02, 0x03, 0x04];

        tokio::spawn(async move {
            // Send MD5 password challenge with salt
            server.write_all(&build_auth_md5(&salt)).await.unwrap();
            server.flush().await.unwrap();

            // Read and discard the password response from client
            let mut discard = vec![0u8; 1024];
            let _ = AsyncReadExt::read(&mut server, &mut discard).await;

            // Send AuthenticationOk
            server.write_all(&build_auth_ok()).await.unwrap();
            server.flush().await.unwrap();
        });

        let mut buf = BytesMut::new();
        let result = authenticate(&mut client, &mut buf, "testuser", Some("testpass"), None).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_auth_cleartext_no_password() {
        let (mut client, mut server) = tokio::io::duplex(8192);

        tokio::spawn(async move {
            server.write_all(&build_auth_cleartext()).await.unwrap();
            server.flush().await.unwrap();
        });

        let mut buf = BytesMut::new();
        let result = authenticate(&mut client, &mut buf, "user", None, None).await;
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(
            err.to_string().contains("password"),
            "Expected password error, got: {err}"
        );
    }

    #[tokio::test]
    async fn test_auth_error_response() {
        let (mut client, mut server) = tokio::io::duplex(8192);

        tokio::spawn(async move {
            let err_msg = build_error_response("FATAL", "28000", "password authentication failed");
            server.write_all(&err_msg).await.unwrap();
            server.flush().await.unwrap();
        });

        let mut buf = BytesMut::new();
        let result = authenticate(&mut client, &mut buf, "user", Some("wrong"), None).await;
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(
            err.to_string().contains("authentication")
                || err.to_string().contains("Authentication"),
            "Expected authentication error, got: {err}"
        );
    }

    #[tokio::test]
    async fn test_auth_unsupported_type() {
        let (mut client, mut server) = tokio::io::duplex(8192);

        tokio::spawn(async move {
            // Send an unsupported auth type (99)
            let mut msg = vec![b'R'];
            msg.extend_from_slice(&8i32.to_be_bytes());
            msg.extend_from_slice(&99i32.to_be_bytes());
            server.write_all(&msg).await.unwrap();
            server.flush().await.unwrap();
        });

        let mut buf = BytesMut::new();
        let result = authenticate(&mut client, &mut buf, "user", Some("pass"), None).await;
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(
            err.to_string().contains("Unsupported") || err.to_string().contains("unsupported"),
            "Expected unsupported auth type error, got: {err}"
        );
    }

    // === SCRAM channel binding mechanism selection tests ===

    /// Build an AuthenticationSASL message with given mechanism names.
    fn build_auth_sasl(mechanisms: &[&str]) -> Vec<u8> {
        let mut payload = Vec::new();
        for mech in mechanisms {
            payload.extend_from_slice(mech.as_bytes());
            payload.push(0);
        }
        payload.push(0); // final terminator

        let body_len = 4 + 4 + payload.len(); // length field + auth_type + mechanisms
        let mut msg = vec![b'R'];
        msg.extend_from_slice(&(body_len as i32).to_be_bytes());
        msg.extend_from_slice(&10i32.to_be_bytes()); // AuthenticationSASL = 10
        msg.extend_from_slice(&payload);
        msg
    }

    #[tokio::test]
    async fn test_scram_no_password_returns_error() {
        let (mut client, mut server) = tokio::io::duplex(8192);

        tokio::spawn(async move {
            let msg = build_auth_sasl(&["SCRAM-SHA-256"]);
            server.write_all(&msg).await.unwrap();
            server.flush().await.unwrap();
        });

        let mut buf = BytesMut::new();
        let result = authenticate(&mut client, &mut buf, "user", None, None).await;
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(
            err.to_string().contains("password"),
            "Expected password error, got: {err}"
        );
    }

    #[tokio::test]
    async fn test_scram_no_supported_mechanism_returns_error() {
        let (mut client, mut server) = tokio::io::duplex(8192);

        tokio::spawn(async move {
            // Server only offers an unknown mechanism
            let msg = build_auth_sasl(&["SCRAM-SHA-512"]);
            server.write_all(&msg).await.unwrap();
            server.flush().await.unwrap();
        });

        let mut buf = BytesMut::new();
        let result = authenticate(&mut client, &mut buf, "user", Some("pass"), None).await;
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(
            err.to_string().contains("SCRAM-SHA-256"),
            "Expected SCRAM-SHA-256 not supported error, got: {err}"
        );
    }

    #[tokio::test]
    async fn test_scram_selects_plain_without_tls_endpoint() {
        let (mut client, mut server) = tokio::io::duplex(8192);

        // Server offers both PLUS and non-PLUS, but we have no TLS endpoint data.
        // The client should select SCRAM-SHA-256 (not PLUS) and send mechanism name.
        tokio::spawn(async move {
            let msg = build_auth_sasl(&["SCRAM-SHA-256-PLUS", "SCRAM-SHA-256"]);
            server.write_all(&msg).await.unwrap();
            server.flush().await.unwrap();

            // Read the SASLInitialResponse from client
            let mut response = vec![0u8; 4096];
            let n = AsyncReadExt::read(&mut server, &mut response)
                .await
                .unwrap();
            let response = &response[..n];

            // Verify: tag='p', then length, then mechanism name as C-string
            assert_eq!(response[0], b'p');
            // Find the null terminator after the 5-byte header
            let mech_start = 5;
            let mech_end =
                response[mech_start..].iter().position(|&b| b == 0).unwrap() + mech_start;
            let mechanism = std::str::from_utf8(&response[mech_start..mech_end]).unwrap();
            assert_eq!(
                mechanism, "SCRAM-SHA-256",
                "Without TLS endpoint, should select SCRAM-SHA-256, not PLUS"
            );

            // Send an error to terminate cleanly (we can't do a full SCRAM exchange)
            let err = build_error_response("FATAL", "28000", "test abort");
            server.write_all(&err).await.unwrap();
            server.flush().await.unwrap();
        });

        let mut buf = BytesMut::new();
        // No TLS endpoint → should use SCRAM-SHA-256
        let _ = authenticate(&mut client, &mut buf, "user", Some("pass"), None).await;
    }

    #[tokio::test]
    async fn test_scram_selects_plus_with_tls_endpoint() {
        let (mut client, mut server) = tokio::io::duplex(8192);

        let fake_tls_hash = vec![0xAA; 32]; // fake SHA-256 hash

        tokio::spawn(async move {
            let msg = build_auth_sasl(&["SCRAM-SHA-256-PLUS", "SCRAM-SHA-256"]);
            server.write_all(&msg).await.unwrap();
            server.flush().await.unwrap();

            // Read the SASLInitialResponse
            let mut response = vec![0u8; 4096];
            let n = AsyncReadExt::read(&mut server, &mut response)
                .await
                .unwrap();
            let response = &response[..n];

            assert_eq!(response[0], b'p');
            let mech_start = 5;
            let mech_end =
                response[mech_start..].iter().position(|&b| b == 0).unwrap() + mech_start;
            let mechanism = std::str::from_utf8(&response[mech_start..mech_end]).unwrap();
            assert_eq!(
                mechanism, "SCRAM-SHA-256-PLUS",
                "With TLS endpoint and server offering PLUS, should select SCRAM-SHA-256-PLUS"
            );

            // Send error to terminate
            let err = build_error_response("FATAL", "28000", "test abort");
            server.write_all(&err).await.unwrap();
            server.flush().await.unwrap();
        });

        let mut buf = BytesMut::new();
        let _ = authenticate(
            &mut client,
            &mut buf,
            "user",
            Some("pass"),
            Some(fake_tls_hash),
        )
        .await;
    }

    #[tokio::test]
    async fn test_scram_falls_back_without_plus_offered() {
        let (mut client, mut server) = tokio::io::duplex(8192);

        let fake_tls_hash = vec![0xBB; 32];

        tokio::spawn(async move {
            // Server only offers non-PLUS, even though client has TLS endpoint
            let msg = build_auth_sasl(&["SCRAM-SHA-256"]);
            server.write_all(&msg).await.unwrap();
            server.flush().await.unwrap();

            let mut response = vec![0u8; 4096];
            let n = AsyncReadExt::read(&mut server, &mut response)
                .await
                .unwrap();
            let response = &response[..n];

            assert_eq!(response[0], b'p');
            let mech_start = 5;
            let mech_end =
                response[mech_start..].iter().position(|&b| b == 0).unwrap() + mech_start;
            let mechanism = std::str::from_utf8(&response[mech_start..mech_end]).unwrap();
            assert_eq!(
                mechanism, "SCRAM-SHA-256",
                "When server doesn't offer PLUS, should fall back to SCRAM-SHA-256"
            );

            let err = build_error_response("FATAL", "28000", "test abort");
            server.write_all(&err).await.unwrap();
            server.flush().await.unwrap();
        });

        let mut buf = BytesMut::new();
        let _ = authenticate(
            &mut client,
            &mut buf,
            "user",
            Some("pass"),
            Some(fake_tls_hash),
        )
        .await;
    }
}