blob: 6a7d96e84e4ff2a4a10ecaa2852546644b340d59 [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
Shraddha Basantwania9a9c4f2025-02-25 09:51:48 -080090 fn getEncryptedStorageSize(&self) -> BinderResult<i64> {
91 unimplemented()
92 }
93
Alan Stokesc874cd22024-06-11 16:23:18 +010094 fn readProperty(&self, _: &str) -> BinderResult<String> {
95 unimplemented()
96 }
97 fn insecurelyExposeAttestationCdi(&self) -> BinderResult<Vec<u8>> {
98 unimplemented()
99 }
100 fn getBcc(&self) -> BinderResult<Vec<u8>> {
101 unimplemented()
102 }
103 fn runEchoReverseServer(&self) -> BinderResult<()> {
104 unimplemented()
105 }
106 fn getEffectiveCapabilities(&self) -> BinderResult<Vec<String>> {
107 unimplemented()
108 }
109 fn getUid(&self) -> BinderResult<i32> {
110 unimplemented()
111 }
112 fn writeToFile(&self, _: &str, _: &str) -> BinderResult<()> {
113 unimplemented()
114 }
115 fn readFromFile(&self, _: &str) -> BinderResult<String> {
116 unimplemented()
117 }
118 fn getFilePermissions(&self, _: &str) -> BinderResult<i32> {
119 unimplemented()
120 }
121 fn getMountFlags(&self, _: &str) -> BinderResult<i32> {
122 unimplemented()
123 }
Nikita Ioffed5846dc2024-11-01 18:44:45 +0000124 fn getPageSize(&self) -> BinderResult<i32> {
125 unimplemented()
126 }
Alan Stokesc874cd22024-06-11 16:23:18 +0100127 fn requestCallback(&self, _: &Strong<dyn IAppCallback + 'static>) -> BinderResult<()> {
128 unimplemented()
129 }
130 fn readLineFromConsole(&self) -> BinderResult<String> {
131 unimplemented()
132 }
Shikha Panwar0c3a2fa2024-12-06 18:38:06 +0000133 fn insecurelyReadPayloadRpData(&self) -> BinderResult<[u8; 32]> {
134 unimplemented()
135 }
136 fn insecurelyWritePayloadRpData(&self, _: &[u8; 32]) -> BinderResult<()> {
137 unimplemented()
138 }
Shikha Panwar5b7b4942024-12-18 15:32:49 +0000139 fn isNewInstance(&self) -> BinderResult<bool> {
140 unimplemented()
141 }
Nikita Ioffe901083f2025-01-20 01:54:28 +0000142 fn checkLibIcuIsAccessible(&self) -> BinderResult<()> {
143 unimplemented()
144 }
Alan Stokesc874cd22024-06-11 16:23:18 +0100145}
146
147fn unimplemented<T>() -> BinderResult<T> {
Alan Stokesf46a17c2025-01-05 15:50:18 +0000148 let message = c"Got a call to an unimplemented ITestService method in testbinary.rs";
Alan Stokesc874cd22024-06-11 16:23:18 +0100149 error!("{message:?}");
150 Err(Status::new_exception(ExceptionCode::UNSUPPORTED_OPERATION, Some(message)))
151}