1 /*
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
19 use anyhow::Result;
20 use 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 };
27 use log::{error, info};
28 use std::process::exit;
29 use std::string::String;
30 use std::vec::Vec;
31
32 vm_payload::main!(main);
33
34 // Entry point of the Service VM client.
main()35 fn 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 );
41 if let Err(e) = try_main() {
42 error!("failed with {:?}", e);
43 exit(1);
44 }
45 }
46
try_main() -> Result<()>47 fn 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
53 struct TestService {}
54
55 impl Interface for TestService {}
56
57 impl TestService {
new_binder() -> Strong<dyn ITestService>58 fn new_binder() -> Strong<dyn ITestService> {
59 BnTestService::new_binder(TestService {}, BinderFeatures::default())
60 }
61 }
62
63 impl ITestService for TestService {
quit(&self) -> BinderResult<()>64 fn quit(&self) -> BinderResult<()> {
65 exit(0)
66 }
67
addInteger(&self, a: i32, b: i32) -> BinderResult<i32>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
getApkContentsPath(&self) -> BinderResult<String>72 fn getApkContentsPath(&self) -> BinderResult<String> {
73 Ok(vm_payload::apk_contents_path().to_string_lossy().to_string())
74 }
75
getEncryptedStoragePath(&self) -> BinderResult<String>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
insecurelyExposeVmInstanceSecret(&self) -> BinderResult<Vec<u8>>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
getEncryptedStorageSize(&self) -> BinderResult<i64>90 fn getEncryptedStorageSize(&self) -> BinderResult<i64> {
91 unimplemented()
92 }
93
readProperty(&self, _: &str) -> BinderResult<String>94 fn readProperty(&self, _: &str) -> BinderResult<String> {
95 unimplemented()
96 }
insecurelyExposeAttestationCdi(&self) -> BinderResult<Vec<u8>>97 fn insecurelyExposeAttestationCdi(&self) -> BinderResult<Vec<u8>> {
98 unimplemented()
99 }
getBcc(&self) -> BinderResult<Vec<u8>>100 fn getBcc(&self) -> BinderResult<Vec<u8>> {
101 unimplemented()
102 }
runEchoReverseServer(&self) -> BinderResult<()>103 fn runEchoReverseServer(&self) -> BinderResult<()> {
104 unimplemented()
105 }
getEffectiveCapabilities(&self) -> BinderResult<Vec<String>>106 fn getEffectiveCapabilities(&self) -> BinderResult<Vec<String>> {
107 unimplemented()
108 }
getUid(&self) -> BinderResult<i32>109 fn getUid(&self) -> BinderResult<i32> {
110 unimplemented()
111 }
writeToFile(&self, _: &str, _: &str) -> BinderResult<()>112 fn writeToFile(&self, _: &str, _: &str) -> BinderResult<()> {
113 unimplemented()
114 }
readFromFile(&self, _: &str) -> BinderResult<String>115 fn readFromFile(&self, _: &str) -> BinderResult<String> {
116 unimplemented()
117 }
getFilePermissions(&self, _: &str) -> BinderResult<i32>118 fn getFilePermissions(&self, _: &str) -> BinderResult<i32> {
119 unimplemented()
120 }
getMountFlags(&self, _: &str) -> BinderResult<i32>121 fn getMountFlags(&self, _: &str) -> BinderResult<i32> {
122 unimplemented()
123 }
getPageSize(&self) -> BinderResult<i32>124 fn getPageSize(&self) -> BinderResult<i32> {
125 unimplemented()
126 }
requestCallback(&self, _: &Strong<dyn IAppCallback + 'static>) -> BinderResult<()>127 fn requestCallback(&self, _: &Strong<dyn IAppCallback + 'static>) -> BinderResult<()> {
128 unimplemented()
129 }
readLineFromConsole(&self) -> BinderResult<String>130 fn readLineFromConsole(&self) -> BinderResult<String> {
131 unimplemented()
132 }
insecurelyReadPayloadRpData(&self) -> BinderResult<[u8; 32]>133 fn insecurelyReadPayloadRpData(&self) -> BinderResult<[u8; 32]> {
134 unimplemented()
135 }
insecurelyWritePayloadRpData(&self, _: &[u8; 32]) -> BinderResult<()>136 fn insecurelyWritePayloadRpData(&self, _: &[u8; 32]) -> BinderResult<()> {
137 unimplemented()
138 }
isNewInstance(&self) -> BinderResult<bool>139 fn isNewInstance(&self) -> BinderResult<bool> {
140 unimplemented()
141 }
checkLibIcuIsAccessible(&self) -> BinderResult<()>142 fn checkLibIcuIsAccessible(&self) -> BinderResult<()> {
143 unimplemented()
144 }
145 }
146
unimplemented<T>() -> BinderResult<T>147 fn unimplemented<T>() -> BinderResult<T> {
148 let message = c"Got a call to an unimplemented ITestService method in testbinary.rs";
149 error!("{message:?}");
150 Err(Status::new_exception(ExceptionCode::UNSUPPORTED_OPERATION, Some(message)))
151 }
152