summary history branches tags files
commit:b05611473b7796b375ab1e804b0fa147c7f82693
author:Trevor Bentley
committer:Trevor Bentley
date:Sun Jan 13 20:12:41 2019 +0100
parents:ab0d628b9d012e3db2eebd13f09d54d2798f3fe2
Clean up test cases
diff --git a/Cargo.toml b/Cargo.toml
line changes: +1/-1
index 561f15b..a3ea4b9
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -12,7 +12,7 @@ opt-level = "z"
 debug = false
 rpath = false
 lto = true
-debug-assertsion = false
+debug-assertions = false
 codegen-units = 1
 panic = 'abort' #'unwind'
 incremental = true

diff --git a/src/clib.rs b/src/clib.rs
line changes: +30/-14
index 2318816..1b4168c
--- a/src/clib.rs
+++ b/src/clib.rs
@@ -115,7 +115,7 @@ mod tests {
     use std::io::{Read,Write};
     use std::net::{TcpListener, TcpStream};
     use crate::clib::*;
-    pub fn server() -> Result<(), std::io::Error> {
+    fn server() -> Result<(), std::io::Error> {
         let listener = TcpListener::bind("127.0.0.1:9989").unwrap();
         for stream in listener.incoming() {
             let mut stream: TcpStream = stream.unwrap();
@@ -138,29 +138,40 @@ mod tests {
 
             let out_buf: [u8; 256] = [0; 256];
             let mut plaintext: [u8; 256] = [0; 256];
-            plaintext[0..11].copy_from_slice("hello world".as_bytes());
+            plaintext[0..13].copy_from_slice("from server 1".as_bytes());
             ossuary_send_data(
                 conn,
-                (&plaintext) as *const u8 as *mut u8, 11 as u16,
+                (&plaintext) as *const u8 as *mut u8, 13 as u16,
                 (&out_buf) as *const u8 as *mut u8, out_buf.len() as u16);
             let _ = stream.write(&out_buf);
 
             let out_buf: [u8; 256] = [0; 256];
             let mut plaintext: [u8; 256] = [0; 256];
-            plaintext[0..13].copy_from_slice("goodbye world".as_bytes());
+            plaintext[0..13].copy_from_slice("from server 2".as_bytes());
             ossuary_send_data(
                 conn,
                 (&plaintext) as *const u8 as *mut u8, 13 as u16,
                 (&out_buf) as *const u8 as *mut u8, out_buf.len() as u16);
             let _ = stream.write(&out_buf);
 
+            let _ = stream.read(&mut in_buf);
+            let out_buf: [u8; 256] = [0; 256];
+            let len = ossuary_recv_data(
+                conn,
+                (&in_buf) as *const u8 as *mut u8, in_buf.len() as u16,
+                (&out_buf) as *const u8 as *mut u8, out_buf.len() as u16);
+            if len != -1 {
+                println!("CLIB READ: {:?}",
+                         std::str::from_utf8(&out_buf[0..len as usize]).unwrap());
+            }
+
             ossuary_destroy_connection(&mut conn);
             break;
         }
         Ok(())
     }
 
-    pub fn client() -> Result<(), std::io::Error> {
+    fn client() -> Result<(), std::io::Error> {
         let mut stream = TcpStream::connect("127.0.0.1:9989").unwrap();
         let mut conn = ossuary_create_connection(0);
 
@@ -179,6 +190,14 @@ mod tests {
             }
         }
 
+        let out_buf: [u8; 256] = [0; 256];
+        let mut plaintext: [u8; 256] = [0; 256];
+        plaintext[0..11].copy_from_slice("from client".as_bytes());
+        ossuary_send_data(
+            conn,
+            (&plaintext) as *const u8 as *mut u8, 11 as u16,
+            (&out_buf) as *const u8 as *mut u8, out_buf.len() as u16);
+        let _ = stream.write(&out_buf);
 
         let _ = stream.read(&mut in_buf);
         let out_buf: [u8; 256] = [0; 256];
@@ -187,7 +206,8 @@ mod tests {
             (&in_buf) as *const u8 as *mut u8, in_buf.len() as u16,
             (&out_buf) as *const u8 as *mut u8, out_buf.len() as u16);
         if len != -1 {
-            println!("FFI RECEIVED: {:?}", std::str::from_utf8(&out_buf[0..len as usize]));
+            println!("CLIB READ: {:?}",
+                     std::str::from_utf8(&out_buf[0..len as usize]).unwrap());
         }
 
         let _ = stream.read(&mut in_buf);
@@ -197,21 +217,17 @@ mod tests {
             (&in_buf) as *const u8 as *mut u8, in_buf.len() as u16,
             (&out_buf) as *const u8 as *mut u8, out_buf.len() as u16);
         if len != -1 {
-            println!("FFI RECEIVED: {:?}", std::str::from_utf8(&out_buf[0..len as usize]));
+            println!("CLIB READ: {:?}",
+                     std::str::from_utf8(&out_buf[0..len as usize]).unwrap());
         }
 
         ossuary_destroy_connection(&mut conn);
         Ok(())
     }
-    pub fn test() {
-        println!("FFI START");
+    #[test]
+    fn test() {
         thread::spawn(move || { let _ = server(); });
         let child = thread::spawn(move || { let _ = client(); });
         let _ = child.join();
-        println!("FFI DONE");
-    }
-    #[test]
-    fn it_works() {
-        test();
     }
 }

diff --git a/src/lib.rs b/src/lib.rs
line changes: +16/-21
index 6ddbf09..1204a7f
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -498,11 +498,12 @@ where T: std::ops::DerefMut<Target = U>,
 #[cfg(test)]
 mod tests {
     use std::thread;
-    use std::time;
     use std::net::{TcpListener, TcpStream};
     use crate::*;
 
-    fn event_loop<T>(mut conn: ConnectionContext, mut stream: T, is_server: bool) -> Result<(), std::io::Error>
+    fn event_loop<T>(mut conn: ConnectionContext,
+                     mut stream: T,
+                     is_server: bool) -> Result<(), std::io::Error>
     where T: std::io::Read + std::io::Write {
         while crypto_handshake_done(&conn) == false {
             if crypto_send_handshake(&mut conn, &mut stream) {
@@ -511,24 +512,22 @@ mod tests {
         }
 
         if is_server {
-            let mut plaintext = "hello, world".as_bytes();
+            let mut plaintext = "message from server".as_bytes();
             let _ = crypto_send_data(&mut conn, &mut plaintext, &mut stream);
-            let _ = stream.flush();
-            loop {
-                std::thread::sleep(time::Duration::from_millis(50));
-            }
         }
-
-        loop {
-            let mut plaintext = vec!();
-            let _ = crypto_recv_data(&mut conn, &mut stream, &mut plaintext);
-            println!("decrypted: {:?}", String::from_utf8(plaintext));
-            std::thread::sleep(time::Duration::from_millis(50));
+        else {
+            let mut plaintext = "message from client".as_bytes();
+            let _ = crypto_send_data(&mut conn, &mut plaintext, &mut stream);
         }
+
+        let mut plaintext = vec!();
+        let _ = crypto_recv_data(&mut conn, &mut stream, &mut plaintext);
+        println!("LIB READ: {:?}", String::from_utf8(plaintext).unwrap());
+        Ok(())
     }
 
 
-    pub fn server() -> Result<(), std::io::Error> {
+    fn server() -> Result<(), std::io::Error> {
         let listener = TcpListener::bind("127.0.0.1:9988").unwrap();
         for stream in listener.incoming() {
             let stream: TcpStream = stream.unwrap();
@@ -538,21 +537,17 @@ mod tests {
         Ok(())
     }
 
-    pub fn client() -> Result<(), std::io::Error> {
+    fn client() -> Result<(), std::io::Error> {
         let stream = TcpStream::connect("127.0.0.1:9988").unwrap();
         let conn = ConnectionContext::new(false);
         let _ = event_loop(conn, stream, false);
         Ok(())
     }
 
-    pub fn test() {
+    #[test]
+    fn test() {
         thread::spawn(move || { let _ = server(); });
         let child = thread::spawn(move || { let _ = client(); });
         let _ = child.join();
     }
-
-    #[test]
-    fn it_works() {
-        test();
-    }
 }