From c2e8670b374894ec8e757449940e212c49ca7f0c Mon Sep 17 00:00:00 2001 From: higuruchi Date: Sun, 21 Jan 2024 17:57:02 +0900 Subject: [PATCH] Add domainname test (#1544) * Add domainname test Signed-off-by: higuruchi * Fix error message from binary to string Signed-off-by: higuruchi * Update runc binary to v1.1.8 from 1.1.0 for domainname test Signed-off-by: higuruchi * Move missing domainname test Signed-off-by: Yashodhan Joshi * Add TODO comments for self-notes Signed-off-by: Yashodhan Joshi --------- Signed-off-by: higuruchi Signed-off-by: Yashodhan Joshi Co-authored-by: Yashodhan Joshi --- Cargo.lock | 37 ++++++++++--------- tests/contest/contest/src/main.rs | 4 ++ .../contest/src/tests/domainname/mod.rs | 32 ++++++++++++++++ tests/contest/contest/src/tests/mod.rs | 1 + tests/contest/runtimetest/Cargo.toml | 1 + tests/contest/runtimetest/src/main.rs | 1 + tests/contest/runtimetest/src/tests.rs | 30 +++++++++++++++ 7 files changed, 88 insertions(+), 18 deletions(-) create mode 100644 tests/contest/contest/src/tests/domainname/mod.rs diff --git a/Cargo.lock b/Cargo.lock index f099bc27..4109eaa5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -444,9 +444,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.4.14" +version = "4.4.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "33e92c5c1a78c62968ec57dbc2440366a2d6e5a23faf829970ff1585dc6b18e2" +checksum = "1e578d6ec4194633722ccf9544794b71b1385c3c027efe0c55db226fc880865c" dependencies = [ "clap_builder", "clap_derive", @@ -454,9 +454,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.4.14" +version = "4.4.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f4323769dc8a61e2c39ad7dc26f6f2800524691a44d74fe3d1071a5c24db6370" +checksum = "4df4df40ec50c46000231c914968278b1eb05098cf8f1b3a518a95030e71d1c7" dependencies = [ "anstream", "anstyle", @@ -466,9 +466,9 @@ dependencies = [ [[package]] name = "clap_complete" -version = "4.4.6" +version = "4.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97aeaa95557bd02f23fbb662f981670c3d20c5a26e69f7354b28f57092437fcd" +checksum = "eaf7dcb7c21d8ca1a2482ee0f1d341f437c9a7af6ca6da359dc5e1b164e98215" dependencies = [ "clap", ] @@ -2339,9 +2339,9 @@ checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" [[package]] name = "openssl" -version = "0.10.62" +version = "0.10.63" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8cde4d2d9200ad5909f8dac647e29482e07c3a35de8a13fce7c9c7747ad9f671" +checksum = "15c9d69dd87a29568d4d017cfe8ec518706046a05184e5aea92d0af890b803c8" dependencies = [ "bitflags 2.4.2", "cfg-if", @@ -2371,9 +2371,9 @@ checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" [[package]] name = "openssl-sys" -version = "0.9.98" +version = "0.9.99" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1665caf8ab2dc9aef43d1c0023bd904633a6a05cb30b0ad59bec2ae986e57a7" +checksum = "22e1bf214306098e4832460f797824c05d25aacdf896f64a985fb0fd992454ae" dependencies = [ "cc", "libc", @@ -2656,9 +2656,9 @@ checksum = "dc375e1527247fe1a97d8b7156678dfe7c1af2fc075c9a4db3690ecd2a148068" [[package]] name = "proc-macro2" -version = "1.0.76" +version = "1.0.78" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95fc56cda0b5c3325f5fbbd7ff9fda9e02bb00bb3dac51252d2f1bfa1cb8cc8c" +checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae" dependencies = [ "unicode-ident", ] @@ -3076,6 +3076,7 @@ name = "runtimetest" version = "0.0.1" dependencies = [ "anyhow", + "libc", "nc", "nix 0.27.1", "oci-spec", @@ -3526,9 +3527,9 @@ checksum = "826167069c09b99d56f31e9ae5c99049e932a98c9dc2dac47645b08dbbf76ba7" [[package]] name = "smallvec" -version = "1.12.0" +version = "1.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2593d31f82ead8df961d8bd23a64c2ccf2eb5dd34b0a34bfb4dd54011c72009e" +checksum = "e6ecd384b10a64542d77071bd64bd7b231f4ed5940fba55e98c3de13824cf3d7" [[package]] name = "socket2" @@ -4206,9 +4207,9 @@ checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" [[package]] name = "uuid" -version = "1.6.1" +version = "1.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e395fcf16a7a3d8127ec99782007af141946b4795001f876d54fb0d55978560" +checksum = "f00cc9702ca12d3c81455259621e676d0f7251cec66a21e98fe2e9a37db93b2a" [[package]] name = "valuable" @@ -5690,9 +5691,9 @@ dependencies = [ [[package]] name = "xattr" -version = "1.2.0" +version = "1.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "914566e6413e7fa959cc394fb30e563ba80f3541fbd40816d4c05a0fc3f2a0f1" +checksum = "8da84f1a25939b27f6820d92aed108f83ff920fdf11a7b19366c27c4cda81d4f" dependencies = [ "libc", "linux-raw-sys", diff --git a/tests/contest/contest/src/main.rs b/tests/contest/contest/src/main.rs index 9b737384..a24e1740 100644 --- a/tests/contest/contest/src/main.rs +++ b/tests/contest/contest/src/main.rs @@ -1,6 +1,7 @@ mod tests; mod utils; +use crate::tests::domainname::get_domainname_tests; use crate::tests::example::get_example_test; use crate::tests::hooks::get_hooks_tests; use crate::tests::hostname::get_hostname_test; @@ -102,6 +103,8 @@ fn main() -> Result<()> { let ro_paths = get_ro_paths_test(); let hostname = get_hostname_test(); let mounts_recursive = get_mounts_recursive_test(); + #[allow(unused_variables)] + let domainname = get_domainname_tests(); let intel_rdt = get_intel_rdt_test(); let sysctl = get_sysctl_test(); #[allow(unused_variables)] @@ -124,6 +127,7 @@ fn main() -> Result<()> { tm.add_test_group(Box::new(ro_paths)); tm.add_test_group(Box::new(hostname)); tm.add_test_group(Box::new(mounts_recursive)); + // tm.add_test_group(Box::new(domainname)); // TODO (YJDoc2) fix in #2616 tm.add_test_group(Box::new(intel_rdt)); tm.add_test_group(Box::new(sysctl)); // tm.add_test_group(Box::new(scheduler)); diff --git a/tests/contest/contest/src/tests/domainname/mod.rs b/tests/contest/contest/src/tests/domainname/mod.rs new file mode 100644 index 00000000..1c0ff2d6 --- /dev/null +++ b/tests/contest/contest/src/tests/domainname/mod.rs @@ -0,0 +1,32 @@ +use crate::utils::test_inside_container; +use oci_spec::runtime::{ProcessBuilder, Spec, SpecBuilder}; +use test_framework::{Test, TestGroup, TestResult}; + +fn get_spec(domainname: &str) -> Spec { + SpecBuilder::default() + .domainname(domainname) + .process( + ProcessBuilder::default() + .args(vec![ + "runtimetest".to_string(), + "domainname_test".to_string(), + ]) + .build() + .expect("error in creating process config"), + ) + .build() + .unwrap() +} + +fn set_domainname_test() -> TestResult { + let spec = get_spec("domainname"); + test_inside_container(spec, &|_| Ok(())) +} + +pub fn get_domainname_tests() -> TestGroup { + let mut tg = TestGroup::new("domainname_test"); + let set_domainname_test = Test::new("set_domainname_test", Box::new(set_domainname_test)); + tg.add(vec![Box::new(set_domainname_test)]); + + tg +} diff --git a/tests/contest/contest/src/tests/mod.rs b/tests/contest/contest/src/tests/mod.rs index 099cf7ff..927fe07c 100644 --- a/tests/contest/contest/src/tests/mod.rs +++ b/tests/contest/contest/src/tests/mod.rs @@ -1,4 +1,5 @@ pub mod cgroups; +pub mod domainname; pub mod example; pub mod hooks; pub mod hostname; diff --git a/tests/contest/runtimetest/Cargo.toml b/tests/contest/runtimetest/Cargo.toml index 63dab30b..79e16fd3 100644 --- a/tests/contest/runtimetest/Cargo.toml +++ b/tests/contest/runtimetest/Cargo.toml @@ -7,5 +7,6 @@ edition = "2021" oci-spec = { version = "0.6.4", features = ["runtime"] } nix = "0.27.1" anyhow = "1.0" +libc = "0.2.139" # TODO (YJDoc2) upgrade to latest nc = "0.8.18" diff --git a/tests/contest/runtimetest/src/main.rs b/tests/contest/runtimetest/src/main.rs index 65e0ed5e..ff685141 100644 --- a/tests/contest/runtimetest/src/main.rs +++ b/tests/contest/runtimetest/src/main.rs @@ -33,6 +33,7 @@ fn main() { "readonly_paths" => tests::validate_readonly_paths(&spec), "set_host_name" => tests::validate_hostname(&spec), "mounts_recursive" => tests::validate_mounts_recursive(&spec), + "domainname_test" => tests::validate_domainname(&spec), "seccomp" => tests::validate_seccomp(&spec), "sysctl" => tests::validate_sysctl(&spec), "scheduler_policy_other" => tests::validate_scheduler_policy(&spec), diff --git a/tests/contest/runtimetest/src/tests.rs b/tests/contest/runtimetest/src/tests.rs index 20c501c2..ceda0ad8 100644 --- a/tests/contest/runtimetest/src/tests.rs +++ b/tests/contest/runtimetest/src/tests.rs @@ -1,8 +1,10 @@ use crate::utils::{self, test_read_access, test_write_access}; use anyhow::{bail, Result}; +use libc::getdomainname; use nix::errno::Errno; use nix::unistd::getcwd; use oci_spec::runtime::{LinuxSchedulerPolicy, Spec}; +use std::ffi::CStr; use std::fs::{self, read_dir}; use std::mem; use std::path::Path; @@ -82,6 +84,34 @@ pub fn validate_hostname(spec: &Spec) { } } +pub fn validate_domainname(spec: &Spec) { + if let Some(expected_domainname) = spec.domainname() { + if expected_domainname.is_empty() { + return; + } + + const MAX_DOMAINNAME_SIZE: usize = 254; + let actual_domainname: [i8; MAX_DOMAINNAME_SIZE] = [0; MAX_DOMAINNAME_SIZE]; + + // TODO (YJDoc2) : libc now has support for getdomainname, update this to use that + let ret = + unsafe { getdomainname(actual_domainname.as_ptr() as *mut i8, MAX_DOMAINNAME_SIZE) }; + if ret == -1 { + eprintln!("Failed to get domainname"); + } + + let actual_domainname_cstr = + unsafe { CStr::from_ptr(actual_domainname.as_ptr() as *mut i8) }; + if actual_domainname_cstr.to_str().unwrap() != expected_domainname { + eprintln!( + "Unexpected domainname, expected: {:?} found: {:?}", + expected_domainname, + actual_domainname_cstr.to_str().unwrap() + ); + } + } +} + // Run argument test recursively for files after base_dir fn do_test_mounts_recursive(base_dir: &Path, test_fn: &dyn Fn(&Path) -> Result<()>) -> Result<()> { let dirs = read_dir(base_dir).unwrap();