summaryrefslogtreecommitdiffstats
path: root/third_party/rust/mio-uds/tests/smoke.rs
blob: 314b1333252509adcd38b62e25c3e87c35b526c7 (plain)
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
extern crate iovec;
extern crate mio;
extern crate tempdir;
extern crate mio_uds;

use std::io::prelude::*;
use std::time::Duration;

use iovec::IoVec;
use mio::*;
use mio_uds::*;
use tempdir::TempDir;

macro_rules! t {
    ($e:expr) => (match $e {
        Ok(e) => e,
        Err(e) => panic!("{} failed with {}", stringify!($e), e),
    })
}

#[test]
fn listener() {
    let td = t!(TempDir::new("uds"));
    let a = t!(UnixListener::bind(td.path().join("foo")));
    assert!(t!(a.accept()).is_none());
    t!(a.local_addr());
    assert!(t!(a.take_error()).is_none());
    let b = t!(a.try_clone());
    assert!(t!(b.accept()).is_none());

    let poll = t!(Poll::new());
    let mut events = Events::with_capacity(1024);

    t!(poll.register(&a, Token(1), Ready::readable(), PollOpt::edge()));

    let s = t!(UnixStream::connect(td.path().join("foo")));

    assert_eq!(t!(poll.poll(&mut events, None)), 1);

    let (s2, addr) = t!(a.accept()).unwrap();

    assert_eq!(t!(s.peer_addr()).as_pathname(), t!(s2.local_addr()).as_pathname());
    assert_eq!(t!(s.local_addr()).as_pathname(), t!(s2.peer_addr()).as_pathname());
    assert_eq!(addr.as_pathname(), t!(s.local_addr()).as_pathname());
}

#[test]
fn stream() {
    let poll = t!(Poll::new());
    let mut events = Events::with_capacity(1024);
    let (mut a, mut b) = t!(UnixStream::pair());

    let both = Ready::readable() | Ready::writable();
    t!(poll.register(&a, Token(1), both, PollOpt::edge()));
    t!(poll.register(&b, Token(2), both, PollOpt::edge()));

    assert_eq!(t!(poll.poll(&mut events, Some(Duration::new(0, 0)))), 2);
    assert_eq!(events.get(0).unwrap().readiness(), Ready::writable());
    assert_eq!(events.get(1).unwrap().readiness(), Ready::writable());

    assert_eq!(t!(a.write(&[3])), 1);

    assert_eq!(t!(poll.poll(&mut events, Some(Duration::new(0, 0)))), 1);
    assert!(events.get(0).unwrap().readiness().is_readable());
    assert_eq!(events.get(0).unwrap().token(), Token(2));

    assert_eq!(t!(b.read(&mut [0; 1024])), 1);
}

#[test]
fn stream_iovec() {
    let poll = t!(Poll::new());
    let mut events = Events::with_capacity(1024);
    let (a, b) = t!(UnixStream::pair());

    let both = Ready::readable() | Ready::writable();
    t!(poll.register(&a, Token(1), both, PollOpt::edge()));
    t!(poll.register(&b, Token(2), both, PollOpt::edge()));

    assert_eq!(t!(poll.poll(&mut events, Some(Duration::new(0, 0)))), 2);
    assert_eq!(events.get(0).unwrap().readiness(), Ready::writable());
    assert_eq!(events.get(1).unwrap().readiness(), Ready::writable());

    let send = b"Hello, World!";
    let vecs: [&IoVec;2] = [ (&send[..6]).into(),
                             (&send[6..]).into() ];

    assert_eq!(t!(a.write_bufs(&vecs)), send.len());

    assert_eq!(t!(poll.poll(&mut events, Some(Duration::new(0, 0)))), 1);
    assert!(events.get(0).unwrap().readiness().is_readable());
    assert_eq!(events.get(0).unwrap().token(), Token(2));

    let mut recv = [0; 13];
    {
        let (mut first, mut last) = recv.split_at_mut(6);
        let mut vecs: [&mut IoVec;2] = [ first.into(), last.into() ];
        assert_eq!(t!(b.read_bufs(&mut vecs)), send.len());
    }
    assert_eq!(&send[..], &recv[..]);
}