Skip to content

Commit 895b001

Browse files
Merge #384
384: [PLAT-268] Test connecting to and binding on `localhost` r=Pagten a=raoulstrackx An enclave should be able to bind a TCP listener on `localhost`. Likewise, it should be able to establish a local connection to the parent VM. Before this PR both scenarios worked correctly, but CI tests were missing. Co-authored-by: Raoul Strackx <[email protected]>
2 parents 3d80d40 + b02e958 commit 895b001

File tree

6 files changed

+32
-14
lines changed

6 files changed

+32
-14
lines changed

fortanix-vme/aws-nitro-enclaves/tests/incoming_connection/out.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
Server run #1
1+
Running server on 127.0.0.1:3400
22
Bind TCP socket to port 3400
33
Listening for incoming connections...
44
Waiting for connection 1
55
Connection 1: Connected
66
Waiting for connection 2
77
Connection 2: Connected
8-
Server run #2
8+
Running server on localhost:3400
99
Bind TCP socket to port 3400
1010
Listening for incoming connections...
1111
Waiting for connection 1

fortanix-vme/aws-nitro-enclaves/tests/incoming_connection/src/main.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#![feature(io_error_uncategorized)]
22
use std::io::{ErrorKind, Read, Write};
3-
use std::net::{IpAddr, Ipv4Addr, Shutdown, SocketAddr, TcpListener, TcpStream};
3+
use std::net::{IpAddr, Ipv4Addr, Shutdown, SocketAddr, TcpListener, TcpStream, ToSocketAddrs};
44
use std::os::unix::io::{AsRawFd, FromRawFd};
55

6-
fn server_run() {
6+
fn server_run<A: ToSocketAddrs>(addr: A) {
77
println!("Bind TCP socket to port 3400");
8-
let listener = TcpListener::bind("127.0.0.1:3400").expect("Bind failed");
8+
let listener = TcpListener::bind(addr).expect("Bind failed");
99
assert_eq!(listener.local_addr().unwrap(), SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 3400));
1010

1111
let fd = listener.as_raw_fd();
@@ -47,9 +47,9 @@ fn server_run() {
4747
}
4848

4949
fn main() {
50-
for run in 1..=2 {
51-
println!("Server run #{}", run);
52-
server_run()
50+
for addr in &["127.0.0.1:3400", "localhost:3400"] {
51+
println!("Running server on {}", addr);
52+
server_run(addr)
5353
}
5454
println!("Bye bye");
5555
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
Connected to Google successfully!
2+
Connected to Google successfully!
3+
Connected to Google successfully!
Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
use std::net::{Ipv4Addr, TcpStream};
1+
use std::net::TcpStream;
22
use std::io::{Read, Write};
33

4-
fn main() {
4+
fn connect(host: &str, port: u16) {
5+
let remote = format!("{}:{}", host, port);
56
println!("# Running outgoing connection test");
6-
let mut socket = TcpStream::connect(format!("google.com:80")).unwrap();
7+
let mut socket = TcpStream::connect(remote).unwrap();
78
// `socket.local_addr()` may return the actual local IP address, not 127.0.0.1
89
assert!(socket.local_addr().unwrap().port() != 80);
9-
assert!(socket.peer_addr().unwrap().ip() != Ipv4Addr::new(127, 0, 0, 1));
10-
assert_eq!(socket.peer_addr().unwrap().port(), 80);
10+
assert_eq!(socket.peer_addr().unwrap().port(), port);
1111
socket.write(b"GET / HTTP/1.1\n\n").unwrap();
1212
socket.flush().unwrap();
1313
let mut page = [0; 4192];
@@ -20,3 +20,9 @@ fn main() {
2020
println!("Failed to read from connection, got: {}", page);
2121
}
2222
}
23+
24+
fn main() {
25+
connect("www.google.com", 80);
26+
connect("127.0.0.1", 3080);
27+
connect("localhost", 3080);
28+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash -ex
2+
3+
function cleanup {
4+
killall socat
5+
}
6+
7+
trap cleanup err
8+
trap cleanup exit
9+
10+
socat -v TCP-LISTEN:3080,fork TCP:www.google.com:80

fortanix-vme/ci-common.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ function cargo_test {
126126
fi
127127

128128
if [ -f ./test_interaction.sh ]; then
129-
kill ${test_interaction}
129+
kill ${test_interaction} || true
130130
fi
131131

132132
popd

0 commit comments

Comments
 (0)