Skip to content
This repository has been archived by the owner on Aug 1, 2023. It is now read-only.

Commit

Permalink
Implement test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiroto Shioi committed Oct 11, 2019
1 parent 31f5c65 commit 3f4934d
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 17 deletions.
1 change: 0 additions & 1 deletion cardano-launcher/cardano-launcher.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ executable cardano-launcher
base >=4.7 && <5
, cardano-prelude
, cardano-launcher
, cardano-sl-x509
-- directory
, directory
, filepath
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ updaterArgs: []
logsPrefix: ${XDG_DATA_HOME}/Daedalus/mainnet/Logs
configuration:
seed: null
filePath: ./configuration/cert-configuration.yaml
filePath: ./cardano-launcher/configuration/cert-configuration.yaml
key: dev
systemStart: null
updateArchive: "main"
Expand Down
25 changes: 14 additions & 11 deletions cardano-launcher/src/Cardano/Shell/Launcher.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ import Control.Exception.Safe (onException)
import Data.X509.Extra (genRSA256KeyPair, validateCertificate,
writeCertificate, writeCredentials)
import Data.X509.Validation (FailedReason)
import System.Directory (createDirectoryIfMissing, doesFileExist)
import System.Directory (createDirectoryIfMissing, doesDirectoryExist,
doesFileExist)
import System.FilePath ((</>))

--------------------------------------------------------------------------------
Expand Down Expand Up @@ -227,18 +228,10 @@ walletRunnerProcess = WalletRunner $ \walletPath walletArgs ->
, Process.std_err = stdStream
}

--------------------------------------------------------------------------------
--
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
-- Types
--------------------------------------------------------------------------------

-- Not used?
-- newtype X509ToolPath = X509ToolPath { getX509ToolPath :: FilePath }
-- deriving (Eq, Show)

newtype TLSPath = TLSPath { getTLSFilePath :: FilePath }
deriving (Eq, Show)

Expand All @@ -248,10 +241,16 @@ newtype TLSPath = TLSPath { getTLSFilePath :: FilePath }

-- | Generation of the TLS certificates.
-- This just covers the generation of the TLS certificates and nothing else.
generateTlsCertificates :: LoggingDependencies -> ConfigurationOptions -> TLSPath -> IO (Either TLSError ())
generateTlsCertificates
:: LoggingDependencies
-> ConfigurationOptions
-> TLSPath
-> IO (Either TLSError ())
generateTlsCertificates externalDependencies' configurationOptions (TLSPath tlsPath) = runExceptT $ do
doesCertConfigExist <- liftIO $ doesFileExist (cfoFilePath configurationOptions)
doesTLSPathExist <- liftIO $ doesDirectoryExist tlsPath
unless doesCertConfigExist $ throwError . CertConfigNotFound . cfoFilePath $ configurationOptions
unless doesTLSPathExist $ throwError . TLSDirectoryNotFound $ tlsPath

let tlsServer = tlsPath </> "server"
let tlsClient = tlsPath </> "client"
Expand All @@ -271,7 +270,7 @@ generateTlsCertificates externalDependencies' configurationOptions (TLSPath tlsP
generateCertificates tlsServer' tlsClient = do

let configFile = cfoFilePath configurationOptions
-- | Configuration key within the config file
-- Configuration key within the config file
let configKey :: ConfigurationKey
configKey = ConfigurationKey . textToFilePath . cfoKey $ configurationOptions

Expand Down Expand Up @@ -325,9 +324,13 @@ data TLSError =
-- ^ Generation of TLS certificates failed dues to following reasons
| InvalidKey Text
-- ^ Given key was invalid therefore @decodeConfigFile@ threw exception
| TLSDirectoryNotFound FilePath
-- ^ TLS path does not exist
deriving (Eq)

instance Show TLSError where
show = \case
CannotGenerateTLS reasons -> "Couldn't generate TLS certificates due to: " <> Prelude.show reasons
CertConfigNotFound filepath -> "Cert configuration file was not found on: " <> Prelude.show filepath
InvalidKey key -> "Cert configuration key value was invalid: " <> Prelude.show key
TLSDirectoryNotFound path -> "Given TLS path does not exist: " <> Prelude.show path
64 changes: 62 additions & 2 deletions cardano-launcher/test/LauncherSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,23 @@ import System.IO.Temp (withSystemTempDirectory)

import Cardano.Shell.CLI (LauncherOptionPath (..),
decodeLauncherOption, setupEnvVars)
import Cardano.Shell.Configuration (LauncherOptions (..),
import Cardano.Shell.Configuration (ConfigurationOptions (..),
LauncherOptions (..),
setWorkingDirectory)
import Cardano.Shell.Launcher (DaedalusExitCode (..),
RestartRunner (..), UpdateRunner (..),
RestartRunner (..), TLSError (..),
TLSPath (..), UpdateRunner (..),
generateTlsCertificates,
handleDaedalusExitCode)
import Cardano.Shell.Types (nullLogging)

-- | The simple launcher spec.
launcherSpec :: Spec
launcherSpec = do
configurationSpec
launcherSystemSpec
setWorkingDirectorySpec
generateTLSCertSpec

-- | The launcher system spec.
launcherSystemSpec :: Spec
Expand Down Expand Up @@ -202,3 +207,58 @@ withSetEnvs path homePath action = bracket
, "DAEDALUS_INSTALL_DIRECTORY"
, "LAUNCHER_CONFIG"
]

generateTLSCertSpec :: Spec
generateTLSCertSpec = describe "TLS certificate generation" $ modifyMaxSuccess (const 1) $ do
it "should generate tls certificates as expected" $ monadicIO $ do
(eTLS, filesExist) <- run $ withSystemTempDirectory "tls-test" $ \tmpDir -> do
let tlsPath = TLSPath $ tmpDir
eTLS <- generateTlsCertificates nullLogging defaultConfigurationOptions tlsPath
filesExist <- and <$> mapM doesFileExist (tlsFiles tmpDir)
return (eTLS, filesExist)
assert $ isRight eTLS
assert filesExist

it "should throw error when TLS path doesn't exist" $ monadicIO $ do
let invalidTLSPath = TLSPath $ "this directory does not exist"
eTLS <- run $ generateTlsCertificates nullLogging defaultConfigurationOptions invalidTLSPath
assert $ eTLS == Left (TLSDirectoryNotFound "this directory does not exist")

it "should throw error when invalid config file path was given" $ monadicIO $ do
let invalidConfigPath = "This path doesn't exist"
(eTLS, filesExist) <- run $ withSystemTempDirectory "tls-test" $ \tmpDir -> do
let tlsPath = TLSPath $ tmpDir
let invalidConfigurationOption = defaultConfigurationOptions { cfoFilePath = invalidConfigPath }
eTLS <- generateTlsCertificates nullLogging invalidConfigurationOption tlsPath
filesExist <- and <$> mapM doesFileExist (tlsFiles tmpDir)
return (eTLS, filesExist)
assert $ eTLS == Left (CertConfigNotFound invalidConfigPath)
assert $ not filesExist

it "should throw error when invalid key was given" $ monadicIO $ do
let invalidKey = "This is invalid key"
(eTLS, filesExist) <- run $ withSystemTempDirectory "tls-test" $ \tmpDir -> do
let tlsPath = TLSPath $ tmpDir
let invalidConfigurationOption = defaultConfigurationOptions { cfoKey = invalidKey}
eTLS <- generateTlsCertificates nullLogging invalidConfigurationOption tlsPath
filesExist <- and <$> mapM doesFileExist (tlsFiles tmpDir)
return (eTLS, filesExist)
assert $ eTLS == Left (InvalidKey invalidKey)
assert $ not filesExist
where
defaultConfigurationOptions :: ConfigurationOptions
defaultConfigurationOptions = ConfigurationOptions {
cfoFilePath = "./configuration/cert-configuration.yaml",
cfoKey = "dev",
cfoSystemStart = Nothing,
cfoSeed = Nothing
}
tlsFiles :: FilePath -> [FilePath]
tlsFiles path =
let clientFiles :: [FilePath]
clientFiles = map (\file -> path </> "client" </> file)
["ca.crt", "client.crt", "client.key", "client.pem"]
serverFiles :: [FilePath]
serverFiles = map (\file -> path </> "server" </> file)
["ca.crt", "server.crt", "server.key", "server.pem"]
in clientFiles <> serverFiles
1 change: 0 additions & 1 deletion nix/.stack.nix/cardano-launcher.nix

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion updater-test-win.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ in pkgs.runCommand "updater_test.zip" { buildInputs = [ pkgs.zip ]; } ''
cp ${crossSelf.nix-tools.cexes.cardano-launcher.mock-installer}/bin/mock-installer.exe ./updater.exe
cp ${pkgsCross.libffi}/bin/libffi-6.dll ./
cp ${./cardano-launcher/configuration/launcher/launcher-config-demo.windows.yaml} ./launcher-config.yaml
cp ${./configuration/cert-configuration.yaml} ./configuration/cert-configuration.yaml
cp ${./cardano-launcher/configuration/cert-configuration.yaml} ./configuration/cert-configuration.yaml
cp ${./configuration/log-configuration.yaml} ./configuration/log-configuration.yaml
chmod +w -R .
cd ..
Expand Down

0 comments on commit 3f4934d

Please sign in to comment.