Skip to main content

network/
lib.rs

1#![no_std]
2
3extern crate alloc;
4
5#[cfg(test)]
6extern crate std;
7
8mod device;
9mod error;
10mod fundamentals;
11mod manager;
12pub mod sntp;
13mod socket;
14
15pub use device::*;
16pub use error::*;
17pub use fundamentals::*;
18pub use manager::*;
19pub use socket::*;
20
21#[cfg(test)]
22pub mod tests {
23    use alloc::boxed::Box;
24    use file_system::AccessFlags;
25    use synchronization::{blocking_mutex::raw::CriticalSectionRawMutex, mutex::Mutex};
26    use virtual_file_system::{File, create_default_hierarchy};
27
28    use super::*;
29
30    testing_support::instantiate_global_allocator!();
31
32    pub(crate) static TEST_TASK_MANAGER: synchronization::once_lock::OnceLock<
33        &'static task::Manager,
34    > = synchronization::once_lock::OnceLock::new();
35
36    pub(crate) async fn initialize() -> &'static crate::Manager {
37        static INITIALIZE_MUTEX: Mutex<CriticalSectionRawMutex, bool> = Mutex::new(false);
38
39        let mut initialized = INITIALIZE_MUTEX.lock().await;
40
41        if *initialized {
42            return crate::test_manager();
43        }
44
45        *initialized = true;
46
47        static RANDOM_DEVICE: testing_support::RandomDevice = testing_support::RandomDevice;
48        static TIME_DEVICE: testing_support::TimeDevice = testing_support::TimeDevice;
49
50        let task_manager = Box::leak(Box::new(task::Manager::new()));
51        TEST_TASK_MANAGER.get_or_init(|| task_manager);
52        // Tests run on separate test-harness executors, unknown to this manager.
53        // Its root and the stack runner live on one shared executor thread.
54        let spawner = testing_support::new_thread_executor(task_manager).await;
55        let (_, task) = task_manager
56            .spawn_send(
57                task::Manager::ROOT_TASK_IDENTIFIER,
58                "Network tests",
59                Some(spawner),
60                |_| core::future::pending::<()>(),
61            )
62            .await
63            .unwrap();
64
65        log::initialize(&testing_support::Logger).unwrap();
66
67        let user_manager = Box::leak(Box::new(users::Manager::new()));
68
69        let time_manager = Box::leak(Box::new(time::Manager::new(&TIME_DEVICE).unwrap()));
70
71        let memory_device = file_system::MemoryDevice::<512>::new_static(10 * 1024 * 1024);
72
73        let root_file_system =
74            little_fs::FileSystem::get_or_format(memory_device, little_fs::Options::new(512))
75                .unwrap();
76
77        let virtual_file_system = Box::leak(Box::new(
78            virtual_file_system::initialize(
79                task_manager,
80                user_manager,
81                time_manager,
82                root_file_system,
83            )
84            .unwrap(),
85        ));
86
87        create_default_hierarchy(virtual_file_system, task)
88            .await
89            .unwrap();
90
91        let network_manager = crate::TEST_MANAGER.get_or_init(|| {
92            crate::Manager::new(
93                task_manager,
94                virtual_file_system,
95                time_manager,
96                &RANDOM_DEVICE,
97            )
98        });
99
100        // `mount_interface` spawns its runner on the calling executor.
101        task_manager
102            .spawn_send(task, "Mount loopback", Some(spawner), move |_| async move {
103                let (device, controller_device) = crate::create_loopback_device();
104                network_manager
105                    .mount_interface(task, "loopback0", device, controller_device, None)
106                    .await
107            })
108            .await
109            .unwrap()
110            .0
111            .join()
112            .await
113            .unwrap();
114
115        let mut file = File::open(
116            virtual_file_system,
117            task,
118            "/devices/network/loopback0",
119            AccessFlags::Write.into(),
120        )
121        .await
122        .unwrap();
123
124        file.control(
125            ADD_IP_ADDRESS,
126            &IpCidrAbi::from(IpCidr::new_ipv4([127, 0, 0, 1], 8)),
127        )
128        .await
129        .unwrap();
130        file.control(
131            ADD_IP_ADDRESS,
132            &IpCidrAbi::from(IpCidr::new_ipv4([127, 0, 0, 2], 8)),
133        )
134        .await
135        .unwrap();
136
137        file.close(virtual_file_system).await.unwrap();
138
139        network_manager
140    }
141
142    pub(crate) fn task_manager() -> &'static task::Manager {
143        crate::tests::TEST_TASK_MANAGER
144            .try_get()
145            .expect("Network test task manager not initialized")
146    }
147}