2019-08-01 00:52:49 +03:00
|
|
|
module AmesTests (tests) where
|
|
|
|
|
2019-08-01 03:27:13 +03:00
|
|
|
import Data.Conduit
|
|
|
|
import Data.Conduit.List hiding (take)
|
|
|
|
import Data.Ord.Unicode
|
2019-08-01 00:52:49 +03:00
|
|
|
import Test.QuickCheck hiding ((.&.))
|
|
|
|
import Test.Tasty
|
|
|
|
import Test.Tasty.QuickCheck
|
|
|
|
import Test.Tasty.TH
|
2020-01-24 08:28:38 +03:00
|
|
|
import Urbit.Arvo
|
2020-06-09 00:27:58 +03:00
|
|
|
import Urbit.EventLog.LMDB
|
2020-01-24 08:28:38 +03:00
|
|
|
import Urbit.King.Config
|
|
|
|
import Urbit.Noun
|
2020-06-08 20:24:05 +03:00
|
|
|
import Urbit.Noun.Time
|
2020-01-24 08:28:38 +03:00
|
|
|
import Urbit.Prelude
|
|
|
|
import Urbit.Vere.Ames
|
|
|
|
import Urbit.Vere.Pier.Types
|
2019-08-01 00:52:49 +03:00
|
|
|
|
2019-09-18 06:33:38 +03:00
|
|
|
import Control.Concurrent (runInBoundThread)
|
2019-08-01 00:52:49 +03:00
|
|
|
import Data.LargeWord (LargeKey(..))
|
|
|
|
import GHC.Natural (Natural)
|
2019-08-01 03:27:13 +03:00
|
|
|
import Network.Socket (tupleToHostAddress)
|
2020-05-30 01:57:35 +03:00
|
|
|
import Urbit.King.App (HasKingId(..))
|
2019-08-01 00:52:49 +03:00
|
|
|
|
2020-06-09 00:27:58 +03:00
|
|
|
import qualified Urbit.EventLog.LMDB as Log
|
2019-08-01 00:52:49 +03:00
|
|
|
|
|
|
|
|
2020-05-30 01:57:35 +03:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
type HasAmes e = (HasLogFunc e, HasNetworkConfig e, HasKingId e)
|
|
|
|
|
2019-08-01 00:52:49 +03:00
|
|
|
-- Utils -----------------------------------------------------------------------
|
|
|
|
|
2019-08-08 01:24:02 +03:00
|
|
|
pid :: KingId
|
|
|
|
pid = KingId 0
|
2019-08-01 00:52:49 +03:00
|
|
|
|
|
|
|
turfEf :: NewtEf
|
|
|
|
turfEf = NewtEfTurf (0, ()) []
|
|
|
|
|
2019-08-01 01:44:14 +03:00
|
|
|
sendEf :: Galaxy -> Wen -> Bytes -> NewtEf
|
2019-12-17 13:14:34 +03:00
|
|
|
sendEf g w bs = NewtEfSend (0, ()) (EachYes g) bs
|
2019-08-01 00:52:49 +03:00
|
|
|
|
2019-10-22 21:25:04 +03:00
|
|
|
data NetworkTestApp = NetworkTestApp
|
|
|
|
{ _ntaLogFunc :: !LogFunc
|
|
|
|
, _ntaNetworkConfig :: !NetworkConfig
|
2020-05-30 01:57:35 +03:00
|
|
|
, _ntaKingId :: !Word16
|
2019-10-22 21:25:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
makeLenses ''NetworkTestApp
|
|
|
|
|
|
|
|
instance HasLogFunc NetworkTestApp where
|
|
|
|
logFuncL = ntaLogFunc
|
|
|
|
|
|
|
|
instance HasNetworkConfig NetworkTestApp where
|
|
|
|
networkConfigL = ntaNetworkConfig
|
|
|
|
|
2020-05-30 01:57:35 +03:00
|
|
|
instance HasKingId NetworkTestApp where
|
|
|
|
kingIdL = ntaKingId
|
|
|
|
|
2019-10-22 21:25:04 +03:00
|
|
|
runNetworkApp :: RIO NetworkTestApp a -> IO a
|
|
|
|
runNetworkApp = runRIO NetworkTestApp
|
2020-05-30 01:57:35 +03:00
|
|
|
{ _ntaLogFunc = mkLogFunc (\_ _ _ _ -> pure ())
|
|
|
|
, _ntaKingId = 34
|
|
|
|
, _ntaNetworkConfig = NetworkConfig { _ncNetMode = NMNormal
|
|
|
|
, _ncAmesPort = Nothing
|
|
|
|
, _ncNoAmes = False
|
|
|
|
, _ncNoHttp = False
|
|
|
|
, _ncNoHttps = False
|
|
|
|
, _ncHttpPort = Nothing
|
|
|
|
, _ncHttpsPort = Nothing
|
|
|
|
, _ncLocalPort = Nothing
|
|
|
|
}
|
2019-10-22 21:25:04 +03:00
|
|
|
}
|
|
|
|
|
2020-05-30 01:57:35 +03:00
|
|
|
runGala
|
2020-06-08 20:24:05 +03:00
|
|
|
:: forall e
|
|
|
|
. HasAmes e
|
|
|
|
=> Word8
|
|
|
|
-> RAcquire e (TQueue EvErr, NewtEf -> IO ())
|
2020-05-30 01:57:35 +03:00
|
|
|
runGala point = do
|
|
|
|
env <- ask
|
|
|
|
que <- newTQueueIO
|
2019-10-10 00:58:27 +03:00
|
|
|
let (_, runAmes) =
|
2020-05-30 01:57:35 +03:00
|
|
|
ames env (fromIntegral point) True (writeTQueue que) noStderr
|
|
|
|
cb <- runAmes
|
2020-06-08 20:24:05 +03:00
|
|
|
io (cb turfEf)
|
2020-05-30 01:57:35 +03:00
|
|
|
pure (que, cb)
|
2019-10-22 21:25:04 +03:00
|
|
|
where
|
|
|
|
noStderr _ = pure ()
|
2019-08-01 00:52:49 +03:00
|
|
|
|
2020-06-02 23:48:07 +03:00
|
|
|
waitForPacket :: TQueue EvErr -> Bytes -> IO Bool
|
2019-08-01 01:44:14 +03:00
|
|
|
waitForPacket q val = go
|
2020-06-08 20:24:05 +03:00
|
|
|
where
|
|
|
|
go = atomically (readTQueue q) >>= \case
|
|
|
|
EvErr (EvBlip (BlipEvNewt (NewtEvBorn (_, ()) ()))) _ -> go
|
|
|
|
EvErr (EvBlip (BlipEvAmes (AmesEvHear () _ bs))) _ -> pure (bs == val)
|
|
|
|
_ -> pure False
|
2019-08-01 00:52:49 +03:00
|
|
|
|
2019-09-18 06:33:38 +03:00
|
|
|
runRAcquire :: RAcquire e a -> RIO e a
|
|
|
|
runRAcquire acq = rwith acq pure
|
2019-08-01 00:52:49 +03:00
|
|
|
|
2020-06-08 20:24:05 +03:00
|
|
|
sendThread :: (NewtEf -> IO ()) -> (Galaxy, Bytes) -> RAcquire e ()
|
2019-09-18 06:33:38 +03:00
|
|
|
sendThread cb (to, val) = void $ mkRAcquire start cancel
|
2019-08-01 01:44:14 +03:00
|
|
|
where
|
|
|
|
start = async $ forever $ do threadDelay 1_000
|
2019-09-18 06:33:38 +03:00
|
|
|
wen <- io $ now
|
2020-06-08 20:24:05 +03:00
|
|
|
io $ cb (sendEf to wen val)
|
2019-08-01 01:44:14 +03:00
|
|
|
threadDelay 10_000
|
2019-08-01 00:52:49 +03:00
|
|
|
|
2019-08-01 01:44:14 +03:00
|
|
|
zodSelfMsg :: Property
|
2019-10-22 21:25:04 +03:00
|
|
|
zodSelfMsg = forAll arbitrary (ioProperty . runNetworkApp . runTest)
|
2020-05-30 01:57:35 +03:00
|
|
|
where
|
|
|
|
runTest
|
|
|
|
:: (HasLogFunc e, HasNetworkConfig e, HasKingId e) => Bytes -> RIO e Bool
|
|
|
|
runTest val = runRAcquire $ do
|
|
|
|
env <- ask
|
|
|
|
(zodQ, zod) <- runGala 0
|
|
|
|
() <- sendThread zod (0, val)
|
|
|
|
liftIO (waitForPacket zodQ val)
|
2019-08-01 01:44:14 +03:00
|
|
|
|
|
|
|
twoTalk :: Property
|
2019-10-22 21:25:04 +03:00
|
|
|
twoTalk = forAll arbitrary (ioProperty . runNetworkApp . runTest)
|
2019-08-01 01:44:14 +03:00
|
|
|
where
|
2020-05-30 01:57:35 +03:00
|
|
|
runTest :: (HasLogFunc e, HasNetworkConfig e, HasKingId e)
|
2019-10-22 21:25:04 +03:00
|
|
|
=> (Word8, Word8, Bytes) -> RIO e Bool
|
2019-08-01 01:44:14 +03:00
|
|
|
runTest (aliceShip, bobShip, val) =
|
|
|
|
if aliceShip == bobShip
|
|
|
|
then pure True
|
|
|
|
else go aliceShip bobShip val
|
|
|
|
|
2020-05-30 01:57:35 +03:00
|
|
|
go :: (HasLogFunc e, HasNetworkConfig e, HasKingId e)
|
2019-10-22 21:25:04 +03:00
|
|
|
=> Word8 -> Word8 -> Bytes -> RIO e Bool
|
2019-09-18 06:33:38 +03:00
|
|
|
go aliceShip bobShip val = runRAcquire $ do
|
2019-08-01 01:44:14 +03:00
|
|
|
(aliceQ, alice) <- runGala aliceShip
|
|
|
|
(bobQ, bob) <- runGala bobShip
|
2019-12-19 17:13:20 +03:00
|
|
|
sendThread alice (Patp bobShip, val)
|
|
|
|
sendThread bob (Patp aliceShip, val)
|
2019-08-01 01:44:14 +03:00
|
|
|
liftIO (waitForPacket aliceQ val >> waitForPacket bobQ val)
|
2019-08-01 00:52:49 +03:00
|
|
|
|
|
|
|
tests :: TestTree
|
|
|
|
tests =
|
|
|
|
testGroup "Ames"
|
|
|
|
[ localOption (QuickCheckTests 10) $
|
|
|
|
testProperty "Zod can send a message to itself" $
|
|
|
|
zodSelfMsg
|
2019-12-11 10:14:51 +03:00
|
|
|
|
|
|
|
-- TODO Why doesn't this work in CI?
|
|
|
|
-- , localOption (QuickCheckTests 10) $
|
|
|
|
-- testProperty "Two galaxies can talk" $
|
|
|
|
-- twoTalk
|
2019-08-01 00:52:49 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
-- Generate Arbitrary Values ---------------------------------------------------
|
|
|
|
|
|
|
|
arb :: Arbitrary a => Gen a
|
|
|
|
arb = arbitrary
|
|
|
|
|
|
|
|
instance Arbitrary Ipv4 where arbitrary = Ipv4 <$> arb
|
|
|
|
instance Arbitrary Port where arbitrary = Port <$> arb
|
|
|
|
instance Arbitrary Wen where arbitrary = Wen <$> arb
|
|
|
|
instance Arbitrary Gap where arbitrary = Gap . abs <$> arb
|
2019-08-01 01:44:14 +03:00
|
|
|
instance Arbitrary Bytes where arbitrary = pure (MkBytes "wtfbbq")
|
|
|
|
-- MkBytes . take 100 <$> arb
|
2019-08-01 00:52:49 +03:00
|
|
|
|
2019-12-19 17:13:20 +03:00
|
|
|
instance Arbitrary a => Arbitrary (Patp a) where
|
|
|
|
arbitrary = Patp <$> arb
|
|
|
|
|
2019-08-01 00:52:49 +03:00
|
|
|
instance Arbitrary ByteString where
|
|
|
|
arbitrary = pack <$> arbitrary
|
|
|
|
|
|
|
|
instance Arbitrary Natural where
|
|
|
|
arbitrary = fromIntegral . abs <$> (arbitrary :: Gen Integer)
|
|
|
|
|
|
|
|
instance (Arbitrary a, Arbitrary b) => Arbitrary (LargeKey a b) where
|
|
|
|
arbitrary = LargeKey <$> arb <*> arb
|
|
|
|
|
2019-08-01 01:44:14 +03:00
|
|
|
genIpv4 :: Gen Ipv4
|
|
|
|
genIpv4 = do
|
|
|
|
x <- arbitrary
|
|
|
|
if (x == 0 || (x≥256 && x≤512))
|
|
|
|
then genIpv4
|
|
|
|
else pure (Ipv4 x)
|
|
|
|
|
2019-09-18 06:33:38 +03:00
|
|
|
instance Arbitrary Text where
|
|
|
|
arbitrary = pack <$> arb
|
|
|
|
|
|
|
|
instance Arbitrary Cord where
|
|
|
|
arbitrary = Cord <$> arb
|
|
|
|
|
|
|
|
instance Arbitrary BigCord where
|
|
|
|
arbitrary = BigCord <$> arb
|
|
|
|
|
2019-08-01 00:52:49 +03:00
|
|
|
instance Arbitrary AmesDest where
|
2019-12-17 13:14:34 +03:00
|
|
|
arbitrary = oneof [ EachYes <$> arb
|
|
|
|
, EachNo <$> arb
|
2019-08-01 00:52:49 +03:00
|
|
|
]
|
|
|
|
|
2019-12-17 13:14:34 +03:00
|
|
|
instance Arbitrary a => Arbitrary (Jammed a) where
|
|
|
|
arbitrary = Jammed <$> arbitrary
|
|
|
|
|
|
|
|
instance Arbitrary AmesAddress where
|
|
|
|
arbitrary = AAIpv4 <$> arb <*> arb
|
|
|
|
|
2019-08-01 00:52:49 +03:00
|
|
|
instance Arbitrary Ship where
|
|
|
|
arbitrary = Ship <$> arb
|
|
|
|
|
|
|
|
instance Arbitrary LogIdentity where
|
|
|
|
arbitrary = LogIdentity <$> arb <*> arb <*> arb
|