Skip to content

Commit

Permalink
landlock: improve README.lhs test
Browse files Browse the repository at this point in the history
The test used to have a comment explaining what the code *should* do,
and failed on systems where the Landlock ABI supported by the kernel is
higher than the ABI supported by this library.

Hence, updating the test to do what production code could/should do.
  • Loading branch information
NicolasT committed Apr 1, 2024
1 parent 2952337 commit e1f0c39
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
10 changes: 7 additions & 3 deletions landlock/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,18 @@ main = withFakeRoot $ \root -> do
readFile ("/etc" </> "hosts")

where
lookupAccessFsFlags version = case lookup version accessFsFlags of
-- In a production implementation, we'd lookup the "best matching" flag
-- set, not require an exact match.
lookupAccessFsFlags version = case lookupMax version accessFsFlags of
Nothing -> fail "Unsupported ABI version"
Just flags -> return flags

assertPermissionDenied act = handleJust permissionDenied return $ do
_ <- act
fail "Expected permission error"
permissionDenied e = if isPermissionError e then Just () else Nothing

lookupMax key = fmap snd
. listToMaybe
. reverse
. sort
. filter (\(k, _) -> key >= k)
```
6 changes: 6 additions & 0 deletions landlock/test/ReadmeUtils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ module ReadmeUtils
handleJust,
-- Control.Monad
unless,
-- Data.List
sort,
-- Data.Maybe
listToMaybe,
-- System.Directory
removeFile,
-- System.FilePath
Expand All @@ -22,6 +26,8 @@ where

import Control.Exception.Base (handleJust)
import Control.Monad (unless)
import Data.List (sort)
import Data.Maybe (listToMaybe)
import System.Directory (createDirectory, removeFile)
import System.FilePath ((</>))
import System.IO (IOMode (..), hPutStrLn, withFile)
Expand Down

0 comments on commit e1f0c39

Please sign in to comment.