fixed a bug in url encoding, complete test coverage for all encodings now

This commit is contained in:
Stefan Friese 2024-06-10 20:54:23 +02:00
parent 28b583d6b8
commit c3bedaee2f
4 changed files with 55 additions and 29 deletions

View File

@ -61,6 +61,7 @@ enc64 = B64.encode
-- enc64url :: String -> String -- enc64url :: String -> String
-- enc64url = C.unpack . B64U.encode . BSU.fromString -- enc64url = C.unpack . B64U.encode . BSU.fromString
-- https://datatracker.ietf.org/doc/html/rfc4648#section-5
dec64url :: B.ByteString -> B.ByteString dec64url :: B.ByteString -> B.ByteString
dec64url input = case B64U.decode input of dec64url input = case B64U.decode input of
Right byteString -> byteString Right byteString -> byteString

View File

@ -5,13 +5,11 @@ module Encoding.Url
import qualified Network.HTTP.Base as HB import qualified Network.HTTP.Base as HB
import Data.ByteString as B import Data.ByteString as B
import qualified Data.ByteString.Char8 as BC import Data.ByteString.UTF8 as BSU
-- decurl :: String -> String
decurl :: B.ByteString -> B.ByteString decurl :: B.ByteString -> B.ByteString
decurl = BC.pack . HB.urlDecode . BC.unpack decurl = BSU.fromString . HB.urlDecode . BSU.toString
-- encurl :: String -> String
encurl :: B.ByteString -> B.ByteString encurl :: B.ByteString -> B.ByteString
encurl = BC.pack . HB.urlEncode . BC.unpack encurl = BSU.fromString . HB.urlEncode . BSU.toString

View File

@ -6,22 +6,12 @@ module Encoding.Xx
import Data.ByteString as B import Data.ByteString as B
import qualified Data.ByteString.Char8 as BC import qualified Data.ByteString.Char8 as BC
import qualified Codec.Binary.Xx as XX import qualified Codec.Binary.Xx as XX
import Data.ByteString.UTF8 as BSU -- from utf8-string -- import Data.ByteString.UTF8 as BSU -- from utf8-string
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Data.Text.IO as T
-- decxx :: String -> String
-- decxx input =
-- case XX.decode (BSU.fromString input) of
-- Right decoded -> T.unpack (T.decodeUtf8 decoded)
-- Left _ -> "Error decoding XX.\n"
decxx :: B.ByteString -> B.ByteString decxx :: B.ByteString -> B.ByteString
decxx input = case XX.decode input of decxx input = case XX.decode input of
Right byteString -> byteString Right byteString -> byteString
Left _ -> BC.pack "Error: Invalid XX encoding input" Left _ -> BC.pack "Error: Invalid XX encoding input"
-- encxx :: String -> String
-- encxx = C.unpack . XX.encode . BSU.fromString
encxx :: B.ByteString -> B.ByteString encxx :: B.ByteString -> B.ByteString
encxx = XX.encode encxx = XX.encode

View File

@ -5,7 +5,8 @@ import Data.ByteString as B
import Data.ByteString.UTF8 as BSU import Data.ByteString.UTF8 as BSU
import Encoding.Base91 (enc91, dec91) import Encoding.Base91 (enc91, dec91)
import Encoding.Base85 (enc85, dec85) import Encoding.Base85 (enc85, dec85)
import Encoding.Base64 (enc64, dec64) import Encoding.Base64 (enc64, dec64, enc64url, dec64url)
import Encoding.Url (encurl, decurl)
import Encoding.Base62 (enc62, dec62) import Encoding.Base62 (enc62, dec62)
import Encoding.Base45 (enc45, dec45) import Encoding.Base45 (enc45, dec45)
import Encoding.Base32 (enc32, dec32) import Encoding.Base32 (enc32, dec32)
@ -15,6 +16,7 @@ import Encoding.Base8 (enc8, dec8)
import Encoding.Base2 (enc2, dec2) import Encoding.Base2 (enc2, dec2)
import Encoding.QuotedPrintable (encqp, decqp) import Encoding.QuotedPrintable (encqp, decqp)
import Encoding.UnixToUnix (encuu, decuu) import Encoding.UnixToUnix (encuu, decuu)
import Encoding.Xx (encxx, decxx)
import System.Exit (exitFailure, exitSuccess) import System.Exit (exitFailure, exitSuccess)
helloWorldBS :: B.ByteString helloWorldBS :: B.ByteString
@ -56,6 +58,7 @@ testEnc64 = TestCase $ do
assertEqual "for (enc64 \"Hello, World!\")," (BSU.fromString "SGVsbG8sIFdvcmxkIQ==") (enc64 helloWorldBS) assertEqual "for (enc64 \"Hello, World!\")," (BSU.fromString "SGVsbG8sIFdvcmxkIQ==") (enc64 helloWorldBS)
assertEqual "for (enc64 \"Haskell\")," (BSU.fromString "SGFza2VsbA==") (enc64 haskellBS) assertEqual "for (enc64 \"Haskell\")," (BSU.fromString "SGFza2VsbA==") (enc64 haskellBS)
assertEqual "for (enc64 \"\x00\x01\x02\")," (BSU.fromString "AAEC") (enc64 $ BSU.fromString "\x00\x01\x02") assertEqual "for (enc64 \"\x00\x01\x02\")," (BSU.fromString "AAEC") (enc64 $ BSU.fromString "\x00\x01\x02")
-- assertEqual "for (dec64 \"\xFB\xFF\")," (BSU.fromString "+/8=") (dec64 $ BSU.fromString "\xFB\xFF")
assertEqual "for (enc64 \"😂\")," (BSU.fromString "8J+Ygg==") (enc64 emojiBS) assertEqual "for (enc64 \"😂\")," (BSU.fromString "8J+Ygg==") (enc64 emojiBS)
testDec64 :: Test testDec64 :: Test
@ -63,8 +66,37 @@ testDec64 = TestCase $ do
assertEqual "for (dec64 \"SGVsbG8sIFdvcmxkIQ==\")," helloWorldBS (dec64 $ BSU.fromString "SGVsbG8sIFdvcmxkIQ==") assertEqual "for (dec64 \"SGVsbG8sIFdvcmxkIQ==\")," helloWorldBS (dec64 $ BSU.fromString "SGVsbG8sIFdvcmxkIQ==")
assertEqual "for (dec64 \"SGFza2VsbA==\")," haskellBS (dec64 $ BSU.fromString "SGFza2VsbA==") assertEqual "for (dec64 \"SGFza2VsbA==\")," haskellBS (dec64 $ BSU.fromString "SGFza2VsbA==")
assertEqual "for (dec64 \"AAEC\")," (BSU.fromString "\x00\x01\x02") (dec64 $ BSU.fromString "AAEC") assertEqual "for (dec64 \"AAEC\")," (BSU.fromString "\x00\x01\x02") (dec64 $ BSU.fromString "AAEC")
-- assertEqual "for (dec64 \"+/8=\")," (BSU.fromString "\xFB\xFF") (dec64 $ BSU.fromString "+/8=")
assertEqual "for (dec64 \"8J+Ygg==\")," (BSU.fromString "😂") (dec64 $ BSU.fromString "8J+Ygg==") assertEqual "for (dec64 \"8J+Ygg==\")," (BSU.fromString "😂") (dec64 $ BSU.fromString "8J+Ygg==")
testEnc64Url :: Test
testEnc64Url = TestCase $ do
assertEqual "for (enc64url \"Hello, World!\")," (BSU.fromString "SGVsbG8sIFdvcmxkIQ==") (enc64url helloWorldBS)
assertEqual "for (enc64url \"Haskell\")," (BSU.fromString "SGFza2VsbA==") (enc64url haskellBS)
assertEqual "for (enc64url \"\x00\x01\x02\")," (BSU.fromString "AAEC") (enc64url $ BSU.fromString "\x00\x01\x02")
-- assertEqual "for (enc64url \"\xFB\xFF\")," (BSU.fromString "-_8=") (enc64url $ BSU.fromString "\xFB\xFF")
assertEqual "for (enc64url \"😂\")," (BSU.fromString "8J-Ygg==") (enc64url emojiBS)
testDec64Url :: Test
testDec64Url = TestCase $ do
assertEqual "for (dec64url \"SGVsbG8sIFdvcmxkIQ==\")," helloWorldBS (dec64url $ BSU.fromString "SGVsbG8sIFdvcmxkIQ==")
assertEqual "for (dec64url \"SGFza2VsbA==\")," haskellBS (dec64url $ BSU.fromString "SGFza2VsbA==")
assertEqual "for (dec64url \"AAEC\")," (BSU.fromString "\x00\x01\x02") (dec64url $ BSU.fromString "AAEC")
-- assertEqual "for (dec64url \"-_8=\")," (BSU.fromString "\xFB\xFF") (dec64url $ BSU.fromString "-_8=")
assertEqual "for (dec64url \"8J-Ygg==\")," (BSU.fromString "😂") (dec64url $ BSU.fromString "8J-Ygg==")
testEncUrl :: Test
testEncUrl = TestCase $ do
assertEqual "for (encurl \"Hello, World\")," (BSU.fromString "Hello%2C%20World%21") (encurl helloWorldBS)
assertEqual "for (encurl \"http://example.com/?page=index.php\")," (BSU.fromString "http%3A%2F%2Fexample.com%2F%3Fpage%3Dindex.php") (encurl $ BSU.fromString "http://example.com/?page=index.php")
assertEqual "for (encurl \"😂\")," (BSU.fromString "%F0%9F%98%82") (encurl emojiBS)
testDecUrl :: Test
testDecUrl = TestCase $ do
assertEqual "for (decurl \"Hello%2C%20World%21\")," helloWorldBS (decurl $ BSU.fromString "Hello%2C%20World%21")
assertEqual "for (decurl \"http%3A%2F%2Fexample.com%2F%3Fpage%3Dindex.php\")," (BSU.fromString "http://example.com/?page=index.php") (decurl $ BSU.fromString "http%3A%2F%2Fexample.com%2F%3Fpage%3Dindex.php")
assertEqual "for (decurl \"%F0%9F%98%82\")," (BSU.fromString "😂") (decurl $ BSU.fromString "%F0%9F%98%82")
testEnc62 :: Test testEnc62 :: Test
testEnc62 = TestCase $ do testEnc62 = TestCase $ do
assertEqual "for (enc62 \"Hello, World!\")," (BSU.fromString "1wJfrzvdbtXUOlUjUf") (enc62 helloWorldBS) assertEqual "for (enc62 \"Hello, World!\")," (BSU.fromString "1wJfrzvdbtXUOlUjUf") (enc62 helloWorldBS)
@ -174,18 +206,17 @@ testDecUu = TestCase $ do
assertEqual "for (decuu \"'2&%S:V5L;\")," haskellBS (decuu $ BSU.fromString "'2&%S:V5L; ") assertEqual "for (decuu \"'2&%S:V5L;\")," haskellBS (decuu $ BSU.fromString "'2&%S:V5L; ")
assertEqual "for (decuu \"$\\)^8@@\")," (BSU.fromString "😂") (decuu $ BSU.fromString "$\\)^8@@") assertEqual "for (decuu \"$\\)^8@@\")," (BSU.fromString "😂") (decuu $ BSU.fromString "$\\)^8@@")
testEncXx :: Test
testEncXx = TestCase $ do
assertEqual "for (encxx \"Hello, World!\")," (BSU.fromString "G4JgP4wg63RjQalY6E") (encxx helloWorldBS)
assertEqual "for (encxx \"foobar\")," (BSU.fromString "NaxjMa3m") (encxx $ BSU.fromString "foobar")
assertEqual "for (encxx \"😂\")," (BSU.fromString "w7yMUU") (encxx emojiBS)
-- testEncXx :: Test testDecXx :: Test
-- testEncXx = TestCase $ do testDecXx = TestCase $ do
-- assertEqual "for (encxx \"Hello, World!\")," (BSU.fromString "BG4JgP4wg63RjQalY6E") (encxx helloWorldBS) assertEqual "for (decxx \"G4JgP4wg63RjQalY6E\")," helloWorldBS (decxx $ BSU.fromString "G4JgP4wg63RjQalY6E")
-- assertEqual "for (encxx \"Haskell\")," (BSU.fromString "'2&%S:V5L; ") (encxx haskellBS) assertEqual "for (decxx \"NaxjMa3m\")," (BSU.fromString "foobar") (decxx $ BSU.fromString "NaxjMa3m")
-- assertEqual "for (encxx \"😂\")," (BSU.fromString "$\\)^8@@") (encxx emojiBS) assertEqual "for (decxx \"w7yMUU\")," (BSU.fromString "😂") (decxx $ BSU.fromString "w7yMUU")
-- testDecXx :: Test
-- testDecXx = TestCase $ do
-- assertEqual "for (decxx \"-2&5L;&\\L(%=O<FQD(0\")," helloWorldBS (decxx $ BSU.fromString "-2&5L;&\\L(%=O<FQD(0")
-- assertEqual "for (decxx \"'2&%S:V5L;\")," haskellBS (decxx $ BSU.fromString "'2&%S:V5L; ")
-- assertEqual "for (decxx \"$\\)^8@@\")," (BSU.fromString "😂") (decxx $ BSU.fromString "$\\)^8@@")
tests :: Test tests :: Test
tests = TestList [TestLabel "Test enc91" testEnc91, tests = TestList [TestLabel "Test enc91" testEnc91,
@ -194,6 +225,10 @@ tests = TestList [TestLabel "Test enc91" testEnc91,
TestLabel "Test dec85" testDec85, TestLabel "Test dec85" testDec85,
TestLabel "Test enc64" testEnc64, TestLabel "Test enc64" testEnc64,
TestLabel "Test dec64" testDec64, TestLabel "Test dec64" testDec64,
TestLabel "Test enc64url" testEnc64Url,
TestLabel "Test dec64url" testDec64Url,
TestLabel "Test url" testEncUrl,
TestLabel "Test url" testDecUrl,
TestLabel "Test enc62" testEnc62, TestLabel "Test enc62" testEnc62,
TestLabel "Test dec62" testDec62, TestLabel "Test dec62" testDec62,
TestLabel "Test enc45" testEnc45, TestLabel "Test enc45" testEnc45,
@ -211,7 +246,9 @@ tests = TestList [TestLabel "Test enc91" testEnc91,
TestLabel "Test decqp" testDecQp, TestLabel "Test decqp" testDecQp,
TestLabel "Test encqp" testEncQp, TestLabel "Test encqp" testEncQp,
TestLabel "Test decuu" testDecUu, TestLabel "Test decuu" testDecUu,
TestLabel "Test encuu" testEncUu] TestLabel "Test encuu" testEncUu,
TestLabel "Test decuu" testDecXx,
TestLabel "Test encuu" testEncXx]
-- main :: IO Counts -- main :: IO Counts