Skip to content

Ability to capture different input paramters per function #108

@ffguven

Description

@ffguven

Hi,

I'm trying to write the unit test below by using injectorpp:

#[test]
    fn mount_happy() {
        const ROOTFS_PATH_STR: &str = "/tmp/mock-root-fs";

        let rootfs_path = Path::new(ROOTFS_PATH_STR);
        // let mount_list = oci_spec::runtime::get_default_mounts();
  
    let mut m_proc = Mount::default();
    m_proc.set_destination(PathBuf::from("/proc"));
    m_proc.set_typ(Some("proc".to_string()));
    m_proc.set_source(Some(PathBuf::from("proc")));
    m_proc.set_options(None);

    let mut m_dev = Mount::default();
    m_dev.set_destination(PathBuf::from("/dev"));
    m_dev.set_typ(Some("tmpfs".to_string()));
    m_dev.set_source(Some(PathBuf::from("tmpfs")));
    m_dev.set_options(Some(vec![
        "nosuid".into(),
        "strictatime".into(),
        "mode=755".into(),
        "size=65536k".into(),
    ]));

    let mount_list = vec![
        m_proc
    ];
        let mut injector = InjectorPP::new();

        injector
            .when_called(injectorpp::func!(
                nix::mount::mount,
                fn(
                    Option<&std::ffi::OsStr>,
                    &std::ffi::OsStr,
                    Option<&str>,
                    MsFlags,
                    Option<&str>,
                ) -> nix::Result<()>
            ))
            .will_execute(injectorpp::fake!(
                func_type: fn(source: Option<&std::ffi::OsStr>, target: &std::ffi::OsStr, fstype: Option<&str>, flags: MsFlags, data: Option<&str>) -> nix::Result<()>,
                when: source.unwrap().to_str().unwrap() == "proc" && 
                    target.to_str() == PathBuf::new().join(ROOTFS_PATH_STR).join("proc").to_str() && 
                    fstype.unwrap() == "proc" && 
                    flags == MsFlags::MS_NOEXEC | MsFlags::MS_NOSUID | MsFlags::MS_NODEV && 
                    data.is_none(),
                returns: Ok(()),
                times: 1
            ));

            injector
            .when_called(injectorpp::func!(
                nix::mount::mount,
                fn(
                    Option<&std::ffi::OsStr>,
                    &std::ffi::OsStr,
                    Option<&str>,
                    MsFlags,
                    Option<&str>,
                ) -> nix::Result<()>
            ))
            .will_execute(injectorpp::fake!(
                func_type: fn(source: Option<&std::ffi::OsStr>, target: &std::ffi::OsStr, fstype: Option<&str>, flags: MsFlags, data: Option<&str>) -> nix::Result<()>,
                when: source.unwrap().to_str().unwrap() == "tmpfs" && 
                    target.to_str() == PathBuf::new().join(ROOTFS_PATH_STR).join("dev").to_str() && 
                    fstype.unwrap() == "tmpfs" && 
                    flags == MsFlags::MS_NOSUID | MsFlags::MS_STRICTATIME && 
                    data.unwrap().contains("mode=755") && data.unwrap().contains("size=65536k"),
                returns: Ok(())
            ));

        let result = mount(rootfs_path, &mount_list);
        assert!(result.is_ok(), "mount() must succeed!, err: {:?}", result);
    }

See that I'm trying to capture whether the function nix::mount::mount is called with different input parameters or not.

The stack trace I'm getting is below:

thread 'linux_util::namespace::mount::tests::mount_happy' panicked at src/linux_util/namespace/mount.rs:228:27:
Fake function called with unexpected arguments
stack backtrace:
   0: __rustc::rust_begin_unwind
             at /rustc/1159e78c4747b02ef996e55082b704c09b970588/library/std/src/panicking.rs:697:5
   1: core::panicking::panic_fmt
             at /rustc/1159e78c4747b02ef996e55082b704c09b970588/library/core/src/panicking.rs:75:14
   2: nucleus::linux_util::namespace::mount::tests::mount_happy::fake
             at /home/fatih/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/injectorpp-0.4.0/src/interface/macros.rs:326:18
   3: nucleus::linux_util::namespace::mount::mount
             at ./src/linux_util/namespace/mount.rs:76:9
   4: nucleus::linux_util::namespace::mount::tests::mount_happy
             at ./src/linux_util/namespace/mount.rs:238:22
   5: nucleus::linux_util::namespace::mount::tests::mount_happy::{{closure}}
             at ./src/linux_util/namespace/mount.rs:167:21
   6: core::ops::function::FnOnce::call_once
             at /home/fatih/.rustup/toolchains/stable-aarch64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:253:5
   7: core::ops::function::FnOnce::call_once
             at /rustc/1159e78c4747b02ef996e55082b704c09b970588/library/core/src/ops/function.rs:253:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
test linux_util::namespace::mount::tests::mount_happy ... FAILED

See that Fake function called with unexpected arguments.

It's all ok when I comment out the will_execute call for the same function but to match different parameters within the when clause.

I was checking the will_execute unit tests you have as well, but I didn't see that you try to capture the function calls for the same signature but with different parameters.

Wondering if I'm missing something or such a feature is not supported by the library?

Thanks a lot for the package! It's super dope!

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions