main.rs 4.8 KB
Newer Older
F
fanxiaoyu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Copyright (C) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//! IPC test server

extern crate ipc_rust;
extern crate test_ipc_service;

use ipc_rust::{
22
    IRemoteBroker, join_work_thread, FileDesc, InterfaceToken, IpcResult,
F
fanxiaoyu 已提交
23
    add_service, get_calling_token_id, get_first_token_id, get_calling_pid,
C
chenchong_666 已提交
24
    get_calling_uid, String16, RemoteObj, IRemoteStub, get_local_device_id,
25
    get_calling_device_id, IpcStatusCode,
F
fanxiaoyu 已提交
26
};
27 28 29 30
use test_ipc_service::{
    ITest, TestStub, IPC_TEST_SERVICE_ID, IPC_TEST_STATUS_SUCCESS,
    reverse, IFoo, FooStub, init_access_token, IPC_TEST_STATUS_FAILED,
};
F
fanxiaoyu 已提交
31 32
use std::io::Write;
use std::fs::OpenOptions;
33
use std::os::fd::AsRawFd;
F
fanxiaoyu 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
use std::{thread, time};

/// FooService type
pub struct FooService;

impl IFoo for FooService {
}

impl IRemoteBroker for FooService {
}

/// test.ipc.ITestService type
pub struct TestService;

impl ITest for TestService {
49
    fn test_sync_transaction(&self, value: i32, delay_time: i32) -> IpcResult<i32> {
F
fanxiaoyu 已提交
50 51 52 53 54 55
        if delay_time > 0 {
            thread::sleep(time::Duration::from_millis(delay_time as u64));
        }
        Ok(reverse(value))
    }

56
    fn test_async_transaction(&self, _value: i32, delay_time: i32) -> IpcResult<()> {
F
fanxiaoyu 已提交
57 58 59 60 61 62
        if delay_time > 0 {
            thread::sleep(time::Duration::from_millis(delay_time as u64));
        }
        Ok(())
    }

63
    fn test_ping_service(&self, service_name: &String16) -> IpcResult<()> {
F
fanxiaoyu 已提交
64 65 66 67 68
        let name = service_name.get_string();
        println!("test_ping_service recv service name: {}", name);
        if name == TestStub::get_descriptor() {
            Ok(())
        } else {
69
            Err(IpcStatusCode::Failed)
F
fanxiaoyu 已提交
70 71 72
        }
    }

73
    fn test_transact_fd(&self) -> IpcResult<FileDesc> {
F
fanxiaoyu 已提交
74 75 76 77 78 79 80 81 82 83
        let path = "/data/test.txt";
        let mut value = OpenOptions::new().read(true)
                                          .write(true)
                                          .create(true)
                                          .open(path).expect("create /data/test.txt failed");
        let file_content = "Sever write!\n";
        write!(value, "{}", file_content).expect("write file success");
        Ok(FileDesc::new(value))
    }

84
    fn test_transact_string(&self, value: &str) -> IpcResult<i32> {
F
fanxiaoyu 已提交
85 86 87
        Ok(value.len() as i32)
    }

88
    fn test_get_foo_service(&self) -> IpcResult<RemoteObj> {
F
fanxiaoyu 已提交
89 90 91 92
        let service = FooStub::new_remote_stub(FooService).expect("create FooService success");
        Ok(service.as_object().expect("get a RemoteObj success"))
    }

93
    fn echo_interface_token(&self, token: &InterfaceToken) -> IpcResult<InterfaceToken> {
F
fanxiaoyu 已提交
94 95 96
        Ok(InterfaceToken::new(&token.get_token()))
    }

97
    fn echo_calling_info(&self) -> IpcResult<(u64, u64, u64, u64)> {
F
fanxiaoyu 已提交
98 99 100 101 102 103
        let token_id = get_calling_token_id();
        let first_token_id = get_first_token_id();
        let pid = get_calling_pid();
        let uid = get_calling_uid();
        Ok((token_id, first_token_id, pid, uid))
    }
C
chenchong_666 已提交
104

105
    fn test_get_device_id(&self) -> IpcResult<(String, String)> {
C
chenchong_666 已提交
106 107 108 109 110 111
        let local_device_id = get_local_device_id();
        let calling_device_id = get_calling_device_id();

        if let (Ok(local_id), Ok(calling_id)) = (local_device_id, calling_device_id) {
            Ok((local_id, calling_id))
        } else {
112
            Err(IpcStatusCode::Failed)
C
chenchong_666 已提交
113 114
        }
    }
F
fanxiaoyu 已提交
115 116
}

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
impl IRemoteBroker for TestService {
    /// Dump implementation of service rewrite ipc
    fn dump(&self, _file: &FileDesc, _args: &mut Vec<String16>) -> i32 {
        let fd = _file.as_raw_fd();
        if fd < 0 {
            println!("fd is invalid: {}", fd);
            return IPC_TEST_STATUS_FAILED;
        }

        for arg in _args.as_slice() {
            println!("{}", arg.get_string().as_str());
            _file.as_ref().write_all(arg.get_string().as_str().as_bytes()).expect("write failed");
        }
        IPC_TEST_STATUS_SUCCESS
    }
}
F
fanxiaoyu 已提交
133 134 135 136 137 138 139 140 141 142

fn main() {
    init_access_token();
    // create stub
    let service = TestStub::new_remote_stub(TestService).expect("create TestService success");
    add_service(&service.as_object().expect("get ITest service failed"),
        IPC_TEST_SERVICE_ID).expect("add server to samgr failed"); 
    println!("join to ipc work thread");
    join_work_thread();   
}