|
4 | 4 | "context"
|
5 | 5 | "crypto/rand"
|
6 | 6 | "encoding/base64"
|
| 7 | + "fmt" |
7 | 8 | "strconv"
|
8 | 9 | "sync"
|
9 | 10 | "testing"
|
@@ -891,6 +892,53 @@ func TestGossipsubAttackSpamIDONTWANT(t *testing.T) {
|
891 | 892 | <-ctx.Done()
|
892 | 893 | }
|
893 | 894 |
|
| 895 | +func TestGossipsubHandleIDontwantSpam(t *testing.T) { |
| 896 | + ctx, cancel := context.WithCancel(context.Background()) |
| 897 | + defer cancel() |
| 898 | + hosts := getDefaultHosts(t, 2) |
| 899 | + |
| 900 | + msgID := func(pmsg *pb.Message) string { |
| 901 | + // silly content-based test message-ID: just use the data as whole |
| 902 | + return base64.URLEncoding.EncodeToString(pmsg.Data) |
| 903 | + } |
| 904 | + |
| 905 | + psubs := make([]*PubSub, 2) |
| 906 | + psubs[0] = getGossipsub(ctx, hosts[0], WithMessageIdFn(msgID)) |
| 907 | + psubs[1] = getGossipsub(ctx, hosts[1], WithMessageIdFn(msgID)) |
| 908 | + |
| 909 | + connect(t, hosts[0], hosts[1]) |
| 910 | + |
| 911 | + topic := "foobar" |
| 912 | + for _, ps := range psubs { |
| 913 | + _, err := ps.Subscribe(topic) |
| 914 | + if err != nil { |
| 915 | + t.Fatal(err) |
| 916 | + } |
| 917 | + } |
| 918 | + exceededIDWLength := GossipSubMaxIDontWantLength + 1 |
| 919 | + var idwIds []string |
| 920 | + for i := 0; i < exceededIDWLength; i++ { |
| 921 | + idwIds = append(idwIds, fmt.Sprintf("idontwant-%d", i)) |
| 922 | + } |
| 923 | + rPid := hosts[1].ID() |
| 924 | + ctrlMessage := &pb.ControlMessage{Idontwant: []*pb.ControlIDontWant{{MessageIDs: idwIds}}} |
| 925 | + grt := psubs[0].rt.(*GossipSubRouter) |
| 926 | + grt.handleIDontWant(rPid, ctrlMessage) |
| 927 | + |
| 928 | + if grt.peerdontwant[rPid] != 1 { |
| 929 | + t.Errorf("Wanted message count of %d but received %d", 1, grt.peerdontwant[rPid]) |
| 930 | + } |
| 931 | + mid := fmt.Sprintf("idontwant-%d", GossipSubMaxIDontWantLength-1) |
| 932 | + if _, ok := grt.unwanted[rPid][computeChecksum(mid)]; !ok { |
| 933 | + t.Errorf("Desired message id was not stored in the unwanted map: %s", mid) |
| 934 | + } |
| 935 | + |
| 936 | + mid = fmt.Sprintf("idontwant-%d", GossipSubMaxIDontWantLength) |
| 937 | + if _, ok := grt.unwanted[rPid][computeChecksum(mid)]; ok { |
| 938 | + t.Errorf("Unwanted message id was stored in the unwanted map: %s", mid) |
| 939 | + } |
| 940 | +} |
| 941 | + |
894 | 942 | type mockGSOnRead func(writeMsg func(*pb.RPC), irpc *pb.RPC)
|
895 | 943 |
|
896 | 944 | func newMockGS(ctx context.Context, t *testing.T, attacker host.Host, onReadMsg mockGSOnRead) {
|
|
0 commit comments