Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions network/test/cohort2/unicast_authorization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,19 @@ func (u *UnicastAuthorizationTestSuite) stopNetworksAndLibp2pNodes() {
unittest.RequireComponentsDoneBefore(u.T(), 1*time.Second, u.senderNetwork, u.receiverNetwork)
}

// requireStreamAcceptedOrReset asserts that a unicast send either succeeded or failed with a
// benign "stream reset" error. When the receiver rejects an unauthorized message, it resets the
// inbound stream (see Network.handleIncomingStream). That reset races with the sender closing its
// side of the stream, so the sender's Unicast call may return a "stream reset" error. This error
// is benign (see Node.OpenAndWriteOnStream); for these tests the authoritative assertion is that
// the receiver reported the violation via the slashing violations consumer.
func (u *UnicastAuthorizationTestSuite) requireStreamAcceptedOrReset(err error) {
if err != nil {
require.ErrorContains(u.T(), err, "stream reset",
"receiver may reset the stream before the sender closes it")
}
}

// TestUnicastAuthorization_UnstakedPeer tests that messages sent via unicast by an unstaked peer is correctly rejected.
func (u *UnicastAuthorizationTestSuite) TestUnicastAuthorization_UnstakedPeer() {
slashingViolationsConsumer := mocknetwork.NewViolationsConsumer(u.T())
Expand Down Expand Up @@ -155,7 +168,7 @@ func (u *UnicastAuthorizationTestSuite) TestUnicastAuthorization_UnstakedPeer()
err = senderCon.Unicast(&libp2pmessage.TestMessage{
Text: string("hello"),
}, u.receiverID.NodeID)
require.NoError(u.T(), err)
u.requireStreamAcceptedOrReset(err)

// wait for slashing violations consumer mock to invoke run func and close ch if expected method call happens
unittest.RequireCloseBefore(u.T(), u.waitCh, u.channelCloseDuration, "could close ch on time")
Expand Down Expand Up @@ -202,7 +215,7 @@ func (u *UnicastAuthorizationTestSuite) TestUnicastAuthorization_EjectedPeer() {
err = senderCon.Unicast(&libp2pmessage.TestMessage{
Text: string("hello"),
}, u.receiverID.NodeID)
require.NoError(u.T(), err)
u.requireStreamAcceptedOrReset(err)

// wait for slashing violations consumer mock to invoke run func and close ch if expected method call happens
unittest.RequireCloseBefore(u.T(), u.waitCh, u.channelCloseDuration, "could close ch on time")
Expand Down Expand Up @@ -244,7 +257,7 @@ func (u *UnicastAuthorizationTestSuite) TestUnicastAuthorization_UnauthorizedPee
err = senderCon.Unicast(&libp2pmessage.TestMessage{
Text: string("hello"),
}, u.receiverID.NodeID)
require.NoError(u.T(), err)
u.requireStreamAcceptedOrReset(err)

// wait for slashing violations consumer mock to invoke run func and close ch if expected method call happens
unittest.RequireCloseBefore(u.T(), u.waitCh, u.channelCloseDuration, "could close ch on time")
Expand Down Expand Up @@ -296,7 +309,7 @@ func (u *UnicastAuthorizationTestSuite) TestUnicastAuthorization_UnknownMsgCode(

// send message via unicast
err = senderCon.Unicast("hello!", u.receiverID.NodeID)
require.NoError(u.T(), err)
u.requireStreamAcceptedOrReset(err)

// wait for slashing violations consumer mock to invoke run func and close ch if expected method call happens
unittest.RequireCloseBefore(u.T(), u.waitCh, u.channelCloseDuration, "could close ch on time")
Expand Down Expand Up @@ -346,7 +359,7 @@ func (u *UnicastAuthorizationTestSuite) TestUnicastAuthorization_WrongMsgCode()
err = senderCon.Unicast(&libp2pmessage.TestMessage{
Text: string("hello"),
}, u.receiverID.NodeID)
require.NoError(u.T(), err)
u.requireStreamAcceptedOrReset(err)

// wait for slashing violations consumer mock to invoke run func and close ch if expected method call happens
unittest.RequireCloseBefore(u.T(), u.waitCh, u.channelCloseDuration, "could close ch on time")
Expand Down Expand Up @@ -422,7 +435,7 @@ func (u *UnicastAuthorizationTestSuite) TestUnicastAuthorization_UnauthorizedUni
payload := messages.Proposal(*unittest.ProposalFixture())
// send message via unicast
err = senderCon.Unicast(&payload, u.receiverID.NodeID)
require.NoError(u.T(), err)
u.requireStreamAcceptedOrReset(err)

// wait for slashing violations consumer mock to invoke run func and close ch if expected method call happens
unittest.RequireCloseBefore(u.T(), u.waitCh, u.channelCloseDuration, "could close ch on time")
Expand Down Expand Up @@ -460,11 +473,7 @@ func (u *UnicastAuthorizationTestSuite) TestUnicastAuthorization_ReceiverHasNoSu
err = senderCon.Unicast(&libp2pmessage.TestMessage{
Text: string("hello"),
}, u.receiverID.NodeID)
if err != nil {
// It can happen that the receiver resets before the sender closes,
// in which case the error will be "stream reset"
require.ErrorContains(u.T(), err, "stream reset", "expected stream-related error when receiver has no subscription")
}
u.requireStreamAcceptedOrReset(err)

// wait for slashing violations consumer mock to invoke run func and close ch if expected method call happens
unittest.RequireCloseBefore(u.T(), u.waitCh, u.channelCloseDuration, "could close ch on time")
Expand Down Expand Up @@ -584,7 +593,7 @@ func (u *UnicastAuthorizationTestSuite) TestUnicastAuthorization_UnauthorizedSen
err = senderCon.Unicast(&libp2pmessage.TestMessage{
Text: "hello",
}, u.receiverID.NodeID)
require.NoError(u.T(), err)
u.requireStreamAcceptedOrReset(err)

// wait for slashing violations consumer to be invoked
unittest.RequireCloseBefore(u.T(), u.waitCh, u.channelCloseDuration, "could close ch on time")
Expand Down
Loading