diff options
author | Carl Lerche <me@carllerche.com> | 2015-02-19 13:35:45 -0800 |
---|---|---|
committer | Carl Lerche <me@carllerche.com> | 2015-02-19 19:30:19 -0800 |
commit | fd45c22f40cc6a0073f791acdd92779a80f33d9b (patch) | |
tree | 336530c67cadc946d198cd609fa1d115f11bb6fe | |
parent | 62040df28f8b677f0202bc95af61286adf833776 (diff) | |
download | nix-fd45c22f40cc6a0073f791acdd92779a80f33d9b.zip |
Test errno defines
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Cargo.toml | 4 | ||||
-rw-r--r-- | nix-test/Cargo.toml | 8 | ||||
-rw-r--r-- | nix-test/Makefile | 10 | ||||
-rw-r--r-- | nix-test/build.rs | 32 | ||||
-rw-r--r-- | nix-test/src/errno.c | 190 | ||||
-rw-r--r-- | nix-test/src/lib.rs | 29 | ||||
-rw-r--r-- | src/errno.rs | 207 | ||||
-rw-r--r-- | src/lib.rs | 3 |
9 files changed, 484 insertions, 0 deletions
@@ -1,3 +1,4 @@ Cargo.lock target/ *.sw* +*.a @@ -13,3 +13,7 @@ bitflags = "0.1" [dev-dependencies] rand = "0.1.2" + +[dev-dependencies.nix-test] +path = "nix-test" +version = "*" diff --git a/nix-test/Cargo.toml b/nix-test/Cargo.toml new file mode 100644 index 00000000..2d581b40 --- /dev/null +++ b/nix-test/Cargo.toml @@ -0,0 +1,8 @@ +[package] + +name = "nix-test" +description = "Testing helpers for Nix" +version = "0.0.1" +authors = ["Carl Lerche <me@carllerche.com>"] +homepage = "https://github.com/carllerche/nix-rust" +build = "build.rs" diff --git a/nix-test/Makefile b/nix-test/Makefile new file mode 100644 index 00000000..96bee792 --- /dev/null +++ b/nix-test/Makefile @@ -0,0 +1,10 @@ +OUT = libnixtest.a +CFLAGS = -fPIC -D$(OS) +OBJS = errno.o + +$(OUT): $(OBJS) + ar -rcs $@ $^ + +clean: + rm -f $(OBJS) + rm -f $(OUT) diff --git a/nix-test/build.rs b/nix-test/build.rs new file mode 100644 index 00000000..cb9cae9b --- /dev/null +++ b/nix-test/build.rs @@ -0,0 +1,32 @@ +#![feature(env, process)] + +use std::env; +use std::process::Command; + +pub fn main() { + let root = env::var("CARGO_MANIFEST_DIR").unwrap(); + let make = root.clone() + "/Makefile"; + let src = root.clone() + "/src"; + let out = env::var("OUT_DIR").unwrap(); + let target = env::var("TARGET").unwrap(); + + let os = if target.contains("linux") { + "LINUX" + } else if target.contains("darwin") { + "DARWIN" + } else { + "UNKNOWN" + }; + + let res = Command::new("make") + .arg("-f").arg(&make) + .current_dir(&out) + .env("VPATH", &src) + .env("OS", os) + .spawn().unwrap() + .wait().unwrap(); + + assert!(res.success()); + + println!("cargo:rustc-flags=-L {}/", out); +} diff --git a/nix-test/src/errno.c b/nix-test/src/errno.c new file mode 100644 index 00000000..9e4817dc --- /dev/null +++ b/nix-test/src/errno.c @@ -0,0 +1,190 @@ +#include <stdio.h> +#include <string.h> +#include <errno.h> + +#define ERRNO_EQ(ERR) \ + do { \ + if (0 == strcmp(err, #ERR)) { \ + return ERR; \ + } \ + } while (0) + +int +assert_errno_eq(const char* err) { + ERRNO_EQ(EPERM); + ERRNO_EQ(ENOENT); + ERRNO_EQ(ESRCH); + ERRNO_EQ(EINTR); + ERRNO_EQ(EIO); + ERRNO_EQ(ENXIO); + ERRNO_EQ(E2BIG); + ERRNO_EQ(ENOEXEC); + ERRNO_EQ(EBADF); + ERRNO_EQ(ECHILD); + ERRNO_EQ(EAGAIN); + ERRNO_EQ(ENOMEM); + ERRNO_EQ(EACCES); + ERRNO_EQ(EFAULT); + ERRNO_EQ(ENOTBLK); + ERRNO_EQ(EBUSY); + ERRNO_EQ(EEXIST); + ERRNO_EQ(EXDEV); + ERRNO_EQ(ENODEV); + ERRNO_EQ(ENOTDIR); + ERRNO_EQ(EISDIR); + ERRNO_EQ(EINVAL); + ERRNO_EQ(ENFILE); + ERRNO_EQ(EMFILE); + ERRNO_EQ(ENOTTY); + ERRNO_EQ(ETXTBSY); + ERRNO_EQ(EFBIG); + ERRNO_EQ(ENOSPC); + ERRNO_EQ(ESPIPE); + ERRNO_EQ(EROFS); + ERRNO_EQ(EMLINK); + ERRNO_EQ(EPIPE); + ERRNO_EQ(EDOM); + ERRNO_EQ(ERANGE); + ERRNO_EQ(EDEADLK); + ERRNO_EQ(ENAMETOOLONG); + ERRNO_EQ(ENOLCK); + ERRNO_EQ(ENOSYS); + ERRNO_EQ(ENOTEMPTY); + ERRNO_EQ(ELOOP); + ERRNO_EQ(ENOMSG); + ERRNO_EQ(EIDRM); + ERRNO_EQ(EINPROGRESS); + ERRNO_EQ(EALREADY); + ERRNO_EQ(ENOTSOCK); + ERRNO_EQ(EDESTADDRREQ); + ERRNO_EQ(EMSGSIZE); + ERRNO_EQ(EPROTOTYPE); + ERRNO_EQ(ENOPROTOOPT); + ERRNO_EQ(EPROTONOSUPPORT); + ERRNO_EQ(ESOCKTNOSUPPORT); + ERRNO_EQ(EPFNOSUPPORT); + ERRNO_EQ(EAFNOSUPPORT); + ERRNO_EQ(EADDRINUSE); + ERRNO_EQ(EADDRNOTAVAIL); + ERRNO_EQ(ENETDOWN); + ERRNO_EQ(ENETUNREACH); + ERRNO_EQ(ENETRESET); + ERRNO_EQ(ECONNABORTED); + ERRNO_EQ(ECONNRESET); + ERRNO_EQ(ENOBUFS); + ERRNO_EQ(EISCONN); + ERRNO_EQ(ENOTCONN); + ERRNO_EQ(ESHUTDOWN); + ERRNO_EQ(ETOOMANYREFS); + ERRNO_EQ(ETIMEDOUT); + ERRNO_EQ(ECONNREFUSED); + ERRNO_EQ(EHOSTDOWN); + ERRNO_EQ(EHOSTUNREACH); + +#ifdef LINUX + ERRNO_EQ(ECHRNG); + ERRNO_EQ(EL2NSYNC); + ERRNO_EQ(EL3HLT); + ERRNO_EQ(EL3RST); + ERRNO_EQ(ELNRNG); + ERRNO_EQ(EUNATCH); + ERRNO_EQ(ENOCSI); + ERRNO_EQ(EL2HLT); + ERRNO_EQ(EBADE); + ERRNO_EQ(EBADR); + ERRNO_EQ(EXFULL); + ERRNO_EQ(ENOANO); + ERRNO_EQ(EBADRQC); + ERRNO_EQ(EBADSLT); + ERRNO_EQ(EBFONT); + ERRNO_EQ(ENOSTR); + ERRNO_EQ(ENODATA); + ERRNO_EQ(ETIME); + ERRNO_EQ(ENOSR); + ERRNO_EQ(ENONET); + ERRNO_EQ(ENOPKG); + ERRNO_EQ(EREMOTE); + ERRNO_EQ(ENOLINK); + ERRNO_EQ(EADV); + ERRNO_EQ(ESRMNT); + ERRNO_EQ(ECOMM); + ERRNO_EQ(EPROTO); + ERRNO_EQ(EMULTIHOP); + ERRNO_EQ(EDOTDOT); + ERRNO_EQ(EBADMSG); + ERRNO_EQ(EOVERFLOW); + ERRNO_EQ(ENOTUNIQ); + ERRNO_EQ(EBADFD); + ERRNO_EQ(EREMCHG); + ERRNO_EQ(ELIBACC); + ERRNO_EQ(ELIBBAD); + ERRNO_EQ(ELIBSCN); + ERRNO_EQ(ELIBMAX); + ERRNO_EQ(ELIBEXEC); + ERRNO_EQ(EILSEQ); + ERRNO_EQ(ERESTART); + ERRNO_EQ(ESTRPIPE); + ERRNO_EQ(EUSERS); + ERRNO_EQ(EOPNOTSUPP); + ERRNO_EQ(ESTALE); + ERRNO_EQ(EUCLEAN); + ERRNO_EQ(ENOTNAM); + ERRNO_EQ(ENAVAIL); + ERRNO_EQ(EISNAM); + ERRNO_EQ(EREMOTEIO); + ERRNO_EQ(EDQUOT); + ERRNO_EQ(ENOMEDIUM); + ERRNO_EQ(EMEDIUMTYPE); + ERRNO_EQ(ECANCELED); + ERRNO_EQ(ENOKEY); + ERRNO_EQ(EKEYEXPIRED); + ERRNO_EQ(EKEYREVOKED); + ERRNO_EQ(EKEYREJECTED); + ERRNO_EQ(EOWNERDEAD); + ERRNO_EQ(ENOTRECOVERABLE); + ERRNO_EQ(ERFKILL); + ERRNO_EQ(EHWPOISON); +#endif + +#ifdef DARWIN + ERRNO_EQ(ENOTSUP); + ERRNO_EQ(EPROCLIM); + ERRNO_EQ(EUSERS); + ERRNO_EQ(EDQUOT); + ERRNO_EQ(ESTALE); + ERRNO_EQ(EREMOTE); + ERRNO_EQ(EBADRPC); + ERRNO_EQ(ERPCMISMATCH); + ERRNO_EQ(EPROGUNAVAIL); + ERRNO_EQ(EPROGMISMATCH); + ERRNO_EQ(EPROCUNAVAIL); + ERRNO_EQ(EFTYPE); + ERRNO_EQ(EAUTH); + ERRNO_EQ(ENEEDAUTH); + ERRNO_EQ(EPWROFF); + ERRNO_EQ(EDEVERR); + ERRNO_EQ(EOVERFLOW); + ERRNO_EQ(EBADEXEC); + ERRNO_EQ(EBADARCH); + ERRNO_EQ(ESHLIBVERS); + ERRNO_EQ(EBADMACHO); + ERRNO_EQ(ECANCELED); + ERRNO_EQ(EILSEQ); + ERRNO_EQ(ENOATTR); + ERRNO_EQ(EBADMSG); + ERRNO_EQ(EMULTIHOP); + ERRNO_EQ(ENODATA); + ERRNO_EQ(ENOLINK); + ERRNO_EQ(ENOSR); + ERRNO_EQ(ENOSTR); + ERRNO_EQ(EPROTO); + ERRNO_EQ(ETIME); + ERRNO_EQ(EOPNOTSUPP); + ERRNO_EQ(ENOPOLICY); + ERRNO_EQ(ENOTRECOVERABLE); + ERRNO_EQ(EOWNERDEAD); + ERRNO_EQ(EQFULL); +#endif + + return 0; +} diff --git a/nix-test/src/lib.rs b/nix-test/src/lib.rs new file mode 100644 index 00000000..a8be2feb --- /dev/null +++ b/nix-test/src/lib.rs @@ -0,0 +1,29 @@ +#![feature(libc, std_misc)] + +extern crate libc; + +use std::ffi::CString; +use libc::{c_int}; + +mod ffi { + use libc::{c_int, c_char}; + + #[link(name = "nixtest", kind = "static")] + extern { + pub fn assert_errno_eq(errno: *const c_char) -> c_int; + } +} + +pub fn assert_errno_eq(err: &str, val: c_int) { + unsafe { + let name = CString::from_slice(err.as_bytes()); + let actual = ffi::assert_errno_eq(name.as_ptr()); + + assert!(actual > 0); + + if val != actual { + panic!("incorrect value for errno {}; got={}; expected={}", + err, val, actual); + } + } +} diff --git a/src/errno.rs b/src/errno.rs index 35809f9b..cc6d750e 100644 --- a/src/errno.rs +++ b/src/errno.rs @@ -659,3 +659,210 @@ mod consts { pub const EL2NSYNC: Errno = Errno::UnknownErrno; } + +#[cfg(test)] +mod test { + use super::*; + use nixtest::assert_errno_eq; + use libc::c_int; + + macro_rules! check_errno { + ($errno:ident) => {{ + assert_errno_eq(stringify!($errno), $errno as c_int); + }}; + + ($errno:ident, $($rest:ident),+) => {{ + check_errno!($errno); + check_errno!($($rest),*); + }}; + } + + #[test] + pub fn test_errno_values() { + check_errno!( + EPERM, + ENOENT, + ESRCH, + EINTR, + EIO, + ENXIO, + E2BIG, + ENOEXEC, + EBADF, + ECHILD, + EAGAIN, + ENOMEM, + EACCES, + EFAULT, + ENOTBLK, + EBUSY, + EEXIST, + EXDEV, + ENODEV, + ENOTDIR, + EISDIR, + EINVAL, + ENFILE, + EMFILE, + ENOTTY, + ETXTBSY, + EFBIG, + ENOSPC, + ESPIPE, + EROFS, + EMLINK, + EPIPE, + EDOM, + ERANGE, + EDEADLK, + ENAMETOOLONG, + ENOLCK, + ENOSYS, + ENOTEMPTY, + ELOOP, + ENOMSG, + EIDRM); + + check_errno!( + EINPROGRESS, + EALREADY, + ENOTSOCK, + EDESTADDRREQ, + EMSGSIZE, + EPROTOTYPE, + ENOPROTOOPT, + EPROTONOSUPPORT, + ESOCKTNOSUPPORT, + EPFNOSUPPORT, + EAFNOSUPPORT, + EADDRINUSE, + EADDRNOTAVAIL, + ENETDOWN, + ENETUNREACH, + ENETRESET, + ECONNABORTED, + ECONNRESET, + ENOBUFS, + EISCONN, + ENOTCONN, + ESHUTDOWN, + ETOOMANYREFS, + ETIMEDOUT, + ECONNREFUSED, + EHOSTDOWN, + EHOSTUNREACH); + } + + #[test] + #[cfg(target_os = "linux")] + pub fn test_linux_errnos() { + check_errno!( + ECHRNG, + EL2NSYNC, + EL3HLT, + EL3RST, + ELNRNG, + EUNATCH, + ENOCSI, + EL2HLT, + EBADE, + EBADR, + EXFULL, + ENOANO, + EBADRQC, + EBADSLT, + EBFONT, + ENOSTR, + ENODATA, + ETIME, + ENOSR, + ENONET, + ENOPKG, + EREMOTE, + ENOLINK, + EADV, + ESRMNT, + ECOMM, + EPROTO, + EMULTIHOP, + EDOTDOT, + EBADMSG, + EOVERFLOW, + ENOTUNIQ, + EBADFD, + EREMCHG, + ELIBACC, + ELIBBAD, + ELIBSCN, + ELIBMAX, + ELIBEXEC, + EILSEQ, + ERESTART, + ESTRPIPE, + EUSERS, + EOPNOTSUPP, + ESTALE, + EUCLEAN, + ENOTNAM, + ENAVAIL, + EISNAM, + EREMOTEIO, + EDQUOT, + ENOMEDIUM, + EMEDIUMTYPE, + ECANCELED, + ENOKEY, + EKEYEXPIRED, + EKEYREVOKED, + EKEYREJECTED, + EOWNERDEAD, + ENOTRECOVERABLE, + ERFKILL, + EHWPOISON); + } + + + #[test] + #[cfg(target_os = "macos")] + pub fn test_darwin_errnos() { + check_errno!( + ENOTSUP, + EPROCLIM, + EUSERS, + EDQUOT, + ESTALE, + EREMOTE, + EBADRPC, + ERPCMISMATCH, + EPROGUNAVAIL, + EPROGMISMATCH, + EPROCUNAVAIL, + EFTYPE, + EAUTH, + ENEEDAUTH, + EPWROFF, + EDEVERR, + EOVERFLOW, + EBADEXEC, + EBADARCH, + ESHLIBVERS, + EBADMACHO, + ECANCELED, + EILSEQ, + ENOATTR, + EBADMSG, + EMULTIHOP, + ENODATA, + ENOLINK, + ENOSR, + ENOSTR, + EPROTO, + ETIME, + EOPNOTSUPP, + ENOPOLICY, + ENOTRECOVERABLE, + EOWNERDEAD, + EQFULL + ); + } +} @@ -9,6 +9,9 @@ extern crate bitflags; extern crate libc; extern crate core; +#[cfg(test)] +extern crate "nix-test" as nixtest; + // Re-export some libc constants pub use libc::{c_int, c_void}; |