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 #[macro_use]
26 mod macros;
27 
28 #[cfg(not(feature = "oh"))]
29 #[macro_use]
30 extern crate log;
31 
32 cfg_oh! {
33     #[macro_use]
34     mod hilog;
35     mod trace;
36     pub mod ability;
37     mod sys_event;
38     pub use service::interface;
39     pub use utils::form_item::FileSpec;
40 }
41 
42 mod error;
43 mod manage;
44 mod service;
45 mod task;
46 mod utils;
47 pub use task::{config, info};
48 
49 cfg_oh! {
50     #[cfg(not(test))]
51     const LOG_LABEL: hilog_rust::HiLogLabel = hilog_rust::HiLogLabel {
52         log_type: hilog_rust::LogType::LogCore,
53         domain: 0xD001C50,
54         tag: "RequestService",
55     };
56 
57     #[cfg(test)]
58     const LOG_LABEL: hilog_rust::HiLogLabel = hilog_rust::HiLogLabel {
59         log_type: hilog_rust::LogType::LogCore,
60         domain: 0xD001C50,
61         tag: "RequestUtTest",
62     };
63 }
64 
65 #[cfg(feature = "oh")]
66 #[cfg(test)]
67 mod tests {
68     use super::manage::database::RequestDb;
69     use super::manage::SystemConfigManager;
70     use crate::ability::SYSTEM_CONFIG_MANAGER;
71     /// test init
test_init()72     pub(crate) fn test_init() {
73         static ONCE: std::sync::Once = std::sync::Once::new();
74         ONCE.call_once(|| {
75             unsafe { SYSTEM_CONFIG_MANAGER.write(SystemConfigManager::init()) };
76         });
77 
78         let _ = std::fs::create_dir("test_files/");
79 
80         unsafe { SetAccessTokenPermission() };
81     }
82 
lock_database<'a>() -> DatabaseLock<'a>83     pub(crate) fn lock_database<'a>() -> DatabaseLock<'a> {
84         let _inner = unsafe {
85             match DB_LOCK.lock() {
86                 Ok(inner) => inner,
87                 Err(_) => {
88                     RequestDb::get_instance()
89                         .execute("DELETE FROM request_task")
90                         .unwrap();
91                     DB_LOCK = std::sync::Mutex::new(());
92                     DB_LOCK.lock().unwrap()
93                 }
94             }
95         };
96         DatabaseLock { _inner }
97     }
98 
99     pub(crate) struct DatabaseLock<'a> {
100         _inner: std::sync::MutexGuard<'a, ()>,
101     }
102 
103     impl<'a> Drop for DatabaseLock<'a> {
drop(&mut self)104         fn drop(&mut self) {
105             RequestDb::get_instance()
106                 .execute("DELETE FROM request_task")
107                 .unwrap();
108         }
109     }
110 
111     static mut DB_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
112 
113     extern "C" {
SetAccessTokenPermission()114         fn SetAccessTokenPermission();
115     }
116 }
117