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 use utils_rust::directory_ex;
17 use cxx::let_cxx_string;
18 use std::fs;
19 use std::fs::File;
20 
21 // This code is converted from 22 functions of the utils_directory_test.cpp.
22 
23 #[test]
test_get_current_proc_full_file_name_001()24 fn test_get_current_proc_full_file_name_001()
25 {
26     let str_base_name = "/data/test/rust_utils_directory_test";
27     let str_filename = directory_ex::ffi::RustGetCurrentProcFullFileName();
28     assert_eq!(str_base_name, str_filename);
29 }
30 
31 #[test]
test_get_current_proc_path_001()32 fn test_get_current_proc_path_001()
33 {
34     let str_path_name = "/data/test/";
35     let str_cur_path_name = directory_ex::ffi::RustGetCurrentProcPath();
36     assert_eq!(str_path_name, str_cur_path_name);
37 }
38 
39 #[test]
test_extract_file_path_001()40 fn test_extract_file_path_001()
41 {
42     let str_file_path = "/data/test/";
43     let str_path = directory_ex::ffi::RustExtractFilePath(&directory_ex::ffi::RustGetCurrentProcFullFileName());
44     assert_eq!(str_file_path, str_path);
45 }
46 
47 #[test]
test_extract_file_name_001()48 fn test_extract_file_name_001()
49 {
50     let str_base_name = "rust_utils_directory_test";
51     let str_name = directory_ex::ffi::RustExtractFileName(&directory_ex::ffi::RustGetCurrentProcFullFileName());
52     assert_eq!(str_base_name, str_name);
53 }
54 
55 #[test]
test_extract_file_ext_001()56 fn test_extract_file_ext_001()
57 {
58     let str_base_name = "test/test.txt";
59     let str_type_name = directory_ex::ffi::RustExtractFileExt(&str_base_name.to_string());
60     assert_eq!(str_type_name, "txt");
61 }
62 
63 #[test]
test_extract_file_ext_002()64 fn test_extract_file_ext_002()
65 {
66     let str_base_name = "test/test_txt";
67     let str_type_name = directory_ex::ffi::RustExtractFileExt(&str_base_name.to_string());
68     assert_eq!(str_type_name, "");
69 }
70 
71 #[test]
test_exclude_trailing_path_delimiter_001()72 fn test_exclude_trailing_path_delimiter_001()
73 {
74     let str_result = "data/test/UtilsDirectoryTest";
75     let str_name = directory_ex::ffi::RustExcludeTrailingPathDelimiter(&str_result.to_string());
76     assert_eq!(str_result, str_name);
77 }
78 
79 #[test]
test_include_trailing_path_delimiter_001()80 fn test_include_trailing_path_delimiter_001()
81 {
82     let str_result = "data/test/UtilsDirectoryTest/";
83     let str_name = directory_ex::ffi::RustIncludeTrailingPathDelimiter(&str_result.to_string());
84     assert_eq!(str_result, str_name);
85 }
86 
87 #[test]
test_get_dir_files_001()88 fn test_get_dir_files_001()
89 {
90     let resultfile = ["/data/test/TestFile.txt", "/data/test/UtilsDirectoryTest"];
91     File::create(resultfile[0]).expect("Failed to create file");
92     File::create(resultfile[1]).expect("Failed to create file");
93 
94     let path = String::from("/data/");
95     let mut files: Vec<String> = Vec::new();
96     directory_ex::ffi::RustGetDirFiles(&path, &mut files);
97 
98     let mut found = false;
99     for element in files.iter() {
100         if element == resultfile[0] {
101             found = true;
102             break;
103         }
104     }
105     assert!(found);
106 
107     found = false;
108     for element in files.iter() {
109         if element == resultfile[1] {
110             found = true;
111             break;
112         }
113     }
114     assert!(found);
115 
116     let_cxx_string!(file1 = "/data/test/TestFile.txt");
117     let_cxx_string!(file2 = "/data/test/UtilsDirectoryTest");
118     directory_ex::ffi::RemoveFile(&file1);
119     directory_ex::ffi::RemoveFile(&file2);
120 }
121 
122 #[test]
test_force_create_directory_001()123 fn test_force_create_directory_001()
124 {
125     let_cxx_string!(dirpath = "/data/test_dir/test2/test3");
126     let mut ret = directory_ex::ffi::ForceCreateDirectory(&dirpath);
127     assert!(ret);
128     ret = directory_ex::ffi::IsEmptyFolder(&dirpath);
129     assert!(ret);
130 }
131 
132 #[test]
test_force_remove_directory_001()133 fn test_force_remove_directory_001()
134 {
135     let_cxx_string!(dirpath = "/data/test_dir/test2/test4");
136     directory_ex::ffi::ForceCreateDirectory(&dirpath);
137     let ret = directory_ex::ffi::ForceRemoveDirectory(&dirpath);
138     assert!(ret);
139 }
140 
141 #[test]
test_force_remove_directory_002()142 fn test_force_remove_directory_002()
143 {
144     let_cxx_string!(dirpath = "/data/test/utils_directory_tmp/");
145     let ret = directory_ex::ffi::ForceRemoveDirectory(&dirpath);
146     assert!(!ret);
147 }
148 
149 #[test]
test_remove_file_001()150 fn test_remove_file_001()
151 {
152     let_cxx_string!(dirpath = "/data/test_dir");
153     let mut ret = directory_ex::ffi::ForceCreateDirectory(&dirpath);
154     assert!(ret);
155     match File::create("/data/test_dir/test.txt") {
156         Ok(_file) => {
157             let_cxx_string!(filename = "/data/test_dir/test.txt");
158             ret = directory_ex::ffi::RemoveFile(&filename);
159             assert!(ret);
160         },
161         Err(error) => {
162             println!("create file error: {}", error);
163         }
164     }
165     ret = directory_ex::ffi::ForceRemoveDirectory(&dirpath);
166     assert!(ret);
167 }
168 
169 #[test]
test_get_folder_size_001()170 fn test_get_folder_size_001()
171 {
172     let_cxx_string!(dirpath = "/data/test_folder/");
173     let mut ret = directory_ex::ffi::ForceCreateDirectory(&dirpath);
174     assert!(ret);
175     fs::write("/data/test_folder/test.txt", "This is a line.\nThis is another line.\n").unwrap();
176     let resultsize = directory_ex::ffi::GetFolderSize(&dirpath);
177     let resultcomp = 38;
178     assert_eq!(resultsize, resultcomp);
179 
180     let mut mode = directory_ex::S_IRWXU | directory_ex::S_IRWXG | directory_ex::S_IRWXO;
181     let_cxx_string!(txt = "/data/test_folder/test.txt");
182     ret = directory_ex::ffi::ChangeModeFile(&txt, &mode);
183     assert!(ret);
184 
185     mode = directory_ex::S_IRUSR  | directory_ex::S_IRGRP | directory_ex::S_IROTH;
186     ret = directory_ex::ffi::ChangeModeDirectory(&dirpath, &mode);
187     assert!(ret);
188 
189     ret = directory_ex::ffi::ForceRemoveDirectory(&dirpath);
190     assert!(ret);
191 }
192 
193 #[test]
test_change_mode_file_001()194 fn test_change_mode_file_001()
195 {
196     let_cxx_string!(dirpath = "/data/test/utils_directory_tmp/test.txt");
197     let mode = directory_ex::S_IRWXU | directory_ex::S_IRWXG | directory_ex::S_IRWXO;
198     let ret = directory_ex::ffi::ChangeModeFile(&dirpath, &mode);
199     assert!(!ret);
200 }
201 
202 #[test]
test_change_mode_directory_001()203 fn test_change_mode_directory_001()
204 {
205     let_cxx_string!(dirpath = "");
206     let mode = directory_ex::S_IRUSR  | directory_ex::S_IRGRP | directory_ex::S_IROTH;
207     let ret = directory_ex::ffi::ChangeModeDirectory(&dirpath, &mode);
208     assert!(!ret);
209 
210     let resultsize = directory_ex::ffi::GetFolderSize(&dirpath);
211     let resultcomp = 0;
212     assert_eq!(resultsize, resultcomp);
213 }
214 
215 #[test]
test_path_to_real_path_001()216 fn test_path_to_real_path_001()
217 {
218     let path = "/data/test".to_string();
219     let mut realpath = String::new();
220     let ret = directory_ex::ffi::RustPathToRealPath(&path, &mut realpath);
221     assert!(ret);
222     assert_eq!(path, realpath);
223 }
224 
225 #[test]
test_path_to_real_path_002()226 fn test_path_to_real_path_002()
227 {
228     let path = "/data/../data/test".to_string();
229     let mut realpath = String::new();
230     let ret = directory_ex::ffi::RustPathToRealPath(&path, &mut realpath);
231     assert!(ret);
232     assert_eq!("/data/test", realpath);
233 }
234 
235 #[test]
test_path_to_real_path_003()236 fn test_path_to_real_path_003()
237 {
238     let path = "./".to_string();
239     let mut realpath = String::new();
240     let ret = directory_ex::ffi::RustPathToRealPath(&path, &mut realpath);
241     assert!(ret);
242     assert_eq!("/data/test", realpath);
243 }
244 
245 #[test]
test_path_to_real_path_004()246 fn test_path_to_real_path_004()
247 {
248     let path = String::new();
249     let mut realpath = String::new();
250     let ret = directory_ex::ffi::RustPathToRealPath(&path, &mut realpath);
251     assert!(!ret);
252 }
253 
254 #[test]
test_path_to_real_path_005()255 fn test_path_to_real_path_005()
256 {
257     let path = "/data/test/data/test/data/test/data/test/data/test/data/ \
258     test/data/test/data/test/data/test/data/test/data/test/data/test/data/ \
259     test/data/test/data/test/data/test/data/test/data/test/data/test/data/ \
260     test/data/test/data/test/data/test/data/test/data/test/data/test/data/ \
261     test/data/test/data/test/data/test".to_string();
262     let mut realpath = String::new();
263     let ret = directory_ex::ffi::RustPathToRealPath(&path, &mut realpath);
264     assert!(!ret);
265 }
266 
267 #[test]
test_path_to_real_path_006()268 fn test_path_to_real_path_006()
269 {
270     let path: String = "x".repeat(directory_ex::PATH_MAX);
271     let mut realpath = String::new();
272     let ret = directory_ex::ffi::RustPathToRealPath(&path, &mut realpath);
273     assert!(!ret);
274 }