From 260133254072c9a2400a472a7a018b9f7d7d9671 Mon Sep 17 00:00:00 2001 From: Marcin Szamotulski Date: Thu, 11 Jul 2024 10:04:22 +0200 Subject: [PATCH] IPv4 address with mask should not contain a white space This IPv4 address should be illegal `0.0.0.0 /32`. This is compatible with `inet_net_pton`. --- Data/IP/Addr.hs | 4 ++-- test/IPSpec.hs | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Data/IP/Addr.hs b/Data/IP/Addr.hs index 511c9b8..a5da4a1 100644 --- a/Data/IP/Addr.hs +++ b/Data/IP/Addr.hs @@ -512,8 +512,8 @@ ip4' checkTermination = do _ <- char '.' a3 <- octet let as = [a0, a1, a2, a3] - skipSpaces - when checkTermination termination + when checkTermination $ + skipSpaces >> termination return as skipSpaces :: Parser () diff --git a/test/IPSpec.hs b/test/IPSpec.hs index b547561..cf97d0e 100644 --- a/test/IPSpec.hs +++ b/test/IPSpec.hs @@ -24,8 +24,11 @@ data InvalidIPv4Str = Iv4 String deriving (Show) instance Arbitrary InvalidIPv4Str where arbitrary = - frequency [(9, arbitraryIIPv4Str arbitrary 32) + frequency [(8, arbitraryIIPv4Str arbitrary 32) + -- an IPv4 address should not end with a trailing `.` ,(1, Iv4 . (++ ".") . show <$> genIPv4) + -- an IPv4 address with mask should not include a white space + ,(1, (\ip (NonNegative len) -> Iv4 (show ip ++ " /" ++ show (len :: Integer))) <$> genIPv4 <*> arbitrary) ] where genIPv4 :: Gen IPv4