1 /*
2  * Copyright 2019 fsyncd, Berlin, Germany.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 use rand::RngCore;
18 use sha2::{Digest, Sha256};
19 use std::io::{Read, Write};
20 use vsock::{get_local_cid, VsockAddr, VsockListener, VsockStream, VMADDR_CID_HOST};
21 
22 const TEST_BLOB_SIZE: usize = 1_000_000;
23 const TEST_BLOCK_SIZE: usize = 5_000;
24 
25 const SERVER_CID: u32 = 3;
26 const SERVER_PORT: u32 = 8000;
27 const LISTEN_PORT: u32 = 9000;
28 
29 /// A simple test for the vsock implementation.
30 /// Generate a large random blob of binary data, and transfer it in chunks over the VsockStream
31 /// interface. The vm enpoint is running a simple echo server, so for each chunk we will read
32 /// it's reply and compute a checksum. Comparing the data sent and received confirms that the
33 /// vsock implementation does not introduce corruption and properly implements the interface
34 /// semantics.
35 #[test]
test_vsock()36 fn test_vsock() {
37     let mut rng = rand::thread_rng();
38     let mut blob: Vec<u8> = vec![];
39     let mut rx_blob = vec![];
40     let mut tx_pos = 0;
41 
42     blob.resize(TEST_BLOB_SIZE, 0);
43     rx_blob.resize(TEST_BLOB_SIZE, 0);
44     rng.fill_bytes(&mut blob);
45 
46     let mut stream =
47         VsockStream::connect(&VsockAddr::new(SERVER_CID, SERVER_PORT)).expect("connection failed");
48 
49     while tx_pos < TEST_BLOB_SIZE {
50         let written_bytes = stream
51             .write(&blob[tx_pos..tx_pos + TEST_BLOCK_SIZE])
52             .expect("write failed");
53         if written_bytes == 0 {
54             panic!("stream unexpectedly closed");
55         }
56 
57         let mut rx_pos = tx_pos;
58         while rx_pos < (tx_pos + written_bytes) {
59             let read_bytes = stream.read(&mut rx_blob[rx_pos..]).expect("read failed");
60             if read_bytes == 0 {
61                 panic!("stream unexpectedly closed");
62             }
63             rx_pos += read_bytes;
64         }
65 
66         tx_pos += written_bytes;
67     }
68 
69     let expected = Sha256::digest(&blob);
70     let actual = Sha256::digest(&rx_blob);
71 
72     assert_eq!(expected, actual);
73 }
74 
75 #[test]
test_get_local_cid()76 fn test_get_local_cid() {
77     assert_eq!(get_local_cid().unwrap(), VMADDR_CID_HOST);
78 }
79 
80 #[test]
test_listener_local_addr()81 fn test_listener_local_addr() {
82     let listener = VsockListener::bind(&VsockAddr::new(VMADDR_CID_HOST, LISTEN_PORT)).unwrap();
83 
84     let local_addr = listener.local_addr().unwrap();
85     assert_eq!(local_addr.cid(), VMADDR_CID_HOST);
86     assert_eq!(local_addr.port(), LISTEN_PORT);
87 }
88 
89 #[test]
test_stream_addresses()90 fn test_stream_addresses() {
91     let stream =
92         VsockStream::connect(&VsockAddr::new(SERVER_CID, SERVER_PORT)).expect("connection failed");
93 
94     let local_addr = stream.local_addr().unwrap();
95     // Apparently on some systems a client socket has the host CID, on some it has CID_ANY. Allow
96     // either.
97     assert!([libc::VMADDR_CID_ANY, VMADDR_CID_HOST].contains(&local_addr.cid()));
98 
99     let peer_addr = stream.peer_addr().unwrap();
100     assert_eq!(peer_addr.cid(), SERVER_CID);
101     assert_eq!(peer_addr.port(), SERVER_PORT);
102 }
103