brlapi 0.4.1

Safe Rust bindings for the BrlAPI library
// SPDX-License-Identifier: LGPL-2.1

//! Connection settings validation and edge case tests

use brlapi::ConnectionSettings;

mod common;

#[test]
fn test_connection_settings_validation() {
    // Test connection settings validation and conversion
    let mut settings = ConnectionSettings::localhost();
    settings.set_auth("/etc/brlapi.key");

    let (c_settings, auth_str, host_str) = settings
        .to_c_settings()
        .expect("Settings conversion should succeed for valid settings");

    // Verify strings are kept alive and properly converted
    assert!(auth_str.is_some());
    assert!(host_str.is_some());
    assert!(!c_settings.auth.is_null());
    assert!(!c_settings.host.is_null());

    // Test default settings
    let default_settings = ConnectionSettings::default();
    assert!(default_settings.auth().is_none());
    assert!(default_settings.host().is_none());

    let (c_default, auth_default, host_default) = default_settings
        .to_c_settings()
        .expect("Default settings conversion should always succeed");
    assert!(auth_default.is_none());
    assert!(host_default.is_none());
    assert!(c_default.auth.is_null());
    assert!(c_default.host.is_null());
}

#[test]
fn test_connection_settings_edge_cases() {
    // Test various edge cases for connection settings

    // Test empty strings
    let mut empty_settings = ConnectionSettings::for_host("");
    empty_settings.set_auth("");
    let (c_settings, _, _) = empty_settings
        .to_c_settings()
        .expect("Empty string settings conversion should succeed");
    assert!(!c_settings.auth.is_null());
    assert!(!c_settings.host.is_null());

    // Test very long strings
    let long_path = "a".repeat(1000);
    let mut long_settings = ConnectionSettings::for_host(&long_path);
    long_settings.set_auth(&long_path);
    let (c_long, _, _) = long_settings
        .to_c_settings()
        .expect("Long string settings conversion should succeed");
    assert!(!c_long.auth.is_null());
    assert!(!c_long.host.is_null());

    // Test special characters
    let mut special_settings = ConnectionSettings::for_host("::1"); // IPv6 localhost
    special_settings.set_auth("/etc/brltty-authkey:with:colons");
    let (c_special, _, _) = special_settings
        .to_c_settings()
        .expect("Special character settings conversion should succeed");
    assert!(!c_special.auth.is_null());
    assert!(!c_special.host.is_null());
}

#[test]
fn test_connection_settings_null_byte_in_auth() {
    // Test that null bytes in auth path return error
    let mut settings = ConnectionSettings::new();
    settings.set_auth("/path/with\0null");

    assert!(settings.to_c_settings().is_err());
}

#[test]
fn test_connection_settings_null_byte_in_host() {
    // Test that null bytes in host return error
    let settings = ConnectionSettings::for_host("host\0name");

    assert!(settings.to_c_settings().is_err());
}