summary refs log tree commit diff
path: root/src/librustc_metadata/dynamic_lib/tests.rs
blob: b2302f2f1b5b725dd99bf0332fa1c36bcf324d9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use super::*;
use std::mem;

#[test]
fn test_loading_atoi() {
    if cfg!(windows) {
        return
    }

    // The C library does not need to be loaded since it is already linked in
    let lib = match DynamicLibrary::open(None) {
        Err(error) => panic!("Could not load self as module: {}", error),
        Ok(lib) => lib
    };

    let atoi: extern fn(*const libc::c_char) -> libc::c_int = unsafe {
        match lib.symbol("atoi") {
            Err(error) => panic!("Could not load function atoi: {}", error),
            Ok(atoi) => mem::transmute::<*mut u8, _>(atoi)
        }
    };

    let argument = CString::new("1383428980").unwrap();
    let expected_result = 0x52757374;
    let result = atoi(argument.as_ptr());
    if result != expected_result {
        panic!("atoi({:?}) != {} but equaled {} instead", argument,
               expected_result, result)
    }
}

#[test]
fn test_errors_do_not_crash() {
    use std::path::Path;

    if !cfg!(unix) {
        return
    }

    // Open /dev/null as a library to get an error, and make sure
    // that only causes an error, and not a crash.
    let path = Path::new("/dev/null");
    match DynamicLibrary::open(Some(&path)) {
        Err(_) => {}
        Ok(_) => panic!("Successfully opened the empty library.")
    }
}