Alan Stokes | c874cd2 | 2024-06-11 16:23:18 +0100 | [diff] [blame] | 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 | }; |
Alan Stokes | c874cd2 | 2024-06-11 16:23:18 +0100 | [diff] [blame] | 27 | use log::{error, info}; |
Alan Stokes | c874cd2 | 2024-06-11 16:23:18 +0100 | [diff] [blame] | 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. |
| 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 | ); |
Alan Stokes | c874cd2 | 2024-06-11 16:23:18 +0100 | [diff] [blame] | 41 | if let Err(e) = try_main() { |
| 42 | error!("failed with {:?}", e); |
| 43 | exit(1); |
| 44 | } |
| 45 | } |
| 46 | |
| 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 { |
| 58 | fn new_binder() -> Strong<dyn ITestService> { |
| 59 | BnTestService::new_binder(TestService {}, BinderFeatures::default()) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | impl 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 Basantwani | a9a9c4f | 2025-02-25 09:51:48 -0800 | [diff] [blame] | 90 | fn getEncryptedStorageSize(&self) -> BinderResult<i64> { |
| 91 | unimplemented() |
| 92 | } |
| 93 | |
Alan Stokes | c874cd2 | 2024-06-11 16:23:18 +0100 | [diff] [blame] | 94 | 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 Ioffe | d5846dc | 2024-11-01 18:44:45 +0000 | [diff] [blame] | 124 | fn getPageSize(&self) -> BinderResult<i32> { |
| 125 | unimplemented() |
| 126 | } |
Alan Stokes | c874cd2 | 2024-06-11 16:23:18 +0100 | [diff] [blame] | 127 | fn requestCallback(&self, _: &Strong<dyn IAppCallback + 'static>) -> BinderResult<()> { |
| 128 | unimplemented() |
| 129 | } |
| 130 | fn readLineFromConsole(&self) -> BinderResult<String> { |
| 131 | unimplemented() |
| 132 | } |
Shikha Panwar | 0c3a2fa | 2024-12-06 18:38:06 +0000 | [diff] [blame] | 133 | fn insecurelyReadPayloadRpData(&self) -> BinderResult<[u8; 32]> { |
| 134 | unimplemented() |
| 135 | } |
| 136 | fn insecurelyWritePayloadRpData(&self, _: &[u8; 32]) -> BinderResult<()> { |
| 137 | unimplemented() |
| 138 | } |
Shikha Panwar | 5b7b494 | 2024-12-18 15:32:49 +0000 | [diff] [blame] | 139 | fn isNewInstance(&self) -> BinderResult<bool> { |
| 140 | unimplemented() |
| 141 | } |
Nikita Ioffe | 901083f | 2025-01-20 01:54:28 +0000 | [diff] [blame] | 142 | fn checkLibIcuIsAccessible(&self) -> BinderResult<()> { |
| 143 | unimplemented() |
| 144 | } |
Alan Stokes | c874cd2 | 2024-06-11 16:23:18 +0100 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | fn unimplemented<T>() -> BinderResult<T> { |
Alan Stokes | f46a17c | 2025-01-05 15:50:18 +0000 | [diff] [blame] | 148 | let message = c"Got a call to an unimplemented ITestService method in testbinary.rs"; |
Alan Stokes | c874cd2 | 2024-06-11 16:23:18 +0100 | [diff] [blame] | 149 | error!("{message:?}"); |
| 150 | Err(Status::new_exception(ExceptionCode::UNSUPPORTED_OPERATION, Some(message))) |
| 151 | } |