[Pkg-haskell-commits] [SCM] haskell-testpack branch, master, updated. debian/1.0.2-1-4-gb0d6b36
John Goerzen
jgoerzen at complete.org
Fri Apr 23 14:45:27 UTC 2010
The following commit has been merged in the master branch:
commit efbda1caaf4a8373c9b4eeb36b738ed6522cd258
Author: John Goerzen <jgoerzen at complete.org>
Date: Sat Oct 23 02:53:53 2004 +0100
Added tests for FTP host stuff
Keywords:
(jgoerzen at complete.org--projects/missingh--head--1.0--patch-102)
diff --git a/ChangeLog b/ChangeLog
index 390557f..b99d3d2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -2,6 +2,19 @@
# arch-tag: automatic-ChangeLog--jgoerzen at complete.org--projects/missingh--head--1.0
#
+2004-10-22 20:53:53 GMT John Goerzen <jgoerzen at complete.org> patch-102
+
+ Summary:
+ Added tests for FTP host stuff
+ Revision:
+ missingh--head--1.0--patch-102
+
+
+ modified files:
+ ChangeLog libsrc/MissingH/Network/FTP/Parser.hs
+ testsrc/Network/FTP/Parsertest.hs
+
+
2004-10-22 20:29:43 GMT John Goerzen <jgoerzen at complete.org> patch-101
Summary:
diff --git a/libsrc/MissingH/Network/FTP/Parser.hs b/libsrc/MissingH/Network/FTP/Parser.hs
index a83945b..e3db642 100644
--- a/libsrc/MissingH/Network/FTP/Parser.hs
+++ b/libsrc/MissingH/Network/FTP/Parser.hs
@@ -34,12 +34,15 @@ Written by John Goerzen, jgoerzen\@complete.org
-}
-module MissingH.Network.FTP.Parser(parseReply, parseGoodReply)
+module MissingH.Network.FTP.Parser(parseReply, parseGoodReply,
+ toPortString, fromPortString)
where
import Text.ParserCombinators.Parsec
import MissingH.Parsec
import MissingH.List
+import MissingH.Bits
+import Network.Socket(SockAddr(..), PortNumber(..))
-- import Control.Exception(Exception(PatternMatchFail), throw)
----------------------------------------------------------------------
@@ -124,6 +127,11 @@ multiReply = try (do
end <- expectedReplyLine (fst start)
return (fst start, snd start : (component ++ [snd end]))
)
+
+----------------------------------------------------------------------
+-- The real code
+----------------------------------------------------------------------
+
-- | Parse a FTP reply. Returns a (result code, text) pair.
parseReply :: String -> (Int, [String])
@@ -144,3 +152,25 @@ parseGoodReply input =
then error ((show (fst reply)) ++ ": " ++ (join "\n" (snd reply)))
else reply
+{- | Converts a socket address to a string suitable for a PORT command.
+
+Example:
+
+> toPortString (SockAddrInet (PortNum 0x1234) (0xaabbccdd)) ->
+ "170,187,204,221,18,52"
+-}
+toPortString :: SockAddr -> String
+toPortString (SockAddrInet (PortNum port) hostaddr) =
+ (genericJoin "," (getBytes hostaddr)) ++ "," ++
+ (genericJoin "," (getBytes port))
+toPortString _ =
+ error "toPortString only works on AF_INET addresses"
+
+-- | Converts a port string to a socket address. This is the inverse calculation of 'toPortString'.
+fromPortString :: String -> SockAddr
+fromPortString instr =
+ let inbytes = split "," instr
+ hostbytes = map read (take 4 inbytes)
+ portbytes = map read (drop 4 inbytes)
+ in
+ SockAddrInet (PortNum (fromBytes portbytes)) (fromBytes hostbytes)
diff --git a/testsrc/Network/FTP/Parsertest.hs b/testsrc/Network/FTP/Parsertest.hs
index f485ec8..0081fa5 100644
--- a/testsrc/Network/FTP/Parsertest.hs
+++ b/testsrc/Network/FTP/Parsertest.hs
@@ -20,6 +20,7 @@ module Network.FTP.Parsertest(tests) where
import HUnit
import MissingH.Network.FTP.Parser
import Testutil
+import Network.Socket
test_parseReply =
let f inp exp = exp @=? parseReply inp in
@@ -30,6 +31,21 @@ test_parseReply =
f "230-Test\r\nLine2\r\n 230 Line3\r\n230 Done\r\n"
(230, ["Test", "Line2", " 230 Line3", "Done"])
-tests = TestList [TestLabel "parseReply" (TestCase test_parseReply)
+test_toPortString =
+ let f inp exp = exp @=? toPortString inp in
+ do
+ f (SockAddrInet (PortNum 0x1234) 0xaabbccdd) "170,187,204,221,18,52"
+
+test_fromPortString =
+ let f inp exp = exp @=? case fromPortString inp of
+ SockAddrInet (PortNum x) y -> (x, y)
+ _ -> (0, 0)
+ in
+ do
+ f "170,187,204,221,18,52" (0x1234, 0xaabbccdd)
+
+tests = TestList [TestLabel "parseReply" (TestCase test_parseReply),
+ TestLabel "toPortString" (TestCase test_toPortString),
+ TestLabel "fromPortString" (TestCase test_fromPortString)
]
\ No newline at end of file
--
haskell-testpack
More information about the Pkg-haskell-commits
mailing list