From eeea68e148c3c0babad6f530c54f256628433091 Mon Sep 17 00:00:00 2001 From: James Crayne Date: Wed, 27 Apr 2016 20:13:10 -0400 Subject: fix first cokiki test --- lib/ProcessUtils.hs | 1 + testkiki/testkiki.hs | 30 +++++++++++++++++++++++------- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/lib/ProcessUtils.hs b/lib/ProcessUtils.hs index 492d666..b89edb9 100644 --- a/lib/ProcessUtils.hs +++ b/lib/ProcessUtils.hs @@ -5,6 +5,7 @@ module ProcessUtils , systemEnv , readPipe , readProcessWithErrorH + , runExternal ) where import GHC.IO.Exception ( ioException, IOErrorType(..) ) diff --git a/testkiki/testkiki.hs b/testkiki/testkiki.hs index ea6ad80..90b6635 100644 --- a/testkiki/testkiki.hs +++ b/testkiki/testkiki.hs @@ -28,6 +28,7 @@ import Crypto.Hash.SHA1 (hash) import System.IO.Unsafe (unsafePerformIO) import ProcessUtils import Data.Bool +import Data.Char #if !MIN_VERSION_base(4,7,0) setEnv k v = System.Posix.Env.setEnv k v True @@ -264,7 +265,7 @@ doTests tkConfig = hspec $ do it "modifies system ssh configuration to respect /var/cache/kiki/ssh_known_hosts." $ onlyIf didInit3 $ do let cfg' = appendpaths tkConfig "3" - home = chroot cfg' "root" + home = "root" -- chroot cfg' "root" gnuhome = home ".gnupg" cfg = cfg' { gnupghome = gnuhome } etcFile = chroot cfg "etc" "ssh" "ssh_config" @@ -280,18 +281,31 @@ doTests tkConfig = hspec $ do let hasSubStr file x = doesFileExist file >>= (bool (return False) $ B.isPrefixOf x . snd . B.breakSubstring x <$> B.readFile file) + let hasUnCommentedSubStr file x = + doesFileExist file >>= (bool (return False) $ do + let dropSp = B.dropWhile isSpace + nonComment x = not ("#" `B.isPrefixOf` x) + lines <- filter nonComment . map dropSp . B.lines <$> B.readFile file + return (any (x `B.isInfixOf`) lines) ) -- does it already mention /var/cache/kiki/ssh_known_hosts? expect not subStr0 <- etcFile `hasSubStr` "/var/cache/kiki/ssh_known_hosts" - cokiki cfg ["ssh-client"] myStdErr + bReplace <- etcFile `hasUnCommentedSubStr` "GlobalKnownHostsFile" + (code,(outs,ers)) <- runExternal (mkCokiki cfg ["ssh-client"]) Nothing + -- outs <- cokiki cfg ["ssh-client"] myStdErr + B.putStrLn outs + B.putStrLn ers -- get counts of lines subtracted, and lines added, expect (1,1) (lost,gained) <- linesSubtractedAndAdded etcFile -- did Sha1 change? expect it did - bChanged <- compareSha1 etcFile + bChanged <- isChangedSha1 etcFile -- does it mention /var/cache/kiki/ssh_known_hosts now? expect it does subStr <- etcFile `hasSubStr` "/var/cache/kiki/ssh_known_hosts" -- new mtime mtime <- getModificationTime etcFile - (lost,gained,bChanged,(subStr0,subStr),compare mtime mtime0) `shouldBe` (1,1,True,(False,True),GT) + if bReplace then + (lost,gained,bChanged,(subStr0,subStr)) `shouldBe` (1,1,True,(False,True)) + else + (lost,gained,bChanged,(subStr0,subStr)) `shouldBe` (0,1,True,(False,True)) describe "cokiki ssh-server" $ do it "modifies system ssh config to use /var/cache/kiki/ssh_host_rsa_key." $ @@ -375,6 +389,8 @@ doTests tkConfig = hspec $ do unsetEnv "GNUPGHOME" return r + mkCokiki config args = unwords ("./dist/build/cokiki/cokiki": args ++ ["--chroot=" ++ chroot config, "--homedir=" ++ gnupghome config]) + cokiki config args eH = do let args' = args ++ ["--chroot=" ++ chroot config, "--homedir=" ++ gnupghome config] readProcessWithErrorH "./dist/build/kiki/kiki" args' "" eH @@ -397,13 +413,13 @@ saveFileInfo file = do readProcess "cp" ["-aR",file,fileDotOld] "" return (mtime,fileDotOld) -compareSha1 :: FilePath -> IO Bool -compareSha1 file = do +isChangedSha1 :: FilePath -> IO Bool +isChangedSha1 file = do let folder = takeDirectory file fileDotOld = file ++ ".old" hash1 <- hash <$> B.readFile fileDotOld hash2 <- hash <$> B.readFile file - return (hash1 == hash2) + return (hash1 /= hash2) getMTimes :: FilePath -> IO (UTCTime, UTCTime) getMTimes file = (,) <$> getModificationTime oldfile <*> getModificationTime file -- cgit v1.2.3