maidsafe-archive / socket-collection

Collection of sockets wrapped for convenience
BSD 3-Clause "New" or "Revised" License
6 stars 11 forks source link

Tests Connected UDP Sockets #25

Closed ustulation closed 5 years ago

ustulation commented 5 years ago
  1. If Sock-0 is connected to sock-1 try sending something (continuously) from Sock-2 to Sock-0 and confirm it's ignored and then after a while while Sock-2 is continuing to send to Sock-0 send something continuously from Sock-1 to Sock-0 and those should pass. This should give more confidence about cross-platform compliance of the behaviour should the test pass. Make sure you assert no errors are generated for Sock-0; it should simply filter the datagrams. Sock-1 should do send() and Sock-2 should do send_to() ofc.
  2. Next (in a different test-case), Sock-0 should be able to send() and Sock-1 rx it. Simultaneously Sock-0 should be able to send_to() to Sock-2 and Sock-2 should rx it too. So while sending a connected socket (Sock-0) should be able to send to anyone but while rx only receive it from Sock-1. This behaviour might fail as I think connected Sock-0 will be disallowed to send to Sock-2 (because it cannot rx from it so OS stack might prevent sending to someone you can't rx from).
povilasb commented 5 years ago

As expected the 2nd test case fails on Windows and Mac OS: https://travis-ci.org/maidsafe/socket-collection/builds/461290797?utm_source=github_status&utm_medium=notification I'm removing this test, but it was:

#[test]
fn connected_socket_can_use_write_and_write_to_simultaneously() {
    const SOCKET1_TOKEN: Token = Token(0);
    const SOCKET2_TOKEN: Token = Token(1);
    const SOCKET3_TOKEN: Token = Token(2);
    const TIMEOUT_TOKEN: Token = Token(3);

    let mut sock2 = unwrap!(UdpSock::bind(&unwrap!("127.0.0.1:0".parse())));
    let sock2_addr = unwrap!(sock2.local_addr());

    let mut sock1 = unwrap!(UdpSock::bind(&unwrap!("127.0.0.1:0".parse())));
    unwrap!(sock1.connect(&sock2_addr));
    let sock1_addr = unwrap!(sock1.local_addr());

    let mut sock3 = unwrap!(UdpSock::bind(&unwrap!("127.0.0.1:0".parse())));
    let sock3_addr = unwrap!(sock3.local_addr());

    let poll = unwrap!(Poll::new());
    unwrap!(poll.register(&sock1, SOCKET1_TOKEN, Ready::writable(), PollOpt::edge()));
    unwrap!(poll.register(&sock2, SOCKET2_TOKEN, Ready::readable(), PollOpt::edge()));
    unwrap!(poll.register(&sock3, SOCKET3_TOKEN, Ready::readable(), PollOpt::edge()));

    let mut timer = Timer::default();
    timer.set_timeout(Duration::from_secs(1), ()); // let's terminate the test after 1 second
    unwrap!(poll.register(&timer, TIMEOUT_TOKEN, Ready::readable(), PollOpt::edge(),));

    let mut sock2_received_from = None;
    let mut sock3_received_from = None;
    let mut retry_send = false;

    let mut events = Events::with_capacity(1024);
    'event_loop: loop {
        let _ = unwrap!(poll.poll(&mut events, None));
        for event in events.iter() {
            match event.token() {
                SOCKET1_TOKEN => {
                    if retry_send {
                        let _ = unwrap!(sock1.write::<Vec<u8>>(None));
                    } else {
                        let sent1 = unwrap!(sock1.write(Some((vec![1u8, 2, 3], 1))));
                        let sent2 = unwrap!(sock1.write_to(Some((vec![1u8, 2, 3], sock3_addr, 1))));
                        if !sent1 || !sent2 {
                            retry_send = true;
                        } else {
                            unwrap!(poll.deregister(&sock1));
                        }
                    }
                }
                SOCKET2_TOKEN => {
                    let res: Option<(Vec<u8>, _)> = unwrap!(sock2.read_frm());
                    sock2_received_from = res.map(|(_data, peer_addr)| peer_addr);
                }
                SOCKET3_TOKEN => {
                    let res: Option<(Vec<u8>, _)> = unwrap!(sock3.read_frm());
                    sock3_received_from = res.map(|(_data, peer_addr)| peer_addr);
                }
                TIMEOUT_TOKEN => break 'event_loop,
                _ => panic!("Unexpected event"),
            }
        }
    }

    assert_that!(sock2_received_from, eq(Some(sock1_addr)));
    assert_that!(sock3_received_from, eq(Some(sock1_addr)));
}
ustulation commented 5 years ago

Done.