File: Main.hs

package info (click to toggle)
haskell-base64 1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 312 kB
  • sloc: haskell: 2,450; makefile: 3
file content (563 lines) | stat: -rw-r--r-- 19,851 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PackageImports #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE DataKinds #-}
-- |
-- Module       : Main
-- Copyright    : (c) 2019-2020 Emily Pillmore
-- License      : BSD-style
--
-- Maintainer   : Emily Pillmore <emilypi@cohomolo.gy>
-- Stability    : Experimental
-- Portability  : portable
--
-- This module contains the test implementation for the `base64` package
--
module Main
( main
) where


import Prelude hiding (length)

import Data.Base64.Types

import Data.Bifunctor (second)
import qualified Data.ByteString as BS
import Data.ByteString.Internal (c2w)
import qualified Data.ByteString.Lazy as LBS
import qualified Data.ByteString.Short as SBS
import "base64" Data.ByteString.Base64 as B64
import "base64" Data.ByteString.Base64.URL as B64U
import qualified "base64-bytestring" Data.ByteString.Base64 as Bos
import qualified "base64-bytestring" Data.ByteString.Base64.URL as BosU
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import Data.Text.Encoding.Base64.Error (Base64Error(..))
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Encoding as TL
import qualified Data.Text.Short as TS
import Data.Word

import Internal

import Test.Tasty
import Test.Tasty.HUnit
import Test.Tasty.QuickCheck (testProperty)
import Data.String (IsString)
import Test.QuickCheck hiding (label)


main :: IO ()
main = defaultMain tests


tests :: TestTree
tests = testGroup "Base64 Tests"
  [ mkTree b64
    [ mkPropTree
    , mkUnitTree BS.last BS.length
    ]
  , mkTree lb64
    [ mkPropTree
    , mkUnitTree LBS.last (fromIntegral . LBS.length)
    ]
  , mkTree sb64
    [ mkPropTree
    , mkUnitTree (BS.last . SBS.fromShort) SBS.length
    ]
  , mkTree t64
    [ mkPropTree
    , mkUnitTree (c2w . T.last) T.length
    , mkDecodeTree T.decodeUtf8' tt64 b64
    ]
  , mkTree tl64
    [ mkPropTree
    , mkUnitTree (c2w . TL.last) (fromIntegral . TL.length)
    , mkDecodeTree TL.decodeUtf8' ttl64 lb64
    ]
  , mkTree ts64
    [ mkPropTree
    , mkUnitTree (c2w . T.last . TS.toText) TS.length
    , mkDecodeTree
      (second TS.fromText . T.decodeUtf8' . SBS.fromShort) tts64 sb64
    ]
  ]

-- ---------------------------------------------------------------- --
-- Test tree generation

-- | Make a test tree for a given label
--
mkTree
  :: ( Arbitrary a
     , IsString a
     , Eq a
     , Show a
     )
  => Harness a
  -> [Harness a -> TestTree]
  -> TestTree
mkTree a = testGroup (label a) . fmap ($ a)

-- | Make a test group with some name, lifting a test tree up to the correct
-- type information via some Harness
--
mkTests
  :: ( Arbitrary a
     , IsString a
     , Eq a
     , Show a
     )
  => String
  -> [Harness a -> TestTree]
  -> Harness a
  -> TestTree
mkTests context ts = testGroup context . (<*>) ts . pure

-- | Make property tests for a given harness instance
--
mkPropTree :: (Arbitrary a, IsString a, Eq a, Show a) => Harness a -> TestTree
mkPropTree = mkTests "Property Tests"
  [ prop_roundtrip
  , prop_correctness
  , prop_url_padding
  , const prop_bos_coherence
  ]

-- | Make unit tests for a given harness instance
--
mkUnitTree
  :: (Arbitrary a, IsString a, Eq a, Show a)
  => (a -> Word8)
  -> (a -> Int)
  -> Harness a
  -> TestTree
mkUnitTree last_ length_ = mkTests "Unit tests"
  [ paddingTests last_ length_
  , rfcVectors
  , offsetVectors
  , validityTests
  , canonicityTests
  ]

-- | Make unit tests for textual 'decode*With' functions
--
mkDecodeTree
  :: ( Arbitrary t
     , Eq t
     , IsString t
     , Show t
     , IsString a
     , Show e
     )
  => (a -> Either e t)
  -> TextHarness a t
  -> Harness a
  -> Harness t
  -> TestTree
mkDecodeTree utf8 t a = mkTests "Decoding tests"
  [ decodeWithVectors utf8 t a
  ]

-- ---------------------------------------------------------------- --
-- Property tests

prop_roundtrip :: (Arbitrary a, IsString a, Eq a, Show a) => Harness a -> TestTree
prop_roundtrip Harness{..} = testGroup "prop_roundtrip"
  [ testProperty "prop_std_roundtrip" $ \(bs :: a) ->
      Right bs == decode (extractBase64 $ encode bs)
  , testProperty "prop_url_roundtrip" $ \(bs :: a) ->
      Right bs == decodeUrl (extractBase64 $ encodeUrl bs)
  , testProperty "prop_url_roundtrip_nopad" $ \(bs :: a) ->
      Right bs
        == decodeUrlNopad (extractBase64 $ encodeUrlNopad bs)
  , testProperty "prop_std_lenient_roundtrip" $ \(bs :: a) ->
      bs == lenient (extractBase64 $ encode bs)
  , testProperty "prop_url_lenient_roundtrip" $ \(bs :: a) ->
      bs == lenientUrl (extractBase64 $ encodeUrl bs)
  ]

prop_correctness :: (Arbitrary a, IsString a, Eq a, Show a) => Harness a -> TestTree
prop_correctness Harness{..} = testGroup "prop_validity"
  [ testProperty "prop_std_valid" $ \(bs :: a) ->
    validate (extractBase64 $ encode bs)
  , testProperty "prop_url_valid" $ \(bs :: a) ->
    validateUrl (extractBase64 $ encodeUrl bs)
  , testProperty "prop_std_correct" $ \(bs :: a) ->
    correct (extractBase64 $ encode bs)
  , testProperty "prop_url_correct" $ \(bs :: a) ->
    correctUrl (extractBase64 $ encodeUrl bs)
  ]

prop_url_padding :: (Arbitrary a, IsString a, Eq a, Show a) => Harness a -> TestTree
prop_url_padding Harness{..}  = testGroup "prop_url_padding"
  [ testProperty "prop_url_nopad_roundtrip_untyped" $ \(bs :: a) ->
      Right bs
        == decodeUrlNopad (extractBase64 $ encodeUrlNopad bs)
  , testProperty "prop_url_nopad_roundtrip_typed" $ \(bs :: a) ->
     bs == decodeUrlTypedNopad (encodeUrlNopad bs)
  , testProperty "prop_url_pad_roundtrip_untyped" $ \(bs :: a) ->
      Right bs == decodeUrlPad (extractBase64 $ encodeUrl bs)
  , testProperty "prop_url_pad_roundtrip_typed" $ \(bs :: a) ->
     bs == decodeUrlTypedPad (encodeUrl bs)
  , testProperty "prop_url_decode_invariant_untyped" $ \(bs :: a) ->
      (decodeUrlNopad (extractBase64 $ encodeUrlNopad bs)
       == decodeUrl (extractBase64 $ encodeUrlNopad bs))
      ||
      (decodeUrlPad (extractBase64 $ encodeUrl bs)
       == decodeUrl (extractBase64 $ encodeUrl bs))

  , testProperty "prop_url_decode_invariant_typed" $ \(bs :: a) ->
      (decodeUrlTypedNopad (encodeUrlNopad bs)
      == decodeUrlTyped (encodeUrlNopad bs))
      ||
      (decodeUrlPad (extractBase64 $ encodeUrl bs)
      == decodeUrl (extractBase64 $ encodeUrl bs))

  -- NOTE: we need to fix the bitmasking issue for "impossible"
  -- inputs

  , testProperty "prop_url_padding_coherence" $ \(bs :: a) ->
      Right bs == decodeUrl (extractBase64 $ encodeUrl bs)
      && Right bs == decodeUrlPad (extractBase64 $ encodeUrl bs)

  , testProperty "prop_url_nopadding_coherence" $ \(bs :: a) ->
      Right bs == decodeUrlNopad (extractBase64 $ encodeUrlNopad bs)
      && Right bs == decodeUrl (extractBase64 $ encodeUrlNopad bs)
  ]

-- | just a sanity check against `base64-bytestring`
--
prop_bos_coherence :: TestTree
prop_bos_coherence = testGroup "prop_bos_coherence"
  [ testProperty "prop_std_bos_coherence" $ \bs ->
      Right bs == B64.decodeBase64Untyped (extractBase64 $ B64.encodeBase64' bs)
      && Right bs == Bos.decode (Bos.encode bs)
      && bs == B64.decodeBase64 (B64.encodeBase64' bs)
  , testProperty "prop_url_bos_coherence" $ \bs ->
      Right bs == B64U.decodeBase64Untyped (extractBase64 $ B64U.encodeBase64' bs)
      && Right bs == BosU.decode (BosU.encode bs)
      && bs == B64U.decodeBase64 (B64U.encodeBase64' bs)
  ]

-- ---------------------------------------------------------------- --
-- Unit tests

-- | RFC 4648 test vectors
--
rfcVectors :: (IsString a, Eq a, Show a) => Harness a -> TestTree
rfcVectors Harness{..} = testGroup "RFC 4648 Test Vectors"
    [ testGroup "std alphabet"
      [ testCaseStd "" ""
      , testCaseStd "f" "Zg=="
      , testCaseStd "f" "Zg=="
      , testCaseStd "fo" "Zm8="
      , testCaseStd "foo" "Zm9v"
      , testCaseStd "foob" "Zm9vYg=="
      , testCaseStd "fooba" "Zm9vYmE="
      , testCaseStd "foobar" "Zm9vYmFy"
      ]
    , testGroup "url-safe alphabet"
      [ testCaseUrl "" ""
      , testCaseUrl "<" "PA=="
      , testCaseUrl "<<" "PDw="
      , testCaseUrl "<<?" "PDw_"
      , testCaseUrl "<<??" "PDw_Pw=="
      , testCaseUrl "<<??>" "PDw_Pz4="
      , testCaseUrl "<<??>>" "PDw_Pz4-"
      ]
    ]
  where
    testCaseStd s t =
      testCaseSteps (show $ if s == "" then "empty" else s) $ \step -> do
        step "encode is sound"
        assertBase64 t @=? encode s

        step "decode is sound - untyped"
        Right s @=? decode (extractBase64 $ encode s)
        Right s @=? decode t

        step "decode is sound - typed"
        s @=? decodeTyped (encode s)
        s @=? decodeTyped (assertBase64 t)

    testCaseUrl s t =
      testCaseSteps (show $ if s == "" then "empty" else s) $ \step -> do
        step "encode is sound"
        assertBase64 t @=? encodeUrl s

        step "decode is sound - untyped"
        Right s @=? decodeUrl (extractBase64 $ encodeUrl s)
        Right s @=? decodeUrl t

        step "decode is sound - typed"
        s @=? decodeUrlTyped (encodeUrl s)
        s @=? decodeUrlTypedPad (encodeUrl s)
        s @=? decodeUrlTyped (assertBase64 @'UrlPadded t)
        s @=? decodeUrlTypedPad (assertBase64 @'UrlPadded t)

-- | Url-safe padding unit tests (stresses entire alphabet)
--
paddingTests
  :: ( IsString a
     , Eq a
     , Show a
     )
  => (a -> Word8)
  -> (a -> Int)
  -> Harness a
  -> TestTree
paddingTests last_ length_ Harness{..} = testGroup "Padding tests"
    [ testGroup "URL decodePadding coherence"
      [ ptest "<" "PA=="
      , ptest "<<" "PDw="
      , ptest "<<?" "PDw_"
      , ptest "<<??" "PDw_Pw=="
      , ptest "<<??>" "PDw_Pz4="
      , ptest "<<??>>" "PDw_Pz4-"
      ]
    , testGroup "URL decodeUnpadded coherence"
      [ utest "<" "PA"
      , utest "<<" "PDw"
      , utest "<<?" "PDw_"
      , utest "<<??" "PDw_Pw"
      , utest "<<??>" "PDw_Pz4"
      , utest "<<??>>" "PDw_Pz4-"
      ]
    , testGroup "url-safe padding case unit tests"
      [ testCase "stress arbitarily padded URL strings" $ do
        decodeUrl "P" @=? Left "Base64-encoded bytestring has invalid size"
        decodeUrl "PA" @=? Right "<"
        decodeUrl "PDw" @=? Right "<<"
        decodeUrl "PDw_" @=? Right "<<?"
      , testCase "stress padded URL strings" $ do
        decodeUrlPad "=" @=? Left "Base64-encoded bytestring has invalid size"
        decodeUrlPad "PA==" @=? Right "<"
        decodeUrlPad "PDw=" @=? Right "<<"
        decodeUrlPad "PDw_" @=? Right "<<?"
      , testCase "stress unpadded URL strings" $ do
        decodeUrlNopad "P" @=? Left "Base64-encoded bytestring has invalid size"
        decodeUrlNopad "PA" @=? Right "<"
        decodeUrlNopad "PDw" @=? Right "<<"
        decodeUrlNopad "PDw_" @=? Right "<<?"
      ]
    ]
  where
    ptest s t =
      testCaseSteps (show $ if t == "" then "empty" else t) $ \step -> do
        let u = decodeUrlNopad t
            v = decodeUrlPad t

        if last_ t == 0x3d then do
          step "Padding required: no padding fails"
          u @=? Left "Base64-encoded bytestring has invalid padding"

          step "Padding required: padding succeeds"
          v @=? Right s
        else do
          step "String has no padding: decodes should coincide"
          u @=? Right s
          v @=? Right s
          v @=? u

    utest s t =
      testCaseSteps (show $ if t == "" then "empty" else t) $ \step -> do
        let u = decodeUrlPad t
            v = decodeUrlNopad t

        if length_ t `mod` 4 == 0 then do
          step "String has no padding: decodes should coincide"
          u @=? Right s
          v @=? Right s
          v @=? u
        else do
          step "Unpadded required: padding fails"
          u @=? Left "Base64-encoded bytestring requires padding"

          step "Unpadded required: unpadding succeeds"
          v @=? Right s

-- | Offset test vectors. This stresses the invalid char + incorrect padding
-- offset error messages
--
offsetVectors :: (IsString a, Eq a, Show a) => Harness a -> TestTree
offsetVectors Harness{..} = testGroup "Offset tests"
  [ testGroup "Invalid padding"
    [ testCase "Invalid staggered padding" $ do
      decodeUrl "=A==" @=? Left "invalid padding near offset: 0"
      decodeUrl "P===" @=? Left "invalid padding near offset: 1"
    , testCase "Invalid character coverage - final chunk" $ do
      decodeUrl "%D==" @=? Left "invalid base64 encoding near offset: 0"
      decodeUrl "P%==" @=? Left "invalid base64 encoding near offset: 1"
      decodeUrl "PD%=" @=? Left "invalid base64 encoding near offset: 2"
      decodeUrl "PA=%" @=? Left "invalid base64 encoding near offset: 3"
      decodeUrl "PDw%" @=? Left "invalid base64 encoding near offset: 3"
    , testCase "Invalid character coverage - decode chunk" $ do
      decodeUrl "%Dw_PDw_" @=? Left "invalid base64 encoding near offset: 0"
      decodeUrl "P%w_PDw_" @=? Left "invalid base64 encoding near offset: 1"
      decodeUrl "PD%_PDw_" @=? Left "invalid base64 encoding near offset: 2"
      decodeUrl "PDw%PDw_" @=? Left "invalid base64 encoding near offset: 3"
    , testCase "Invalid padding in body" $ do
      decodeUrl "PD=_PDw_" @=? Left "invalid padding near offset: 2"
      decodeUrl "PDw=PDw_" @=? Left "invalid padding near offset: 3"
    , testCase "Padding fails everywhere but end" $ do
      decode "=eAoeAo=" @=? Left "invalid padding near offset: 0"
      decode "e=AoeAo=" @=? Left "invalid padding near offset: 1"
      decode "eA=oeAo=" @=? Left "invalid padding near offset: 2"
      decode "eAo=eAo=" @=? Left "invalid padding near offset: 3"
      decode "eAoe=Ao=" @=? Left "invalid padding near offset: 4"
      decode "eAoeA=o=" @=? Left "invalid padding near offset: 5"
    ]
  ]

canonicityTests :: (IsString a, Eq a, Show a) => Harness a -> TestTree
canonicityTests Harness{..} =  testGroup "Canonicity unit tests"
    [ testCase "roundtrip for d ~ ZA==" $ do
      decode "ZE==" @=? Left "non-canonical encoding detected at offset: 1"
      decode "ZK==" @=? Left "non-canonical encoding detected at offset: 1"
      decode "ZA==" @=? Right "d"
    , testCase "roundtrip for f` ~ ZmA=" $ do
      decode "ZmC=" @=? Left "non-canonical encoding detected at offset: 2"
      decode "ZmD=" @=? Left "non-canonical encoding detected at offset: 2"
      decode "ZmA=" @=? Right "f`"

    , testCase "roundtrip for foo` ~ Zm9vYA==" $ do
      decode "Zm9vYE==" @=? Left "non-canonical encoding detected at offset: 5"
      decode "Zm9vYK==" @=? Left "non-canonical encoding detected at offset: 5"
      decode "Zm9vYA==" @=? Right "foo`"

    , testCase "roundtrip for foob` ~ Zm9vYmA=" $ do
      decode "Zm9vYmC=" @=? Left "non-canonical encoding detected at offset: 6"
      decode "Zm9vYmD=" @=? Left "non-canonical encoding detected at offset: 6"
      decode "Zm9vYmA=" @=? Right "foob`"
    ]

-- | Unit test trees for the `decode*With` family of text-valued functions
--
decodeWithVectors
  :: ( IsString a
     , IsString t
     , Eq t
     , Show e
     , Show t
     )
  => (a -> Either e t)
    -- ^ utf8
  -> TextHarness a t
    -- ^ witness to the bytestring-ey dictionaries
  -> Harness a
    -- ^ witness to the text dictionaries
  -> Harness t
  -> TestTree
decodeWithVectors utf8 TextHarness{..} h t = testGroup "DecodeWith* unit tests"
  [ testGroup "decodeWith negative tests"
    [ testCase "decodeWith non-utf8 inputs on decodeUtf8" $ do
      case decodeWith_ utf8 "\1079743" of
        Left (DecodeError _) -> return ()
        _ -> assertFailure "decoding phase"
    , testCase "decodeWith valid utf8 inputs on decodeUtf8" $ do
      case decodeWith_ utf8 (extractBase64 $ encode h "\1079743") of
        Left (ConversionError _) -> return ()
        _ -> assertFailure "conversion phase"
    , testCase "decodeUrlWith non-utf8 inputs on decodeUtf8" $ do
      case decodeUrlWith_ utf8 "\1079743" of
        Left (DecodeError _) -> return ()
        _ -> assertFailure "decoding phase"
    , testCase "decodeUrlWith valid utf8 inputs on decodeUtf8" $ do
      case decodeUrlWith_ utf8 (extractBase64 $ encodeUrl h "\1079743") of
        Left (ConversionError _) -> return ()
        _ -> assertFailure "conversion phase"
    , testCase "decodeUrlPaddedWith non-utf8 inputs on decodeUtf8" $ do
      case decodeUrlPaddedWith_ utf8 "\1079743" of
        Left (DecodeError _) -> return ()
        _ -> assertFailure "decoding phase"
    , testCase "decodePaddedWith valid utf8 inputs on decodeUtf8" $ do
      case decodeUrlPaddedWith_ utf8 (extractBase64 $ encodeUrl h "\1079743") of
        Left (ConversionError _) -> return ()
        _ -> assertFailure "conversion phase"
    , testCase "decodeUnpaddedWith non-utf8 inputs on decodeUtf8" $ do
      case decodeUrlUnpaddedWith_ utf8 "\1079743" of
        Left (DecodeError _) -> return ()
        _ -> assertFailure "decoding phase"
    , testCase "decodeUnpaddedWith valid utf8 inputs on decodeUtf8" $ do
      case decodeUrlUnpaddedWith_ utf8 (extractBase64 $ encodeUrlNopad h "\1079743") of
        Left (ConversionError _) -> return ()
        _ -> assertFailure "conversion phase"
    ]
  , testGroup "decodeWith positive tests"
    [ testCase "decodeWith utf8 inputs on decodeUtf8" $ do
      a <- either (assertFailure . show) pure $ decode t "Zm9vYmFy"
      b <- either (assertFailure . show) pure $ decodeWith_ utf8 "Zm9vYmFy"
      a @=? b
    , testCase "decodeUrlWith utf8 inputs on decodeUtf8" $ do
      a <- either (assertFailure . show) pure $ decodeUrl t "PDw_Pz4-"
      b <- either (assertFailure . show) pure $ decodeUrlWith_ utf8 "PDw_Pz4-"
      a @=? b
    , testCase "decodeUrlPaddedWith utf8 inputs on decodeUtf8" $ do
      a <- either (assertFailure . show) pure $ decodeUrlPad t "PDw_Pz4-"
      b <- either (assertFailure . show) pure $ decodeUrlPaddedWith_ utf8 "PDw_Pz4-"
      a @=? b
    , testCase "decodeUrlUnpaddedWith utf8 inputs on decodeUtf8" $ do
      a <- either (assertFailure . show) pure $ decodeUrlNopad t "PDw_Pz4-"
      b <- either (assertFailure . show) pure $ decodeUrlUnpaddedWith_ utf8 "PDw_Pz4-"
      a @=? b
    ]
  ]

-- | Validity unit tests for the URL workflow
--
validityTests :: IsString a => Harness a -> TestTree
validityTests Harness{..} = testGroup "Validity and correctness unit tests"
  [ testGroup "Validity unit tests"
    [ testCase "Padding tests" $ do
      not (validateUrl "P") @? "P"
      validateUrl "PA" @? "PA"
      validateUrl "PDw" @? "PDw"
      validateUrl "PDw_" @? "PDw_"
      validateUrl "PA==" @? "PA=="
      validateUrl "PDw=" @? "PDw="
      validateUrl "PDw_" @? "PDw_"

    , testCase "Canonicity tests" $ do
      validateUrl "ZK==" @? "ZK=="
      validateUrl "ZE==" @? "ZE=="

      validateUrl "ZA==" @? "ZA=="
      validateUrl "ZK==" @? "ZK=="
      validateUrl "ZK" @? "ZK"

      validateUrl "ZmA=" @? "ZmA="
      validateUrl "ZmC=" @? "ZmC="
      validateUrl "ZmE" @? "ZmE"

      validateUrl "Zm9vYmA=" @? "Zm9vYmA="
      validateUrl "Zm9vYmC=" @? "Zm9vYmC="
      validateUrl "Zm9vYmC" @? "Zm9vYmC"
    ]
  , testGroup "Correctness unit tests"
    [ testCase "Padding tests" $ do
      not (validateUrl "P") @? "P"
      correctUrl "PA" @? "PA"
      correctUrl "PDw" @? "PDw"
      correctUrl "PDw_" @? "PDw_"
      correctUrl "PA==" @? "PA=="
      correctUrl "PDw=" @? "PDw="
      correctUrl "PDw_" @? "PDw_"

    , testCase "Canonicity tests" $ do
      not (correctUrl "ZK==") @? "ZK=="
      not (correctUrl "ZE==") @? "ZE=="
      correctUrl "ZA==" @? "ZA=="

      correctUrl "ZmA=" @? "ZmA="
      not (correctUrl "ZmC=") @? "ZmC="
      not (correctUrl "ZmD") @? "ZmD"

      correctUrl "Zm9vYmA=" @? "Zm9vYmA="
      not (correctUrl "Zm9vYmC=") @? "Zm9vYmC="
      not (correctUrl "Zm9vYmC") @? "Zm9vYmC"
    ]
  ]