on builder help sign in
text/tests/Regressions.hs
51 lines 2.0 kB history raw
1{-# LANGUAGE OverloadedStrings, ScopedTypeVariables #-}
3-- Regression tests for specific bugs.
5import Control.Exception (SomeException, handle)
6import System.IO
7import qualified Data.ByteString as B
8import qualified Data.ByteString.Lazy as LB
9import qualified Data.Text as T
10import qualified Data.Text.IO as T
11import qualified Data.Text.Lazy as LT
12import qualified Data.Text.Lazy.Encoding as LE
13import qualified Test.Framework as F
14import qualified Test.Framework.Providers.HUnit as F
15import Test.HUnit (assertFailure)
16import TestUtils (withTempFile)
18-- Reported by Michael Snoyman: UTF-8 encoding a large lazy bytestring
19-- caused either a segfault or attempt to allocate a negative number
20-- of bytes.
21lazy_encode_crash = withTempFile $ \ _ h ->
22 LB.hPut h . LE.encodeUtf8 . LT.pack . replicate 100000 $ 'a'
24-- Reported by Pieter Laeremans: attempting to read an incorrectly
25-- encoded file can result in a crash in the RTS (i.e. not merely an
26-- exception).
27hGetContents_crash = withTempFile $ \ path h -> do
28 B.hPut h (B.pack [0x78, 0xc4 ,0x0a]) >> hClose h
29 h' <- openFile path ReadMode
30 hSetEncoding h' utf8
31 handle (\(_::SomeException) -> return ()) $
32 T.hGetContents h' >> assertFailure "T.hGetContents should crash"
34-- Reported by Ian Lynagh: attempting to allocate a sufficiently large
35-- string (via either Array.new or Text.replicate) could result in an
36-- integer overflow.
37replicate_crash = handle (\(_::SomeException) -> return ()) $
38 T.replicate (2^power) "0123456789abcdef" `seq`
39 assertFailure "T.replicate should crash"
40 where
41 power | maxBound == (2147483647::Int) = 28
42 | otherwise = 60 :: Int
44tests :: F.Test
45tests = F.testGroup "crashers" [
46 F.testCase "hGetContents_crash" hGetContents_crash
47 , F.testCase "lazy_encode_crash" lazy_encode_crash
48 , F.testCase "replicate_crash" replicate_crash
49 ]
51main = F.defaultMain [tests]