2023-12-17 12:57:26 +01:00
|
|
|
module Test.Main where
|
|
|
|
|
2024-01-27 07:05:58 +01:00
|
|
|
import GenericParser.Parser (Parser(..))
|
2024-01-19 15:32:18 +01:00
|
|
|
import GenericParser.DomainParser.Common (ldh_str, DomainError(..))
|
2024-01-19 18:24:02 +01:00
|
|
|
import GenericParser.DomainParserRFC1035 as RFC1035
|
2024-01-19 19:32:43 +01:00
|
|
|
import GenericParser.DomainParser as ModernDomains
|
2024-01-23 03:55:45 +01:00
|
|
|
import GenericParser.IPAddress (IPv4Error(..))
|
|
|
|
import GenericParser.IPAddress as IP
|
2024-01-25 04:59:18 +01:00
|
|
|
import GenericParser.EmailAddress as E
|
2023-12-17 12:57:26 +01:00
|
|
|
|
2024-01-20 01:05:11 +01:00
|
|
|
import Prelude (Unit, discard, show, ($), (<>))
|
2024-01-19 02:03:32 +01:00
|
|
|
import Data.Either (Either(..))
|
2024-01-25 04:59:18 +01:00
|
|
|
import Data.Maybe (Maybe(..), maybe)
|
2024-01-19 02:03:32 +01:00
|
|
|
import Data.String.CodeUnits (fromCharArray)
|
|
|
|
import Effect.Console (log)
|
2024-01-19 18:24:02 +01:00
|
|
|
import Effect (Effect, foreachE)
|
2024-01-19 02:03:32 +01:00
|
|
|
|
|
|
|
logtest :: forall e v. String -> Parser e v -> String -> (v -> String) -> (e -> String) -> Effect Unit
|
|
|
|
logtest fname (Parser p) str r e = do
|
|
|
|
log $ "(" <> fname <> ") parsing '" <> str <> "': "
|
|
|
|
<> case p { string: str, position: 0 } of
|
|
|
|
Left { position, error } -> "failed at position " <> show position <> case error of
|
|
|
|
Nothing -> " -> no error reported"
|
|
|
|
Just err -> " -> error: " <> e err
|
2024-01-23 03:55:45 +01:00
|
|
|
Right { suffix, result } -> (r result) <> " '" <> suffix.string <> "'"
|
2024-01-19 02:03:32 +01:00
|
|
|
|
|
|
|
id :: forall a. a -> a
|
|
|
|
id a = a
|
|
|
|
|
|
|
|
showerror :: DomainError -> String
|
2024-01-19 15:08:52 +01:00
|
|
|
showerror (LabelTooLarge size) = "LabelTooLarge (size: " <> show size <> ")"
|
|
|
|
showerror (DomainTooLarge size) = "DomainTooLarge (size: " <> show size <> ")"
|
|
|
|
showerror (InvalidCharacter) = "InvalidCharacter"
|
|
|
|
showerror (EOFExpected) = "EOFExpected"
|
2023-12-17 12:57:26 +01:00
|
|
|
|
2024-01-23 03:55:45 +01:00
|
|
|
test_series :: forall e v
|
|
|
|
. String
|
|
|
|
-> Parser e v
|
|
|
|
-> (v -> String)
|
|
|
|
-> (e -> String)
|
|
|
|
-> Array String
|
|
|
|
-> Effect Unit
|
2024-01-19 18:24:02 +01:00
|
|
|
test_series l p v e a = foreachE a (\s -> logtest l p s v e)
|
|
|
|
|
2024-01-23 04:46:08 +01:00
|
|
|
showerror_ipv6 :: IP.IPv6Error -> String
|
2024-01-24 04:21:25 +01:00
|
|
|
showerror_ipv6 (IP.InvalidCharacter) = "InvalidCharacter"
|
|
|
|
showerror_ipv6 (IP.TooManyHexaDecimalCharacters) = "TooManyHexaDecimalCharacters"
|
|
|
|
showerror_ipv6 (IP.NotEnoughChunks) = "NotEnoughChunks"
|
|
|
|
showerror_ipv6 (IP.TooManyChunks) = "TooManyChunks"
|
|
|
|
showerror_ipv6 IP.IPv6UnrelevantShortRepresentation = "useless double dots"
|
2024-01-23 04:46:08 +01:00
|
|
|
|
2024-01-23 03:55:45 +01:00
|
|
|
showerror_ipv4 :: IP.IPv4Error -> String
|
|
|
|
showerror_ipv4 (NumberTooBig x) = "value '" <> show x <> "' is > 255"
|
2024-01-24 04:21:25 +01:00
|
|
|
showerror_ipv4 IPv4UnrelevantShortRepresentation = "useless double dots"
|
2024-01-23 03:55:45 +01:00
|
|
|
|
2024-01-25 04:59:18 +01:00
|
|
|
showerror_email :: E.EmailError -> String
|
|
|
|
showerror_email E.InvalidCharacter = "InvalidCharacter"
|
|
|
|
showerror_email (E.InvalidDomain e) = "invalid domain: " <> maybe "no domain error provided, weird" showerror e
|
|
|
|
|
2023-12-17 12:57:26 +01:00
|
|
|
main :: Effect Unit
|
|
|
|
main = do
|
2024-01-19 18:24:02 +01:00
|
|
|
let domains = [
|
|
|
|
"",
|
|
|
|
"-",
|
|
|
|
".",
|
|
|
|
"a",
|
|
|
|
"a.",
|
|
|
|
"a-",
|
|
|
|
"a.x",
|
|
|
|
"a2.org",
|
|
|
|
"a33.org",
|
|
|
|
"xblah.a.x",
|
|
|
|
"xblah.a2.org",
|
|
|
|
"xblah.a33.org",
|
2024-01-19 19:32:43 +01:00
|
|
|
"_dmarc.example.com"
|
2024-01-19 18:24:02 +01:00
|
|
|
]
|
|
|
|
test_series "ldh_str" ldh_str fromCharArray showerror domains
|
2024-01-19 02:03:32 +01:00
|
|
|
log ""
|
2024-01-19 18:24:02 +01:00
|
|
|
test_series "RFC1035.label" RFC1035.label id showerror domains
|
2024-01-19 02:03:32 +01:00
|
|
|
log ""
|
2024-01-19 18:24:02 +01:00
|
|
|
test_series "RFC1035.subdomain" RFC1035.subdomain id showerror domains
|
2024-01-19 02:03:32 +01:00
|
|
|
log ""
|
2024-01-19 18:24:02 +01:00
|
|
|
test_series "RFC1035.sub_eof" RFC1035.sub_eof id showerror domains
|
2024-01-19 02:03:32 +01:00
|
|
|
log ""
|
2024-01-19 18:24:02 +01:00
|
|
|
test_series "RFC1035.domain" RFC1035.domain id showerror domains
|
2024-01-19 19:32:43 +01:00
|
|
|
log ""
|
|
|
|
test_series "ModernDomains.domain" ModernDomains.domain id showerror domains
|
2024-01-23 03:55:45 +01:00
|
|
|
|
|
|
|
log ""
|
2024-01-27 07:05:58 +01:00
|
|
|
let ipv4_addresses = [ "10..1."
|
|
|
|
, "10..1"
|
|
|
|
, "1..2"
|
|
|
|
, "1.2.3.4"
|
|
|
|
, "192.168..1"
|
|
|
|
, "1..2.3.4"
|
|
|
|
, "1.5.10.255"
|
|
|
|
, "100.200.300.400"
|
|
|
|
]
|
|
|
|
test_series "IP.ipv4" IP.ipv4 id showerror_ipv4 ipv4_addresses
|
2024-01-23 04:46:08 +01:00
|
|
|
|
|
|
|
log ""
|
2024-01-27 07:05:58 +01:00
|
|
|
let ipv6_addresses = [ "2001:0"
|
|
|
|
, "2001::x:0"
|
|
|
|
, "2001:x::0"
|
|
|
|
, "2001::0"
|
|
|
|
, "2001::1:"
|
|
|
|
, "::"
|
|
|
|
, "2001::"
|
|
|
|
, "::1"
|
|
|
|
, "2001:0db8:0000:0000:0000:8a2e:0370:7334:30:1035:3"
|
|
|
|
, "2001:0db8:0000:0000:0000:8a2e:0370:7334"
|
|
|
|
, "2001:0db8::8a2e:0370:7334"
|
|
|
|
]
|
|
|
|
test_series "IP.ipv6" IP.ipv6 id showerror_ipv6 ipv6_addresses
|
2024-01-25 04:59:18 +01:00
|
|
|
|
|
|
|
log ""
|
2024-01-27 07:05:58 +01:00
|
|
|
let email_addresses = [ "guy@example.com"
|
|
|
|
, "guy.name@example.com"
|
|
|
|
, "well-look-at-this-domain@.com"
|
|
|
|
, "guy-@example.com"
|
|
|
|
]
|
|
|
|
test_series "E.email" E.email id showerror_email email_addresses
|