Cotton code
91 lines · 3.6 KB · RustRaw
//! The three functions, end to end, against a temporary data directory. No//! token is given, so nothing reaches the network.use std::ffi::{CStr, CString};use cotton_mac_ffi::{cotton_mac_call, cotton_mac_free, cotton_mac_open};use serde_json::{json, Value};fn take(ptr: *mut std::ffi::c_char) -> Value {    assert!(!ptr.is_null());    let text = unsafe { CStr::from_ptr(ptr) }.to_str().unwrap().to_string();    unsafe { cotton_mac_free(ptr) };    serde_json::from_str(&text).unwrap()}fn call(method: &str, args: Option<&Value>) -> Value {    let method = CString::new(method).unwrap();    let args = args.map(|a| CString::new(a.to_string()).unwrap());    take(unsafe { cotton_mac_call(method.as_ptr(), args.as_ref().map_or(std::ptr::null(), |a| a.as_ptr())) })}#[test]fn opens_and_answers_status() {    // Before open: a refusal, not a crash.    let early = call("status", None);    assert_eq!(early["ok"], false);    assert_eq!(early["code"], "not_open");    // Bad config: refused with a code.    let bad = CString::new("{}").unwrap();    let refused = take(unsafe { cotton_mac_open(bad.as_ptr()) });    assert_eq!(refused["ok"], false);    assert_eq!(refused["code"], "bad_request");    assert_eq!(take(unsafe { cotton_mac_open(std::ptr::null()) })["ok"], false);    let dir = tempfile::tempdir().unwrap();    let home = dir.path().join("home");    std::fs::create_dir_all(&home).unwrap();    let config = json!({        "dataDir": dir.path().join("data"),        "objectUrl": "https://cotton.invalid",        "deviceId": "test-device",        "machine": "test-mac",        "deviceName": "Test Mac",        "appVersion": "0.0.0",        "home": home,        "helpersDir": dir.path().join("helpers"),    });    std::fs::create_dir_all(dir.path().join("data")).unwrap();    let config = CString::new(config.to_string()).unwrap();    let opened = take(unsafe { cotton_mac_open(config.as_ptr()) });    // The device key lives in the login Keychain (6eefe9a23a): a system with    // none refuses to open with words, never a key kept in a file, and the    // rest of this test is the Mac's.    if cfg!(not(target_os = "macos")) {        assert_eq!(opened["ok"], false, "{opened}");        assert_eq!(opened["code"], "open_failed");        assert!(opened["error"].as_str().unwrap_or_default().contains("no Keychain"), "{opened}");        return;    }    assert_eq!(opened["ok"], true, "{opened}");    let status = call("status", None);    assert_eq!(status["ok"], true, "{status}");    assert_eq!(status["signedIn"], false);    assert_eq!(status["signedOut"], false);    let sources = status["sources"].as_array().unwrap();    assert!(!sources.is_empty());    for source in sources {        assert!(source["source"].is_string());        assert!(source["schedule"].is_string());        assert!(source["probe"]["ready"].is_boolean());    }    // Arguments as `{}` or an empty string are both fine; garbage is refused.    assert_eq!(call("status", Some(&json!({})))["ok"], true);    let garbage = CString::new("{not json").unwrap();    let method = CString::new("status").unwrap();    let answer = take(unsafe { cotton_mac_call(method.as_ptr(), garbage.as_ptr()) });    assert_eq!(answer["code"], "bad_request");    let unknown = call("nope", None);    assert_eq!(unknown["ok"], false);    assert_eq!(unknown["code"], "unknown_method");    let bad_source = call("runNow", Some(&json!({ "source": "fax" })));    assert_eq!(bad_source["code"], "bad_source");    assert_eq!(call("stop", None)["ok"], true);    unsafe { cotton_mac_free(std::ptr::null_mut()) };}