Skip to content

Commit d420263

Browse files
Add Unix socket test
1 parent c02b8d9 commit d420263

File tree

3 files changed

+79
-59
lines changed

3 files changed

+79
-59
lines changed

grapesy/grapesy.cabal

+9-8
Original file line numberDiff line numberDiff line change
@@ -274,14 +274,15 @@ test-suite test-grapesy
274274

275275
build-depends:
276276
-- Additional dependencies
277-
, proto-lens-runtime >= 0.7 && < 0.8
278-
, QuickCheck >= 2.14 && < 2.16
279-
, serialise >= 0.2 && < 0.3
280-
, tasty >= 1.4 && < 1.6
281-
, tasty-hunit >= 0.10 && < 0.11
282-
, tasty-quickcheck >= 0.10 && < 0.12
283-
, temporary >= 1.3 && < 1.4
284-
, unix >= 2.7 && < 2.9
277+
, filepath >= 1.4.2.1 && < 1.6
278+
, proto-lens-runtime >= 0.7 && < 0.8
279+
, QuickCheck >= 2.14 && < 2.16
280+
, serialise >= 0.2 && < 0.3
281+
, tasty >= 1.4 && < 1.6
282+
, tasty-hunit >= 0.10 && < 0.11
283+
, tasty-quickcheck >= 0.10 && < 0.12
284+
, temporary >= 1.3 && < 1.4
285+
, unix >= 2.7 && < 2.9
285286

286287
test-suite test-stress
287288
import: lang, common-executable-flags

grapesy/test-grapesy/Test/Driver/ClientServer.hs

+65-51
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ data ClientServerConfig = ClientServerConfig {
7575
-- The client will query the server for its port; this makes it possible
7676
-- to use @0@ for 'serverPort', so that the server picks a random
7777
-- available port (this is the default).
78-
serverPort :: PortNumber
78+
serverPort :: Either FilePath PortNumber
7979

8080
-- | Compression algorithms supported by the client
8181
, clientCompr :: Compr.Negotation
@@ -117,7 +117,7 @@ data ContentTypeOverride =
117117

118118
instance Default ClientServerConfig where
119119
def = ClientServerConfig {
120-
serverPort = 0
120+
serverPort = Right 0
121121
, clientCompr = def
122122
, clientInitCompr = Nothing
123123
, serverCompr = def
@@ -366,32 +366,40 @@ withTestServer cfg firstTestFailure handlerLock serverHandlers k = do
366366

367367
let serverConfig :: Server.ServerConfig
368368
serverConfig =
369-
case useTLS cfg of
370-
Nothing -> Server.ServerConfig {
371-
serverInsecure = Just Server.InsecureConfig {
372-
insecureHost = Just "127.0.0.1"
373-
, insecurePort = serverPort cfg
369+
case serverPort cfg of
370+
Left socketPath -> Server.ServerConfig {
371+
serverInsecure = Just Server.InsecureUnix {
372+
insecurePath = socketPath
374373
}
375374
, serverSecure = Nothing
376375
}
377-
Just (TlsFail TlsFailUnsupported) -> Server.ServerConfig {
378-
serverInsecure = Just Server.InsecureConfig {
379-
insecureHost = Just "127.0.0.1"
380-
, insecurePort = serverPort cfg
376+
Right portNumber ->
377+
case useTLS cfg of
378+
Nothing -> Server.ServerConfig {
379+
serverInsecure = Just Server.InsecureConfig {
380+
insecureHost = Just "127.0.0.1"
381+
, insecurePort = portNumber
382+
}
383+
, serverSecure = Nothing
381384
}
382-
, serverSecure = Nothing
383-
}
384-
Just _tlsSetup -> Server.ServerConfig {
385-
serverInsecure = Nothing
386-
, serverSecure = Just $ Server.SecureConfig {
387-
secureHost = "127.0.0.1"
388-
, securePort = serverPort cfg
389-
, securePubCert = pubCert
390-
, secureChainCerts = []
391-
, securePrivKey = privKey
392-
, secureSslKeyLog = SslKeyLogNone
385+
Just (TlsFail TlsFailUnsupported) -> Server.ServerConfig {
386+
serverInsecure = Just Server.InsecureConfig {
387+
insecureHost = Just "127.0.0.1"
388+
, insecurePort = portNumber
389+
}
390+
, serverSecure = Nothing
391+
}
392+
Just _tlsSetup -> Server.ServerConfig {
393+
serverInsecure = Nothing
394+
, serverSecure = Just $ Server.SecureConfig {
395+
secureHost = "127.0.0.1"
396+
, securePort = portNumber
397+
, securePubCert = pubCert
398+
, secureChainCerts = []
399+
, securePrivKey = privKey
400+
, secureSslKeyLog = SslKeyLogNone
401+
}
393402
}
394-
}
395403

396404
serverParams :: Server.ServerParams
397405
serverParams = def {
@@ -437,10 +445,10 @@ simpleTestClient test params testServer delimitTestScope =
437445
runTestClient ::
438446
ClientServerConfig
439447
-> TMVar FirstTestFailure
440-
-> PortNumber
448+
-> Either FilePath PortNumber
441449
-> TestClient
442450
-> IO ()
443-
runTestClient cfg firstTestFailure port clientRun = do
451+
runTestClient cfg firstTestFailure pathOrPort clientRun = do
444452
pubCert <- getDataFileName "grpc-demo.pem"
445453

446454
let clientParams :: Client.ConnParams
@@ -469,36 +477,40 @@ runTestClient cfg firstTestFailure port clientRun = do
469477

470478
clientServer :: Client.Server
471479
clientServer =
472-
case useTLS cfg of
473-
Just tlsSetup ->
474-
Client.ServerSecure
475-
( case tlsSetup of
476-
TlsOk TlsOkCertAsRoot ->
477-
correctClientSetup
478-
TlsOk TlsOkSkipValidation ->
479-
Client.NoServerValidation
480-
TlsFail TlsFailValidation ->
481-
Client.ValidateServer mempty
482-
TlsFail TlsFailUnsupported ->
483-
correctClientSetup
484-
)
485-
-- We enable key logging in the client and disable it in the
486-
-- server. This avoids the client and server trying to write
487-
-- to the same file.
488-
SslKeyLogFromEnv
489-
clientAuthority
490-
491-
Nothing ->
492-
Client.ServerInsecure
493-
clientAuthority
480+
case pathOrPort of
481+
Left socketPath ->
482+
Client.ServerUnix socketPath
483+
Right port ->
484+
case useTLS cfg of
485+
Just tlsSetup ->
486+
Client.ServerSecure
487+
( case tlsSetup of
488+
TlsOk TlsOkCertAsRoot ->
489+
correctClientSetup
490+
TlsOk TlsOkSkipValidation ->
491+
Client.NoServerValidation
492+
TlsFail TlsFailValidation ->
493+
Client.ValidateServer mempty
494+
TlsFail TlsFailUnsupported ->
495+
correctClientSetup
496+
)
497+
-- We enable key logging in the client and disable it in the
498+
-- server. This avoids the client and server trying to write
499+
-- to the same file.
500+
SslKeyLogFromEnv
501+
(clientAuthority port)
502+
503+
Nothing ->
504+
Client.ServerInsecure
505+
(clientAuthority port)
494506
where
495507
correctClientSetup :: Client.ServerValidation
496508
correctClientSetup =
497509
Client.ValidateServer $
498510
Client.certStoreFromPath pubCert
499511

500-
clientAuthority :: Client.Address
501-
clientAuthority =
512+
clientAuthority :: PortNumber -> Client.Address
513+
clientAuthority port =
502514
case useTLS cfg of
503515
Just _tlsSetup -> Client.Address {
504516
addressHost = "127.0.0.1"
@@ -545,12 +557,14 @@ runTestClientServer (ClientServerTest cfg clientRun handlers) = do
545557
let server :: (Server.RunningServer -> IO a) -> IO a
546558
server = withTestServer cfg firstTestFailure serverHandlerLock handlers
547559

548-
let client :: PortNumber -> IO ()
560+
let client :: Either FilePath PortNumber -> IO ()
549561
client port = runTestClient cfg firstTestFailure port clientRun
550562

551563
-- Run the test
552564
server $ \runningServer -> do
553-
port <- Server.getServerPort runningServer
565+
port <- case serverPort cfg of
566+
Left unixPath -> pure $ Left unixPath
567+
Right _ -> Right <$> Server.getServerPort runningServer
554568

555569
withAsync (client port) $ \clientThread -> do
556570
let failure = waitForFailure runningServer clientThread firstTestFailure

grapesy/test-grapesy/Test/Sanity/StreamingType/NonStreaming.hs

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ module Test.Sanity.StreamingType.NonStreaming (tests) where
77
import Data.Word
88
import Test.Tasty
99
import Test.Tasty.HUnit
10+
import System.IO.Temp (getCanonicalTemporaryDirectory)
11+
import System.FilePath ((</>))
1012

1113
import Network.GRPC.Client qualified as Client
1214
import Network.GRPC.Client.Binary qualified as Binary
@@ -25,6 +27,9 @@ tests = testGroup "Test.Sanity.StreamingType.NonStreaming" [
2527
testGroup "increment" [
2628
testCase "default" $
2729
test_increment def
30+
, testCase "unix socket" $ do
31+
tmpDir <- getCanonicalTemporaryDirectory
32+
test_increment def { serverPort = Left (tmpDir </> "grapesy.sock") }
2833
, testGroup "Content-Type" [
2934
testGroup "ok" [
3035
-- Without the +format part

0 commit comments

Comments
 (0)