bacnet-cli 0.14.0

BACnet command-line tool for device discovery, reading, and writing
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
//! Real loopback lifecycle tests plus pure UI generation/admission checks.

use super::port_retry::rerun_on_lost_port;
use super::*;
use crate::tui::app::{Link, Overlay};
use crate::tui::message::Failure;
use crate::tui::profile::service::{Request, Service};
use crate::tui::profile::store::Store;
use crate::tui::profile::{Overrides, Profile};
use crate::tui::worker::session::{ConnectPlan, TransportKind};
use crate::tui::worker::{coordinator, EventSink};
use std::net::{Ipv4Addr, SocketAddrV4};
use std::time::Duration;
use tokio::sync::mpsc;

fn profile() -> Profile {
    let mut p = Profile::default();
    p.bip_mut().interface = Ipv4Addr::LOCALHOST;
    p.bip_mut().port = 0;
    p.bip_mut().broadcast = Ipv4Addr::new(127, 0, 0, 255);
    p
}

pub(super) async fn event(events: &mut mpsc::Receiver<WorkerEvent>) -> WorkerEvent {
    tokio::time::timeout(Duration::from_secs(5), events.recv())
        .await
        .expect("worker event timeout")
        .expect("coordinator stopped")
}

pub(super) async fn connected(events: &mut mpsc::Receiver<WorkerEvent>, generation: u64) -> String {
    match outcome(events, generation).await {
        Ok(local) => local,
        Err(error) => panic!("connection failed: {error}"),
    }
}

/// How connection attempt `generation` ends: the local address, or why it
/// failed.
async fn outcome(
    events: &mut mpsc::Receiver<WorkerEvent>,
    generation: u64,
) -> Result<String, Failure> {
    loop {
        match event(events).await {
            WorkerEvent::Session {
                generation: actual,
                event,
            } if actual == generation => match *event {
                WorkerEvent::Connected { local } => return Ok(local),
                WorkerEvent::ConnectFailed { error } => return Err(error),
                _ => {}
            },
            _ => {}
        }
    }
}

/// [`connected`] on a port the test picked and freed, which another process
/// may take before the session binds it (#1877). A failed attempt probes the
/// port: when another socket holds it, the probe's bind error goes back to
/// [`rerun_on_lost_port`], which reruns the test on a fresh port. A failure
/// on a free port panics.
async fn connected_on(
    events: &mut mpsc::Receiver<WorkerEvent>,
    generation: u64,
    port: u16,
) -> std::io::Result<String> {
    let error = match outcome(events, generation).await {
        Ok(local) => return Ok(local),
        Err(error) => error,
    };
    std::net::UdpSocket::bind(SocketAddrV4::new(Ipv4Addr::LOCALHOST, port))?;
    panic!("connection failed on a free port: {error}")
}

pub(super) async fn disconnected(events: &mut mpsc::Receiver<WorkerEvent>, generation: u64) {
    loop {
        if event(events).await == (WorkerEvent::Disconnected { generation }) {
            return;
        }
    }
}

/// The session binds a port the test freed, frees it on each disconnect and
/// binds it again, and the closing probe binds it once more. Another process
/// can take it in any of those gaps, so the test then reruns on a fresh port
/// (#1877).
#[tokio::test]
async fn coordinator_disconnects_reconnects_same_port_and_joins_on_quit() {
    rerun_on_lost_port(async || {
        let mut p = profile();
        let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
        let port = socket.local_addr().unwrap().port();
        p.bip_mut().port = port;
        drop(socket);
        let args = p.transport_args().unwrap();
        let plan = ConnectPlan {
            kind: TransportKind::Bip,
            args,
            await_interface: false,
        };
        let (sink, mut events) = EventSink::channel(32);
        let (commands, rx) = mpsc::channel(16);
        let task = coordinator::spawn(Some(plan), None, sink, rx, Default::default());
        let cycled = async {
            let first = connected_on(&mut events, 1, port).await?;
            for generation in 1..=3 {
                commands.send(Command::Disconnect).await.unwrap();
                disconnected(&mut events, generation).await;
                commands.send(Command::Reconnect).await.unwrap();
                assert_eq!(
                    connected_on(&mut events, generation + 1, port).await?,
                    first
                );
            }
            std::io::Result::Ok(())
        }
        .await;
        // Join the coordinator on a lost port too, so a rerun starts clean.
        drop((commands, events));
        tokio::time::timeout(Duration::from_secs(2), task)
            .await
            .unwrap()
            .unwrap();
        cycled?;
        // The joined coordinator released the port.
        std::net::UdpSocket::bind(SocketAddrV4::new(Ipv4Addr::LOCALHOST, port)).map(drop)
    })
    .await;
}

#[tokio::test]
async fn failed_attempt_keeps_coordinator_available_for_corrected_interface() {
    let mut p = profile();
    p.bip_mut().interface = Ipv4Addr::new(203, 0, 113, 222);
    let plan = ConnectPlan {
        kind: TransportKind::Bip,
        args: p.transport_args().unwrap(),
        await_interface: false,
    };
    let (sink, mut events) = EventSink::channel(32);
    let (commands, rx) = mpsc::channel(16);
    let task = coordinator::spawn(Some(plan), None, sink, rx, Default::default());
    loop {
        if let WorkerEvent::Session {
            generation: 1,
            event,
        } = event(&mut events).await
        {
            if matches!(*event, WorkerEvent::ConnectFailed { .. }) {
                break;
            }
        }
    }
    commands
        .send(Command::Connect {
            interface: Ipv4Addr::LOCALHOST,
            broadcast: p.bip().broadcast,
        })
        .await
        .unwrap();
    connected(&mut events, 2).await;
    drop((commands, events));
    tokio::time::timeout(Duration::from_secs(2), task)
        .await
        .unwrap()
        .unwrap();
}

#[tokio::test]
async fn disconnect_cancels_pending_foreign_registration_and_stops_renewals() {
    let bbmd = tokio::net::UdpSocket::bind("127.0.0.1:0").await.unwrap();
    let mut p = profile();
    p.bip_mut().bbmd = Some(match bbmd.local_addr().unwrap() {
        std::net::SocketAddr::V4(a) => a,
        _ => unreachable!(),
    });
    p.bip_mut().ttl = 2;
    p.bip_mut().renewal_secs = Some(1);
    let plan = ConnectPlan {
        kind: TransportKind::Bip,
        args: p.transport_args().unwrap(),
        await_interface: false,
    };
    let (sink, mut events) = EventSink::channel(32);
    let (commands, rx) = mpsc::channel(16);
    let task = coordinator::spawn(Some(plan), None, sink, rx, Default::default());
    let mut packet = [0; 256];
    let (len, peer) = tokio::time::timeout(Duration::from_secs(2), bbmd.recv_from(&mut packet))
        .await
        .unwrap()
        .unwrap();
    assert_eq!(&packet[..len], &[0x81, 0x05, 0x00, 0x06, 0x00, 0x02]);
    commands.send(Command::Disconnect).await.unwrap();
    tokio::time::timeout(Duration::from_millis(500), disconnected(&mut events, 1))
        .await
        .expect("disconnect must not wait for registration timeout");
    assert!(
        tokio::time::timeout(Duration::from_millis(1200), bbmd.recv_from(&mut packet))
            .await
            .is_err(),
        "retired connection renewed at {peer}"
    );
    drop((commands, events));
    task.await.unwrap();
}

#[tokio::test]
async fn a_saturated_event_channel_cannot_block_retirement_or_quit() {
    let (sink, events) = EventSink::channel(1);
    let (commands, rx) = mpsc::channel(16);
    let plan = ConnectPlan {
        kind: TransportKind::Bip,
        args: profile().transport_args().unwrap(),
        await_interface: false,
    };
    let task = coordinator::spawn(Some(plan), None, sink, rx, Default::default());
    // SessionStarted occupies the only slot; Connected waits for room.
    tokio::time::sleep(Duration::from_millis(30)).await;
    commands.send(Command::Disconnect).await.unwrap();
    drop((events, commands));
    tokio::time::timeout(Duration::from_secs(2), task)
        .await
        .expect("full control channel deadlocked shutdown")
        .unwrap();
}

#[tokio::test]
async fn unauthorized_or_unsaved_profiles_never_start_a_network_attempt() {
    let root = tempfile::tempdir().unwrap();
    let store = Store::new(root.path().into());
    let p = profile();
    store.save("local", &p, true).unwrap();
    let service = Service {
        store: Ok(store.clone()),
        overrides: Overrides::default(),
    };
    let (sink, mut events) = EventSink::channel(16);
    let (commands, rx) = mpsc::channel(16);
    let task = coordinator::spawn(None, Some(service), sink, rx, Default::default());
    commands
        .send(Command::Profile(Request::Connect {
            name: "local".into(),
            profile: p.clone(),
            attest: false,
            accepted: Vec::new(),
        }))
        .await
        .unwrap();
    assert!(matches!(
        event(&mut events).await,
        WorkerEvent::Profile(Err(_))
    ));
    assert!(events.try_recv().is_err());
    let mut changed = p.clone();
    changed.bip_mut().port = 47808;
    commands
        .send(Command::Profile(Request::Connect {
            name: "local".into(),
            profile: changed,
            attest: true,
            accepted: Vec::new(),
        }))
        .await
        .unwrap();
    assert!(matches!(
        event(&mut events).await,
        WorkerEvent::Profile(Err(_))
    ));
    assert!(events.try_recv().is_err());
    assert!(store.load("local").unwrap().authorized_at_unix.is_none());
    commands
        .send(Command::Profile(Request::Connect {
            name: "local".into(),
            profile: p,
            attest: true,
            accepted: Vec::new(),
        }))
        .await
        .unwrap();
    // Before SessionStarted can be emitted, persistence has completed.
    assert!(matches!(
        event(&mut events).await,
        WorkerEvent::Profile(Ok(_))
    ));
    assert!(store.load("local").unwrap().authorized_at_unix.is_some());
    assert!(root.path().join("workspaces/local").is_dir());
    connected(&mut events, 1).await;
    drop((commands, events));
    task.await.unwrap();
}

#[test]
fn stale_sessions_cannot_restore_rows_collisions_or_connection_state() {
    let mut app = connected_app();
    five_devices_for_session(&mut app);
    worker(&mut app, WorkerEvent::SessionStarted { generation: 2 });
    assert!(app.devices.is_empty());
    assert!(app.duplicates.banner().is_empty());
    for event in [
        WorkerEvent::Connected {
            local: "old".into(),
        },
        WorkerEvent::ConnectFailed {
            error: "old".into(),
        },
        WorkerEvent::Snapshot(vec![row(7, "old", 1, 0)]),
        WorkerEvent::Discovered(row(8, "old", 1, 0)),
        WorkerEvent::Collision {
            retained: row(9, "old-a", 1, 0),
            incoming: row(9, "old-b", 1, 0),
        },
    ] {
        worker(
            &mut app,
            WorkerEvent::Session {
                generation: 1,
                event: Box::new(event),
            },
        );
    }
    assert_eq!(app.link, Link::Connecting);
    assert!(app.devices.is_empty());
    assert!(app.duplicates.banner().is_empty());
    worker(
        &mut app,
        WorkerEvent::Session {
            generation: 2,
            event: Box::new(WorkerEvent::Connected {
                local: "new".into(),
            }),
        },
    );
    assert_eq!(
        app.link,
        Link::Up {
            local: "new".into()
        }
    );
    assert_eq!(press(&mut app, KeyCode::Char('x')), [Command::Disconnect]);
    assert_eq!(app.link, Link::Disconnecting);
    worker(
        &mut app,
        WorkerEvent::Session {
            generation: 2,
            event: Box::new(WorkerEvent::Discovered(row(10, "late", 1, 0))),
        },
    );
    assert!(app.devices.is_empty());
    worker(&mut app, WorkerEvent::Disconnected { generation: 2 });
    assert_eq!(app.link, Link::Down);
}

fn five_devices_for_session(app: &mut App) {
    worker(app, WorkerEvent::Discovered(row(1, "old-a", 1, 0)));
    worker(
        app,
        WorkerEvent::Collision {
            retained: row(1, "old-a", 1, 0),
            incoming: row(1, "old-b", 1, 0),
        },
    );
}

#[test]
fn rejected_disconnect_keeps_the_live_link_and_reports_the_failure() {
    let mut app = connected_app();
    let prior = app.link.clone();
    let command = press(&mut app, KeyCode::Char('x')).pop().unwrap();
    assert_eq!(app.link, Link::Disconnecting);
    app.command_rejected(command, prior.clone(), "queue full".into());
    assert_eq!(app.link, prior);
    assert_eq!(app.flash.unwrap().text, "queue full");
}

#[test]
fn connected_profile_cannot_be_edited_or_switched() {
    use crate::tui::app::connection::{Connection, Key};
    let mut app = connected_app();
    app.connection = Some(Connection::new(Overrides::default()));
    press(&mut app, KeyCode::Char('p'));
    assert_eq!(app.overlay, Overlay::Connection);
    assert!(act(&mut app, Action::Connection(Key::New)).is_empty());
    assert!(!app.connection.as_ref().unwrap().creating);
    assert!(app.flash.unwrap().text.contains("Disconnect"));
}

#[test]
fn a_start_ack_does_not_hide_an_already_requested_disconnect() {
    let mut app = new_app(None);
    assert_eq!(press(&mut app, KeyCode::Char('x')), [Command::Disconnect]);
    worker(&mut app, WorkerEvent::SessionStarted { generation: 1 });
    assert_eq!(app.link, Link::Disconnecting);
    worker(
        &mut app,
        WorkerEvent::Session {
            generation: 1,
            event: Box::new(WorkerEvent::Connected {
                local: "late".into(),
            }),
        },
    );
    assert_eq!(app.link, Link::Disconnecting);
    worker(&mut app, WorkerEvent::Disconnected { generation: 1 });
    assert_eq!(app.link, Link::Down);
}

#[tokio::test]
async fn a_refused_first_registration_still_connects_and_retries_in_background() {
    let bbmd = tokio::net::UdpSocket::bind("127.0.0.1:0").await.unwrap();
    let mut p = profile();
    p.bip_mut().bbmd = Some(match bbmd.local_addr().unwrap() {
        std::net::SocketAddr::V4(a) => a,
        _ => unreachable!(),
    });
    p.bip_mut().ttl = 2;
    p.bip_mut().renewal_secs = Some(1);
    let plan = ConnectPlan {
        kind: TransportKind::Bip,
        args: p.transport_args().unwrap(),
        await_interface: false,
    };
    let (sink, mut events) = EventSink::channel(32);
    let (commands, rx) = mpsc::channel(16);
    let task = coordinator::spawn(Some(plan), None, sink, rx, Default::default());
    let mut packet = [0; 256];
    let (_, peer) = tokio::time::timeout(Duration::from_secs(2), bbmd.recv_from(&mut packet))
        .await
        .unwrap()
        .unwrap();
    bbmd.send_to(&[0x81, 0x00, 0x00, 0x06, 0x00, 0x30], peer)
        .await
        .unwrap();
    connected(&mut events, 1).await;
    let (len, next_peer) =
        tokio::time::timeout(Duration::from_secs(2), bbmd.recv_from(&mut packet))
            .await
            .unwrap()
            .unwrap();
    assert_eq!(next_peer, peer);
    assert_eq!(&packet[..len], &[0x81, 0x05, 0x00, 0x06, 0x00, 0x02]);
    drop((commands, events));
    task.await.unwrap();
}