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
16extern crate stacktrace_rust;
17
18use std::fs;
19use std::fs::File;
20use std::os::fd::AsRawFd;
21use std::path::Path;
22use std::thread;
23
24#[test]
25fn test_get_trace() {
26    let trace = stacktrace_rust::get_trace(false);
27    assert!(!trace.is_empty());
28    assert!(trace.contains("#00"));
29    assert!(trace.contains("libstacktrace_rust.dylib.so"));
30    assert!(trace.contains("rust_stacktrace_test"));
31}
32
33#[test]
34fn test_get_trace_in_multithread() {
35    let mut handles = vec![];
36    for _ in 0..50 {
37        let handle = thread::spawn(move || {
38            let trace = stacktrace_rust::get_trace(false);
39            assert!(!trace.is_empty());
40            assert!(trace.contains("#00"));
41            assert!(trace.contains("libstacktrace_rust.dylib.so"));
42            assert!(trace.contains("rust_stacktrace_test"));
43        });
44        handles.push(handle);
45    }
46    for handle in handles {
47        handle.join().unwrap();
48    }
49}
50
51#[test]
52fn test_print_trace() {
53    let path = Path::new("/data/stacktrace_test_file001");
54    let display = path.display();
55    let output = match File::create(path) {
56        Err(why) => panic!("couldn't create {}: {:?}", display, why),
57        Ok(output) => output,
58    };
59    assert!(stacktrace_rust::print_trace(output.as_raw_fd()));
60    let trace = fs::read_to_string(path).unwrap();
61    assert!(!trace.is_empty());
62    assert!(trace.contains("#00"));
63    assert!(trace.contains("rust_stacktrace_test"));
64}