File: Base64.hs

package info (click to toggle)
haskell-hsopenssl 0.11.7.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 556 kB
  • sloc: haskell: 1,562; ansic: 451; makefile: 16
file content (64 lines) | stat: -rw-r--r-- 1,989 bytes parent folder | download | duplicates (3)
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
{-# LANGUAGE CPP               #-}
{-# LANGUAGE OverloadedStrings #-}
-- | Unittest for Base64 [en|de]coding.
module Main (main) where
#if !MIN_VERSION_bytestring(0,9,1)
import Data.Char (ord)
import Data.String
#endif
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BSL
import OpenSSL.EVP.Base64
import TestUtils

-- NOTE: bytestring-0.9.0.4 has these instances too, while
-- bytestring-0.9.0.3 does not. If our bytestring is 0.9.0.4 we'll
-- have duplicate instances, but that's not our fault, is it?
#if !MIN_VERSION_bytestring(0,9,1)
instance IsString BS.ByteString where
  fromString = BS.pack . map (fromIntegral . ord)

-- Note that this instance packs each charactor as a separate lazy chunk.
-- This is to stress the lazy code - not because it's a good idea generally
instance IsString BSL.ByteString where
  fromString = BSL.fromChunks . map (BS.singleton . fromIntegral . ord)
#endif

encodeTests :: IO ()
encodeTests =
    assertFunction "encodeBase64BS" encodeBase64BS pairs
    where
      pairs :: [(BS.ByteString, BS.ByteString)]
      pairs = [ (""   , ""    )
              , ("a"  , "YQ==")
              , ("aa" , "YWE=")
              , ("aaa", "YWFh")
              ]

lazyEncodeTests :: IO ()
lazyEncodeTests =
    assertFunction "encodeBase64LBS" encodeBase64LBS pairs
    where
      pairs :: [(BSL.ByteString, BSL.ByteString)]
      pairs = [ (""   , ""    )
              , ("a"  , "YQ==")
              , ("aa" , "YWE=")
              , ("aaa", "YWFh")
              ]

decodeTests :: IO ()
decodeTests =
    assertFunction "decodeBase64BS" decodeBase64BS pairs
    where
      pairs :: [(BS.ByteString, BS.ByteString)]
      pairs = [ (""                  , ""           )
              , ("aGFza2VsbA=="      , "haskell"    )
              , ("YWJjZGVmZ2hpams="  , "abcdefghijk")
              , ("YWJjZGVmZ2hpams=\n", "abcdefghijk")
              ]

main :: IO ()
main = do
    encodeTests
    lazyEncodeTests
    decodeTests