From 8b6484c870da4b52e9624d78f82e08f5c79a8ce2 Mon Sep 17 00:00:00 2001 From: Alex David Date: Mon, 1 Sep 2025 21:57:41 -0700 Subject: [PATCH] Generate random DBus bus names for tests --- tests/jail-nix-tests.cabal | 1 + tests/package.yaml | 1 + tests/spec/DBusCombinatorsSpec.hs | 30 ++++++++++++++++-------------- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/tests/jail-nix-tests.cabal b/tests/jail-nix-tests.cabal index f1c1ead..32d4c82 100644 --- a/tests/jail-nix-tests.cabal +++ b/tests/jail-nix-tests.cabal @@ -45,6 +45,7 @@ test-suite spec , interpolate , mtl , process + , random , string-conversions , temporary default-language: Haskell2010 diff --git a/tests/package.yaml b/tests/package.yaml index 8293d0f..ccc851e 100644 --- a/tests/package.yaml +++ b/tests/package.yaml @@ -16,6 +16,7 @@ tests: - interpolate - mtl - process + - random - string-conversions - temporary diff --git a/tests/spec/DBusCombinatorsSpec.hs b/tests/spec/DBusCombinatorsSpec.hs index b8cf165..9ecadcc 100644 --- a/tests/spec/DBusCombinatorsSpec.hs +++ b/tests/spec/DBusCombinatorsSpec.hs @@ -4,12 +4,12 @@ import Control.Concurrent (modifyMVar_, newMVar, readMVar) import Control.Exception (bracket) import DBus qualified import DBus.Client qualified as DBus +import System.Random import Test.Hspec import TestPrelude spec :: Spec spec = inTestM $ do - let mockBusName = "foo.bar" let mockMethod = MockDBusMethod { objPath = "/foo/bar/baz", @@ -19,7 +19,7 @@ spec = inTestM $ do describe "unsafe-dbus" $ do it "gives unfiltered access to DBus method calls" $ do - (sendDBusOutput, methodCalls) <- withDBusClient mockBusName $ \client -> do + (sendDBusOutput, methodCalls) <- withDBusClient $ \mockBusName client -> do withMockDBusMethodHandler client mockMethod "some return value" $ do runNixDrv [i| @@ -32,7 +32,7 @@ spec = inTestM $ do methodCalls `shouldBe` ["some arg"] it "gives unfiltered access to DBus signals" $ do - (_, signals) <- withDBusClient mockBusName $ \client -> do + (_, signals) <- withDBusClient $ \mockBusName client -> do withMockDBusSignalHandler client mockMethod $ do runNixDrv [i| @@ -45,7 +45,7 @@ spec = inTestM $ do describe "dbus" $ do describe "sending signals from within the jail" $ do it "filters all signals if there are no rules" $ do - (_, signals) <- withDBusClient mockBusName $ \client -> do + (_, signals) <- withDBusClient $ \mockBusName client -> do withMockDBusSignalHandler client mockMethod $ do runNixDrv [i| @@ -57,7 +57,7 @@ spec = inTestM $ do describe "calling methods from within the jail" $ do it "allows method calls that match the call filter rules" $ do - (sendDBusOutput, methodCalls) <- withDBusClient mockBusName $ \client -> do + (sendDBusOutput, methodCalls) <- withDBusClient $ \mockBusName client -> do withMockDBusMethodHandler client mockMethod "some return value" $ do runNixDrv [i| @@ -72,7 +72,7 @@ spec = inTestM $ do methodCalls `shouldBe` ["some arg"] it "allows method calls if the receiving bus has `own` privileges" $ do - (sendDBusOutput, methodCalls) <- withDBusClient mockBusName $ \client -> do + (sendDBusOutput, methodCalls) <- withDBusClient $ \mockBusName client -> do withMockDBusMethodHandler client mockMethod "some return value" $ do runNixDrv [i| @@ -87,7 +87,7 @@ spec = inTestM $ do methodCalls `shouldBe` ["some arg"] it "allows method calls if the receiving bus has `talk` privileges" $ do - (sendDBusOutput, methodCalls) <- withDBusClient mockBusName $ \client -> do + (sendDBusOutput, methodCalls) <- withDBusClient $ \mockBusName client -> do withMockDBusMethodHandler client mockMethod "some return value" $ do runNixDrv [i| @@ -102,7 +102,7 @@ spec = inTestM $ do methodCalls `shouldBe` ["some arg"] it "filters method calls if the receiving bus only has `see` privileges" $ do - (sendDBusOutput, methodCalls) <- withDBusClient mockBusName $ \client -> do + (sendDBusOutput, methodCalls) <- withDBusClient $ \mockBusName client -> do withMockDBusMethodHandler client mockMethod "some return value" $ do runNixDrv [i| @@ -117,7 +117,7 @@ spec = inTestM $ do methodCalls `shouldBe` [] it "filters all method calls if there are no rules" $ do - (sendDBusOutput, methodCalls) <- withDBusClient mockBusName $ \client -> do + (sendDBusOutput, methodCalls) <- withDBusClient $ \mockBusName client -> do withMockDBusMethodHandler client mockMethod "some return value" $ do runNixDrv [i| @@ -130,7 +130,7 @@ spec = inTestM $ do methodCalls `shouldBe` [] it "filters method calls if the rules do not match" $ do - (sendDBusOutput, methodCalls) <- withDBusClient mockBusName $ \client -> do + (sendDBusOutput, methodCalls) <- withDBusClient $ \mockBusName client -> do withMockDBusMethodHandler client mockMethod "some return value" $ do let otherMockMethod = mockMethod {objPath = "/some/unrelated/object/path"} runNixDrv @@ -147,14 +147,16 @@ spec = inTestM $ do -- * DBus test helpers -withDBusClient :: DBus.BusName -> (DBus.Client -> TestM a) -> TestM a -withDBusClient busName action = do +withDBusClient :: (DBus.BusName -> DBus.Client -> TestM a) -> TestM a +withDBusClient action = do + rand :: Int <- liftIO randomIO + mockBusName <- liftIO $ DBus.parseBusName $ "id.alexdav.jailnix.test" <> show rand let setup = do client <- liftIO DBus.connectSession - DBus.requestName client busName [DBus.nameDoNotQueue] >>= \case + DBus.requestName client mockBusName [DBus.nameDoNotQueue] >>= \case DBus.NamePrimaryOwner -> pure client reply -> error $ "Got unexpected requestName reply: " <> show reply - lifted (bracket setup DBus.disconnect) action + lifted (bracket setup DBus.disconnect) $ action mockBusName data MockDBusMethod = MockDBusMethod { objPath :: DBus.ObjectPath,