File: Tests.hs

package info (click to toggle)
haskell-snap-server 1.1.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 536 kB
  • sloc: haskell: 5,445; ansic: 4; makefile: 2
file content (262 lines) | stat: -rw-r--r-- 10,269 bytes parent folder | download | duplicates (4)
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
{-# LANGUAGE BangPatterns             #-}
{-# LANGUAGE CPP                      #-}
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE OverloadedStrings        #-}
module System.SendFile.Tests (tests) where

------------------------------------------------------------------------------
import           Control.Concurrent.MVar        (MVar, modifyMVar, modifyMVar_, newMVar, readMVar)
import           Control.Exception              (evaluate)
import           Data.ByteString.Builder        (byteString)
import qualified Data.ByteString.Char8          as S
import           Foreign.C.Error                (Errno (..), eAGAIN, eCONNRESET, eOK)
import           Foreign.C.Types                (CChar, CInt (..), CSize)
import           Foreign.Storable               (peek)
import           Test.Framework                 (Test)
import           Test.Framework.Providers.HUnit (testCase)
import           Test.HUnit                     (assertEqual)
------------------------------------------------------------------------------
import           Snap.Test.Common               (expectException)
import qualified System.SendFile                as SF

#if defined(LINUX)
import           Control.Monad                  (void)
import           Foreign.Ptr                    (Ptr, nullPtr)
import           System.Posix.Types             (COff, CSsize, Fd)
import qualified System.SendFile.Linux          as SFI
#elif defined(FREEBSD)
import           Control.Monad                  (void)
import           Foreign.Ptr                    (Ptr)
import           Foreign.Storable
import           System.Posix.Types             (COff, Fd)
import qualified System.SendFile.FreeBSD        as SFI
#elif defined(OSX)
import           Control.Monad                  (void, when)
import           Foreign.Ptr                    (Ptr)
import           Foreign.Storable               (poke)
import           System.Posix.Types             (COff, Fd)
import qualified System.SendFile.Darwin         as SFI
#endif


------------------------------------------------------------------------------
tests :: [Test]
tests = [ testSendHeaders
        , testSendHeaderCrash
        , testSendFile
        , testSendFileCrash
        , testSendFileZero
        , testTrivials
        ]


------------------------------------------------------------------------------
testSendHeaders :: Test
testSendHeaders = testCase "sendfile/sendHeaders" $ do
    callLog <- newMVar []
    sampleData <- newMVar sampleActions
    nWaits <- newMVar (0 :: Int)
    let bumpWaits = \x -> x `seq` modifyMVar_ nWaits (return . (+1))
    SF.sendHeadersImpl (sendHeadersMockSendFunc sampleData callLog) bumpWaits
                       builder 100
    [c1, c2, c3] <- readMVar callLog
    assertEqual "sendHeaders1" c1 c2
    assertEqual "sendHeaders2" 8 (_sz c3)
    readMVar nWaits >>= assertEqual "sendHeaders3" 1

  where
    builder       = byteString $ S.replicate 10 ' '
    sampleActions = [ c_set_errno eAGAIN >> return (-1)
                    , return 2
                    , return 8
                    ]


------------------------------------------------------------------------------
testSendHeaderCrash :: Test
testSendHeaderCrash = testCase "sendfile/sendHeaders/crash" $ do
    callLog <- newMVar []
    sampleData <- newMVar sampleActions
    nWaits <- newMVar (0 :: Int)
    let bumpWaits = \x -> x `seq` modifyMVar_ nWaits (return . (+1))
    expectException $
        SF.sendHeadersImpl (sendHeadersMockSendFunc sampleData callLog) bumpWaits
                           builder 100
  where
    builder       = byteString $ S.replicate 10 ' '
    sampleActions = [ c_set_errno eCONNRESET >> return (-1) ]


------------------------------------------------------------------------------
testTrivials :: Test
testTrivials = testCase "sendfile/trivials" $
               void (evaluate $ length SF.sendFileMode)


------------------------------------------------------------------------------
data SendHeadersCallLog = SendHeadersCallLog {
      _fd    :: Fd
    , _str   :: Ptr CChar
    , _sz    :: CSize
    , _flags :: CInt
    }
  deriving (Eq, Show, Ord)


------------------------------------------------------------------------------
sendHeadersMockSendFunc :: MVar [IO CSize]             -- ^ sample outputs
                        -> MVar [SendHeadersCallLog]   -- ^ log of calls
                        -> Fd -> Ptr CChar -> CSize -> CInt -> IO CSize
sendHeadersMockSendFunc sampleData callLog !fd !cstr !clen !flags = do
    modifyMVar_ callLog (return . (++ [SendHeadersCallLog fd cstr clen flags]))
    x <- modifyMVar sampleData $ \xs -> return $!
            if null xs then ([], return Nothing) else (tail xs, fmap Just $! head xs)
    x >>= maybe (c_set_errno eCONNRESET >> return (-1))
                (return)


foreign import ccall unsafe "set_errno" c_set_errno :: Errno -> IO ()


------------------------------------------------------------------------------
-- Testing for internal sendfile via dep injection
#if defined(LINUX)
data SendFileCallLog = SendFileCallLog {
      _sf_fd1 :: Fd
    , _sf_fd2 :: Fd
    , _sf_off :: COff
    , _sf_sz  :: CSize
    }
  deriving (Eq, Show, Ord)


------------------------------------------------------------------------------
sendFileMockSendFunc :: MVar [IO CSize]          -- ^ sample outputs
                     -> MVar [SendFileCallLog]   -- ^ log of calls
                     -> Fd -> Fd -> Ptr COff -> CSize -> IO CSsize
sendFileMockSendFunc sampleData callLog !fd1 !fd2 !cstr !clen = do
    cp <- if cstr == nullPtr then return (-1) else peek cstr

    modifyMVar_ callLog (return . (++ [SendFileCallLog fd1 fd2 cp clen]))
    x <- modifyMVar sampleData $ \xs -> return $!
            if null xs then ([], return Nothing) else (tail xs, fmap Just $! head xs)
    x >>= maybe (c_set_errno eCONNRESET >> return (-1))
                (return . fromIntegral)

#elif defined(FREEBSD)
data SendFileCallLog = SendFileCallLog {
      _sf_fd1 :: Fd
    , _sf_fd2 :: Fd
    , _sf_off :: COff
    , _sf_sz  :: CSize
    }
  deriving (Eq, Show, Ord)


------------------------------------------------------------------------------
sendFileMockSendFunc :: MVar [IO CInt]          -- ^ sample outputs
                     -> MVar [SendFileCallLog]   -- ^ log of calls
                     -> Fd -> Fd -> COff -> CSize -> Ptr () -> Ptr COff
                     -> CInt -> IO CInt
sendFileMockSendFunc sampleData callLog !fd1 !fd2 !off !clen !_ !pbytes !_ = do
    modifyMVar_ callLog (return . (++ [SendFileCallLog fd1 fd2 off clen]))
    x <- modifyMVar sampleData $ \xs -> return $!
            if null xs then ([], return Nothing) else (tail xs, fmap Just $! head xs)
    x >>= maybe (c_set_errno eCONNRESET >> return (-1)) return


#elif defined(OSX)
data SendFileCallLog = SendFileCallLog {
      _sf_fd1 :: Fd
    , _sf_fd2 :: Fd
    , _sf_off :: COff
    , _sf_sz  :: COff
    }
  deriving (Eq, Show, Ord)


------------------------------------------------------------------------------
sendFileMockSendFunc :: MVar [IO CInt]          -- ^ sample outputs
                     -> MVar [SendFileCallLog]   -- ^ log of calls
                     -> Fd -> Fd -> COff -> Ptr COff -> IO CInt
sendFileMockSendFunc sampleData callLog !fd1 !fd2 !off !pnbytes = do
    !clen <- peek pnbytes
    modifyMVar_ callLog (return . (++ [SendFileCallLog fd1 fd2 off clen]))
    x <- modifyMVar sampleData $ \xs -> return $!
            if null xs then ([], return Nothing) else (tail xs, fmap Just $! head xs)
    x >>= maybe (c_set_errno eCONNRESET >> return (-1))
                (\l -> do when (l > 0) (poke pnbytes (fromIntegral l))
                          return l)

#endif


------------------------------------------------------------------------------
testSendFile :: Test
testSendFile = testCase "sendfile/sendfile-impl" $ do
    callLog <- newMVar []
    sampleData <- newMVar sampleActions
    nWaits <- newMVar (0 :: Int)
    let bumpWaits = \x -> x `seq` modifyMVar_ nWaits (return . (+1))
    SFI.sendFileImpl (sendFileMockSendFunc sampleData callLog) bumpWaits
                       100 101 0 10
    [c1, c2] <- readMVar callLog
    assertEqual "sendFile1" c1 c2
    assertEqual "sendFile2" 10 (_sf_sz c2)
    readMVar nWaits >>= assertEqual "sendFile3" 1

    modifyMVar_ callLog $ const $ return []
    modifyMVar_ nWaits $ const $ return 0
    modifyMVar_ sampleData $ const $ return sampleActions2

    SFI.sendFileImpl (sendFileMockSendFunc sampleData callLog) bumpWaits
                       100 101 1 9

    [_, c4] <- readMVar callLog

    readMVar nWaits >>= assertEqual "nwaits-2" 1
    assertEqual "sendFile3" 9 (_sf_sz c4)
    assertEqual "sendFile3-off" 1 (_sf_off c4)


  where
    sampleActions = [ c_set_errno eAGAIN >> return (-1)
                    , c_set_errno eOK >> return 2
                    ]

    sampleActions2 = [ c_set_errno eAGAIN >> return (-1)
                     , c_set_errno eOK >> return 2
                     ]


------------------------------------------------------------------------------
testSendFileZero :: Test
testSendFileZero = testCase "sendfile/sendfile-zero" $ do
    callLog <- newMVar []
    sampleData <- newMVar sampleActions
    nWaits <- newMVar (0 :: Int)
    let bumpWaits = \x -> x `seq` modifyMVar_ nWaits (return . (+1))
    c <- SFI.sendFileImpl (sendFileMockSendFunc sampleData callLog) bumpWaits
                           100 101 0 0
    readMVar callLog >>= assertEqual "empty call log" []
    readMVar nWaits >>= assertEqual "no waits" 0
    assertEqual "no bytes read" 0 c

  where
    sampleActions = [ c_set_errno eAGAIN >> return (-1)
                    , c_set_errno eOK >> return 2
                    ]


------------------------------------------------------------------------------
testSendFileCrash :: Test
testSendFileCrash = testCase "sendfile/sendFile/crash" $ do
    callLog <- newMVar []
    sampleData <- newMVar sampleActions
    nWaits <- newMVar (0 :: Int)
    let bumpWaits = \x -> x `seq` modifyMVar_ nWaits (return . (+1))
    expectException $
        SFI.sendFileImpl (sendFileMockSendFunc sampleData callLog) bumpWaits
                         100 101 0 10
  where
    sampleActions = [ c_set_errno eCONNRESET >> return (-1) ]