github.com/status-im/status-go@v1.1.0/waku/v1/peer_test.go (about) 1 // Copyright 2019 The Waku Library Authors. 2 // 3 // The Waku library is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Lesser General Public License as published by 5 // the Free Software Foundation, either version 3 of the License, or 6 // (at your option) any later version. 7 // 8 // The Waku library is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty off 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Lesser General Public License for more details. 12 // 13 // You should have received a copy of the GNU Lesser General Public License 14 // along with the Waku library. If not, see <http://www.gnu.org/licenses/>. 15 // 16 // This software uses the go-ethereum library, which is licensed 17 // under the GNU Lesser General Public Library, version 3 or any later. 18 19 package v1 20 21 import ( 22 mrand "math/rand" 23 "testing" 24 "time" 25 26 "github.com/stretchr/testify/require" 27 28 "github.com/ethereum/go-ethereum/crypto" 29 "github.com/ethereum/go-ethereum/p2p" 30 31 "github.com/status-im/status-go/waku/common" 32 ) 33 34 var seed int64 35 36 // initSingleTest should be called in the beginning of every 37 // test, which uses RNG, in order to make the tests 38 // reproduciblity independent of their sequence. 39 func initSingleTest() { 40 seed = time.Now().Unix() 41 mrand.Seed(seed) 42 } 43 44 var sharedTopic = common.TopicType{0xF, 0x1, 0x2, 0} 45 var wrongTopic = common.TopicType{0, 0, 0, 0} 46 47 // two generic waku node handshake. one don't send light flag 48 func TestTopicOrBloomMatch(t *testing.T) { 49 p := Peer{} 50 p.setTopicInterest([]common.TopicType{sharedTopic}) 51 envelope := &common.Envelope{Topic: sharedTopic} 52 if !p.topicOrBloomMatch(envelope) { 53 t.Fatal("envelope should match") 54 } 55 56 badEnvelope := &common.Envelope{Topic: wrongTopic} 57 if p.topicOrBloomMatch(badEnvelope) { 58 t.Fatal("envelope should not match") 59 } 60 61 } 62 63 func TestTopicOrBloomMatchFullNode(t *testing.T) { 64 p := Peer{} 65 // Set as full node 66 p.fullNode = true 67 p.setTopicInterest([]common.TopicType{sharedTopic}) 68 envelope := &common.Envelope{Topic: sharedTopic} 69 if !p.topicOrBloomMatch(envelope) { 70 t.Fatal("envelope should match") 71 } 72 73 badEnvelope := &common.Envelope{Topic: wrongTopic} 74 if p.topicOrBloomMatch(badEnvelope) { 75 t.Fatal("envelope should not match") 76 } 77 } 78 79 func TestPeerBasic(t *testing.T) { 80 initSingleTest() 81 82 params, err := generateMessageParams() 83 if err != nil { 84 t.Fatalf("failed generateMessageParams with seed %d.", seed) 85 } 86 87 params.PoW = 0.001 88 msg, err := common.NewSentMessage(params) 89 if err != nil { 90 t.Fatalf("failed to create new message with seed %d: %s.", seed, err) 91 } 92 env, err := msg.Wrap(params, time.Now()) 93 if err != nil { 94 t.Fatalf("failed Wrap with seed %d.", seed) 95 } 96 97 p := NewPeer(nil, nil, nil, nil, nil) 98 p.Mark(env) 99 if !p.Marked(env) { 100 t.Fatalf("failed mark with seed %d.", seed) 101 } 102 } 103 104 func TestSendBundle(t *testing.T) { 105 rw1, rw2 := p2p.MsgPipe() 106 defer func() { handleError(t, rw1.Close()) }() 107 defer func() { handleError(t, rw2.Close()) }() 108 envelopes := []*common.Envelope{{ 109 Expiry: 0, 110 TTL: 30, 111 Topic: common.TopicType{1}, 112 Data: []byte{1, 1, 1}, 113 }} 114 115 errc := make(chan error) 116 go func() { 117 stats := &common.StatsTracker{} 118 p := NewPeer(nil, nil, rw1, nil, stats) 119 _, err := p.SendBundle(envelopes) 120 errc <- err 121 }() 122 require.NoError(t, p2p.ExpectMsg(rw2, messagesCode, envelopes)) 123 require.NoError(t, <-errc) 124 } 125 126 func generateMessageParams() (*common.MessageParams, error) { 127 // set all the parameters except p.Dst and p.Padding 128 129 buf := make([]byte, 4) 130 mrand.Read(buf) // nolint: gosec 131 sz := mrand.Intn(400) // nolint: gosec 132 133 var p common.MessageParams 134 p.PoW = 0.01 135 p.WorkTime = 1 136 p.TTL = uint32(mrand.Intn(1024)) // nolint: gosec 137 p.Payload = make([]byte, sz) 138 p.KeySym = make([]byte, common.AESKeyLength) 139 mrand.Read(p.Payload) // nolint: gosec 140 mrand.Read(p.KeySym) // nolint: gosec 141 p.Topic = common.BytesToTopic(buf) 142 143 var err error 144 p.Src, err = crypto.GenerateKey() 145 if err != nil { 146 return nil, err 147 } 148 149 return &p, nil 150 } 151 152 func handleError(t *testing.T, err error) { 153 if err != nil { 154 t.Logf("deferred function error: '%s'", err) 155 } 156 }