From 027234cdfaea1a26331a6728db95b0f7e4e5a02d Mon Sep 17 00:00:00 2001 From: vyzo Date: Fri, 24 Apr 2020 23:06:34 +0300 Subject: [PATCH] add tests for message rejection paths --- score_test.go | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) diff --git a/score_test.go b/score_test.go index 0b68114..488132f 100644 --- a/score_test.go +++ b/score_test.go @@ -531,6 +531,113 @@ func TestScoreInvalidMessageDeliveriesDecay(t *testing.T) { } } +func TestScoreRejectMessageDeliveries(t *testing.T) { + // this tests adds coverage for the dark corners of rejection tracing + mytopic := "mytopic" + params := &PeerScoreParams{ + AppSpecificScore: func(peer.ID) float64 { return 0 }, + Topics: make(map[string]*TopicScoreParams), + } + topicScoreParams := &TopicScoreParams{ + TopicWeight: 1, + TimeInMeshQuantum: time.Second, + InvalidMessageDeliveriesWeight: -1, + InvalidMessageDeliveriesDecay: 1.0, + } + params.Topics[mytopic] = topicScoreParams + + peerA := peer.ID("A") + peerB := peer.ID("B") + + ps := newPeerScore(params) + ps.AddPeer(peerA, "myproto") + ps.AddPeer(peerB, "myproto") + + pbMsg := makeTestMessage(0) + pbMsg.TopicIDs = []string{mytopic} + msg := Message{ReceivedFrom: peerA, Message: pbMsg} + msg2 := Message{ReceivedFrom: peerB, Message: pbMsg} + + // these should have no effect in the score + ps.RejectMessage(&msg, rejectBlacklstedPeer) + ps.RejectMessage(&msg, rejectBlacklistedSource) + ps.RejectMessage(&msg, rejectValidationQueueFull) + + aScore := ps.Score(peerA) + expected := 0.0 + if aScore != expected { + t.Fatalf("Score: %f. Expected %f", aScore, expected) + } + + // insert a record in the message deliveries + ps.ValidateMessage(&msg) + + // this should have no effect in the score, and subsequent duplicate messages should have no + // effect either + ps.RejectMessage(&msg, rejectValidationThrottled) + ps.DuplicateMessage(&msg2) + + aScore = ps.Score(peerA) + expected = 0.0 + if aScore != expected { + t.Fatalf("Score: %f. Expected %f", aScore, expected) + } + + bScore := ps.Score(peerB) + expected = 0.0 + if bScore != expected { + t.Fatalf("Score: %f. Expected %f", aScore, expected) + } + + // now clear the delivery record + ps.deliveries.head.expire = time.Now() + time.Sleep(1 * time.Millisecond) + ps.deliveries.gc() + + // insert a new record in the message deliveries + ps.ValidateMessage(&msg) + + // and reject the message to make sure duplicates are also penalized + ps.RejectMessage(&msg, rejectValidationFailed) + ps.DuplicateMessage(&msg2) + + aScore = ps.Score(peerA) + expected = -1.0 + if aScore != expected { + t.Fatalf("Score: %f. Expected %f", aScore, expected) + } + + bScore = ps.Score(peerB) + expected = -1.0 + if bScore != expected { + t.Fatalf("Score: %f. Expected %f", bScore, expected) + } + + // now clear the delivery record again + ps.deliveries.head.expire = time.Now() + time.Sleep(1 * time.Millisecond) + ps.deliveries.gc() + + // insert a new record in the message deliveries + ps.ValidateMessage(&msg) + + // and reject the message after a duplciate has arrived + ps.DuplicateMessage(&msg2) + ps.RejectMessage(&msg, rejectValidationFailed) + + aScore = ps.Score(peerA) + expected = -2.0 + if aScore != expected { + t.Fatalf("Score: %f. Expected %f", aScore, expected) + } + + bScore = ps.Score(peerB) + expected = -2.0 + if bScore != expected { + t.Fatalf("Score: %f. Expected %f", bScore, expected) + } +} + func TestScoreApplicationScore(t *testing.T) { // Create parameters with reasonable default values mytopic := "mytopic"