Skip to content

Commit 6059598

Browse files
committed
Add tests for long arrays of strings. Bump bytebuild for buffer overflow fix.
1 parent c997841 commit 6059598

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

json-syntax.cabal

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ library
3131
, array-builder >=0.1 && <0.2
3232
, array-chunks >=0.1.3 && <0.2
3333
, base >=4.12 && <5
34-
, bytebuild >=0.3.4 && <0.4
34+
, bytebuild >=0.3.8 && <0.4
3535
, byteslice >=0.1.3 && <0.3
3636
, bytesmith >=0.3.2 && <0.4
3737
, bytestring >=0.10.8 && <0.11
@@ -52,19 +52,21 @@ test-suite test
5252
Twitter100
5353
ghc-options: -Wall -O2
5454
build-depends:
55+
, QuickCheck >=2.14.2
5556
, aeson
5657
, array-chunks
5758
, base >=4.12.0.0 && <5
59+
, bytebuild
5860
, byteslice >=0.1.3
5961
, bytestring
6062
, json-syntax
6163
, neat-interpolation >=0.3.2
6264
, primitive
6365
, scientific
6466
, scientific-notation >=0.1.1
65-
, bytebuild
6667
, tasty >=1.2.3 && <1.3
6768
, tasty-hunit >=0.10.0.2 && <0.11
69+
, tasty-quickcheck >=0.10.1.2 && <0.11
6870
, text >=1.2
6971
, text-short
7072
, unordered-containers

test/Main.hs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
{-# language LambdaCase #-}
22
{-# language OverloadedStrings #-}
3+
{-# language ScopedTypeVariables #-}
34

45
import Control.Monad (when)
56
import Data.ByteString.Short.Internal (ShortByteString(SBS))
67
import Data.Bytes (Bytes)
78
import Data.Primitive (ByteArray(ByteArray))
89
import Data.Scientific (Scientific,scientific)
10+
import Test.QuickCheck ((===))
911
import Data.Text.Short (ShortText)
1012
import Test.Tasty (defaultMain,testGroup,TestTree)
1113
import Test.Tasty.HUnit ((@=?))
@@ -20,7 +22,9 @@ import qualified Data.Number.Scientific as SCI
2022
import qualified Data.Text.Short as TS
2123
import qualified GHC.Exts as Exts
2224
import qualified Json as J
25+
import qualified Test.QuickCheck as QC
2326
import qualified Test.Tasty.HUnit as THU
27+
import qualified Test.Tasty.QuickCheck as TQC
2428

2529
main :: IO ()
2630
main = defaultMain tests
@@ -77,6 +81,26 @@ tests = testGroup "Tests"
7781
BChunks.concat (Builder.run 1 (J.encode (J.String "Hello\nWorld")))
7882
@=?
7983
Bytes.fromLatinString "\"Hello\\nWorld\""
84+
, TQC.testProperty "M" $ QC.forAll (jsonFromPrintableStrings <$> QC.vectorOf 10 QC.arbitrary) $ \val0 -> do
85+
let enc = BChunks.concat (Builder.run 128 (J.encode val0))
86+
case J.decode enc of
87+
Left _ -> QC.property False
88+
Right val1 -> val0 === val1
89+
, TQC.testProperty "N" $ QC.forAll (jsonFromPrintableStrings <$> QC.vectorOf 400 QC.arbitrary) $ \val0 -> do
90+
let enc = BChunks.concat (Builder.run 128 (J.encode val0))
91+
case J.decode enc of
92+
Left e -> QC.counterexample (show e) False
93+
Right val1 -> val0 === val1
94+
, TQC.testProperty "O" $ QC.forAll (jsonFromAsciiStrings <$> QC.vectorOf 10 QC.arbitrary) $ \val0 -> do
95+
let enc = BChunks.concat (Builder.run 128 (J.encode val0))
96+
case J.decode enc of
97+
Left _ -> QC.property False
98+
Right val1 -> val0 === val1
99+
, TQC.testProperty "P" $ QC.forAll (jsonFromAsciiStrings <$> QC.vectorOf 400 QC.arbitrary) $ \val0 -> do
100+
let enc = BChunks.concat (Builder.run 128 (J.encode val0))
101+
case J.decode enc of
102+
Left e -> QC.counterexample (show e) False
103+
Right val1 -> val0 === val1
80104
, THU.testCase "Twitter100" $
81105
case J.decode (Bytes.fromByteArray encodedTwitter100) of
82106
Left _ -> fail "nope"
@@ -91,6 +115,12 @@ tests = testGroup "Tests"
91115
Right j' -> when (j /= j') (fail "document was not the same after roundtrip")
92116
]
93117

118+
jsonFromPrintableStrings :: [QC.PrintableString] -> J.Value
119+
jsonFromPrintableStrings xs = J.Array (Exts.fromList (map (J.String . TS.pack . QC.getPrintableString) xs))
120+
121+
jsonFromAsciiStrings :: [QC.ASCIIString] -> J.Value
122+
jsonFromAsciiStrings xs = J.Array (Exts.fromList (map (J.String . TS.pack . QC.getASCIIString) xs))
123+
94124
toBadSci :: SCI.Scientific -> Scientific
95125
toBadSci = SCI.withExposed
96126
(\a b -> scientific (fromIntegral a) b)

0 commit comments

Comments
 (0)