base64

Fast Haskell base64 encoding/decoding (docs.ppad.tech/base64).
git clone git://git.ppad.tech/base64.git
Log | Files | Refs | README | LICENSE

commit 64f88b77ced149213b05f5dc7eb57e437de7bf41
parent 511e3335542fbc4a96b2970bab0003f4c00d4744
Author: Jared Tobin <jared@jtobin.io>
Date:   Sat, 10 Oct 2026 15:24:34 +0400

test: exercise both codec paths and invalid inputs

Every property and unit test now runs against both the public
(dispatching) codec and the pure Haskell one, so the scalar path is
covered on aarch64 hosts too.  Generated inputs are slices at non-zero
offsets.

Adds a differential decode test against base64-bytestring, which is
likewise strict about padding and canonical form, on valid encodings
subjected to random substitutions, deletions and insertions; explicit
malformed-padding and non-canonical-tail cases, alone and after a
NEON-length body; a check of every byte value at every position of
encodings with each final-quartet shape; single-byte corruption at
every position for lengths spanning several NEON iterations; and
rejection of lengths not a multiple of four.

Diffstat:
Mtest/Main.hs | 202+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------
1 file changed, 172 insertions(+), 30 deletions(-)

diff --git a/test/Main.hs b/test/Main.hs @@ -4,50 +4,106 @@ module Main where +import Control.Monad (forM_, when) import qualified Data.ByteString as BS +import qualified Data.ByteString.Char8 as B8 import qualified "ppad-base64" Data.ByteString.Base64 as B64 +import qualified "ppad-base64" Data.ByteString.Base64.Pure as Pure import qualified "base64-bytestring" Data.ByteString.Base64 as R0 +import Data.Word (Word8) import Test.Tasty import qualified Test.Tasty.QuickCheck as Q import qualified Test.Tasty.HUnit as H -newtype BS = BS BS.ByteString - deriving (Eq, Show) +-- generators ----------------------------------------------------------------- +-- random bytes, as a slice at a random offset into a larger buffer, so +-- that inputs with a non-zero 'ByteString' offset are exercised bytes :: Int -> Q.Gen BS.ByteString bytes k = do l <- Q.chooseInt (0, k) - v <- Q.vectorOf l Q.arbitrary - pure (BS.pack v) + o <- Q.chooseInt (0, 32) + v <- Q.vectorOf (o + l) Q.arbitrary + pure (BS.drop o (BS.pack v)) + +newtype BS = BS BS.ByteString + deriving (Eq, Show) instance Q.Arbitrary BS where arbitrary = do b <- bytes 1024 pure (BS b) -decode_inverts_encode :: BS -> Bool -decode_inverts_encode (BS bs) = case B64.decode (B64.encode bs) of - Nothing -> False - Just b -> b == bs +alphabet :: BS.ByteString +alphabet = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" + +-- valid encodings subjected to a few random mutations: substitution +-- (by an alphabet char, '=', or an arbitrary byte), deletion, or +-- insertion, with positions biased toward the final quartet +newtype Base64ish = Base64ish BS.ByteString + deriving (Eq, Show) + +instance Q.Arbitrary Base64ish where + arbitrary = do + b <- Q.frequency [(3, bytes 64), (1, bytes 384)] + k <- Q.chooseInt (0, 3) + s <- go k (B64.encode b) + o <- Q.chooseInt (0, 32) + p <- Q.vectorOf o (Q.elements (BS.unpack alphabet)) + pure (Base64ish (BS.drop o (BS.pack p <> s))) + where + go :: Int -> BS.ByteString -> Q.Gen BS.ByteString + go 0 s = pure s + go j s = mutate s >>= go (j - 1) + + mutate s = do + let l = BS.length s + i <- Q.oneof [ + Q.chooseInt (0, l) + , Q.chooseInt (max 0 (l - 4), l) + ] + c <- Q.frequency [ + (6, Q.elements (BS.unpack alphabet)) + , (2, pure 0x3d) + , (1, Q.arbitrary) + ] + Q.elements [ + BS.take i s <> BS.singleton c <> BS.drop (i + 1) s + , BS.take i s <> BS.drop (i + 1) s + , BS.take i s <> BS.singleton c <> BS.drop i s + ] + +-- decoders under test -------------------------------------------------------- + +decoders :: [(String, BS.ByteString -> Maybe BS.ByteString)] +decoders = [("public", B64.decode), ("pure", Pure.decode)] + +ref_decode :: BS.ByteString -> Maybe BS.ByteString +ref_decode bs = case R0.decode bs of + Left _ -> Nothing + Right d -> Just d + +-- properties ----------------------------------------------------------------- encode_matches_reference :: BS -> Bool encode_matches_reference (BS bs) = - let us = B64.encode bs - r0 = R0.encode bs - in us == r0 - -decode_matches_reference :: BS -> Bool -decode_matches_reference (BS bs) = - let enc = R0.encode bs - us = B64.decode enc - r0 = R0.decode enc - in case us of - Nothing -> case r0 of - Left _ -> True - _ -> False - Just du -> case r0 of - Left _ -> False - Right d0 -> du == d0 + let r0 = R0.encode bs + in B64.encode bs == r0 && Pure.encode bs == r0 + +decode_inverts_encode :: BS -> Bool +decode_inverts_encode (BS bs) = + let enc = B64.encode bs + in all (\(_, dec) -> dec enc == Just bs) decoders + +-- base64-bytestring's 'decode' is likewise strict (RFC 4648): it +-- rejects missing or misplaced padding and non-canonical final quartets +decode_matches_reference :: Base64ish -> Bool +decode_matches_reference (Base64ish bs) = + let r0 = ref_decode bs + in B64.decode bs == r0 && Pure.decode bs == r0 + +-- unit tests ----------------------------------------------------------------- case_rfc_vectors :: TestTree case_rfc_vectors = H.testCase "RFC 4648 \167 10 vectors" $ do @@ -63,22 +119,108 @@ case_rfc_vectors = H.testCase "RFC 4648 \167 10 vectors" $ do check (input, expected) = do H.assertEqual ("encode " <> show input) expected (B64.encode input) - H.assertEqual ("decode " <> show expected) - (Just input) (B64.decode expected) + H.assertEqual ("pure encode " <> show input) + expected (Pure.encode input) + forM_ decoders $ \(nam, dec) -> + H.assertEqual (nam <> " decode " <> show expected) + (Just input) (dec expected) mapM_ check vectors +-- malformed final quartets, alone and after a body long enough to +-- take the NEON path +malformed :: TestTree +malformed = H.testCase "malformed padding and non-canonical tails" $ do + let bad = [ + "Zh==" -- non-canonical: nonzero trailing bits + , "Zm9=" -- non-canonical: nonzero trailing bits + , "Zm=v" -- '=' before a data char + , "====" + , "Z===" + , "=Zg=" + , "Zg=a" + , "Zg=", "Zg", "Z" + , "Zm9vY" + , "Zm=vYmFy" -- '=' in the body + , "Zg==Zg==" -- padding mid-stream + , "Zm9v\n" + ] + good = [("Zg==", "f"), ("Zm8=", "fo"), ("Zm9v", "foo")] + body = B64.encode (BS.replicate 72 0xa5) + forM_ decoders $ \(nam, dec) -> do + forM_ bad $ \s -> do + H.assertEqual (nam <> " " <> show s) Nothing (dec s) + H.assertEqual (nam <> " body <> " <> show s) Nothing (dec (body <> s)) + forM_ good $ \(s, d) -> + H.assertEqual (nam <> " body <> " <> show s) + (Just (BS.replicate 72 0xa5 <> d)) (dec (body <> s)) + +-- every byte value, at every position of encodings that span the NEON +-- body loop, the scalar body tail, and each kind of final quartet, +-- decodes exactly as the reference does; in the body, a byte is +-- accepted iff it is an alphabet char +every_byte_every_position :: TestTree +every_byte_every_position = + H.testCase "every byte at every position" $ + forM_ [28, 29, 30 :: Int] $ \n -> do + let base = B64.encode (BS.replicate n 0x00) + l = BS.length base + forM_ [0 .. 255 :: Int] $ \c -> + forM_ [0 .. l - 1] $ \i -> do + let w = fromIntegral c :: Word8 + inp = BS.take i base <> BS.singleton w <> BS.drop (i + 1) base + pec = ref_decode inp + msg = "n = " <> show n <> ", byte " <> show c + <> ", position " <> show i + when (i < l - 4) $ + H.assertEqual ("reference, " <> msg) + (BS.elem w alphabet) (pec /= Nothing) + forM_ decoders $ \(nam, dec) -> + H.assertEqual (nam <> ", " <> msg) pec (dec inp) + +-- for every length spanning several NEON iterations and each final +-- quartet shape, a single invalid byte at any position (or '=' anywhere +-- in the body) causes decoding to fail +single_corruption :: TestTree +single_corruption = H.testCase "single invalid byte anywhere" $ do + let bad = BS.pack [0x00, 0x20, 0x7f, 0x80, 0xff] <> "-_.:@[`{" + forM_ [1 .. 100 :: Int] $ \n -> do + let raw = BS.pack (fmap fromIntegral [7 * j + 3 | j <- [1 .. n]]) + enc = B64.encode raw + l = BS.length enc + put i w = BS.take i enc <> BS.singleton w <> BS.drop (i + 1) enc + forM_ decoders $ \(nam, dec) -> + H.assertEqual (nam <> ", intact, n = " <> show n) (Just raw) (dec enc) + forM_ [0 .. l - 1] $ \i -> do + let ws = BS.unpack bad <> (if i < l - 4 then [0x3d] else []) + forM_ ws $ \w -> do + let msg = "n = " <> show n <> ", position " <> show i + <> ", byte " <> show w + forM_ decoders $ \(nam, dec) -> + H.assertEqual (nam <> ", " <> msg) Nothing (dec (put i w)) + +bad_lengths :: TestTree +bad_lengths = H.testCase "lengths not a multiple of 4" $ + forM_ (filter (\l -> l `rem` 4 /= 0) [1 .. 99 :: Int]) $ \l -> + forM_ decoders $ \(nam, dec) -> + H.assertEqual (nam <> ", length " <> show l) Nothing + (dec (B8.replicate l 'A')) + main :: IO () main = defaultMain $ testGroup "ppad-base64" [ testGroup "property tests" [ - Q.testProperty "decode . encode ~ id" $ - Q.withMaxSuccess 5000 decode_inverts_encode - , Q.testProperty "encode matches reference" $ + Q.testProperty "encode matches reference" $ Q.withMaxSuccess 5000 encode_matches_reference + , Q.testProperty "decode . encode ~ id" $ + Q.withMaxSuccess 5000 decode_inverts_encode , Q.testProperty "decode matches reference" $ - Q.withMaxSuccess 5000 decode_matches_reference + Q.withMaxSuccess 10000 decode_matches_reference ] , testGroup "unit tests" [ case_rfc_vectors + , malformed + , every_byte_every_position + , single_corruption + , bad_lengths ] ]