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 //! unittest
16 #![allow(missing_docs)]
17 
18 /// #[cfg(test)]
19 /// mod session_test {
20 ///     use ylong_runtime;
21 ///
22 ///     use crate::common::hsession;
23 ///     use crate::common::hsession::{ActionType, HdcSession};
24 ///     use crate::config::{ConnectType, NodeType};
25 ///     use std::time::Duration;
26 ///
27 ///     #[ylong_runtime::test]
28 ///     async fn if_hsession_query_work() {
29 ///         let t1 = ylong_runtime::spawn(async {
30 ///             let hs = HdcSession::new(
31 ///                 111,
32 ///                 "test_key".to_string(),
33 ///                 NodeType::Daemon,
34 ///                 ConnectType::Tcp,
35 ///             );
36 ///             hsession::admin_session(ActionType::Add(hs)).await;
37 ///         });
38 ///
39 ///         let t2 = ylong_runtime::spawn(async {
40 ///             let _ = ylong_runtime::time::sleep(Duration::from_millis(200)).await;
41 ///             let opt = hsession::admin_session(ActionType::Query(111)).await;
42 ///             let lock = opt.unwrap();
43 ///             let hs = lock.lock().await;
44 ///             assert_eq!(hs.connect_key, "test_key".to_string());
45 ///         });
46 ///
47 ///         t1.await.unwrap();
48 ///         t2.await.unwrap();
49 ///     }
50 ///
51 ///     #[ylong_runtime::test]
52 ///     async fn if_hsession_query_ref_work() {
53 ///         let t1 = ylong_runtime::spawn(async {
54 ///             let hs = HdcSession::new(
55 ///                 222,
56 ///                 "test_key".to_string(),
57 ///                 NodeType::Daemon,
58 ///                 ConnectType::Tcp,
59 ///             );
60 ///             hsession::admin_session(ActionType::Add(hs)).await;
61 ///         });
62 ///
63 ///         let t2 = ylong_runtime::spawn(async {
64 ///             let _ = ylong_runtime::time::sleep(Duration::from_millis(200)).await;
65 ///             let _ = hsession::admin_session(ActionType::QueryRef(222)).await;
66 ///         });
67 ///
68 ///         let t3 = ylong_runtime::spawn(async {
69 ///             let _ = ylong_runtime::time::sleep(Duration::from_millis(400)).await;
70 ///             let opt = hsession::admin_session(ActionType::Query(222)).await;
71 ///             let lock = opt.unwrap();
72 ///             let hs = lock.lock().await;
73 ///             assert_eq!(hs.ref_cnt, 1);
74 ///         });
75 ///
76 ///         t1.await.unwrap();
77 ///         t2.await.unwrap();
78 ///         t3.await.unwrap();
79 ///     }
80 ///
81 ///     #[ylong_runtime::test]
82 ///     async fn if_hsession_update_work() {
83 ///         let t1 = ylong_runtime::spawn(async {
84 ///             let hs = HdcSession::new(
85 ///                 333,
86 ///                 "test_key".to_string(),
87 ///                 NodeType::Daemon,
88 ///                 ConnectType::Tcp,
89 ///             );
90 ///             hsession::admin_session(ActionType::Add(hs)).await;
91 ///         });
92 ///
93 ///         let t2 = ylong_runtime::spawn(async {
94 ///             let hs = HdcSession::new(
95 ///                 3333,
96 ///                 "test_key2".to_string(),
97 ///                 NodeType::Server,
98 ///                 ConnectType::Bt,
99 ///             );
100 ///             let _ = ylong_runtime::time::sleep(Duration::from_millis(300)).await;
101 ///             let _ = hsession::admin_session(ActionType::Update(333, hs)).await;
102 ///         });
103 ///
104 ///         let t3 = ylong_runtime::spawn(async {
105 ///             let _ = ylong_runtime::time::sleep(Duration::from_millis(400)).await;
106 ///             let opt = hsession::admin_session(ActionType::Query(3333)).await;
107 ///             let lock = opt.unwrap();
108 ///             let hs = lock.lock().await;
109 ///             assert_eq!(hs.connect_key, "test_key2".to_string());
110 ///             assert!(matches!(hs.connect_type, ConnectType::Bt));
111 ///             assert!(matches!(hs.node_type, NodeType::Server));
112 ///         });
113 ///
114 ///         let t4 = ylong_runtime::spawn(async {
115 ///             let _ = ylong_runtime::time::sleep(Duration::from_millis(500)).await;
116 ///             let opt = hsession::admin_session(ActionType::Query(333)).await;
117 ///             assert!(opt.is_none());
118 ///         });
119 ///
120 ///         t1.await.unwrap();
121 ///         t2.await.unwrap();
122 ///         t3.await.unwrap();
123 ///         t4.await.unwrap();
124 ///     }
125 ///
126 ///     #[ylong_runtime::test]
127 ///     async fn if_hsession_update_outside_admin_work() {
128 ///         let t1 = ylong_runtime::spawn(async {
129 ///             let hs = HdcSession::new(
130 ///                 444,
131 ///                 "test_key".to_string(),
132 ///                 NodeType::Daemon,
133 ///                 ConnectType::Tcp,
134 ///             );
135 ///             hsession::admin_session(ActionType::Add(hs)).await;
136 ///         });
137 ///
138 ///         let t2 = ylong_runtime::spawn(async {
139 ///             let _ = ylong_runtime::time::sleep(Duration::from_millis(200)).await;
140 ///             let opt = hsession::admin_session(ActionType::Query(444)).await;
141 ///             let lock = opt.unwrap();
142 ///             let mut hs = lock.lock().await;
143 ///             hs.connect_key = "new_key".to_string();
144 ///         });
145 ///
146 ///         let t3 = ylong_runtime::spawn(async {
147 ///             let _ = ylong_runtime::time::sleep(Duration::from_millis(400)).await;
148 ///             let opt = hsession::admin_session(ActionType::Query(444)).await;
149 ///             let lock = opt.unwrap();
150 ///             let hs = lock.lock().await;
151 ///             assert_eq!(hs.connect_key, "new_key".to_string());
152 ///         });
153 ///
154 ///         t1.await.unwrap();
155 ///         t2.await.unwrap();
156 ///         t3.await.unwrap();
157 ///     }
158 /// }
159 ///
160 /// #[cfg(test)]
161 /// mod file_test {
162 ///     use crate::{
163 ///         common::{base::Base, hdcfile::HdcFile},
164 ///         serializer::{serialize::Serialization, native_struct::TransferConfig},
165 ///     };
166 ///     #[ylong_runtime::test]
167 ///     async fn test_base_fn() {
168 ///         let command = String::from(
169 ///             "-cwd \"C:\\Users\\\" C:\\Users\\Desktop\\hdcfile\\hdcd_system /data/hdcd",
170 ///         );
171 ///         let mut argc = 0;
172 ///         let argv = Base::split_command_to_args(&command, &mut argc);
173 ///         assert_eq!(argc, 4);
174 ///         assert_eq!(argv.len(), 4);
175 ///         assert_eq!(argv.get(0), Some(&"-cwd".to_string()));
176 ///         assert_eq!(argv.get(1), Some(&"C:\\Users\\".to_string()));
177 ///         assert_eq!(
178 ///             argv.get(2),
179 ///             Some(&"C:\\Users\\Desktop\\hdcfile\\hdcd_system".to_string())
180 ///         );
181 ///         assert_eq!(argv.get(3), Some(&"/data/hdcd".to_string()));
182 ///     }
183 ///
184 ///     #[ylong_runtime::test]
185 ///     async fn test_get_file_name() {
186 ///         let mut path = String::from("/home/test/hdctest.log");
187 ///         let file_name = Base::get_file_name(&mut path).unwrap();
188 ///         assert_eq!(file_name, "hdctest.log");
189 ///     }
190 ///
191 ///     #[ylong_runtime::test]
192 ///     async fn test_file_task_master() {
193 ///         let mut task = HdcFile::new(1, 2);
194 ///         let mut command = String::from("-cwd \"C:\\Users\\\" /system/bin /data/hdcd");
195 ///         if cfg!(target_os = "windows") {
196 ///             command = String::from("-cwd \"C:\\Users\\\" C:\\Users\\ /data/");
197 ///         }
198 ///         let result = task.begin_transfer(&command);
199 ///         if !result {
200 ///             assert!(true);
201 ///         } else {
202 ///             assert_eq!(task.transfer.is_dir, true);
203 ///             println!("{}", task.transfer.base_local_path);
204 ///             if cfg!(target_os = "linux") {
205 ///                 assert_eq!(task.transfer.base_local_path, String::from("/system/bin/"));
206 ///             } else {
207 ///                 assert_eq!(task.transfer.base_local_path, String::from("C:\\Users\\"));
208 ///             }
209 ///             assert_eq!(task.transfer.remote_path, String::from("/data/hdcd"));
210 ///             assert_ne!(task.transfer.task_queue.len(), 0);
211 ///         }
212 ///     }
213 ///
214 ///     #[ylong_runtime::test]
215 ///     async fn test_file_task_slave() {
216 ///         let mut task = HdcFile::new(1, 2);
217 ///         let mut transfer_config = TransferConfig::default();
218 ///         transfer_config.file_size = 88888888;
219 ///         transfer_config.path = "/data/hdcd".to_string();
220 ///         transfer_config.optional_name = "hdcd".to_string();
221 ///         let payload = TransferConfig::serialize(&transfer_config);
222 ///         task.check_slaver(&payload[..]);
223 ///         assert_eq!(task.transfer.file_size, 88888888);
224 ///         assert_eq!(task.transfer.local_path, String::from("/data/hdcd"));
225 ///     }
226 /// }
227 #[cfg(test)]
228 mod sync_session_test {}
229