diff options
author | Markus Jais <markusjais@gmx.de> | 2015-05-16 10:45:01 +0200 |
---|---|---|
committer | Markus Jais <markusjais@gmx.de> | 2015-05-16 10:45:01 +0200 |
commit | a0073d1f9f7c98a0dcef84fe5c152340906a825c (patch) | |
tree | 683f9770111c57dbd0e0a1bb9bf1341c76af1082 | |
parent | c4a9598ec16a01e11e66bea1eb34cea9b4646326 (diff) | |
download | nix-a0073d1f9f7c98a0dcef84fe5c152340906a825c.zip |
cleaned up mq unit tests
-rw-r--r-- | test/test_mq.rs | 19 |
1 files changed, 6 insertions, 13 deletions
diff --git a/test/test_mq.rs b/test/test_mq.rs index 6d03bd06..a320a52c 100644 --- a/test/test_mq.rs +++ b/test/test_mq.rs @@ -3,13 +3,15 @@ use nix::mq::{O_CREAT, O_WRONLY, O_RDONLY}; use nix::mq::MqAttr; use nix::sys::stat::{S_IWUSR, S_IRUSR, S_IRGRP, S_IROTH}; use std::ffi::CString; -use libc::{c_long, strlen}; +use std::str; +use libc::c_long; use nix::unistd::{fork, read, write, pipe}; use nix::unistd::Fork::{Child, Parent}; use nix::sys::wait::*; + #[test] fn mq_send_and_receive() { @@ -19,7 +21,6 @@ fn mq_send_and_receive() { let mq_name_in_parent = &CString::new(b"/a_nix_test_queue".as_ref()).unwrap(); let mqd_in_parent = mq_open(mq_name_in_parent, O_CREAT | O_WRONLY, S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH, &attr).unwrap(); let msg_to_send = &CString::new("msg_1").unwrap(); - let len = unsafe { strlen(msg_to_send.as_ptr()) as usize }; mq_send(mqd_in_parent, msg_to_send, 1).unwrap(); @@ -31,11 +32,7 @@ fn mq_send_and_receive() { let mq_name_in_child = &CString::new(b"/a_nix_test_queue".as_ref()).unwrap(); let mqd_in_child = mq_open(mq_name_in_child, O_CREAT | O_RDONLY, S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH, &attr).unwrap(); let mut buf = [0u8; 32]; - let length_msg_received = mq_receive(mqd_in_child, &mut buf, 1).unwrap(); - assert!(length_msg_received == len); - let message_str = String::from_utf8_lossy(&buf[0 .. len]); - let expected_str = String::from_utf8_lossy(msg_to_send.as_bytes()); - assert!(message_str == expected_str); + mq_receive(mqd_in_child, &mut buf, 1).unwrap(); write(writer, &buf).unwrap(); // pipe result to parent process. Otherwise cargo does not report test failures correctly mq_close(mqd_in_child).unwrap(); } @@ -47,14 +44,10 @@ fn mq_send_and_receive() { // Read 1024 bytes. let mut read_buf = [0u8; 32]; read(reader, &mut read_buf).unwrap(); - let message_str = String::from_utf8_lossy(&read_buf); - assert!(message_str.contains("msg_1")); + let message_str = str::from_utf8(&read_buf).unwrap(); + assert_eq!(&message_str[.. message_str.char_indices().nth(5).unwrap().0], "msg_1"); }, // panic, fork should never fail unless there is a serious problem with the OS Err(_) => panic!("Error: Fork Failed") } } - - - -// cargo clean; cargo test -- --nocapture |