From 946fd79e834dd6a5d8f6d0d7b26ca5a51a655af5 Mon Sep 17 00:00:00 2001
From: Jeremy Soller <jackpot51@gmail.com>
Date: Sun, 14 Aug 2016 19:17:55 -0600
Subject: [PATCH] Test for BadFile

---
 syscall/fs.rs |  7 ++++++-
 tests/mod.rs  | 17 +++++++++++++----
 2 files changed, 19 insertions(+), 5 deletions(-)

diff --git a/syscall/fs.rs b/syscall/fs.rs
index 1ffb91e7..25735e41 100644
--- a/syscall/fs.rs
+++ b/syscall/fs.rs
@@ -5,7 +5,12 @@ use super::{Error, Result};
 /// Read syscall
 pub fn read(fd: usize, buf: &mut [u8]) -> Result<usize> {
     println!("Read {}: {}", fd, buf.len());
-    Ok(0)
+    if let Some(file) = unsafe { &mut ::context::CONTEXT }.files.get(fd) {
+        println!("{:?}", file);
+        Ok(0)
+    } else {
+        Err(Error::BadFile)
+    }
 }
 
 /// Write syscall
diff --git a/tests/mod.rs b/tests/mod.rs
index fa55c4d5..67e13c94 100644
--- a/tests/mod.rs
+++ b/tests/mod.rs
@@ -1,17 +1,19 @@
 use arch::interrupt::{enable_interrupts, halt};
 
-use syscall;
+use syscall::{self, Error};
 
+/// Test halting
 #[test]
 fn halt_with_interrupts() {
     unsafe {
-        enable_interrupts();
-        halt();
+        //enable_interrupts();
+        //halt();
     }
 }
 
+/// Test stdio
 #[test]
-fn open_stdio() {
+fn stdio() {
     // Test opening stdin
     assert_eq!(syscall::open(b"debug:", 0), Ok(0));
 
@@ -29,3 +31,10 @@ fn open_stdio() {
     let stderr_str = b"STDERR";
     assert_eq!(syscall::write(2, stderr_str), Ok(stderr_str.len()));
 }
+
+/// Test that invalid reads/writes cause errors
+#[test]
+fn invalid_path() {
+    assert_eq!(syscall::read(999, &mut []), Err(Error::BadFile));
+    assert_eq!(syscall::write(999, &[]), Err(Error::BadFile));
+}
-- 
GitLab