1#![allow(clippy::disallowed_types, reason = "host-only std test support crate")]
10
11extern crate alloc;
12extern crate std;
13
14#[cfg(feature = "environment")]
16pub use drivers_core::wifi;
17#[cfg(feature = "graphics")]
18pub use graphics::headless_screen::{HeadlessError, HeadlessScreenDevice};
19pub use testing_support::*;
20
21#[cfg(feature = "environment")]
22use alloc::boxed::Box;
23#[cfg(feature = "environment")]
24use executable::{ExecutableContext, Standard};
25#[cfg(feature = "environment")]
26use file_system::{AccessFlags, CreateFlags, Flags, MemoryDevice, Path};
27#[cfg(feature = "environment")]
28use users::GroupIdentifier;
29#[cfg(feature = "environment")]
30use virtual_file_system::{VirtualFileSystem, create_default_hierarchy, mount_static};
31
32#[cfg(feature = "environment")]
33static TIME: TimeDevice = TimeDevice;
34#[cfg(feature = "environment")]
35static RANDOM: RandomDevice = RandomDevice;
36
37#[cfg(feature = "environment")]
38pub async fn initialize(graphics_enabled: bool, network_enabled: bool) -> Standard<'static> {
47 let _ = ::log::initialize(&Logger);
48
49 let task_manager = task::test_manager();
50 let users: &'static users::Manager = Box::leak(Box::new(users::Manager::new()));
51 let time: &'static time::Manager<'static> =
52 Box::leak(Box::new(time::Manager::new(&TIME).unwrap()));
53
54 #[cfg(feature = "graphics")]
55 let graphics_manager = if graphics_enabled {
56 let resolution = graphics::Point::new(800, 600);
57 let screen: &'static HeadlessScreenDevice =
58 Box::leak(Box::new(HeadlessScreenDevice::new(resolution).unwrap()));
59 let manager = graphics::initialize_with_input_devices(
60 time,
61 screen,
62 &[],
63 graphics::get_minimal_buffer_size(&resolution),
64 true,
65 )
66 .await;
67 let manager: &'static graphics::Manager = Box::leak(Box::new(manager));
68 graphics::set_ffi_manager(manager);
69 task_manager
70 .spawn(
71 task_manager.get_current_task_identifier().await,
72 "Graphics",
73 None,
74 |_| manager.r#loop(task::Manager::sleep),
75 )
76 .await
77 .unwrap();
78 Some(manager)
79 } else {
80 None
81 };
82 #[cfg(not(feature = "graphics"))]
83 assert!(
84 !graphics_enabled,
85 "enable the `graphics` feature of xila-testing"
86 );
87
88 let file_system = little_fs::FileSystem::new_format(
89 MemoryDevice::<512>::new_static(1024 * 512),
90 little_fs::Options::new(256),
91 )
92 .unwrap();
93 let virtual_file_system: &'static VirtualFileSystem = Box::leak(Box::new(
94 virtual_file_system::initialize(task_manager, users, time, file_system).unwrap(),
95 ));
96 let task = task_manager.get_current_task_identifier().await;
97 create_default_hierarchy(virtual_file_system, task)
98 .await
99 .unwrap();
100
101 mount_static!(
102 virtual_file_system,
103 task,
104 &[
105 (&"/devices/random/source", CharacterDevice, RandomDevice),
106 (
107 &"/devices/standard_io/input",
108 CharacterDevice,
109 ConsoleDevice::Input
110 ),
111 (
112 &"/devices/standard_io/output",
113 CharacterDevice,
114 ConsoleDevice::Output
115 ),
116 (
117 &"/devices/standard_io/error",
118 CharacterDevice,
119 ConsoleDevice::Error
120 ),
121 (&"/devices/time/clock", CharacterDevice, TimeDevice),
122 (
123 &"/devices/standard_io/null",
124 CharacterDevice,
125 drivers_core::NullDevice
126 ),
127 (&"/devices/compute/hasher", CharacterDevice, HashDevice),
128 ]
129 )
130 .await
131 .unwrap();
132
133 let network_manager = network_enabled.then(|| {
134 &*Box::leak(Box::new(network::initialize(
135 task_manager,
136 virtual_file_system,
137 time,
138 &RANDOM,
139 )))
140 });
141
142 let mut context = ExecutableContext::new(task_manager, users, virtual_file_system, time);
145 context.network_manager = network_manager;
146 #[cfg(feature = "graphics")]
147 {
148 context.graphics_manager = graphics_manager;
149 }
150 let executable_context: &'static ExecutableContext = Box::leak(Box::new(context));
151
152 let group = GroupIdentifier::new(1000);
153 let authentication_context = authentication::Context {
154 virtual_file_system,
155 task_manager,
156 users_manager: users,
157 task,
158 };
159 authentication::create_group(&authentication_context, "administrator", Some(group))
160 .await
161 .unwrap();
162 authentication::create_user(&authentication_context, "administrator", "", group, None)
163 .await
164 .unwrap();
165
166 task_manager
167 .set_environment_variable(task, "Paths", "/")
168 .await
169 .unwrap();
170 task_manager
171 .set_environment_variable(task, "Host", "xila")
172 .await
173 .unwrap();
174
175 Standard::open(
176 &"/devices/standard_io/input",
177 &"/devices/standard_io/output",
178 &"/devices/standard_io/error",
179 task,
180 virtual_file_system,
181 executable_context,
182 )
183 .await
184 .unwrap()
185}
186
187#[cfg(feature = "environment")]
188pub async fn load_file(
190 virtual_file_system: &VirtualFileSystem,
191 source: impl AsRef<std::path::Path>,
192 destination: impl AsRef<Path>,
193) -> Result<(), LoadError> {
194 let bytes = std::fs::read(source).map_err(LoadError::Host)?;
195 let task = task::test_manager().get_current_task_identifier().await;
196 let mut file = virtual_file_system::File::open(
197 virtual_file_system,
198 task,
199 &destination.as_ref(),
200 Flags::new(AccessFlags::Write, Some(CreateFlags::CREATE_TRUNCATE), None),
201 )
202 .await
203 .map_err(LoadError::VirtualFileSystem)?;
204 file.write(&bytes)
205 .await
206 .map_err(LoadError::VirtualFileSystem)?;
207 file.close(virtual_file_system)
208 .await
209 .map_err(LoadError::VirtualFileSystem)
210}
211
212#[cfg(feature = "environment")]
213#[derive(Debug)]
214pub enum LoadError {
215 Host(std::io::Error),
216 VirtualFileSystem(virtual_file_system::IoErrorKind),
217}