fn test_read_bytes_with_files()

in src/connection.rs [1042:1110]


    fn test_read_bytes_with_files() {
        let (sender, receiver) = UnixStream::pair().unwrap();
        receiver.set_nonblocking(true).expect("Can't modify socket");
        let mut conn = HttpConnection::new(receiver);

        // Create 3 files, edit the content and rewind back to the start.
        let mut file1 = TempFile::new().unwrap().into_file();
        let mut file2 = TempFile::new().unwrap().into_file();
        let mut file3 = TempFile::new().unwrap().into_file();
        file1.write(b"foo").unwrap();
        file1.seek(SeekFrom::Start(0)).unwrap();
        file2.write(b"bar").unwrap();
        file2.seek(SeekFrom::Start(0)).unwrap();
        file3.write(b"foobar").unwrap();
        file3.seek(SeekFrom::Start(0)).unwrap();

        // Send 2 file descriptors along with 3 bytes of data.
        assert_eq!(
            sender.send_with_fds(
                &[[1, 2, 3].as_ref()],
                &[file1.into_raw_fd(), file2.into_raw_fd()]
            ),
            Ok(3)
        );

        // Check we receive the right amount of data along with the right
        // amount of file descriptors.
        assert_eq!(conn.read_bytes(), Ok(3));
        assert_eq!(conn.files.len(), 2);

        // Check the content of the data received
        assert_eq!(conn.buffer[0], 1);
        assert_eq!(conn.buffer[1], 2);
        assert_eq!(conn.buffer[2], 3);

        // Check the file descriptors are usable by checking the content that
        // can be read.
        let mut buf = [0; 10];
        assert_eq!(conn.files[0].read(&mut buf).unwrap(), 3);
        assert_eq!(&buf[..3], b"foo");
        assert_eq!(conn.files[1].read(&mut buf).unwrap(), 3);
        assert_eq!(&buf[..3], b"bar");

        // Send the 3rd file descriptor along with 1 byte of data.
        assert_eq!(
            sender.send_with_fds(&[[10].as_ref()], &[file3.into_raw_fd()]),
            Ok(1)
        );

        // Check the amount of data along with the amount of file descriptors
        // are updated.
        assert_eq!(conn.read_bytes(), Ok(1));
        assert_eq!(conn.files.len(), 3);

        // Check the content of the new data received
        assert_eq!(conn.buffer[0], 10);

        // Check the latest file descriptor is usable by checking the content
        // that can be read.
        let mut buf = [0; 10];
        assert_eq!(conn.files[2].read(&mut buf).unwrap(), 6);
        assert_eq!(&buf[..6], b"foobar");

        sender.shutdown(Shutdown::Write).unwrap();
        assert_eq!(
            conn.read_bytes().unwrap_err(),
            ConnectionError::ConnectionClosed
        );
    }