noxious/
socket.rs

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
use async_trait::async_trait;
#[cfg(test)]
use mockall::automock;
use pin_project_lite::pin_project;
use std::{io, net::SocketAddr};
use tokio::{
    io::{AsyncRead, AsyncWrite},
    net::{TcpListener as TokioTcpListener, TcpStream as TokioTcpStream},
};

#[cfg(not(test))]
use tokio::net::tcp::{OwnedReadHalf, OwnedWriteHalf};

/// The TcpListener interface we need to mock
#[cfg_attr(test, automock(type Stream=TcpStream;))]
#[async_trait]
pub trait SocketListener: Sized + Send + Sync {
    /// The associated listener interface to be mocked too
    type Stream: SocketStream + 'static;

    /// Creates a new SocketListener, which will be bound to the specified address.
    async fn bind(addr: &str) -> io::Result<Self>
    where
        Self: Sized;

    /// Accepts a new incoming connection from this listener.
    async fn accept(&self) -> io::Result<(Self::Stream, SocketAddr)>;
}

/// The TcpStream interface we need to mock
#[cfg_attr(test, automock)]
#[async_trait]
pub trait SocketStream: Sized + Send + Sync {
    /// Opens a TCP connection to a remote host.
    async fn connect(addr: &str) -> io::Result<Self>
    where
        Self: Sized + 'static;

    /// Splits the inner `TcpStream` into a read half and a write half, which
    /// can be used to read and write the stream concurrently.
    fn into_split(self) -> (ReadStream, WriteStream);
}

/// A simple wrapper around Tokio TcpListener to make it mockable
#[derive(Debug)]
pub struct TcpListener {
    inner: TokioTcpListener,
}

/// A simple wrapper around Tokio TcpStream to make it mockable
#[derive(Debug)]
pub struct TcpStream {
    inner: TokioTcpStream,
}

#[async_trait]
impl SocketListener for TcpListener {
    type Stream = TcpStream;

    async fn bind(addr: &str) -> io::Result<TcpListener>
    where
        Self: Sized,
    {
        Ok(TcpListener {
            inner: TokioTcpListener::bind(addr).await?,
        })
    }

    async fn accept(&self) -> io::Result<(Self::Stream, SocketAddr)> {
        let (stream, addr) = self.inner.accept().await?;
        let wrapper = TcpStream { inner: stream };
        Ok((wrapper, addr))
    }
}
#[async_trait]
impl SocketStream for TcpStream {
    async fn connect(addr: &str) -> io::Result<Self>
    where
        Self: Sized,
    {
        let inner = TokioTcpStream::connect(addr).await?;
        Ok(TcpStream { inner })
    }

    #[cfg(not(test))]
    fn into_split(self) -> (ReadStream, WriteStream) {
        let (read_half, write_half) = self.inner.into_split();
        (ReadStream::new(read_half), WriteStream::new(write_half))
    }

    #[cfg(test)]
    fn into_split(self) -> (ReadStream, WriteStream) {
        unimplemented!("must mock")
    }
}

#[cfg(not(test))]
type ReadHalf = OwnedReadHalf;
#[cfg(test)]
type ReadHalf = tokio_test::io::Mock;

#[cfg(not(test))]
type WriteHalf = OwnedWriteHalf;
#[cfg(test)]
type WriteHalf = tokio_test::io::Mock;

pin_project! {
    /// Wrapper for OwnedReadHalf for mocking
    #[derive(Debug)]
    pub struct ReadStream {
        #[pin]
        inner: ReadHalf,
    }
}

pin_project! {
    /// Wrapper for OwnedWriteHalf for mocking
    #[derive(Debug)]
    pub struct WriteStream {
        #[pin]
        inner: WriteHalf,

    }
}

#[cfg_attr(test, automock)]
impl ReadStream {
    pub(crate) fn new(inner: ReadHalf) -> ReadStream {
        ReadStream { inner }
    }
}

#[cfg_attr(test, automock)]
impl WriteStream {
    pub(crate) fn new(inner: WriteHalf) -> WriteStream {
        WriteStream { inner }
    }
}

impl AsyncRead for ReadStream {
    fn poll_read(
        self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
        buf: &mut tokio::io::ReadBuf<'_>,
    ) -> std::task::Poll<io::Result<()>> {
        self.project().inner.poll_read(cx, buf)
    }
}

impl AsyncWrite for WriteStream {
    fn poll_write(
        self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
        buf: &[u8],
    ) -> std::task::Poll<Result<usize, io::Error>> {
        self.project().inner.poll_write(cx, buf)
    }

    fn poll_flush(
        self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Result<(), io::Error>> {
        self.project().inner.poll_flush(cx)
    }

    fn poll_shutdown(
        self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Result<(), io::Error>> {
        self.project().inner.poll_shutdown(cx)
    }
}

#[cfg(test)]
mod tests {
    use tokio_test::assert_ok;

    use super::*;

    // Dummy test for coverage's sake
    #[tokio::test]
    async fn test_tcp_stream() {
        let (ready_tx, ready_rx) = tokio::sync::oneshot::channel::<()>();
        tokio::spawn(async move {
            let listener = TcpListener {
                inner: TokioTcpListener::bind("127.0.0.1:9909").await.unwrap(),
            };
            let _ = ready_tx.send(());
            let _ = listener.accept().await.unwrap();
        });

        assert_ok!(ready_rx.await);
        let _stream = TcpStream::connect("127.0.0.1:9909").await.unwrap();
        // let _ = stream.into_split();
    }
}