diff --git a/Cargo.lock b/Cargo.lock index 90ebe03..c89b607 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -38,7 +38,7 @@ version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6d36fc52c7f6c869915e99412912f22093507da8d9e942ceaf66fe4b7c14422a" dependencies = [ - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -48,7 +48,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5bf74e1b6e971609db8ca7a9ce79fd5768ab6ae46441c572e46cf596f59e57f8" dependencies = [ "anstyle", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -130,6 +130,28 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" +[[package]] +name = "errno" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + +[[package]] +name = "fastrand" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8c02a5121d4ea3eb16a80748c74f5549a5665e4c21333c6098f283870fbdea6" + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + [[package]] name = "foreign-types" version = "0.3.2" @@ -190,6 +212,12 @@ version = "0.2.159" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "561d97a539a36e26a9a5fad1ea11a3039a67714694aaa379433e580854bc3dc5" +[[package]] +name = "linux-raw-sys" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" + [[package]] name = "memchr" version = "2.7.4" @@ -280,6 +308,12 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "quick-error" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" + [[package]] name = "quote" version = "1.0.37" @@ -326,11 +360,37 @@ dependencies = [ "clap", "openssl", "rand", + "rusty-fork", "serde", "sntpc", "toml", ] +[[package]] +name = "rustix" +version = "0.38.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8acb788b847c24f28525660c4d7758620a7210875711f79e7f663cc152726811" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys 0.52.0", +] + +[[package]] +name = "rusty-fork" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb3dcc6e454c328bb824492db107ab7c0ae8fcffe4ad210136ef014458c1bc4f" +dependencies = [ + "fnv", + "quick-error", + "tempfile", + "wait-timeout", +] + [[package]] name = "serde" version = "1.0.210" @@ -392,6 +452,19 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "tempfile" +version = "3.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0f2c9fc62d0beef6951ccffd757e241266a2c833136efbe35af6cd2567dca5b" +dependencies = [ + "cfg-if", + "fastrand", + "once_cell", + "rustix", + "windows-sys 0.59.0", +] + [[package]] name = "toml" version = "0.8.19" @@ -444,6 +517,15 @@ version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" +[[package]] +name = "wait-timeout" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f200f5b12eb75f8c1ed65abd4b2db8a6e1b138a20de009dacee265a2498f3f6" +dependencies = [ + "libc", +] + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" @@ -459,6 +541,15 @@ dependencies = [ "windows-targets", ] +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets", +] + [[package]] name = "windows-targets" version = "0.52.6" diff --git a/Cargo.toml b/Cargo.toml index f6bb350..0761a29 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ release-build = ["openssl/vendored"] [dev-dependencies] rand = "0.8.5" +rusty-fork = "0.3.0" [profile.release] opt-level = "z" # Optimize for binary size diff --git a/src/config_server.rs b/src/config_server.rs index 16c8619..4230ec0 100644 --- a/src/config_server.rs +++ b/src/config_server.rs @@ -175,6 +175,26 @@ mod tests { default_config_path, default_ntp, default_socket_group, default_socket_user, ConfigServer, }; use std::collections::HashMap; + use std::path::PathBuf; + + #[test] + fn test_get_pem_files() { + let expected: Vec = vec![]; + assert_eq!(ConfigServer::get_pem_files(&PathBuf::from("/foo/bar/baz")), expected); + } + + #[test] + fn test_get_pem_path() { + let config_server = ConfigServer { + config_dir: PathBuf::from("/foo/bar/baz"), + ..Default::default() + }; + + assert_eq!( + config_server.get_pem_path().unwrap_err().to_string(), + r#"Could not find any .pem files in "/foo/bar/baz""# + ); + } #[test] fn test_create_deserialize() { diff --git a/tests/server_test.rs b/tests/server_test.rs index 782efe3..18dc61e 100644 --- a/tests/server_test.rs +++ b/tests/server_test.rs @@ -2,26 +2,35 @@ mod tests { use ruroco::config_server::ConfigServer; use ruroco::server::Server; + use rusty_fork::rusty_fork_test; use std::env; use std::path::PathBuf; - #[test] - fn test_create_invalid_pid() { - env::set_var("LISTEN_PID", "12345"); - - let config_dir = - env::current_dir().unwrap_or(PathBuf::from("/tmp")).join("tests").join("conf_dir"); - - let result = Server::create( - ConfigServer { - config_dir, - ..Default::default() - }, - None, - ); - - assert!(result.is_err()); - assert_eq!(result.err().unwrap(), "LISTEN_PID was set, but not to our PID"); + rusty_fork_test! { + #[test] + fn test_create_server_udp_socket() { + let result = ConfigServer::default().create_server_udp_socket(None).unwrap(); + assert_eq!(format!("{result:?}"), "UdpSocket { addr: [::]:34020, fd: 3 }"); + } + + #[test] + fn test_create_invalid_pid() { + env::set_var("LISTEN_PID", "12345"); + + let config_dir = + env::current_dir().unwrap_or(PathBuf::from("/tmp")).join("tests").join("conf_dir"); + + let result = Server::create( + ConfigServer { + config_dir, + ..Default::default() + }, + None, + ); + + assert!(result.is_err()); + assert_eq!(result.err().unwrap(), "LISTEN_PID was set, but not to our PID"); + } } #[test]