Implementation of the IP loopback. (#19)
This patch lets users connect to `ip:127.0.0.1/*`. Bytes written to this connection can only be read from the same connection.
This commit is contained in:
parent
ead01ea2da
commit
a72750cc41
1 changed files with 204 additions and 65 deletions
|
|
@ -13,7 +13,10 @@
|
|||
//! To open a IP connection, use `ip:[host]/protocol`.
|
||||
//!
|
||||
//! * If `host` is specified, it must be an ipv4 number (e.g. `192.168.0.1`)
|
||||
//! and the connection may be used immediately to send/receive data.
|
||||
//! and the connection may be used immediately to send/receive data. Ip v4 number
|
||||
//! `127.0.0.1` is hardwired to the loopback device (i.e. localhost), which doesn't
|
||||
//! access any physical device and in which data can only be read by the same
|
||||
//! connection that has written it.
|
||||
//! * If `host` is omitted, this connectino will wait for a distant peer to
|
||||
//! connect.
|
||||
//! * The `protocol` is the hex-based number of the ip protocol
|
||||
|
|
@ -47,3 +50,43 @@ fn main() {
|
|||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn test() {
|
||||
use scheme::IpScheme;
|
||||
|
||||
println!("* Test that we can read a simple packet from the same connection.");
|
||||
let bytes = "TEST".as_bytes();
|
||||
let mut scheme = IpScheme::new();
|
||||
let a = scheme.open("ip:127.0.0.1/11").unwrap();
|
||||
let b = scheme.open("ip:127.0.0.1/11").unwrap();
|
||||
let num_bytes_written = scheme.write(a, bytes).unwrap();
|
||||
assert_eq!(num_bytes_written, bytes.len());
|
||||
|
||||
let mut buf = [0;65536];
|
||||
let num_bytes_read = scheme.read(a, &mut buf).unwrap();
|
||||
assert_eq!(num_bytes_read, num_bytes_written);
|
||||
|
||||
let bytes_read = &buf[0..num_bytes_read];
|
||||
assert_eq!(bytes, bytes_read);
|
||||
|
||||
println!("* Test that we cannot read the same packet from a different connection.");
|
||||
let num_bytes_read = scheme.read(b, &mut buf).unwrap();
|
||||
assert_eq!(num_bytes_read, 0);
|
||||
|
||||
println!("* Push a number of packets, check that we get them in the right order.");
|
||||
let mut payloads : Vec<String> = 0..100.map(|i| format!("TEST {}", i)).collect();
|
||||
for payload in &payloads {
|
||||
let bytes = payload.into_bytes();
|
||||
let num_bytes_written = scheme.write(a, &bytes).unwrap();
|
||||
assert_eq!(bytes.len(), num_bytes_written);
|
||||
}
|
||||
for payload in &payloads {
|
||||
let bytes = payload.into_bytes();
|
||||
let mut buf = [0;65536];
|
||||
let num_bytes_read = scheme.read(a, &mut buf).unwrap();
|
||||
assert_eq!(bytes.len(), num_bytes_read);
|
||||
let bytes_read = &buf[0..num_bytes_read];
|
||||
assert_eq!(bytes, bytes_read);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue