]> code.delx.au - offlineimap/commitdiff
Test for failure in readLine and readBytes
authorJohn Goerzen <jgoerzen@complete.org>
Tue, 12 Aug 2008 13:33:27 +0000 (08:33 -0500)
committerJohn Goerzen <jgoerzen@complete.org>
Tue, 12 Aug 2008 13:33:27 +0000 (08:33 -0500)
src/Network/IMAP/Connection.hs
testsrc/TestConnection.hs

index ace53e85d82509a1fb071bd18713104e7752b487..998f0b550bf4af80a40bcd6f6f747d1d4f084129 100644 (file)
@@ -39,13 +39,15 @@ newStringConnection =
                     writeBytes = lwriteBytes,
                     closeConn = return ()}
     where 
-          lreadBytes count = 
-              do (s,sw) <- get
-                 if genericLength s < count
-                    then fail "EOF in input in readBytes"
-                    else do let (r, s') = genericSplitAt count s
-                            put (s', sw)
-                            return r
+          lreadBytes count
+              | count < 0 = fail "readBytes: negative count"
+              | otherwise =
+                  do (s,sw) <- get
+                     if genericLength s < count
+                       then fail "EOF in input in readBytes"
+                       else do let (r, s') = genericSplitAt count s
+                               put (s', sw)
+                               return r
           lreadLine =
               do (s, sw) <- get
                  let (line, remainder) = spanList (\x -> "\r\n" /= take 2 x) s
index 681d45e3ca5270be420fadfa6fc46d152f32213a..275c259e05c2ae0713a64b7d697052c09966fbde 100644 (file)
@@ -47,15 +47,21 @@ expectedString f =
 
 prop_readLine :: [String] -> Property
 prop_readLine s =
-    (not (null s)) && (and (map (notElem '\r') s)) ==> 
+    (and (map (notElem '\r') s)) ==> 
         runLinesConnection s readLine @?=
-            Right (head s, (expectedString (tail s), []))
+            if null s
+               then Left "EOF in input in readLine"
+               else Right (head s, (expectedString (tail s), []))
 
-prop_readBytes :: String -> Int -> Property
+prop_readBytes :: String -> Int -> Result
 prop_readBytes s l =
-    l <= length s && l >= 0 ==> 
-      runStringConnection s (\c -> readBytes c (fromIntegral l)) ==
-                         Right (take l s, (drop l s, []))
+      runStringConnection s (\c -> readBytes c (fromIntegral l)) @?=
+          if l < 0
+             then Left "readBytes: negative count"
+             else case compare l (length s) of
+                    EQ -> Right (take l s, (drop l s, []))
+                    LT -> Right (take l s, (drop l s, []))
+                    GT -> Left "EOF in input in readBytes"
 
 q :: Testable a => String -> a -> HU.Test
 q = qccheck (defaultConfig {configMaxTest = 250, configMaxFail = 5000})