Skip to content

renamed test methods for mqueue and added mq_unlink #167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/mqueue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ mod ffi {

pub fn mq_close (mqd: MQd) -> c_int;

pub fn mq_unlink(name: *const c_char) -> c_int;

pub fn mq_receive (mqd: MQd, msg_ptr: *const c_char, msg_len: size_t, msq_prio: *const c_uint) -> ssize_t;

pub fn mq_send (mqd: MQd, msg_ptr: *const c_char, msg_len: size_t, msq_prio: c_uint) -> c_int;
Expand Down Expand Up @@ -78,6 +80,11 @@ pub fn mq_open(name: &CString, oflag: MQ_OFlag, mode: Mode, attr: &MqAttr) -> Re
Ok(res)
}

pub fn mq_unlink(name: &CString) -> Result<()> {
let res = unsafe { ffi::mq_unlink(name.as_ptr()) };
from_ffi(res)
}

pub fn mq_close(mqdes: MQd) -> Result<()> {
let res = unsafe { ffi::mq_close(mqdes) };
from_ffi(res)
Expand Down
30 changes: 25 additions & 5 deletions test/test_mq.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use nix::mqueue::{mq_open, mq_close, mq_send, mq_receive, mq_getattr};
use nix::mqueue::{mq_open, mq_close, mq_send, mq_receive, mq_getattr, mq_unlink};
use nix::mqueue::{O_CREAT, O_WRONLY, O_RDONLY};
use nix::mqueue::MqAttr;
use nix::sys::stat::{S_IWUSR, S_IRUSR, S_IRGRP, S_IROTH};
Expand All @@ -9,11 +9,11 @@ use libc::c_long;
use nix::unistd::{fork, read, write, pipe};
use nix::unistd::Fork::{Child, Parent};
use nix::sys::wait::*;


use nix::errno::Errno::*;
use nix::Error::Sys;

#[test]
fn mq_send_and_receive() {
fn test_mq_send_and_receive() {

const MSG_SIZE: c_long = 32;
let attr = MqAttr::new(0, 10, MSG_SIZE, 0);
Expand Down Expand Up @@ -53,7 +53,7 @@ fn mq_send_and_receive() {


#[test]
fn mq_get_attr() {
fn test_mq_get_attr() {
const MSG_SIZE: c_long = 32;
let initial_attr = MqAttr::new(0, 10, MSG_SIZE, 0);
let mq_name = &CString::new("/attr_test_get_attr".as_bytes().as_ref()).unwrap();
Expand All @@ -62,3 +62,23 @@ fn mq_get_attr() {
assert!(read_attr.unwrap() == initial_attr);
mq_close(mqd).unwrap();
}

#[test]
fn test_mq_unlink() {
const MSG_SIZE: c_long = 32;
let initial_attr = MqAttr::new(0, 10, MSG_SIZE, 0);
let mq_name_opened = &CString::new("/mq_unlink_test".as_bytes().as_ref()).unwrap();
let mq_name_not_opened = &CString::new("/mq_unlink_test".as_bytes().as_ref()).unwrap();
let mqd = mq_open(mq_name_opened, O_CREAT | O_WRONLY, S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH, &initial_attr).unwrap();

let res_unlink = mq_unlink(mq_name_opened);
assert!(res_unlink == Ok(()) );

let res_unlink_not_opened = mq_unlink(mq_name_not_opened);
assert!(res_unlink_not_opened == Err(Sys(ENOENT)) );

mq_close(mqd).unwrap();
let res_unlink_after_close = mq_unlink(mq_name_opened);
assert!(res_unlink_after_close == Err(Sys(ENOENT)) );

}