1 // Copyright (C) 2023 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 //     http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 //! This create implement the request proxy and stub
15 #![cfg_attr(test, feature(future_join))]
16 #![cfg_attr(test, allow(clippy::redundant_clone))]
17 #![allow(unreachable_pub, clippy::new_without_default)]
18 #![warn(
19     missing_docs,
20     clippy::redundant_static_lifetimes,
21     clippy::enum_variant_names,
22     clippy::clone_on_copy,
23     clippy::unused_async
24 )]
25 
26 #[macro_use]
27 mod macros;
28 
29 #[cfg(not(feature = "oh"))]
30 #[macro_use]
31 extern crate log;
32 
33 cfg_oh! {
34     #[macro_use]
35     mod hilog;
36     mod trace;
37     pub mod ability;
38     mod sys_event;
39     pub use service::interface;
40     pub use utils::form_item::FileSpec;
41 }
42 
43 mod error;
44 mod manage;
45 mod service;
46 mod task;
47 mod utils;
48 pub use task::{config, info};
49 
50 cfg_oh! {
51     #[cfg(not(test))]
52     const LOG_LABEL: hilog_rust::HiLogLabel = hilog_rust::HiLogLabel {
53         log_type: hilog_rust::LogType::LogCore,
54         domain: 0xD001C50,
55         tag: "RequestService",
56     };
57 
58     #[cfg(test)]
59     const LOG_LABEL: hilog_rust::HiLogLabel = hilog_rust::HiLogLabel {
60         log_type: hilog_rust::LogType::LogCore,
61         domain: 0xD001C50,
62         tag: "RequestUtTest",
63     };
64 }
65 
66 #[cfg(feature = "oh")]
67 #[cfg(test)]
68 mod tests {
69     use super::manage::database::RequestDb;
70     use super::manage::SystemConfigManager;
71     use crate::ability::SYSTEM_CONFIG_MANAGER;
72     /// test init
73     pub(crate) fn test_init() {
74         static ONCE: std::sync::Once = std::sync::Once::new();
75         ONCE.call_once(|| {
76             unsafe { SYSTEM_CONFIG_MANAGER.write(SystemConfigManager::init()) };
77         });
78 
79         let _ = std::fs::create_dir("test_files/");
80 
81         unsafe { SetAccessTokenPermission() };
82     }
83 
84     pub(crate) fn lock_database<'a>() -> DatabaseLock<'a> {
85         let _inner = unsafe {
86             match DB_LOCK.lock() {
87                 Ok(inner) => inner,
88                 Err(_) => {
89                     RequestDb::get_instance()
90                         .execute("DELETE FROM request_task")
91                         .unwrap();
92                     DB_LOCK = std::sync::Mutex::new(());
93                     DB_LOCK.lock().unwrap()
94                 }
95             }
96         };
97         DatabaseLock { _inner }
98     }
99 
100     pub(crate) struct DatabaseLock<'a> {
101         _inner: std::sync::MutexGuard<'a, ()>,
102     }
103 
104     impl<'a> Drop for DatabaseLock<'a> {
dropnull105         fn drop(&mut self) {
106             RequestDb::get_instance()
107                 .execute("DELETE FROM request_task")
108                 .unwrap();
109         }
110     }
111 
112     static mut DB_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
113 
114     extern "C" {
SetAccessTokenPermissionnull115         fn SetAccessTokenPermission();
116     }
117 }
118