blob: 3900cadae63ecd2695af49aa63b11e8fe045b3e5 [file] [log] [blame]
Alan Stokesc874cd22024-06-11 16:23:18 +01001/*
2 * Copyright 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//! A VM payload that exists to allow testing of the Rust wrapper for the VM payload APIs.
18
19use anyhow::Result;
20use com_android_microdroid_testservice::{
21 aidl::com::android::microdroid::testservice::{
22 IAppCallback::IAppCallback,
23 ITestService::{BnTestService, ITestService, PORT},
24 },
25 binder::{BinderFeatures, ExceptionCode, Interface, Result as BinderResult, Status, Strong},
26};
Alan Stokesc874cd22024-06-11 16:23:18 +010027use log::{error, info};
Alan Stokesc874cd22024-06-11 16:23:18 +010028use std::process::exit;
29use std::string::String;
30use std::vec::Vec;
31
32vm_payload::main!(main);
33
34// Entry point of the Service VM client.
35fn main() {
36 android_logger::init_once(
37 android_logger::Config::default()
38 .with_tag("microdroid_testlib_rust")
39 .with_max_level(log::LevelFilter::Debug),
40 );
Alan Stokesc874cd22024-06-11 16:23:18 +010041 if let Err(e) = try_main() {
42 error!("failed with {:?}", e);
43 exit(1);
44 }
45}
46
47fn try_main() -> Result<()> {
48 info!("Welcome to the Rust test binary");
49
50 vm_payload::run_single_vsock_service(TestService::new_binder(), PORT.try_into()?)
51}
52
53struct TestService {}
54
55impl Interface for TestService {}
56
57impl TestService {
58 fn new_binder() -> Strong<dyn ITestService> {
59 BnTestService::new_binder(TestService {}, BinderFeatures::default())
60 }
61}
62
63impl ITestService for TestService {
64 fn quit(&self) -> BinderResult<()> {
65 exit(0)
66 }
67
68 fn addInteger(&self, a: i32, b: i32) -> BinderResult<i32> {
69 a.checked_add(b).ok_or_else(|| Status::new_exception(ExceptionCode::ILLEGAL_ARGUMENT, None))
70 }
71
72 fn getApkContentsPath(&self) -> BinderResult<String> {
73 Ok(vm_payload::apk_contents_path().to_string_lossy().to_string())
74 }
75
76 fn getEncryptedStoragePath(&self) -> BinderResult<String> {
77 Ok(vm_payload::encrypted_storage_path()
78 .map(|p| p.to_string_lossy().to_string())
79 .unwrap_or("".to_string()))
80 }
81
82 fn insecurelyExposeVmInstanceSecret(&self) -> BinderResult<Vec<u8>> {
83 let mut secret = vec![0u8; 32];
84 vm_payload::get_vm_instance_secret(b"identifier", secret.as_mut_slice());
85 Ok(secret)
86 }
87
88 // Everything below here is unimplemented. Implementations may be added as needed.
89
90 fn readProperty(&self, _: &str) -> BinderResult<String> {
91 unimplemented()
92 }
93 fn insecurelyExposeAttestationCdi(&self) -> BinderResult<Vec<u8>> {
94 unimplemented()
95 }
96 fn getBcc(&self) -> BinderResult<Vec<u8>> {
97 unimplemented()
98 }
99 fn runEchoReverseServer(&self) -> BinderResult<()> {
100 unimplemented()
101 }
102 fn getEffectiveCapabilities(&self) -> BinderResult<Vec<String>> {
103 unimplemented()
104 }
105 fn getUid(&self) -> BinderResult<i32> {
106 unimplemented()
107 }
108 fn writeToFile(&self, _: &str, _: &str) -> BinderResult<()> {
109 unimplemented()
110 }
111 fn readFromFile(&self, _: &str) -> BinderResult<String> {
112 unimplemented()
113 }
114 fn getFilePermissions(&self, _: &str) -> BinderResult<i32> {
115 unimplemented()
116 }
117 fn getMountFlags(&self, _: &str) -> BinderResult<i32> {
118 unimplemented()
119 }
Nikita Ioffed5846dc2024-11-01 18:44:45 +0000120 fn getPageSize(&self) -> BinderResult<i32> {
121 unimplemented()
122 }
Alan Stokesc874cd22024-06-11 16:23:18 +0100123 fn requestCallback(&self, _: &Strong<dyn IAppCallback + 'static>) -> BinderResult<()> {
124 unimplemented()
125 }
126 fn readLineFromConsole(&self) -> BinderResult<String> {
127 unimplemented()
128 }
Shikha Panwar0c3a2fa2024-12-06 18:38:06 +0000129 fn insecurelyReadPayloadRpData(&self) -> BinderResult<[u8; 32]> {
130 unimplemented()
131 }
132 fn insecurelyWritePayloadRpData(&self, _: &[u8; 32]) -> BinderResult<()> {
133 unimplemented()
134 }
Shikha Panwar5b7b4942024-12-18 15:32:49 +0000135 fn isNewInstance(&self) -> BinderResult<bool> {
136 unimplemented()
137 }
Nikita Ioffe901083f2025-01-20 01:54:28 +0000138 fn checkLibIcuIsAccessible(&self) -> BinderResult<()> {
139 unimplemented()
140 }
Alan Stokesc874cd22024-06-11 16:23:18 +0100141}
142
143fn unimplemented<T>() -> BinderResult<T> {
Alan Stokesf46a17c2025-01-05 15:50:18 +0000144 let message = c"Got a call to an unimplemented ITestService method in testbinary.rs";
Alan Stokesc874cd22024-06-11 16:23:18 +0100145 error!("{message:?}");
146 Err(Status::new_exception(ExceptionCode::UNSUPPORTED_OPERATION, Some(message)))
147}