1 /*
2  * Copyright (C) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 extern crate ipc_rust;
17 extern crate rust_idl_ipc_test_service;
18 
19 use std::ptr;
20 use std::ffi::{c_char, CString};
21 
22 use ipc_rust::{FromRemoteObj, RemoteObjRef, get_service};
23 use rust_idl_ipc_test_service::{IIdlTestService};
24 
25 const IPC_TEST_SERVICE_ID: i32 = 1118;
26 
27 #[repr(C)]
28 struct TokenInfoParams {
29     dcaps_num: i32,
30     perms_num: i32,
31     acls_num: i32,
32     dcaps: *const *const c_char,
33     perms: *const *const c_char,
34     acls: *const *const c_char,
35     process_name: *const c_char,
36     apl_str: *const c_char,
37 }
38 
39 extern "C" {
GetAccessTokenId(tokenInfo: *mut TokenInfoParams) -> u6440     fn GetAccessTokenId(tokenInfo: *mut TokenInfoParams) -> u64;
SetSelfTokenID(tokenID: u64) -> i3241     fn SetSelfTokenID(tokenID: u64) -> i32;
42 }
43 
get_test_service() -> RemoteObjRef<dyn IIdlTestService>44 fn get_test_service() -> RemoteObjRef<dyn IIdlTestService>
45 {
46     let object = get_service(IPC_TEST_SERVICE_ID).expect("get itest service failed");
47     let remote = <dyn IIdlTestService as FromRemoteObj>::from(object);
48     let remote = match remote {
49         Ok(x) => x,
50         Err(error) => {
51             println!("convert RemoteObj to TestProxy failed: {}", error);
52             panic!();
53         }
54     };
55     remote
56 }
57 
58 /// init access token for client
59 #[test]
init_access_token()60 fn init_access_token() {
61     let name = CString::new("com.ipc.test").expect("process name is invalid");
62     let apl = CString::new("normal").expect("apl string is invalid");
63     let mut param = TokenInfoParams {
64         dcaps_num: 0,
65         perms_num: 0,
66         acls_num: 0,
67         dcaps: ptr::null(),
68         perms: ptr::null(),
69         acls: ptr::null(),
70         process_name: name.as_ptr(),
71         apl_str: apl.as_ptr(),
72     };
73 
74     unsafe {
75         let token_id = GetAccessTokenId(&mut param as *mut TokenInfoParams);
76         SetSelfTokenID(token_id);
77     }
78 }
79 
80 #[test]
idl_ipc_test_basic_001()81 fn idl_ipc_test_basic_001() {
82     let remote = get_test_service();
83     remote.idl_ipc_test_basic_001().expect("should success");
84     assert_eq!(true, true);
85 }
86 
87 #[test]
idl_ipc_test_basic_002()88 fn idl_ipc_test_basic_002() {
89     let remote = get_test_service();
90     remote.idl_ipc_test_basic_002().expect("should success");
91     assert_eq!(true, true);
92 }
93 
94 #[test]
idl_ipc_test_basic_101()95 fn idl_ipc_test_basic_101() {
96     let remote = get_test_service();
97     let anchor = remote.idl_ipc_test_basic_101(&true).expect("should success");
98     assert!(!anchor);
99 }
100 
101 #[test]
idl_ipc_test_basic_102()102 fn idl_ipc_test_basic_102() {
103     let remote = get_test_service();
104     let anchor = remote.idl_ipc_test_basic_102(&12).expect("should success");
105     println!("idl_ipc_test_basic_102: {}", anchor);
106     assert_eq!(anchor, 24);
107 }
108 
109 #[test]
idl_ipc_test_basic_103()110 fn idl_ipc_test_basic_103() {
111     let remote = get_test_service();
112     let anchor = remote.idl_ipc_test_basic_103(&24).expect("should success");
113     assert_eq!(anchor, 48);
114 }
115 
116 #[test]
idl_ipc_test_basic_104()117 fn idl_ipc_test_basic_104() {
118     let remote = get_test_service();
119     let anchor = remote.idl_ipc_test_basic_104(&48).expect("should success");
120     assert_eq!(anchor, 96);
121 }
122 
123 #[test]
idl_ipc_test_basic_105()124 fn idl_ipc_test_basic_105() {
125     let remote = get_test_service();
126     let anchor = remote.idl_ipc_test_basic_105(&96).expect("should success");
127     assert_eq!(anchor, 192);
128 }
129 
130 #[test]
idl_ipc_test_basic_106()131 fn idl_ipc_test_basic_106() {
132     let remote = get_test_service();
133     let anchor = remote.idl_ipc_test_basic_106(&1.7).expect("should success");
134     assert!((anchor - 1.7 * 2.0).abs() < 0.1);
135 }
136 
137 #[test]
idl_ipc_test_basic_107()138 fn idl_ipc_test_basic_107() {
139     let remote = get_test_service();
140     let anchor = remote.idl_ipc_test_basic_107(&3.4).expect("should success");
141     assert!((anchor - 3.4 * 2.0).abs() < 0.1);
142 }
143 
144 #[test]
idl_ipc_test_basic_108()145 fn idl_ipc_test_basic_108() {
146     let remote = get_test_service();
147     let anchor = remote.idl_ipc_test_basic_108("Basic").expect("should success");
148     assert_eq!(anchor, "BasicBasic");
149 }
150 
151 #[test]
idl_ipc_test_basic_199()152 fn idl_ipc_test_basic_199() {
153     let remote = get_test_service();
154     let anchor = remote.idl_ipc_test_basic_199(&true, &96, &1.7, &3.4, "Basic").expect("should success");
155     assert!(anchor);
156 }