1 /*
2 * Copyright (c) 2023-2024 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 //! This module implements the capability of processing the identity information of the Asset caller.
17
18 use asset_definition::Value;
19
20 use crate::{process_info::ProcessInfoDetail, OwnerType, ProcessInfo};
21
22 /// The identity of calling process.
23 #[derive(Clone)]
24 #[derive(PartialEq, Eq)]
25 pub struct CallingInfo {
26 user_id: i32,
27 owner_type: OwnerType,
28 owner_info: Vec<u8>,
29 }
30
31 impl CallingInfo {
32 /// Build identity of current process.
new_self() -> Self33 pub fn new_self() -> Self {
34 Self::new(0, OwnerType::Native, "asset_service_8100".as_bytes().to_vec())
35 }
36
37 /// Build identity of the specified owner.
new(user_id: i32, owner_type: OwnerType, owner_info: Vec<u8>) -> Self38 pub fn new(user_id: i32, owner_type: OwnerType, owner_info: Vec<u8>) -> Self {
39 Self { user_id, owner_type, owner_info }
40 }
41
42 /// Build a instance of CallingInfo.
build(specific_user_id: Option<Value>, process_info: &ProcessInfo) -> Self43 pub fn build(specific_user_id: Option<Value>, process_info: &ProcessInfo) -> Self {
44 let mut owner_info = Vec::new();
45 match &process_info.process_info_detail {
46 ProcessInfoDetail::Hap(hap_info) => {
47 owner_info.append(&mut hap_info.app_id.clone());
48 owner_info.append(&mut "_".to_string().as_bytes().to_vec());
49 owner_info.append(&mut hap_info.app_index.to_string().as_bytes().to_vec());
50 },
51 ProcessInfoDetail::Native(native_info) => {
52 owner_info.append(&mut process_info.process_name.clone());
53 owner_info.append(&mut "_".to_string().as_bytes().to_vec());
54 owner_info.append(&mut native_info.uid.to_string().as_bytes().to_vec());
55 },
56 };
57 let mut user_id = process_info.user_id;
58 if let Some(Value::Number(specific_user_id)) = specific_user_id {
59 user_id = specific_user_id;
60 };
61
62 CallingInfo { user_id: user_id as i32, owner_type: process_info.owner_type, owner_info }
63 }
64
65 /// Get owner type of calling.
owner_type(&self) -> u3266 pub fn owner_type(&self) -> u32 {
67 self.owner_type as u32
68 }
69
70 /// Get owner info of calling.
owner_info(&self) -> &Vec<u8>71 pub fn owner_info(&self) -> &Vec<u8> {
72 &self.owner_info
73 }
74
75 /// Get user id of calling.
user_id(&self) -> i3276 pub fn user_id(&self) -> i32 {
77 self.user_id
78 }
79 }
80
81 #[cfg(test)]
82 use crate::process_info::{HapInfo, NativeInfo};
83
84 #[test]
test_build_callig_info_specific_and_hap()85 fn test_build_callig_info_specific_and_hap() {
86 let specific_user_id = 100;
87 let process_name = "test_process".as_bytes().to_vec();
88 let app_id = "test_app_id".as_bytes().to_vec();
89 let app_index = 0;
90 let process_info = ProcessInfo {
91 user_id: 0,
92 owner_type: OwnerType::Hap,
93 process_name,
94 process_info_detail: ProcessInfoDetail::Hap(HapInfo { app_id, app_index }),
95 };
96
97 let calling_info = CallingInfo::build(Some(Value::Number(specific_user_id)), &process_info);
98 assert_eq!(calling_info.user_id(), specific_user_id as i32);
99
100 let owner_info = "test_app_id_0".as_bytes().to_vec();
101 assert_eq!(calling_info.owner_info(), &owner_info);
102 }
103
104 #[test]
test_build_callig_info_hap()105 fn test_build_callig_info_hap() {
106 let process_name = "test_process".as_bytes().to_vec();
107 let app_id = "test_app_id".as_bytes().to_vec();
108 let app_index = 0;
109 let user_id = 0;
110 let process_info = ProcessInfo {
111 user_id,
112 owner_type: OwnerType::Hap,
113 process_name,
114 process_info_detail: ProcessInfoDetail::Hap(HapInfo { app_id, app_index }),
115 };
116
117 let calling_info = CallingInfo::build(None, &process_info);
118 assert_eq!(calling_info.user_id(), user_id as i32);
119 let owner_info = "test_app_id_0".as_bytes().to_vec();
120 assert_eq!(calling_info.owner_info(), &owner_info);
121 }
122
123 #[test]
test_build_callig_info_native()124 fn test_build_callig_info_native() {
125 let process_name = "test_process".as_bytes().to_vec();
126 let user_id = 0;
127 let uid = 999;
128 let process_info = ProcessInfo {
129 user_id,
130 owner_type: OwnerType::Native,
131 process_name,
132 process_info_detail: ProcessInfoDetail::Native(NativeInfo { uid }),
133 };
134
135 let calling_info = CallingInfo::build(None, &process_info);
136 assert_eq!(calling_info.user_id(), user_id as i32);
137 let owner_info = "test_process_999".as_bytes().to_vec();
138 assert_eq!(calling_info.owner_info(), &owner_info);
139 }
140
141 #[test]
test_build_callig_info_specific_and_native()142 fn test_build_callig_info_specific_and_native() {
143 let specific_user_id = 100;
144 let process_name = "test_process".as_bytes().to_vec();
145 let user_id = 0;
146 let uid = 999;
147 let process_info = ProcessInfo {
148 user_id,
149 owner_type: OwnerType::Native,
150 process_name,
151 process_info_detail: ProcessInfoDetail::Native(NativeInfo { uid }),
152 };
153
154 let calling_info = CallingInfo::build(Some(Value::Number(specific_user_id)), &process_info);
155
156 assert_eq!(calling_info.user_id(), specific_user_id as i32);
157 let owner_info = "test_process_999".as_bytes().to_vec();
158 assert_eq!(calling_info.owner_info(), &owner_info);
159 }
160