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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
use quilkin::{
    components::{self, RunArgs},
    config::Providers,
    net::TcpListener,
    test::TestConfig,
    Config, ShutdownTx,
};
pub use serde_json::json;
use std::{net::SocketAddr, num::NonZeroUsize, path::PathBuf, sync::Arc};
use tokio::sync::mpsc;

pub static BUFFER_POOL: once_cell::sync::Lazy<Arc<quilkin::pool::BufferPool>> =
    once_cell::sync::Lazy::new(|| Arc::new(quilkin::pool::BufferPool::default()));

#[inline]
pub fn alloc_buffer(data: impl AsRef<[u8]>) -> quilkin::pool::PoolBuffer {
    BUFFER_POOL.clone().alloc_slice(data.as_ref())
}

/// Macro that can get the function name of the function the macro is invoked
/// within
#[macro_export]
macro_rules! func_name {
    () => {{
        fn f() {}
        fn type_name_of<T>(_: T) -> &'static str {
            std::any::type_name::<T>()
        }
        let name = type_name_of(f);
        &name[..name.len() - 3]
    }};
}

/// Creates a temporary file with the specified prefix in a directory named
/// after the calling function, ie using it within a test will place it in a
/// temporary directory named after the test
#[macro_export]
macro_rules! temp_file {
    ($prefix:expr) => {{
        let name = $crate::func_name!();
        let name = name.strip_suffix("::{{closure}}").unwrap_or(name);
        let mut name = name.replace("::", ".");
        name.push('-');
        name.push_str($prefix);
        name.push('-');
        tempfile::NamedTempFile::with_prefix(name).unwrap()
    }};
}

pub use tracing::{subscriber::DefaultGuard, Level};

pub fn init_logging(level: Level, test_pkg: &'static str) -> DefaultGuard {
    use tracing_subscriber::{layer::SubscriberExt as _, Layer as _};
    let layer = tracing_subscriber::fmt::layer()
        .with_test_writer()
        .with_filter(tracing_subscriber::filter::LevelFilter::from_level(level))
        .with_filter(tracing_subscriber::EnvFilter::new(format!(
            "{test_pkg}=trace,qt=trace,quilkin=trace,xds=trace"
        )));
    let sub = tracing_subscriber::Registry::default().with(layer);
    let disp = tracing::dispatcher::Dispatch::new(sub);
    tracing::dispatcher::set_default(&disp)
}

#[macro_export]
macro_rules! trace_test {
    ($(#[$attr:meta])* $name:ident, $body:block) => {
        $(#[$attr])*
        #[tokio::test]
        async fn $name() {
            // Get the module name
            let fname = $crate::func_name!();
            let mname = fname.rsplit("::").nth(2).unwrap();

            let _guard = init_logging($crate::Level::DEBUG, mname);

            $body
        }
    };
}

pub struct ServerPailConfig {
    pub packet_size: u16,
    pub num_packets: Option<usize>,
}

impl Default for ServerPailConfig {
    fn default() -> Self {
        Self {
            packet_size: 1024,
            num_packets: None,
        }
    }
}

#[derive(Default)]
pub struct AdminPailConfig;

#[derive(Default)]
pub struct RelayPailConfig {
    pub config: Option<quilkin::test::TestConfig>,
}

#[derive(Default)]
pub struct ProxyPailConfig {
    pub config: Option<quilkin::test::TestConfig>,
}

#[derive(Default)]
pub struct ManagementPailConfig {}

#[derive(Default)]
pub struct AgentPailConfig {
    pub endpoints: Vec<(&'static str, &'static [&'static str])>,
    pub icao_code: quilkin::config::IcaoCode,
}

pub enum PailConfig {
    /// Creates a UDP socket with an ephemeral port that will send received
    /// packets to a channel
    Server(ServerPailConfig),
    // Creates an admin server that is applied to any following relay, proxy, or management pails
    //Admin(AdminPailConfig),
    Relay(RelayPailConfig),
    Proxy(ProxyPailConfig),
    //Management(ManagementPailConfig),
    Agent(AgentPailConfig),
}

impl From<ServerPailConfig> for PailConfig {
    fn from(value: ServerPailConfig) -> Self {
        Self::Server(value)
    }
}

impl From<RelayPailConfig> for PailConfig {
    fn from(value: RelayPailConfig) -> Self {
        Self::Relay(value)
    }
}

impl From<AgentPailConfig> for PailConfig {
    fn from(value: AgentPailConfig) -> Self {
        Self::Agent(value)
    }
}

impl From<ProxyPailConfig> for PailConfig {
    fn from(value: ProxyPailConfig) -> Self {
        Self::Proxy(value)
    }
}

pub struct ConfigFile {
    pub path: PathBuf,
    pub config: TestConfig,
}

impl ConfigFile {
    pub fn update(&mut self, update: impl FnOnce(&mut TestConfig)) {
        update(&mut self.config);
        self.config.write_to_file(&self.path)
    }
}

macro_rules! abort_task {
    ($pail:ty) => {
        impl Drop for $pail {
            fn drop(&mut self) {
                self.task.abort();
            }
        }
    };
}

pub struct SandboxPailConfig {
    pub name: &'static str,
    pub config: PailConfig,
    pub dependencies: &'static [&'static str],
}

pub struct SandboxConfig {
    pub name: String,
    pub pails: Vec<SandboxPailConfig>,
}

pub type JoinHandle = tokio::task::JoinHandle<quilkin::Result<()>>;

pub struct ServerPail {
    /// The server socket's ephmeral port
    pub port: u16,
    pub packet_rx: Option<mpsc::Receiver<String>>,
    /// The join handle to the task driving the socket. Used to both cancel the task
    /// and/or wait for it to finish
    pub task: tokio::task::JoinHandle<usize>,
}

abort_task!(ServerPail);

pub struct RelayPail {
    pub xds_port: u16,
    pub mds_port: u16,
    pub task: JoinHandle,
    pub shutdown: ShutdownTx,
    pub config_file: Option<ConfigFile>,
    pub config: Arc<Config>,
}

abort_task!(RelayPail);

pub struct AgentPail {
    pub qcmp_port: u16,
    pub task: JoinHandle,
    pub shutdown: ShutdownTx,
    pub config_file: Option<ConfigFile>,
    pub config: Arc<Config>,
}

abort_task!(AgentPail);

pub struct ProxyPail {
    pub port: u16,
    pub qcmp_port: u16,
    pub phoenix_port: u16,
    pub task: JoinHandle,
    pub shutdown: ShutdownTx,
    pub config: Arc<Config>,
    pub delta_applies: Option<tokio::sync::mpsc::UnboundedReceiver<String>>,
}

abort_task!(ProxyPail);

pub type Pails = std::collections::BTreeMap<&'static str, Pail>;

pub enum Pail {
    Server(ServerPail),
    Relay(RelayPail),
    Agent(AgentPail),
    Proxy(ProxyPail),
}

impl Pail {
    #[inline]
    pub fn config(&self) -> Arc<Config> {
        match self {
            Self::Relay(p) => p.config.clone(),
            Self::Agent(p) => p.config.clone(),
            Self::Proxy(p) => p.config.clone(),
            Self::Server(_) => panic!("no config"),
        }
    }
}

impl Pail {
    pub fn construct(
        spc: SandboxPailConfig,
        pails: &Pails,
        td: &std::path::Path,
    ) -> (Self, Option<tokio::sync::oneshot::Receiver<()>>) {
        let mut rx = None;

        let pail = match spc.config {
            PailConfig::Server(sspc) => {
                let (packet_tx, packet_rx) = mpsc::channel::<String>(10);
                let socket = quilkin::net::DualStackEpollSocket::new(0)
                    .expect("failed to create server socket");

                let port = socket
                    .local_addr()
                    .expect("failed to bind server socket")
                    .port();

                tracing::debug!(port, spc.name, "bound server socket");

                let packet_size = sspc.packet_size as usize;
                let mut num_packets = sspc.num_packets.unwrap_or(usize::MAX);

                let task = tokio::spawn(async move {
                    let mut buf = vec![0; packet_size];
                    let mut received = 0;

                    while num_packets > 0 {
                        let (size, _) = socket
                            .recv_from(&mut buf)
                            .await
                            .expect("failed to receive packet");
                        received += size;
                        let pstr = std::str::from_utf8(&buf[..size])
                            .expect("received non-utf8 string in packet")
                            .to_owned();

                        packet_tx.send(pstr).await.expect("packet receiver dropped");

                        num_packets -= 1;
                    }

                    received
                });

                Self::Server(ServerPail {
                    port,
                    task,
                    packet_rx: Some(packet_rx),
                })
            }
            PailConfig::Relay(rpc) => {
                use components::relay;

                let xds_listener = TcpListener::bind(None).unwrap();
                let mds_listener = TcpListener::bind(None).unwrap();

                let xds_port = xds_listener.port();
                let mds_port = mds_listener.port();

                let path = td.join(spc.name);
                let mut tc = rpc.config.unwrap_or_default();
                tc.id = spc.name.into();
                tc.write_to_file(&path);

                let config_path = path.clone();

                let (shutdown, shutdown_rx) =
                    quilkin::make_shutdown_channel(quilkin::ShutdownKind::Testing);

                let config = Arc::new(Config::default_non_agent());
                config.id.store(Arc::new(spc.name.into()));

                let task = tokio::spawn(
                    relay::Relay {
                        xds_listener,
                        mds_listener,
                        provider: Some(Providers::File { path }),
                    }
                    .run(RunArgs {
                        config: config.clone(),
                        ready: relay::Ready::default(),
                        shutdown_rx,
                    }),
                );

                Self::Relay(RelayPail {
                    xds_port,
                    mds_port,
                    task,
                    shutdown,
                    config_file: Some(ConfigFile {
                        path: config_path,
                        config: tc,
                    }),
                    config,
                })
            }
            PailConfig::Agent(apc) => {
                let mut endpoints = std::collections::BTreeSet::new();

                for (dep_name, tokens) in apc.endpoints {
                    let Pail::Server(server) = &pails[dep_name] else {
                        panic!("expected '{dep_name}' to be a server pail for endpoint");
                    };

                    endpoints.insert(quilkin::net::Endpoint::with_metadata(
                        (std::net::Ipv4Addr::UNSPECIFIED, server.port).into(),
                        quilkin::net::endpoint::Metadata {
                            tokens: tokens.iter().map(|t| Vec::from(*t)).collect(),
                        },
                    ));
                }

                let mut tc = TestConfig::new();
                tc.clusters.insert_default(endpoints);
                tc.id = spc.name.into();

                let path = td.join(spc.name);
                tc.write_to_file(&path);

                let relay_servers = spc
                    .dependencies
                    .iter()
                    .filter_map(|dname| {
                        let Pail::Relay(RelayPail { mds_port, .. }) = &pails[dname] else {
                            return None;
                        };
                        Some(
                            format!("http://localhost:{mds_port}")
                                .parse()
                                .expect("failed to parse endpoint"),
                        )
                    })
                    .collect();

                let (shutdown, shutdown_rx) =
                    quilkin::make_shutdown_channel(quilkin::ShutdownKind::Testing);

                let qcmp_socket =
                    quilkin::net::raw_socket_with_reuse(0).expect("failed to bind qcmp socket");
                let qcmp_port = quilkin::net::socket_port(&qcmp_socket);

                let config_path = path.clone();
                let config = Arc::new(Config::default_agent());
                config.id.store(Arc::new(spc.name.into()));
                let acfg = config.clone();

                let task = tokio::spawn(async move {
                    components::agent::Agent {
                        locality: None,
                        icao_code: Some(apc.icao_code),
                        relay_servers,
                        qcmp_socket,
                        provider: Some(Providers::File { path }),
                        address_selector: None,
                    }
                    .run(RunArgs {
                        config,
                        ready: Default::default(),
                        shutdown_rx,
                    })
                    .await
                });

                Self::Agent(AgentPail {
                    qcmp_port,
                    task,
                    shutdown,
                    config_file: Some(ConfigFile {
                        path: config_path,
                        config: tc,
                    }),
                    config: acfg,
                })
            }
            PailConfig::Proxy(ppc) => {
                let socket = quilkin::net::raw_socket_with_reuse(0).expect("failed to bind socket");
                let qcmp =
                    quilkin::net::raw_socket_with_reuse(0).expect("failed to bind qcmp socket");
                let qcmp_port = quilkin::net::socket_port(&qcmp);
                let phoenix = TcpListener::bind(None).expect("failed to bind phoenix socket");
                let phoenix_port = phoenix.port();

                let port = quilkin::net::socket_port(&socket);

                let management_servers = spc
                    .dependencies
                    .iter()
                    .filter_map(|dname| {
                        let Pail::Relay(RelayPail { xds_port, .. }) = &pails[dname] else {
                            return None;
                        };
                        Some(
                            format!("http://localhost:{xds_port}")
                                .parse()
                                .expect("failed to parse endpoint"),
                        )
                    })
                    .collect();

                let (shutdown, shutdown_rx) =
                    quilkin::make_shutdown_channel(quilkin::ShutdownKind::Testing);

                let (tx, orx) = tokio::sync::oneshot::channel();

                let config = Arc::new(Config::default_non_agent());

                if let Some(cfg) = ppc.config {
                    if !cfg.clusters.is_empty() {
                        panic!("not implemented");
                    }

                    if !cfg.filters.is_empty() {
                        config.filters.store(Arc::new(cfg.filters));
                    }
                }

                let endpoints: std::collections::BTreeSet<_> = spc
                    .dependencies
                    .iter()
                    .filter_map(|dname| {
                        let Pail::Server(ServerPail { port, .. }) = &pails[dname] else {
                            return None;
                        };

                        Some(quilkin::net::Endpoint::new(
                            (std::net::Ipv6Addr::LOCALHOST, *port).into(),
                        ))
                    })
                    .collect();

                if !endpoints.is_empty() {
                    config
                        .clusters
                        .modify(|clusters| clusters.insert_default(endpoints));
                }

                config.id.store(Arc::new(spc.name.into()));
                let pconfig = config.clone();

                let (rttx, rtrx) = tokio::sync::mpsc::unbounded_channel();

                let task = tokio::spawn(async move {
                    components::proxy::Proxy {
                        num_workers: NonZeroUsize::new(1).unwrap(),
                        mmdb: None,
                        to: Vec::new(),
                        to_tokens: None,
                        management_servers,
                        socket,
                        qcmp,
                        phoenix,
                        notifier: Some(rttx),
                    }
                    .run(
                        RunArgs {
                            config: pconfig,
                            ready: Default::default(),
                            shutdown_rx,
                        },
                        Some(tx),
                    )
                    .await
                });

                rx = Some(orx);

                Self::Proxy(ProxyPail {
                    port,
                    qcmp_port,
                    phoenix_port,
                    shutdown,
                    task,
                    config,
                    delta_applies: Some(rtrx),
                })
            }
        };
        (pail, rx)
    }
}

pub struct Sandbox {
    pub pails: Pails,
    pub td: tempfile::TempDir,
}

#[macro_export]
macro_rules! sandbox_config {
    () => {{
        let fname = $crate::func_name!();
        let mut name = String::new();
        for comp in fname.split("::") {
            if comp.starts_with("{{") {
                continue;
            }

            if !name.is_empty() {
                name.push('.');
            }

            name.push_str(comp);
        }
        $crate::SandboxConfig::new(name)
    }};
}

impl SandboxConfig {
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            pails: Vec::new(),
        }
    }

    pub fn push(
        &mut self,
        name: &'static str,
        config: impl Into<PailConfig>,
        deps: &'static [&'static str],
    ) {
        self.pails.push(SandboxPailConfig {
            name,
            config: config.into(),
            dependencies: deps,
        });
    }

    pub async fn spinup(self) -> Sandbox {
        // Validate that every dependency is satisfied
        for i in 0..self.pails.len() {
            let deps = self.pails[i].dependencies;

            for dep in deps {
                if !self.pails[..i].iter().any(|sp| sp.name == *dep) {
                    panic!(
                        "failed to locate dependency '{dep}' for '{}'",
                        self.pails[i].name
                    );
                }
            }
        }

        tracing::trace!("dependencies resolved");

        // Create a channel that is used to communicate readiness of each pail we create
        //let (tx, rx) = tokio::sync::mpsc::unbounded_channel();

        // Not necessarily used in every test, but in most
        let td = match tempfile::TempDir::with_prefix(&self.name) {
            Ok(td) => td,
            Err(err) => {
                panic!("failed to create temp dir '{}': {err:#}", self.name);
            }
        };

        let mut pails = Pails::new();
        for pc in self.pails {
            let name = pc.name;
            let (pail, rx) = Pail::construct(pc, &pails, td.path());

            if let Some(rx) = rx {
                rx.await.unwrap();
            }

            if pails.insert(name, pail).is_some() {
                panic!("{name} already existed");
            }
        }

        Sandbox { pails, td }
    }
}

impl Sandbox {
    #[inline]
    pub fn proxy(
        &mut self,
        name: &str,
    ) -> (SocketAddr, tokio::sync::mpsc::UnboundedReceiver<String>) {
        let Some(Pail::Proxy(pp)) = self.pails.get_mut(name) else {
            unreachable!()
        };
        (
            SocketAddr::from((std::net::Ipv6Addr::LOCALHOST, pp.port)),
            pp.delta_applies.take().unwrap(),
        )
    }

    #[inline]
    pub fn packet_rx(&mut self, name: &str) -> mpsc::Receiver<String> {
        let Some(Pail::Server(pp)) = self.pails.get_mut(name) else {
            unreachable!()
        };
        pp.packet_rx.take().unwrap()
    }

    #[inline]
    pub fn server(&mut self, name: &str) -> (mpsc::Receiver<String>, SocketAddr) {
        let Some(Pail::Server(pp)) = self.pails.get_mut(name) else {
            unreachable!()
        };
        (
            pp.packet_rx.take().unwrap(),
            SocketAddr::from((std::net::Ipv6Addr::LOCALHOST, pp.port)),
        )
    }

    #[inline]
    pub fn config_file(&mut self, name: &str) -> ConfigFile {
        let pail = self.pails.get_mut(name).unwrap();

        match pail {
            Pail::Relay(rp) => rp.config_file.take().unwrap(),
            Pail::Agent(ap) => ap.config_file.take().unwrap(),
            _ => unimplemented!("no config_file for this pail"),
        }
    }

    #[inline]
    pub fn socket(&self) -> (socket2::Socket, SocketAddr) {
        let socket = quilkin::net::raw_socket_with_reuse(0).unwrap();
        let port = quilkin::net::socket_port(&socket);

        (
            socket,
            SocketAddr::from((std::net::Ipv6Addr::LOCALHOST, port)),
        )
    }

    /// Creates an ephemeral socket that can be used to send messages to sandbox
    /// pails
    #[inline]
    pub fn client(&self) -> quilkin::net::DualStackEpollSocket {
        quilkin::net::DualStackEpollSocket::new(0).unwrap()
    }

    /// Sleeps for the specified number of milliseconds
    #[inline]
    pub async fn sleep(&self, ms: u64) {
        tokio::time::sleep(std::time::Duration::from_millis(ms)).await
    }

    /// Runs a future, expecting it complete before the specified timeout
    #[inline]
    pub async fn timeout<F>(&self, ms: u64, fut: F) -> F::Output
    where
        F: std::future::Future,
    {
        tokio::time::timeout(std::time::Duration::from_millis(ms), fut)
            .await
            .expect("operation timed out")
    }

    #[inline]
    pub async fn maybe_timeout<F>(&self, ms: u64, fut: F) -> Option<F::Output>
    where
        F: std::future::Future,
    {
        tokio::time::timeout(std::time::Duration::from_millis(ms), fut)
            .await
            .ok()
    }

    /// Runs a future, expecting it to timeout instead of resolving, panics if
    /// the future finishes before the timeout
    #[inline]
    pub async fn expect_timeout<F>(&self, ms: u64, fut: F)
    where
        F: std::future::Future,
        F::Output: std::fmt::Debug,
    {
        tokio::time::timeout(std::time::Duration::from_millis(ms), fut)
            .await
            .expect_err("expected future to timeout");
    }
}