Skip to main content

fdf_component/testing/
dut.rs

1// Copyright 2025 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::macros::{DriverRegistration, make_driver_registration};
6use crate::testing::get_driver_from_token;
7use crate::testing::harness::TestHarness;
8use crate::testing::node::NodeHandle;
9use crate::{Driver, Incoming};
10use fdf::{
11    AsyncDispatcher, AutoReleaseDispatcher, DispatcherBuilder, OnDispatcher, WeakDispatcher,
12};
13use fdf_env::Environment;
14use fdf_fidl::DriverChannel;
15use fidl_next::{Client as NextClient, ClientDispatcher, ClientEnd as NextClientEnd};
16use fidl_next_fuchsia_driver_framework::{Driver as NextDriver, DriverStartArgs};
17use futures::channel::oneshot;
18use std::marker::PhantomData;
19use std::sync::{Arc, mpsc};
20use zx::Status;
21
22/// This manages the driver under test's lifecycle.
23pub struct DriverUnderTest<'a, D> {
24    driver_outgoing: Incoming,
25    driver: Option<fdf_env::Driver<u32>>,
26    dispatcher: AutoReleaseDispatcher,
27    registration: DriverRegistration,
28    token: usize,
29    client: NextClient<NextDriver, DriverChannel>,
30    client_exit_rx: Option<mpsc::Receiver<()>>,
31    started: bool,
32    harness: &'a mut TestHarness<D>,
33    node_id: usize,
34    _d: PhantomData<D>,
35}
36
37impl<D> Drop for DriverUnderTest<'_, D> {
38    fn drop(&mut self) {
39        if !self.started {
40            self.client_exit_rx.take().expect("exit rx").recv().unwrap();
41        }
42        assert!(
43            self.client_exit_rx.is_none(),
44            "DriverUnderTest's stop_driver must be called before letting it go out of scope."
45        );
46
47        let (shutdown_tx, shutdown_rx) = mpsc::channel();
48        let destroy_fn = self.registration.v1.destroy.unwrap();
49        let driver_token = self.token;
50        self.driver.take().expect("driver").shutdown(move |driver_ref| {
51            // SAFTEY: we created this through Box::into_raw below inside of new.
52            let driver_value = unsafe { Box::from_raw(driver_ref.0 as *mut u32) };
53            assert_eq!(*driver_value, 0x1337);
54
55            // SAFETY: We ensures that the client_exit_rx has been called, which means that
56            // the handle from initialize is dropped.
57            unsafe {
58                destroy_fn(driver_token as *mut _);
59            }
60
61            shutdown_tx.send(()).unwrap();
62        });
63
64        shutdown_rx.recv().unwrap();
65    }
66}
67
68impl<'a, D: Driver> DriverUnderTest<'a, D> {
69    pub(crate) async fn new(
70        harness: &'a mut TestHarness<D>,
71        fdf_env_environment: Arc<Environment>,
72        driver_outgoing: Incoming,
73        node_id: usize,
74    ) -> Self {
75        // Leak this to a raw, we will reconstitue a Box inside drop.
76        let driver_value_ptr = Box::into_raw(Box::new(0x1337_u32));
77
78        let driver = fdf_env_environment.new_driver(driver_value_ptr);
79        let dispatcher_builder = DispatcherBuilder::new()
80            .name("driver_under_test")
81            .shutdown_observer(move |dispatcher| {
82                // We verify that the dispatcher has no tasks left queued in it,
83                // just because this is testing code.
84                assert!(
85                    !fdf_env_environment
86                        .dispatcher_has_queued_tasks(dispatcher.as_dispatcher_ref())
87                );
88            });
89
90        let registration = make_driver_registration::<D>();
91        let dispatcher =
92            AutoReleaseDispatcher::from(driver.new_dispatcher(dispatcher_builder).unwrap());
93        let (server_chan, client_chan) = fdf::Channel::<[fidl_next::Chunk]>::create();
94        let channel_handle = server_chan.into_driver_handle().into_raw().get();
95        let (client_exit_tx, client_exit_rx) = mpsc::channel();
96        let (token_tx, token_rx) = oneshot::channel();
97        let initialize_fn = registration.v1.initialize.unwrap();
98        dispatcher
99            .post_task_sync(move |status| {
100                assert_eq!(status, Status::OK);
101                // SAFETY: We know it's safe to call initialize from the initial dispatcher and we
102                // know channel_handle is non-zero.
103                token_tx.send(unsafe { initialize_fn(channel_handle) }.addr()).unwrap();
104            })
105            .unwrap();
106        let token = token_rx.await.unwrap();
107
108        let client_end: NextClientEnd<NextDriver, DriverChannel> =
109            NextClientEnd::from_untyped(DriverChannel::new(client_chan));
110        let client_dispatcher = ClientDispatcher::new(client_end);
111        let client = client_dispatcher.client();
112        WeakDispatcher::from(&dispatcher)
113            .spawn(async move {
114                // We have to manually run the client indefinitely until it returns a PEER_CLOSED.
115                // At that point the driver has closed its server which signifies it has
116                // completed its stop or has failed to start.
117                client_dispatcher.run_client().await.unwrap_err();
118                client_exit_tx.send(()).unwrap();
119            })
120            .unwrap();
121
122        Self {
123            driver_outgoing,
124            driver: Some(driver),
125            dispatcher,
126            registration,
127            token,
128            client,
129            client_exit_rx: Some(client_exit_rx),
130            started: false,
131            harness,
132            node_id,
133            _d: PhantomData,
134        }
135    }
136
137    pub(crate) async fn start_driver(&mut self, start_args: DriverStartArgs) -> Result<(), Status> {
138        let start_result = self.client.start(start_args).await.expect("start call success");
139        match start_result {
140            fidl_next::FlexibleResult::Ok(_) => {
141                self.started = true;
142                Ok(())
143            }
144            fidl_next::FlexibleResult::Err(e) => Err(Status::from_raw(e)),
145            fidl_next::FlexibleResult::FrameworkErr(e) => {
146                log::error!("DriverUnderTest::start failed with framework error: {:?}", e);
147                Err(Status::INTERNAL)
148            }
149        }
150    }
151
152    /// Allows the test to connect to capabilities that are provided by the driver through its
153    /// outgoing namespace.
154    pub fn driver_outgoing(&self) -> &Incoming {
155        &self.driver_outgoing
156    }
157
158    /// Returns a reference to the driver instance.
159    pub fn get_driver(&self) -> Option<&'_ D> {
160        unsafe {
161            // SAFETY: We know that the driver_token is valid and that the driver is of type T.
162            get_driver_from_token(self.token)
163        }
164    }
165
166    /// Gets the driver's initial dispatcher.
167    pub fn dispatcher(&self) -> WeakDispatcher {
168        WeakDispatcher::from(&self.dispatcher)
169    }
170
171    /// Gets the TestNode that the driver-under-test is bound to.
172    pub fn node(&self) -> NodeHandle {
173        NodeHandle::new(self.harness.node_manager(), self.node_id)
174    }
175
176    /// Get a reference to the harness that started the driver.
177    pub fn harness(&self) -> &'_ TestHarness<D> {
178        self.harness
179    }
180
181    /// Stop the driver.
182    pub async fn stop_driver(mut self) {
183        // We should only send a stop request if the driver started successfully.
184        if self.started {
185            // Sometimes the channel closes earlier than we get the stop result.
186            let _stop_res = self.client.stop().await;
187            self.client_exit_rx.take().expect("exit rx").recv().unwrap();
188        }
189    }
190}