Skip to content

Commit fe62da6

Browse files
committed
Add tests for fillMediaBag/extractMedia.
1 parent df4f13b commit fe62da6

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

pandoc.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,13 +767,15 @@ test-suite test-pandoc
767767
tasty-hunit >= 0.9 && < 0.11,
768768
tasty-quickcheck >= 0.8 && < 0.11,
769769
text >= 1.1.1.0 && < 2.1,
770+
temporary >= 1.1 && < 1.4,
770771
time >= 1.5 && < 1.14,
771772
xml >= 1.3.12 && < 1.4,
772773
zip-archive >= 0.4.3 && < 0.5
773774
other-modules: Tests.Old
774775
Tests.Command
775776
Tests.Helpers
776777
Tests.Shared
778+
Tests.MediaBag
777779
Tests.Readers.LaTeX
778780
Tests.Readers.HTML
779781
Tests.Readers.JATS

test/Tests/MediaBag.hs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{-# LANGUAGE OverloadedStrings #-}
2+
module Tests.MediaBag (tests) where
3+
4+
import Test.Tasty
5+
import Test.Tasty.HUnit
6+
-- import Tests.Helpers
7+
import Text.Pandoc.Class.IO (extractMedia)
8+
import Text.Pandoc.Class (fillMediaBag, runIOorExplode)
9+
import System.IO.Temp (withTempDirectory)
10+
import System.FilePath
11+
import Text.Pandoc.Builder as B
12+
import System.Directory (doesFileExist, copyFile, setCurrentDirectory, getCurrentDirectory)
13+
14+
tests :: [TestTree]
15+
tests = [
16+
testCase "test fillMediaBag & extractMedia" $
17+
withTempDirectory "." "extractMediaTest" $ \tmpdir -> do
18+
olddir <- getCurrentDirectory
19+
setCurrentDirectory tmpdir
20+
copyFile "../../test/lalune.jpg" "moon.jpg"
21+
let d = B.doc $
22+
B.para (B.image "../../test/lalune.jpg" "" mempty) <>
23+
B.para (B.image "moon.jpg" "" mempty) <>
24+
B.para (B.image "data://image/png;base64,cHJpbnQgImhlbGxvIgo=;.lua+%2f%2e%2e%2f%2e%2e%2fa%2elua" "" mempty) <>
25+
B.para (B.image "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" "" mempty)
26+
runIOorExplode $ do
27+
fillMediaBag d
28+
extractMedia "foo" d
29+
exists1 <- doesFileExist ("foo" </> "moon.jpg")
30+
assertBool "file in directory extract with original name" exists1
31+
exists2 <- doesFileExist ("foo" </> "f9d88c3dbe18f6a7f5670e994a947d51216cdf0e.jpg")
32+
assertBool "file above directory extracted with hashed name" exists2
33+
exists3 <- doesFileExist ("foo" </> "2a0eaa89f43fada3e6c577beea4f2f8f53ab6a1d.lua")
34+
exists4 <- doesFileExist "a.lua"
35+
assertBool "data uri with malicious payload does not get written to arbitrary location"
36+
(exists3 && not exists4)
37+
exists5 <- doesFileExist ("foo" </> "d5fceb6532643d0d84ffe09c40c481ecdf59e15a.gif")
38+
assertBool "data uri with gif is properly decoded" exists5
39+
setCurrentDirectory olddir
40+
]

test/test-pandoc.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,15 @@ import qualified Tests.Writers.RST
5050
import qualified Tests.Writers.AnnotatedTable
5151
import qualified Tests.Writers.TEI
5252
import qualified Tests.Writers.Markua
53+
import qualified Tests.MediaBag
5354
import Text.Pandoc.Shared (inDirectory)
5455

5556
tests :: FilePath -> TestTree
5657
tests pandocPath = testGroup "pandoc tests"
5758
[ Tests.Command.tests
5859
, testGroup "Old" (Tests.Old.tests pandocPath)
5960
, testGroup "Shared" Tests.Shared.tests
61+
, testGroup "MediaBag" Tests.MediaBag.tests
6062
, testGroup "Writers"
6163
[ testGroup "Native" Tests.Writers.Native.tests
6264
, testGroup "ConTeXt" Tests.Writers.ConTeXt.tests

0 commit comments

Comments
 (0)