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
48
49
50
51
//! OSX specific extensions.

pub mod access;
pub mod certificate;
pub mod certificate_oids;
pub mod digest_transform;
pub mod encrypt_transform;
pub mod identity;
pub mod import_export;
pub mod item;
pub mod key;
pub mod keychain;
pub mod keychain_item;
pub mod passwords;
pub mod secure_transport;
pub mod transform;

#[cfg(test)]
pub mod test {
    use std::fs::File;
    use std::io::prelude::*;
    use std::path::Path;

    use identity::SecIdentity;
    use item::{ItemClass, ItemSearchOptions, Reference, SearchResult};
    use os::macos::keychain::SecKeychain;

    pub fn identity(dir: &Path) -> SecIdentity {
        // FIXME https://github.com/rust-lang/rust/issues/30018
        let keychain = keychain(dir);
        let mut items = p!(ItemSearchOptions::new()
            .class(ItemClass::identity())
            .keychains(&[keychain])
            .search());
        match items.pop().unwrap() {
            SearchResult::Ref(Reference::Identity(identity)) => identity,
            _ => panic!("expected identity"),
        }
    }

    pub fn keychain(dir: &Path) -> SecKeychain {
        let path = dir.join("server.keychain");
        let mut file = p!(File::create(&path));
        p!(file.write_all(include_bytes!("../../../test/server.keychain")));
        drop(file);

        let mut keychain = p!(SecKeychain::open(&path));
        p!(keychain.unlock(Some("password123")));
        keychain
    }
}