tds-driver 0.2.0

A TDS (Microsoft SQL Server) driver for Rust
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
#[cfg(any(
    feature = "rustls",
    feature = "native-tls",
    feature = "vendored-openssl"
))]
use super::tls_stream::TlsStream;
#[cfg(any(
    feature = "rustls",
    feature = "native-tls",
    feature = "vendored-openssl"
))]
use crate::tds::{
    codec::{Decode, Encode, PacketHeader, PacketStatus, PacketType},
    HEADER_BYTES,
};
#[cfg(any(
    feature = "rustls",
    feature = "native-tls",
    feature = "vendored-openssl"
))]
use bytes::BytesMut;
use futures_util::io::{AsyncRead, AsyncWrite};
#[cfg(any(
    feature = "rustls",
    feature = "native-tls",
    feature = "vendored-openssl"
))]
use futures_util::ready;
#[cfg(any(
    feature = "rustls",
    feature = "native-tls",
    feature = "vendored-openssl"
))]
use std::cmp;
use std::{
    io,
    pin::Pin,
    task::{self, Poll},
};
#[cfg(any(
    feature = "rustls",
    feature = "native-tls",
    feature = "vendored-openssl"
))]
use tracing::{event, Level};

/// A wrapper to handle either TLS or bare connections.
pub(crate) enum MaybeTlsStream<S: AsyncRead + AsyncWrite + Unpin + Send> {
    Raw(S),
    #[cfg(any(
        feature = "rustls",
        feature = "native-tls",
        feature = "vendored-openssl"
    ))]
    /// With the bytes of the TDS packet being written that the TLS stream
    /// has yet to take.
    Tls(TlsStream<TlsPreloginWrapper<S>>, usize),
}

#[cfg(any(
    feature = "rustls",
    feature = "native-tls",
    feature = "vendored-openssl"
))]
impl<S: AsyncRead + AsyncWrite + Unpin + Send> MaybeTlsStream<S> {
    pub fn into_inner(self) -> S {
        match self {
            Self::Raw(s) => s,
            #[cfg(any(
                feature = "rustls",
                feature = "native-tls",
                feature = "vendored-openssl"
            ))]
            Self::Tls(mut tls, _) => tls.get_mut().stream.take().unwrap(),
        }
    }
}

impl<S: AsyncRead + AsyncWrite + Unpin + Send> AsyncRead for MaybeTlsStream<S> {
    fn poll_read(
        self: Pin<&mut Self>,
        cx: &mut task::Context<'_>,
        buf: &mut [u8],
    ) -> Poll<io::Result<usize>> {
        match self.get_mut() {
            MaybeTlsStream::Raw(s) => Pin::new(s).poll_read(cx, buf),
            #[cfg(any(
                feature = "rustls",
                feature = "native-tls",
                feature = "vendored-openssl"
            ))]
            MaybeTlsStream::Tls(s, _) => Pin::new(s).poll_read(cx, buf),
        }
    }
}

impl<S: AsyncRead + AsyncWrite + Unpin + Send> AsyncWrite for MaybeTlsStream<S> {
    fn poll_write(
        self: Pin<&mut Self>,
        cx: &mut task::Context<'_>,
        buf: &[u8],
    ) -> Poll<io::Result<usize>> {
        match self.get_mut() {
            MaybeTlsStream::Raw(s) => Pin::new(s).poll_write(cx, buf),
            #[cfg(any(
                feature = "rustls",
                feature = "native-tls",
                feature = "vendored-openssl"
            ))]
            MaybeTlsStream::Tls(s, left) => {
                // SQL Server drops the connection when a TLS record holds
                // parts of two TDS packets, so the TLS stream gets at most
                // the rest of one packet per write.
                if *left == 0 && buf.len() >= HEADER_BYTES {
                    *left = usize::from(u16::from_be_bytes([buf[2], buf[3]])).max(HEADER_BYTES);
                }
                let end = if *left == 0 {
                    buf.len()
                } else {
                    buf.len().min(*left)
                };
                let written = ready!(Pin::new(s).poll_write(cx, &buf[..end]))?;
                *left = left.saturating_sub(written);
                Poll::Ready(Ok(written))
            }
        }
    }

    fn poll_flush(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<io::Result<()>> {
        match self.get_mut() {
            MaybeTlsStream::Raw(s) => Pin::new(s).poll_flush(cx),
            #[cfg(any(
                feature = "rustls",
                feature = "native-tls",
                feature = "vendored-openssl"
            ))]
            MaybeTlsStream::Tls(s, _) => Pin::new(s).poll_flush(cx),
        }
    }

    fn poll_close(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<io::Result<()>> {
        match self.get_mut() {
            MaybeTlsStream::Raw(s) => Pin::new(s).poll_close(cx),
            #[cfg(any(
                feature = "rustls",
                feature = "native-tls",
                feature = "vendored-openssl"
            ))]
            MaybeTlsStream::Tls(s, _) => Pin::new(s).poll_close(cx),
        }
    }
}

/// TDS 7.x carries the TLS handshake in PRELOGIN packets; this wrapper adds
/// and strips that framing until `handshake_complete`, then passes through.
#[cfg(any(
    feature = "rustls",
    feature = "native-tls",
    feature = "vendored-openssl"
))]
pub(crate) struct TlsPreloginWrapper<S> {
    stream: Option<S>,
    pending_handshake: bool,
    /// The handshake travels in PRELOGIN packets (TDS 7.x, not strict).
    framed: bool,

    header_buf: [u8; HEADER_BYTES],
    header_pos: usize,
    read_remaining: usize,

    /// TLS bytes written but not yet framed.
    wr_buf: Vec<u8>,
    /// Bytes for the wire, of which `out_pos` are on it: framed handshake
    /// packets, then TLS records gathered into fewer, larger socket writes.
    out: Vec<u8>,
    out_pos: usize,
    packet_id: u8,
}

/// Packet size before login negotiates one; SQL Server drops larger
/// handshake packets.
#[cfg(any(
    feature = "rustls",
    feature = "native-tls",
    feature = "vendored-openssl"
))]
const PRELOGIN_PACKET_SIZE: usize = 4096;

/// How many TLS bytes a write gathers before it goes to the socket.
#[cfg(any(
    feature = "rustls",
    feature = "native-tls",
    feature = "vendored-openssl"
))]
const MAX_GATHERED: usize = 64 * 1024;

#[cfg(any(
    feature = "rustls",
    feature = "native-tls",
    feature = "vendored-openssl"
))]
impl<S> TlsPreloginWrapper<S> {
    pub fn new(stream: S) -> Self {
        TlsPreloginWrapper {
            stream: Some(stream),
            pending_handshake: true,
            framed: true,

            header_buf: [0u8; HEADER_BYTES],
            header_pos: 0,
            read_remaining: 0,
            wr_buf: Vec::new(),
            out: Vec::new(),
            out_pos: 0,
            packet_id: 1,
        }
    }

    /// For TDS 8.0 strict, whose handshake is raw TLS.
    pub fn unframed(stream: S) -> Self {
        Self {
            framed: false,
            ..Self::new(stream)
        }
    }

    pub fn handshake_complete(&mut self) {
        self.pending_handshake = false;
    }

    fn framing(&self) -> bool {
        self.pending_handshake && self.framed
    }

    /// Writes the framed packets still queued from a flush that would have
    /// blocked.
    fn poll_drain(&mut self, cx: &mut task::Context<'_>) -> Poll<io::Result<()>>
    where
        S: AsyncWrite + Unpin,
    {
        while self.out_pos < self.out.len() {
            let written =
                ready!(Pin::new(self.stream.as_mut().unwrap())
                    .poll_write(cx, &self.out[self.out_pos..]))?;

            if written == 0 {
                return Poll::Ready(Err(io::ErrorKind::WriteZero.into()));
            }

            self.out_pos += written;
        }

        Poll::Ready(Ok(()))
    }

    /// Moves `wr_buf` into `out` as one PRELOGIN message of packets no
    /// larger than `PRELOGIN_PACKET_SIZE`.
    fn frame_pending(&mut self) {
        let mut chunks = self
            .wr_buf
            .chunks(PRELOGIN_PACKET_SIZE - HEADER_BYTES)
            .peekable();

        while let Some(chunk) = chunks.next() {
            let mut header = PacketHeader::new(chunk.len() + HEADER_BYTES, self.packet_id);
            header.set_type(PacketType::PreLogin);
            header.set_status(if chunks.peek().is_some() {
                PacketStatus::NormalMessage
            } else {
                PacketStatus::EndOfMessage
            });
            header
                .encode(&mut self.out)
                .expect("encoding a header into a Vec cannot fail");
            self.out.extend_from_slice(chunk);
            self.packet_id = self.packet_id.wrapping_add(1);
        }

        self.wr_buf.clear();
    }
}

#[cfg(any(
    feature = "rustls",
    feature = "native-tls",
    feature = "vendored-openssl"
))]
impl<S: AsyncRead + AsyncWrite + Unpin + Send> AsyncRead for TlsPreloginWrapper<S> {
    fn poll_read(
        self: Pin<&mut Self>,
        cx: &mut task::Context<'_>,
        buf: &mut [u8],
    ) -> Poll<io::Result<usize>> {
        let inner = self.get_mut();

        // The answer can't come before the flight it answers is on the wire.
        ready!(inner.poll_drain(cx))?;

        // Normal operation does not need any extra treatment, we handle packets
        // in the codec.
        if !inner.framing() {
            return Pin::new(inner.stream.as_mut().unwrap()).poll_read(cx, buf);
        }

        if buf.is_empty() {
            return Poll::Ready(Ok(0));
        }

        // Strip packet headers; an empty packet has no bytes for TLS.
        while inner.read_remaining == 0 {
            while inner.header_pos < HEADER_BYTES {
                let read = ready!(Pin::new(inner.stream.as_mut().unwrap())
                    .poll_read(cx, &mut inner.header_buf[inner.header_pos..]))?;

                if read == 0 {
                    return Poll::Ready(Ok(0));
                }

                inner.header_pos += read;
            }

            let header = PacketHeader::decode(&mut BytesMut::from(&inner.header_buf[..]))
                .map_err(io::Error::other)?;

            // This runs before any certificate has been validated, so the
            // bytes are fully untrusted: reject anything unexpected instead of
            // panicking.
            if header.r#type() != PacketType::PreLogin {
                return Poll::Ready(Err(io::Error::new(
                    io::ErrorKind::InvalidData,
                    "expected a pre-login packet during the TLS handshake",
                )));
            }

            inner.read_remaining = (header.length() as usize)
                .checked_sub(HEADER_BYTES)
                .ok_or_else(|| {
                    io::Error::new(
                        io::ErrorKind::InvalidData,
                        "pre-login packet length shorter than its header",
                    )
                })?;
            inner.header_pos = 0;

            event!(
                Level::TRACE,
                "Reading packet of {} bytes",
                inner.read_remaining,
            );
        }

        let max_read = cmp::min(inner.read_remaining, buf.len());

        let read = ready!(
            Pin::new(&mut inner.stream.as_mut().unwrap()).poll_read(cx, &mut buf[..max_read])
        )?;

        inner.read_remaining -= read;

        Poll::Ready(Ok(read))
    }
}

#[cfg(any(
    feature = "rustls",
    feature = "native-tls",
    feature = "vendored-openssl"
))]
impl<S: AsyncRead + AsyncWrite + Unpin + Send> AsyncWrite for TlsPreloginWrapper<S> {
    fn poll_write(
        mut self: Pin<&mut Self>,
        cx: &mut task::Context<'_>,
        buf: &[u8],
    ) -> Poll<io::Result<usize>> {
        // Normal operation does not need any extra treatment, we handle
        // packets in the codec.
        if !self.framing() {
            let inner = self.get_mut();
            if inner.out.len() - inner.out_pos >= MAX_GATHERED {
                ready!(inner.poll_drain(cx))?;
            }
            if inner.out_pos == inner.out.len() {
                inner.out.clear();
                inner.out_pos = 0;
            }
            // Sent on flush, or before the next read.
            inner.out.extend_from_slice(buf);
            return Poll::Ready(Ok(buf.len()));
        }

        // Framed on flush, which is where a TLS flight ends.
        self.wr_buf.extend_from_slice(buf);

        Poll::Ready(Ok(buf.len()))
    }

    fn poll_flush(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<io::Result<()>> {
        let inner = self.get_mut();

        // Bytes written while a flight is partly on the wire become the next
        // message, never the tail of the current one.
        loop {
            match inner.poll_drain(cx) {
                Poll::Ready(result) => result?,
                // OpenSSL fails the handshake on a flush that would block
                // (rust-openssl sets no retry flag for BIO flush). Reporting
                // it done is safe: `poll_read` sends the rest first.
                Poll::Pending if inner.pending_handshake => return Poll::Ready(Ok(())),
                Poll::Pending => return Poll::Pending,
            }

            if inner.wr_buf.is_empty() {
                break;
            }

            inner.out.clear();
            inner.out_pos = 0;
            inner.frame_pending();

            event!(
                Level::TRACE,
                "Writing {} bytes of pre-login packets",
                inner.out.len(),
            );
        }

        match Pin::new(inner.stream.as_mut().unwrap()).poll_flush(cx) {
            Poll::Pending if inner.pending_handshake => Poll::Ready(Ok(())),
            flushed => flushed,
        }
    }

    fn poll_close(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<io::Result<()>> {
        let inner = self.get_mut();
        ready!(inner.poll_drain(cx))?;
        Pin::new(inner.stream.as_mut().unwrap()).poll_close(cx)
    }
}

#[cfg(all(
    test,
    any(
        feature = "rustls",
        feature = "native-tls",
        feature = "vendored-openssl"
    )
))]
mod tests {
    use super::*;
    use std::task::{Context, Waker};

    /// Moves at most `max` bytes per call and returns `Pending` on every
    /// other call.
    struct Choppy {
        input: Vec<u8>,
        read_pos: usize,
        written: Vec<u8>,
        max: usize,
        flip: bool,
    }

    impl Choppy {
        fn new(max: usize, input: Vec<u8>) -> Self {
            Self {
                input,
                read_pos: 0,
                written: Vec::new(),
                max,
                flip: false,
            }
        }

        fn pending(&mut self, cx: &mut Context<'_>) -> bool {
            self.flip = !self.flip;
            if self.flip {
                cx.waker().wake_by_ref();
            }
            self.flip
        }
    }

    impl AsyncRead for Choppy {
        fn poll_read(
            mut self: Pin<&mut Self>,
            cx: &mut Context<'_>,
            buf: &mut [u8],
        ) -> Poll<io::Result<usize>> {
            if self.pending(cx) {
                return Poll::Pending;
            }
            let n = buf
                .len()
                .min(self.max)
                .min(self.input.len() - self.read_pos);
            buf[..n].copy_from_slice(&self.input[self.read_pos..self.read_pos + n]);
            self.read_pos += n;
            Poll::Ready(Ok(n))
        }
    }

    impl AsyncWrite for Choppy {
        fn poll_write(
            mut self: Pin<&mut Self>,
            cx: &mut Context<'_>,
            buf: &[u8],
        ) -> Poll<io::Result<usize>> {
            if self.pending(cx) {
                return Poll::Pending;
            }
            let n = buf.len().min(self.max);
            self.written.extend_from_slice(&buf[..n]);
            Poll::Ready(Ok(n))
        }

        fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<()>> {
            Poll::Ready(Ok(()))
        }

        fn poll_close(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<()>> {
            Poll::Ready(Ok(()))
        }
    }

    fn cx() -> Context<'static> {
        Context::from_waker(Waker::noop())
    }

    fn write(w: &mut TlsPreloginWrapper<Choppy>, data: &[u8]) {
        assert_eq!(
            Pin::new(w).poll_write(&mut cx(), data).map(Result::unwrap),
            Poll::Ready(data.len())
        );
    }

    /// Flushes until nothing is queued; mid-handshake, a flush that would
    /// block reports done and leaves the rest queued.
    fn flush(w: &mut TlsPreloginWrapper<Choppy>) {
        loop {
            let flushed = Pin::new(&mut *w).poll_flush(&mut cx()).map(Result::unwrap);
            if flushed.is_ready() && w.out_pos == w.out.len() && w.wr_buf.is_empty() {
                return;
            }
        }
    }

    /// Splits wire bytes into messages, checking every packet header.
    fn messages(mut wire: &[u8]) -> Vec<Vec<u8>> {
        let mut messages = vec![Vec::new()];
        while !wire.is_empty() {
            assert_eq!(wire[0], PacketType::PreLogin as u8);
            let len = u16::from_be_bytes([wire[2], wire[3]]) as usize;
            assert!(
                (HEADER_BYTES + 1..=PRELOGIN_PACKET_SIZE).contains(&len),
                "packet of {len} bytes"
            );
            messages
                .last_mut()
                .unwrap()
                .extend_from_slice(&wire[HEADER_BYTES..len]);
            if wire[1] == PacketStatus::EndOfMessage as u8 {
                messages.push(Vec::new());
            } else {
                assert_eq!(wire[1], PacketStatus::NormalMessage as u8);
            }
            wire = &wire[len..];
        }
        assert!(
            messages.pop().unwrap().is_empty(),
            "last message lacks EndOfMessage"
        );
        messages
    }

    fn pattern(len: usize, seed: u8) -> Vec<u8> {
        (0..len)
            .map(|i| (i as u8).wrapping_mul(31).wrapping_add(seed))
            .collect()
    }

    #[test]
    fn flights_survive_partial_writes_and_are_split_into_packets() {
        for (len, max) in [(1, 1), (9, 3), (4088, 3), (4089, 7), (70_000, 1000)] {
            let flight = pattern(len, 1);
            let mut w = TlsPreloginWrapper::new(Choppy::new(max, Vec::new()));
            write(&mut w, &flight);
            flush(&mut w);
            assert_eq!(
                messages(&w.stream.as_ref().unwrap().written),
                vec![flight],
                "len {len}"
            );
        }
    }

    #[test]
    fn a_flush_that_would_block_mid_handshake_is_finished_by_the_next_read() {
        let flight = pattern(100, 5);
        let mut w = TlsPreloginWrapper::new(Choppy::new(7, Vec::new()));
        write(&mut w, &flight);

        // The first write is `Pending`; OpenSSL would fail the handshake if
        // the flush were.
        assert!(Pin::new(&mut w).poll_flush(&mut cx()).is_ready());
        assert!(w.stream.as_ref().unwrap().written.is_empty());

        let mut buf = [0u8; 16];
        while Pin::new(&mut w).poll_read(&mut cx(), &mut buf).is_pending() {}
        assert_eq!(messages(&w.stream.as_ref().unwrap().written), vec![flight]);
    }

    #[test]
    fn writes_after_the_handshake_go_out_together_on_flush() {
        let (a, b) = (pattern(5000, 6), pattern(300, 7));
        let mut w = TlsPreloginWrapper::new(Choppy::new(1 << 20, Vec::new()));
        w.handshake_complete();
        write(&mut w, &a);
        write(&mut w, &b);
        assert!(w.stream.as_ref().unwrap().written.is_empty());

        flush(&mut w);
        assert_eq!(w.stream.as_ref().unwrap().written, [a, b].concat());
    }

    #[test]
    fn a_read_sends_gathered_writes_first() {
        let a = pattern(100, 8);
        let mut w = TlsPreloginWrapper::new(Choppy::new(7, vec![1, 2, 3]));
        w.handshake_complete();
        write(&mut w, &a);

        let mut buf = [0u8; 3];
        while Pin::new(&mut w).poll_read(&mut cx(), &mut buf).is_pending() {}
        assert_eq!(w.stream.as_ref().unwrap().written, a);
    }

    #[test]
    fn a_strict_handshake_is_unframed_and_never_blocks_a_flush() {
        let flight = pattern(100, 9);
        let mut w = TlsPreloginWrapper::unframed(Choppy::new(7, Vec::new()));
        write(&mut w, &flight);
        assert!(Pin::new(&mut w).poll_flush(&mut cx()).is_ready());

        let mut buf = [0u8; 1];
        while Pin::new(&mut w).poll_read(&mut cx(), &mut buf).is_pending() {}
        assert_eq!(w.stream.as_ref().unwrap().written, flight);
    }

    #[test]
    fn bytes_written_mid_flush_start_a_new_message() {
        let (a, b) = (pattern(5000, 1), pattern(300, 2));
        let mut w = TlsPreloginWrapper::new(Choppy::new(3, Vec::new()));
        write(&mut w, &a);
        for _ in 0..10 {
            let _ = Pin::new(&mut w).poll_flush(&mut cx());
        }
        write(&mut w, &b);
        flush(&mut w);
        assert_eq!(messages(&w.stream.as_ref().unwrap().written), vec![a, b]);
    }

    #[test]
    fn reads_strip_headers_and_skip_empty_packets() {
        let packet = |status: PacketStatus, payload: &[u8]| {
            let mut header = PacketHeader::new(payload.len() + HEADER_BYTES, 1);
            header.set_type(PacketType::PreLogin);
            header.set_status(status);
            let mut out = Vec::new();
            header.encode(&mut out).unwrap();
            out.extend_from_slice(payload);
            out
        };
        let (a, b) = (pattern(5000, 3), pattern(10, 4));
        let mut wire = packet(PacketStatus::NormalMessage, &a);
        wire.extend(packet(PacketStatus::NormalMessage, &[]));
        wire.extend(packet(PacketStatus::EndOfMessage, &b));

        let mut w = TlsPreloginWrapper::new(Choppy::new(5, wire));
        let mut read = Vec::new();
        let mut buf = [0u8; 64];
        while read.len() < a.len() + b.len() {
            if let Poll::Ready(n) = Pin::new(&mut w).poll_read(&mut cx(), &mut buf) {
                let n = n.unwrap();
                assert_ne!(n, 0, "early EOF after {} bytes", read.len());
                read.extend_from_slice(&buf[..n]);
            }
        }
        assert_eq!(read, [a, b].concat());
    }
}