1800b99b8Sopenharmony_ci/*
2800b99b8Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3800b99b8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4800b99b8Sopenharmony_ci * you may not use this file except in compliance with the License.
5800b99b8Sopenharmony_ci * You may obtain a copy of the License at
6800b99b8Sopenharmony_ci *
7800b99b8Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8800b99b8Sopenharmony_ci *
9800b99b8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10800b99b8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11800b99b8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12800b99b8Sopenharmony_ci * See the License for the specific language governing permissions and
13800b99b8Sopenharmony_ci * limitations under the License.
14800b99b8Sopenharmony_ci */
15800b99b8Sopenharmony_ci
16800b99b8Sopenharmony_ciextern crate stacktrace_rust;
17800b99b8Sopenharmony_ci
18800b99b8Sopenharmony_ciuse std::fs;
19800b99b8Sopenharmony_ciuse std::fs::File;
20800b99b8Sopenharmony_ciuse std::os::fd::AsRawFd;
21800b99b8Sopenharmony_ciuse std::path::Path;
22800b99b8Sopenharmony_ciuse std::thread;
23800b99b8Sopenharmony_ci
24800b99b8Sopenharmony_ci#[test]
25800b99b8Sopenharmony_cifn test_get_trace() {
26800b99b8Sopenharmony_ci    let trace = stacktrace_rust::get_trace(false);
27800b99b8Sopenharmony_ci    assert!(!trace.is_empty());
28800b99b8Sopenharmony_ci    assert!(trace.contains("#00"));
29800b99b8Sopenharmony_ci    assert!(trace.contains("libstacktrace_rust.dylib.so"));
30800b99b8Sopenharmony_ci    assert!(trace.contains("rust_stacktrace_test"));
31800b99b8Sopenharmony_ci}
32800b99b8Sopenharmony_ci
33800b99b8Sopenharmony_ci#[test]
34800b99b8Sopenharmony_cifn test_get_trace_in_multithread() {
35800b99b8Sopenharmony_ci    let mut handles = vec![];
36800b99b8Sopenharmony_ci    for _ in 0..50 {
37800b99b8Sopenharmony_ci        let handle = thread::spawn(move || {
38800b99b8Sopenharmony_ci            let trace = stacktrace_rust::get_trace(false);
39800b99b8Sopenharmony_ci            assert!(!trace.is_empty());
40800b99b8Sopenharmony_ci            assert!(trace.contains("#00"));
41800b99b8Sopenharmony_ci            assert!(trace.contains("libstacktrace_rust.dylib.so"));
42800b99b8Sopenharmony_ci            assert!(trace.contains("rust_stacktrace_test"));
43800b99b8Sopenharmony_ci        });
44800b99b8Sopenharmony_ci        handles.push(handle);
45800b99b8Sopenharmony_ci    }
46800b99b8Sopenharmony_ci    for handle in handles {
47800b99b8Sopenharmony_ci        handle.join().unwrap();
48800b99b8Sopenharmony_ci    }
49800b99b8Sopenharmony_ci}
50800b99b8Sopenharmony_ci
51800b99b8Sopenharmony_ci#[test]
52800b99b8Sopenharmony_cifn test_print_trace() {
53800b99b8Sopenharmony_ci    let path = Path::new("/data/stacktrace_test_file001");
54800b99b8Sopenharmony_ci    let display = path.display();
55800b99b8Sopenharmony_ci    let output = match File::create(path) {
56800b99b8Sopenharmony_ci        Err(why) => panic!("couldn't create {}: {:?}", display, why),
57800b99b8Sopenharmony_ci        Ok(output) => output,
58800b99b8Sopenharmony_ci    };
59800b99b8Sopenharmony_ci    assert!(stacktrace_rust::print_trace(output.as_raw_fd()));
60800b99b8Sopenharmony_ci    let trace = fs::read_to_string(path).unwrap();
61800b99b8Sopenharmony_ci    assert!(!trace.is_empty());
62800b99b8Sopenharmony_ci    assert!(trace.contains("#00"));
63800b99b8Sopenharmony_ci    assert!(trace.contains("rust_stacktrace_test"));
64800b99b8Sopenharmony_ci}