blob: 2502113df4b3e3a45352c0d8adb8ad7015424837 [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};
27use cstr::cstr;
28use log::{error, info};
Alan Stokesc874cd22024-06-11 16:23:18 +010029use std::process::exit;
30use std::string::String;
31use std::vec::Vec;
32
33vm_payload::main!(main);
34
35// Entry point of the Service VM client.
36fn main() {
37 android_logger::init_once(
38 android_logger::Config::default()
39 .with_tag("microdroid_testlib_rust")
40 .with_max_level(log::LevelFilter::Debug),
41 );
Alan Stokesc874cd22024-06-11 16:23:18 +010042 if let Err(e) = try_main() {
43 error!("failed with {:?}", e);
44 exit(1);
45 }
46}
47
48fn try_main() -> Result<()> {
49 info!("Welcome to the Rust test binary");
50
51 vm_payload::run_single_vsock_service(TestService::new_binder(), PORT.try_into()?)
52}
53
54struct TestService {}
55
56impl Interface for TestService {}
57
58impl TestService {
59 fn new_binder() -> Strong<dyn ITestService> {
60 BnTestService::new_binder(TestService {}, BinderFeatures::default())
61 }
62}
63
64impl ITestService for TestService {
65 fn quit(&self) -> BinderResult<()> {
66 exit(0)
67 }
68
69 fn addInteger(&self, a: i32, b: i32) -> BinderResult<i32> {
70 a.checked_add(b).ok_or_else(|| Status::new_exception(ExceptionCode::ILLEGAL_ARGUMENT, None))
71 }
72
73 fn getApkContentsPath(&self) -> BinderResult<String> {
74 Ok(vm_payload::apk_contents_path().to_string_lossy().to_string())
75 }
76
77 fn getEncryptedStoragePath(&self) -> BinderResult<String> {
78 Ok(vm_payload::encrypted_storage_path()
79 .map(|p| p.to_string_lossy().to_string())
80 .unwrap_or("".to_string()))
81 }
82
83 fn insecurelyExposeVmInstanceSecret(&self) -> BinderResult<Vec<u8>> {
84 let mut secret = vec![0u8; 32];
85 vm_payload::get_vm_instance_secret(b"identifier", secret.as_mut_slice());
86 Ok(secret)
87 }
88
89 // Everything below here is unimplemented. Implementations may be added as needed.
90
91 fn readProperty(&self, _: &str) -> BinderResult<String> {
92 unimplemented()
93 }
94 fn insecurelyExposeAttestationCdi(&self) -> BinderResult<Vec<u8>> {
95 unimplemented()
96 }
97 fn getBcc(&self) -> BinderResult<Vec<u8>> {
98 unimplemented()
99 }
100 fn runEchoReverseServer(&self) -> BinderResult<()> {
101 unimplemented()
102 }
103 fn getEffectiveCapabilities(&self) -> BinderResult<Vec<String>> {
104 unimplemented()
105 }
106 fn getUid(&self) -> BinderResult<i32> {
107 unimplemented()
108 }
109 fn writeToFile(&self, _: &str, _: &str) -> BinderResult<()> {
110 unimplemented()
111 }
112 fn readFromFile(&self, _: &str) -> BinderResult<String> {
113 unimplemented()
114 }
115 fn getFilePermissions(&self, _: &str) -> BinderResult<i32> {
116 unimplemented()
117 }
118 fn getMountFlags(&self, _: &str) -> BinderResult<i32> {
119 unimplemented()
120 }
Nikita Ioffed5846dc2024-11-01 18:44:45 +0000121 fn getPageSize(&self) -> BinderResult<i32> {
122 unimplemented()
123 }
Alan Stokesc874cd22024-06-11 16:23:18 +0100124 fn requestCallback(&self, _: &Strong<dyn IAppCallback + 'static>) -> BinderResult<()> {
125 unimplemented()
126 }
127 fn readLineFromConsole(&self) -> BinderResult<String> {
128 unimplemented()
129 }
130}
131
132fn unimplemented<T>() -> BinderResult<T> {
133 let message = cstr!("Got a call to an unimplemented ITestService method in testbinary.rs");
134 error!("{message:?}");
135 Err(Status::new_exception(ExceptionCode::UNSUPPORTED_OPERATION, Some(message)))
136}