-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.rs
247 lines (209 loc) · 6.89 KB
/
test.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
use crate::config::{PeerConfig, RouterConfig};
use crate::connection_channel::{BgpConnectionChannel, BgpListenerChannel};
use crate::session::{FsmStateKind, SessionInfo};
use rdb::{Asn, Prefix};
use std::collections::BTreeMap;
use std::sync::mpsc::channel;
use std::sync::{Arc, Mutex};
use std::thread::sleep;
use std::thread::spawn;
use std::time::Duration;
type Router = crate::router::Router<BgpConnectionChannel>;
type Dispatcher = crate::dispatcher::Dispatcher<BgpConnectionChannel>;
type FsmEvent = crate::session::FsmEvent<BgpConnectionChannel>;
macro_rules! wait_for_eq {
($lhs:expr, $rhs:expr, $period:expr, $count:expr) => {
let mut ok = false;
for _ in 0..$count {
if $lhs == $rhs {
ok = true;
break;
}
sleep(Duration::from_secs($period));
}
if !ok {
assert_eq!($lhs, $rhs);
}
};
($lhs:expr, $rhs:expr) => {
wait_for_eq!($lhs, $rhs, 1, 30);
};
}
macro_rules! parse {
($x:expr, $err:expr) => {
$x.parse().expect($err)
};
}
macro_rules! ip {
($x:expr) => {
parse!($x, "ip address")
};
}
macro_rules! cidr {
($x:expr) => {
parse!($x, "ip cidr")
};
}
macro_rules! sockaddr {
($x:expr) => {
parse!($x, "socket address")
};
}
#[test]
fn test_basic_peering() {
let (r1, _d1, r2, d2) = two_router_test_setup(
"basic_peering",
Some(SessionInfo {
passive_tcp_establishment: true,
..Default::default()
}),
None,
);
let r1_session = r1.get_session(ip!("2.0.0.1")).expect("get session one");
let r2_session = r2.get_session(ip!("1.0.0.1")).expect("get session two");
// Give peer sessions a few seconds and ensure we have reached the
// established state on both sides.
wait_for_eq!(r1_session.state(), FsmStateKind::Established);
wait_for_eq!(r2_session.state(), FsmStateKind::Established);
// Shut down r2 and ensure that r2's peer session has gone back to idle.
// Ensure that r1's peer session to r2 has gone back to connect.
r2.shutdown();
d2.shutdown();
wait_for_eq!(r1_session.state(), FsmStateKind::Connect);
wait_for_eq!(r2_session.state(), FsmStateKind::Idle);
r2.run();
spawn(move || {
d2.run::<BgpListenerChannel>();
});
r2.send_event(FsmEvent::ManualStart)
.expect("manual start session two");
wait_for_eq!(r1_session.state(), FsmStateKind::Established);
wait_for_eq!(r2_session.state(), FsmStateKind::Established);
}
#[test]
fn test_basic_update() {
let (r1, d1, r2, _d2) = two_router_test_setup("basic_update", None, None);
// originate a prefix
r1.create_origin4(vec![ip!("1.2.3.0/24")])
.expect("originate");
// once we reach established the originated routes should have propagated
let r1_session = r1.get_session(ip!("2.0.0.1")).expect("get session one");
let r2_session = r2.get_session(ip!("1.0.0.1")).expect("get session two");
wait_for_eq!(r1_session.state(), FsmStateKind::Established);
wait_for_eq!(r2_session.state(), FsmStateKind::Established);
let prefix = Prefix::V4(cidr!("1.2.3.0/24"));
wait_for_eq!(r2.db.get_prefix_paths(&prefix).is_empty(), false);
// shut down r1 and ensure that the prefixes are withdrawn from r2 on
// session timeout.
r1.shutdown();
d1.shutdown();
wait_for_eq!(r2_session.state(), FsmStateKind::Connect);
wait_for_eq!(r1_session.state(), FsmStateKind::Idle);
wait_for_eq!(r2.db.get_prefix_paths(&prefix).is_empty(), true);
}
fn two_router_test_setup(
name: &str,
r1_info: Option<SessionInfo>,
r2_info: Option<SessionInfo>,
) -> (Arc<Router>, Arc<Dispatcher>, Arc<Router>, Arc<Dispatcher>) {
let log = mg_common::log::init_file_logger(&format!("r1.{name}.log"));
std::fs::create_dir_all("/tmp").expect("create tmp dir");
// Router 1 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
let db_path = format!("/tmp/r1.{name}.db");
let _ = std::fs::remove_dir_all(&db_path);
let db = rdb::Db::new(&db_path, log.clone()).expect("create db");
let a2s1 = Arc::new(Mutex::new(BTreeMap::new()));
let d1 =
Arc::new(crate::dispatcher::Dispatcher::<BgpConnectionChannel>::new(
a2s1.clone(),
"1.0.0.1:179".into(),
log.clone(),
));
let (r1_event_tx, event_rx) = channel();
let r1 = Arc::new(Router::new(
RouterConfig {
asn: Asn::FourOctet(4200000001),
id: 1,
},
log.clone(),
db.clone(),
a2s1.clone(),
));
r1.run();
let d = d1.clone();
spawn(move || {
d.run::<BgpListenerChannel>();
});
r1.new_session(
PeerConfig {
name: "r2".into(),
host: sockaddr!("2.0.0.1:179"),
hold_time: 6,
idle_hold_time: 6,
delay_open: 0,
connect_retry: 1,
keepalive: 3,
resolution: 100,
},
sockaddr!("1.0.0.1:179"),
r1_event_tx.clone(),
event_rx,
r1_info.unwrap_or_default(),
)
.expect("new session on router one");
r1_event_tx
.send(FsmEvent::ManualStart)
.expect("session manual start on router one");
// Router 2 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
let log = mg_common::log::init_file_logger(&format!("r2.{name}.log"));
let db_path = format!("/tmp/r2.{name}.db");
let _ = std::fs::remove_dir_all(&db_path);
let db = rdb::Db::new(&db_path, log.clone())
.expect("create datastore for router 2");
let a2s2 = Arc::new(Mutex::new(BTreeMap::new()));
let d2 =
Arc::new(crate::dispatcher::Dispatcher::<BgpConnectionChannel>::new(
a2s2.clone(),
"2.0.0.1:179".into(),
log.clone(),
));
let (r2_event_tx, event_rx) = channel();
let r2 = Arc::new(Router::new(
RouterConfig {
asn: Asn::FourOctet(4200000002),
id: 2,
},
log.clone(),
db.clone(),
a2s2.clone(),
));
r2.run();
let d = d2.clone();
spawn(move || {
d.run::<BgpListenerChannel>();
});
r2.new_session(
PeerConfig {
name: "r1".into(),
host: sockaddr!("1.0.0.1:179"),
hold_time: 6,
idle_hold_time: 6,
delay_open: 0,
connect_retry: 1,
keepalive: 3,
resolution: 100,
},
sockaddr!("2.0.0.1:179"),
r2_event_tx.clone(),
event_rx,
r2_info.unwrap_or_default(),
)
.expect("new session on router two");
r2_event_tx
.send(FsmEvent::ManualStart)
.expect("start session on router two");
(r1, d1, r2, d2)
}