xref: /aosp_15_r20/system/libfmq/tests/msgq_rust_test_client.rs (revision be431cd81a9a2349eaea34eb56fcf6d1608da596)
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 use android_fmq_test::aidl::android::fmq::test::ITestAidlMsgQ::ITestAidlMsgQ;
18 use binder::Strong;
19 use fmq::MessageQueue;
20 
wait_get_test_service() -> Result<Strong<dyn ITestAidlMsgQ>, String>21 fn wait_get_test_service() -> Result<Strong<dyn ITestAidlMsgQ>, String> {
22     const SERVICE_IDENTIFIER: &str = "android.fmq.test.ITestAidlMsgQ/default";
23     let service = binder::get_interface::<dyn ITestAidlMsgQ>(SERVICE_IDENTIFIER)
24         .map_err(|e| format!("Failed to connect to service {SERVICE_IDENTIFIER}: {e}"))?;
25     Ok(service)
26 }
27 
setup_test_service() -> (MessageQueue<i32>, Strong<dyn ITestAidlMsgQ>)28 fn setup_test_service() -> (MessageQueue<i32>, Strong<dyn ITestAidlMsgQ>) {
29     let service = wait_get_test_service().expect("failed to obtain test service");
30 
31     /* SAFETY: `sysconf` simply returns an integer. */
32     let page_size: usize = unsafe { libc::sysconf(libc::_SC_PAGESIZE) }.try_into().unwrap();
33     let num_elements_in_sync_queue: usize = (page_size - 16) / std::mem::size_of::<i32>();
34 
35     /* Create a queue on the client side. */
36     let mq = MessageQueue::<i32>::new(
37         num_elements_in_sync_queue,
38         true, /* configure event flag word */
39     );
40     let desc = mq.dupe_desc();
41 
42     let result = service.configureFmqSyncReadWrite(&desc);
43     assert!(result.is_ok(), "configuring event queue failed");
44 
45     (mq, service)
46 }
47 
48 mod synchronized_read_write_client {
49     use super::*;
50 
51     /*
52      * Write a small number of messages to FMQ. Request
53      * mService to read and verify that the write was successful.
54      */
55     #[test]
small_input_writer_test()56     fn small_input_writer_test() {
57         let (mut mq, service) = setup_test_service();
58         const DATA_LEN: usize = 16;
59 
60         let data: [i32; DATA_LEN] = init_data();
61         let mut wc = mq.write_many(DATA_LEN).expect("write_many(DATA_LEN) failed");
62         for x in data {
63             wc.write(x).expect("writing i32 failed");
64         }
65         drop(wc);
66         let ret = service.requestReadFmqSync(DATA_LEN as _);
67         assert!(ret.is_ok());
68     }
69 }
70 
init_data<const N: usize>() -> [i32; N]71 fn init_data<const N: usize>() -> [i32; N] {
72     let mut data = [0; N];
73     for (i, elem) in data.iter_mut().enumerate() {
74         *elem = i as _;
75     }
76     data
77 }
78