From 6260d694a99348bc474b87b3d2184ead70cf3511 Mon Sep 17 00:00:00 2001 From: James Crayne Date: Wed, 27 Apr 2016 15:20:53 -0400 Subject: new testcase: cokiki ssh-client --- testkiki/testkiki.hs | 80 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 75 insertions(+), 5 deletions(-) (limited to 'testkiki/testkiki.hs') diff --git a/testkiki/testkiki.hs b/testkiki/testkiki.hs index b693601..2a19889 100644 --- a/testkiki/testkiki.hs +++ b/testkiki/testkiki.hs @@ -1,6 +1,8 @@ {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE DoAndIfThenElse #-} {-# LANGUAGE CPP #-} +{-# LANGUAGE ScopedTypeVariables #-} +{-# LANGUAGE TupleSections #-} #if !MIN_VERSION_base(4,7,0) import qualified System.Posix.Env #endif @@ -256,18 +258,86 @@ doTests tkConfig = hspec $ do -- **** cokiki tests ***** describe "cokiki ssh-client" $ do it "modifies system ssh configuration to respect /var/cache/kiki/ssh_known_hosts." $ do - pending + bDidInit3 <-readIORef didInit3 + if not bDidInit3 then skipThisTest else do + let cfg' = appendpaths tkConfig "3" + home = chroot cfg' "root" + gnuhome = home ".gnupg" + cfg = cfg' { gnupghome = gnuhome } + etcFile = chroot cfg "etc" "ssh" "ssh_config" + etcFileBS = B.pack etcFile + -- dump stderr here + withErrFile (takeDirectory (chroot cfg)) $ \myStdErr -> do + -- initialize config file with actual systems + bNotHere <- not <$> doesFileExist etcFile + when bNotHere $ do + createDirectoryIfMissing True (takeDirectory etcFile) + copyFile "/etc/ssh/ssh_config" etcFile + -- copy ssh_config to ssh_config.old, preserving timestamps + (mtime0,etcFile0) <- saveFileInfo etcFile + let hasSubStr x file = B.isPrefixOf x . snd . B.breakSubstring x <$> B.readFile file + -- does it already mention /var/cache/kiki/ssh_known_hosts? expect not + subStr0 <- etcFileBS `hasSubStr` "/var/cache/kiki/ssh_known_hosts" + cokiki cfg ["ssh-client"] myStdErr + -- get counts of lines subtracted, and lines added, expect (1,1) + (lost,gained) <- linesSubtractedAndAdded etcFile + -- did Sha1 change? expect it did + bChanged <- compareSha1 etcFile + -- does it mention /var/cache/kiki/ssh_known_hosts now? expect it does + subStr <- etcFileBS `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) describe "cokiki ssh-server" $ do it "modifies system ssh config to use /var/cache/kiki/ssh_host_rsa_key." $ do - pending + bDidInit3 <-readIORef didInit3 + if not bDidInit3 then skipThisTest else do + let cfg' = appendpaths tkConfig "3" + home = chroot cfg' "root" + gnuhome = home ".gnupg" + cfg = cfg' { gnupghome = gnuhome } + -- dump stderr here + withErrFile (takeDirectory (chroot cfg)) $ \myStdErr -> do + cokiki cfg ["ssh-server"] myStdErr + pending describe "cokiki strongswan" $ do it "modifies /etc/ipsec.conf to include settings from /var/cache/kiki/ipsec.conf." $ do - pending + bDidInit3 <-readIORef didInit3 + if not bDidInit3 then skipThisTest else do + let cfg' = appendpaths tkConfig "3" + home = chroot cfg' "root" + gnuhome = home ".gnupg" + cfg = cfg' { gnupghome = gnuhome } + -- dump stderr here + withErrFile (takeDirectory (chroot cfg)) $ \myStdErr -> do + cokiki cfg ["strongswan"] myStdErr + pending where skipThisTest = pendingWith "SKIPPING due to prior failure." + + freshStdErrHandle tmpdir = openTempFile tmpdir "err.tmp" + + withErrFile :: FilePath -> (Handle -> IO ()) -> IO () + withErrFile dir action = do + len <- (+1) . length <$> getCurrentDirectory + -- dump stderr here + bracket (freshStdErrHandle dir) + (\(_,h) -> hClose h) $ \(errfile,myStdErr) -> do + flip catch (\(e::SomeException) -> (do + b <- hIsOpen myStdErr + x <- if b then do + x' <- hGetContents myStdErr + hClose myStdErr + return x' + else readFile errfile + let e' = show e + if "Pending" `isPrefixOf` e' then throw e + else return (x,show e) + ) `shouldReturn` (drop len errfile,"EXCEPTION")) $ action myStdErr + kiki'Env config args = do setEnv "GNUPGHOME" (chroot config gnupghome config) let args' = args ++ ["--chroot=" ++ chroot config] @@ -296,9 +366,9 @@ doTests tkConfig = hspec $ do unsetEnv "GNUPGHOME" return r - cokiki config args = do + cokiki config args eH = do let args' = args ++ ["--chroot=" ++ chroot config, "--homedir=" ++ gnupghome config] - readProcess "./dist/build/kiki/kiki" args' "" + readProcessWithErrorH "./dist/build/kiki/kiki" args' "" eH -- UTILS isInfixOf sub str = let (_,match) = B.breakSubstring (B.pack sub) (B.pack str) -- cgit v1.2.3