/__w/smoldot/smoldot/repo/lib/src/libp2p/connection/single_stream_handshake/tests.rs
Line | Count | Source (jump to first uncovered line) |
1 | | // Smoldot |
2 | | // Copyright (C) 2019-2022 Parity Technologies (UK) Ltd. |
3 | | // SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 |
4 | | |
5 | | // This program is free software: you can redistribute it and/or modify |
6 | | // it under the terms of the GNU General Public License as published by |
7 | | // the Free Software Foundation, either version 3 of the License, or |
8 | | // (at your option) any later version. |
9 | | |
10 | | // This program is distributed in the hope that it will be useful, |
11 | | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | | // GNU General Public License for more details. |
14 | | |
15 | | // You should have received a copy of the GNU General Public License |
16 | | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | | |
18 | | #![cfg(test)] |
19 | | |
20 | | use core::{cmp, mem}; |
21 | | |
22 | | use super::{super::super::read_write::ReadWrite, Handshake, NoiseKey}; |
23 | | |
24 | | #[test] |
25 | 1 | fn handshake_basic_works() { |
26 | 1 | fn test_with_buffer_sizes(mut size1: usize, mut size2: usize) { |
27 | 1 | let key1 = NoiseKey::new(&rand::random(), &rand::random()); |
28 | 1 | let key2 = NoiseKey::new(&rand::random(), &rand::random()); |
29 | 1 | |
30 | 1 | let mut handshake1 = Handshake::noise_yamux(&key1, &rand::random(), true); |
31 | 1 | let mut handshake2 = Handshake::noise_yamux(&key2, &rand::random(), false); |
32 | 1 | |
33 | 1 | let mut buf_1_to_2 = Vec::new(); |
34 | 1 | let mut buf_2_to_1 = Vec::new(); |
35 | 1 | |
36 | 5 | while !matches!( |
37 | 6 | (&handshake1, &handshake2), |
38 | 1 | (Handshake::Success { .. }, Handshake::Success { .. }) |
39 | 1 | ) { |
40 | 5 | match handshake1 { |
41 | 1 | Handshake::Success { .. } => {}0 |
42 | 5 | Handshake::Healthy(nego) => { |
43 | 5 | let mut read_write = ReadWrite { |
44 | 5 | now: 0, |
45 | 5 | incoming_buffer: buf_2_to_1, |
46 | 5 | expected_incoming_bytes: Some(0), |
47 | 5 | read_bytes: 0, |
48 | 5 | write_bytes_queued: buf_1_to_2.len(), |
49 | 5 | write_bytes_queueable: Some(size1 - buf_1_to_2.len()), |
50 | 5 | write_buffers: vec![mem::take(&mut buf_1_to_2)], |
51 | 5 | wake_up_after: None, |
52 | 5 | }; |
53 | 5 | handshake1 = nego.read_write(&mut read_write).unwrap(); |
54 | 5 | buf_2_to_1 = read_write.incoming_buffer; |
55 | 5 | buf_1_to_2.extend( |
56 | 5 | read_write |
57 | 5 | .write_buffers |
58 | 5 | .drain(..) |
59 | 15 | .flat_map(|b| b.into_iter()), |
60 | 5 | ); |
61 | 5 | size2 = cmp::max(size2, read_write.expected_incoming_bytes.unwrap_or(0)); |
62 | 5 | } |
63 | 1 | } |
64 | 1 | |
65 | 5 | match handshake2 { |
66 | 2 | Handshake::Success { .. } => {} |
67 | 3 | Handshake::Healthy(nego) => { |
68 | 3 | let mut read_write = ReadWrite { |
69 | 3 | now: 0, |
70 | 3 | incoming_buffer: buf_1_to_2, |
71 | 3 | expected_incoming_bytes: Some(0), |
72 | 3 | read_bytes: 0, |
73 | 3 | write_bytes_queued: buf_2_to_1.len(), |
74 | 3 | write_bytes_queueable: Some(size2 - buf_2_to_1.len()), |
75 | 3 | write_buffers: vec![mem::take(&mut buf_2_to_1)], |
76 | 3 | wake_up_after: None, |
77 | 3 | }; |
78 | 3 | handshake2 = nego.read_write(&mut read_write).unwrap(); |
79 | 3 | buf_1_to_2 = read_write.incoming_buffer; |
80 | 3 | buf_2_to_1.extend( |
81 | 3 | read_write |
82 | 3 | .write_buffers |
83 | 3 | .drain(..) |
84 | 17 | .flat_map(|b| b.into_iter()), |
85 | 3 | ); |
86 | 3 | size1 = cmp::max(size1, read_write.expected_incoming_bytes.unwrap_or(0)); |
87 | 3 | } |
88 | 1 | } |
89 | 1 | } |
90 | 1 | } |
91 | 1 | |
92 | 1 | test_with_buffer_sizes(256, 256); |
93 | 1 | // TODO: not passing because Noise wants at least 19 bytes of buffer |
94 | 1 | //test_with_buffer_sizes(1, 1); |
95 | 1 | //test_with_buffer_sizes(1, 2048); |
96 | 1 | //test_with_buffer_sizes(2048, 1); |
97 | 1 | } |