2018-04-11 15:41:51 +00:00
|
|
|
package shhext
|
|
|
|
|
|
|
|
import (
|
2018-04-26 05:56:19 +00:00
|
|
|
"context"
|
2018-06-25 13:27:17 +00:00
|
|
|
"errors"
|
2018-04-11 15:41:51 +00:00
|
|
|
"fmt"
|
2018-04-26 05:56:19 +00:00
|
|
|
"math"
|
2018-09-24 18:07:34 +00:00
|
|
|
"os"
|
2018-04-11 15:41:51 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/node"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
2018-07-04 09:30:57 +00:00
|
|
|
"github.com/status-im/status-go/t/helpers"
|
2018-09-25 07:05:38 +00:00
|
|
|
whisper "github.com/status-im/whisper/whisperv6"
|
2018-04-11 15:41:51 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
)
|
|
|
|
|
2018-04-13 05:52:22 +00:00
|
|
|
func newHandlerMock(buf int) handlerMock {
|
|
|
|
return handlerMock{
|
2018-06-15 15:12:31 +00:00
|
|
|
confirmations: make(chan common.Hash, buf),
|
|
|
|
expirations: make(chan common.Hash, buf),
|
|
|
|
requestsCompleted: make(chan common.Hash, buf),
|
|
|
|
requestsExpired: make(chan common.Hash, buf),
|
2018-04-13 05:52:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type handlerMock struct {
|
2018-06-15 15:12:31 +00:00
|
|
|
confirmations chan common.Hash
|
|
|
|
expirations chan common.Hash
|
|
|
|
requestsCompleted chan common.Hash
|
|
|
|
requestsExpired chan common.Hash
|
2018-04-13 05:52:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t handlerMock) EnvelopeSent(hash common.Hash) {
|
|
|
|
t.confirmations <- hash
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t handlerMock) EnvelopeExpired(hash common.Hash) {
|
|
|
|
t.expirations <- hash
|
|
|
|
}
|
|
|
|
|
2018-07-02 07:38:10 +00:00
|
|
|
func (t handlerMock) MailServerRequestCompleted(requestID common.Hash, lastEnvelopeHash common.Hash, cursor []byte) {
|
|
|
|
t.requestsCompleted <- requestID
|
2018-06-15 15:12:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t handlerMock) MailServerRequestExpired(hash common.Hash) {
|
|
|
|
t.requestsExpired <- hash
|
|
|
|
}
|
|
|
|
|
2018-04-11 15:41:51 +00:00
|
|
|
func TestShhExtSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(ShhExtSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type ShhExtSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
|
|
|
nodes []*node.Node
|
|
|
|
services []*Service
|
|
|
|
whisper []*whisper.Whisper
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ShhExtSuite) SetupTest() {
|
|
|
|
s.nodes = make([]*node.Node, 2)
|
|
|
|
s.services = make([]*Service, 2)
|
|
|
|
s.whisper = make([]*whisper.Whisper, 2)
|
|
|
|
for i := range s.nodes {
|
|
|
|
i := i // bind i to be usable in service constructors
|
|
|
|
cfg := &node.Config{
|
|
|
|
Name: fmt.Sprintf("node-%d", i),
|
|
|
|
P2P: p2p.Config{
|
|
|
|
NoDiscovery: true,
|
2018-04-13 05:52:22 +00:00
|
|
|
MaxPeers: 1,
|
|
|
|
ListenAddr: ":0",
|
2018-04-11 15:41:51 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
stack, err := node.New(cfg)
|
|
|
|
s.NoError(err)
|
|
|
|
s.whisper[i] = whisper.New(nil)
|
|
|
|
s.NoError(stack.Register(func(n *node.ServiceContext) (node.Service, error) {
|
|
|
|
return s.whisper[i], nil
|
|
|
|
}))
|
2018-09-24 18:07:34 +00:00
|
|
|
config := &ServiceConfig{
|
|
|
|
InstallationID: "1",
|
|
|
|
DataDir: os.TempDir(),
|
|
|
|
Debug: true,
|
|
|
|
PFSEnabled: false,
|
|
|
|
}
|
|
|
|
s.services[i] = New(s.whisper[i], nil, nil, config)
|
2018-04-11 15:41:51 +00:00
|
|
|
s.NoError(stack.Register(func(n *node.ServiceContext) (node.Service, error) {
|
|
|
|
return s.services[i], nil
|
|
|
|
}))
|
|
|
|
s.Require().NoError(stack.Start())
|
|
|
|
s.nodes[i] = stack
|
|
|
|
}
|
2018-04-13 05:52:22 +00:00
|
|
|
s.services[0].tracker.handler = newHandlerMock(1)
|
2018-04-11 15:41:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ShhExtSuite) TestPostMessageWithConfirmation() {
|
2018-04-13 05:52:22 +00:00
|
|
|
mock := newHandlerMock(1)
|
|
|
|
s.services[0].tracker.handler = mock
|
|
|
|
s.nodes[0].Server().AddPeer(s.nodes[1].Server().Self())
|
2018-04-11 15:41:51 +00:00
|
|
|
symID, err := s.whisper[0].GenerateSymKey()
|
|
|
|
s.NoError(err)
|
|
|
|
client, err := s.nodes[0].Attach()
|
|
|
|
s.NoError(err)
|
|
|
|
var hash common.Hash
|
|
|
|
s.NoError(client.Call(&hash, "shhext_post", whisper.NewMessage{
|
|
|
|
SymKeyID: symID,
|
|
|
|
PowTarget: whisper.DefaultMinimumPoW,
|
|
|
|
PowTime: 200,
|
|
|
|
Topic: whisper.TopicType{0x01, 0x01, 0x01, 0x01},
|
|
|
|
Payload: []byte("hello"),
|
|
|
|
}))
|
|
|
|
s.NoError(err)
|
|
|
|
select {
|
2018-04-13 05:52:22 +00:00
|
|
|
case confirmed := <-mock.confirmations:
|
2018-04-11 15:41:51 +00:00
|
|
|
s.Equal(hash, confirmed)
|
2018-05-04 09:08:04 +00:00
|
|
|
case <-time.After(5 * time.Second):
|
2018-04-11 15:41:51 +00:00
|
|
|
s.Fail("timed out while waiting for confirmation")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-13 05:52:22 +00:00
|
|
|
func (s *ShhExtSuite) TestWaitMessageExpired() {
|
|
|
|
mock := newHandlerMock(1)
|
|
|
|
s.services[0].tracker.handler = mock
|
|
|
|
symID, err := s.whisper[0].GenerateSymKey()
|
|
|
|
s.NoError(err)
|
|
|
|
client, err := s.nodes[0].Attach()
|
|
|
|
s.NoError(err)
|
|
|
|
var hash common.Hash
|
|
|
|
s.NoError(client.Call(&hash, "shhext_post", whisper.NewMessage{
|
|
|
|
SymKeyID: symID,
|
|
|
|
PowTarget: whisper.DefaultMinimumPoW,
|
|
|
|
PowTime: 200,
|
|
|
|
TTL: 1,
|
|
|
|
Topic: whisper.TopicType{0x01, 0x01, 0x01, 0x01},
|
|
|
|
Payload: []byte("hello"),
|
|
|
|
}))
|
|
|
|
s.NoError(err)
|
|
|
|
select {
|
|
|
|
case expired := <-mock.expirations:
|
|
|
|
s.Equal(hash, expired)
|
|
|
|
case confirmed := <-mock.confirmations:
|
|
|
|
s.Fail("unexpected confirmation for hash", confirmed)
|
2018-05-04 09:08:04 +00:00
|
|
|
case <-time.After(10 * time.Second):
|
2018-04-13 05:52:22 +00:00
|
|
|
s.Fail("timed out while waiting for confirmation")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-04 09:30:57 +00:00
|
|
|
func (s *ShhExtSuite) TestRequestMessagesErrors() {
|
2018-04-26 05:56:19 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
shh := whisper.New(nil)
|
|
|
|
aNode, err := node.New(&node.Config{
|
|
|
|
P2P: p2p.Config{
|
|
|
|
MaxPeers: math.MaxInt32,
|
|
|
|
NoDiscovery: true,
|
|
|
|
},
|
|
|
|
}) // in-memory node as no data dir
|
|
|
|
s.NoError(err)
|
2018-07-04 09:30:57 +00:00
|
|
|
err = aNode.Register(func(*node.ServiceContext) (node.Service, error) {
|
2018-04-26 05:56:19 +00:00
|
|
|
return shh, nil
|
|
|
|
})
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
err = aNode.Start()
|
|
|
|
s.NoError(err)
|
2018-07-04 09:30:57 +00:00
|
|
|
defer func() { s.NoError(aNode.Stop()) }()
|
2018-04-26 05:56:19 +00:00
|
|
|
|
|
|
|
mock := newHandlerMock(1)
|
2018-09-24 18:07:34 +00:00
|
|
|
config := &ServiceConfig{
|
|
|
|
InstallationID: "1",
|
|
|
|
DataDir: os.TempDir(),
|
|
|
|
Debug: false,
|
|
|
|
PFSEnabled: false,
|
|
|
|
}
|
|
|
|
service := New(shh, mock, nil, config)
|
2018-04-26 05:56:19 +00:00
|
|
|
api := NewPublicAPI(service)
|
|
|
|
|
|
|
|
const (
|
|
|
|
mailServerPeer = "enode://b7e65e1bedc2499ee6cbd806945af5e7df0e59e4070c96821570bd581473eade24a489f5ec95d060c0db118c879403ab88d827d3766978f28708989d35474f87@[::]:51920"
|
|
|
|
)
|
|
|
|
|
2018-06-15 15:12:31 +00:00
|
|
|
var hash []byte
|
2018-04-26 05:56:19 +00:00
|
|
|
|
|
|
|
// invalid MailServer enode address
|
2018-06-15 15:12:31 +00:00
|
|
|
hash, err = api.RequestMessages(context.TODO(), MessagesRequest{MailServerPeer: "invalid-address"})
|
|
|
|
s.Nil(hash)
|
2018-04-26 05:56:19 +00:00
|
|
|
s.EqualError(err, "invalid mailServerPeer value: invalid URL scheme, want \"enode\"")
|
|
|
|
|
|
|
|
// non-existent symmetric key
|
2018-06-15 15:12:31 +00:00
|
|
|
hash, err = api.RequestMessages(context.TODO(), MessagesRequest{
|
2018-04-26 05:56:19 +00:00
|
|
|
MailServerPeer: mailServerPeer,
|
2018-07-04 09:30:57 +00:00
|
|
|
SymKeyID: "invalid-sym-key-id",
|
2018-04-26 05:56:19 +00:00
|
|
|
})
|
2018-06-15 15:12:31 +00:00
|
|
|
s.Nil(hash)
|
2018-04-26 05:56:19 +00:00
|
|
|
s.EqualError(err, "invalid symKeyID value: non-existent key ID")
|
|
|
|
|
|
|
|
// with a symmetric key
|
|
|
|
symKeyID, symKeyErr := shh.AddSymKeyFromPassword("some-pass")
|
|
|
|
s.NoError(symKeyErr)
|
2018-06-15 15:12:31 +00:00
|
|
|
hash, err = api.RequestMessages(context.TODO(), MessagesRequest{
|
2018-04-26 05:56:19 +00:00
|
|
|
MailServerPeer: mailServerPeer,
|
|
|
|
SymKeyID: symKeyID,
|
|
|
|
})
|
2018-06-15 15:12:31 +00:00
|
|
|
s.Nil(hash)
|
2018-07-04 09:30:57 +00:00
|
|
|
s.Contains(err.Error(), "Could not find peer with ID")
|
2018-04-26 05:56:19 +00:00
|
|
|
|
2018-06-26 08:41:03 +00:00
|
|
|
// from is greater than to
|
|
|
|
hash, err = api.RequestMessages(context.TODO(), MessagesRequest{
|
|
|
|
From: 10,
|
|
|
|
To: 5,
|
|
|
|
})
|
|
|
|
s.Nil(hash)
|
2018-07-04 09:30:57 +00:00
|
|
|
s.Contains(err.Error(), "Query range is invalid: from > to (10 > 5)")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ShhExtSuite) TestRequestMessagesSuccess() {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
shh := whisper.New(nil)
|
|
|
|
aNode, err := node.New(&node.Config{
|
|
|
|
P2P: p2p.Config{
|
|
|
|
MaxPeers: math.MaxInt32,
|
|
|
|
NoDiscovery: true,
|
|
|
|
},
|
|
|
|
}) // in-memory node as no data dir
|
|
|
|
s.NoError(err)
|
|
|
|
err = aNode.Register(func(*node.ServiceContext) (node.Service, error) { return shh, nil })
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
err = aNode.Start()
|
|
|
|
s.NoError(err)
|
|
|
|
defer func() { err := aNode.Stop(); s.NoError(err) }()
|
|
|
|
|
|
|
|
mock := newHandlerMock(1)
|
2018-09-24 18:07:34 +00:00
|
|
|
config := &ServiceConfig{
|
|
|
|
InstallationID: "1",
|
|
|
|
DataDir: os.TempDir(),
|
|
|
|
Debug: false,
|
|
|
|
PFSEnabled: false,
|
|
|
|
}
|
|
|
|
service := New(shh, mock, nil, config)
|
2018-07-04 09:30:57 +00:00
|
|
|
api := NewPublicAPI(service)
|
2018-06-26 08:41:03 +00:00
|
|
|
|
2018-04-26 05:56:19 +00:00
|
|
|
// with a peer acting as a mailserver
|
|
|
|
// prepare a node first
|
|
|
|
mailNode, err := node.New(&node.Config{
|
|
|
|
P2P: p2p.Config{
|
|
|
|
MaxPeers: math.MaxInt32,
|
|
|
|
NoDiscovery: true,
|
|
|
|
ListenAddr: ":0",
|
|
|
|
},
|
|
|
|
}) // in-memory node as no data dir
|
|
|
|
s.NoError(err)
|
2018-07-04 09:30:57 +00:00
|
|
|
err = mailNode.Register(func(*node.ServiceContext) (node.Service, error) {
|
2018-04-26 05:56:19 +00:00
|
|
|
return whisper.New(nil), nil
|
|
|
|
})
|
|
|
|
s.NoError(err)
|
|
|
|
err = mailNode.Start()
|
|
|
|
s.NoError(err)
|
2018-07-04 09:30:57 +00:00
|
|
|
defer func() { s.NoError(mailNode.Stop()) }()
|
2018-04-26 05:56:19 +00:00
|
|
|
|
|
|
|
// add mailPeer as a peer
|
2018-07-04 09:30:57 +00:00
|
|
|
waitErr := helpers.WaitForPeerAsync(aNode.Server(), mailNode.Server().Self().String(), p2p.PeerEventTypeAdd, time.Second)
|
2018-07-11 14:42:51 +00:00
|
|
|
aNode.Server().AddPeer(mailNode.Server().Self())
|
2018-07-04 09:30:57 +00:00
|
|
|
s.NoError(<-waitErr)
|
|
|
|
|
|
|
|
var hash []byte
|
2018-04-26 05:56:19 +00:00
|
|
|
|
2018-07-04 09:30:57 +00:00
|
|
|
// send a request with a symmetric key
|
|
|
|
symKeyID, symKeyErr := shh.AddSymKeyFromPassword("some-pass")
|
|
|
|
s.NoError(symKeyErr)
|
2018-06-15 15:12:31 +00:00
|
|
|
hash, err = api.RequestMessages(context.TODO(), MessagesRequest{
|
2018-04-26 05:56:19 +00:00
|
|
|
MailServerPeer: mailNode.Server().Self().String(),
|
|
|
|
SymKeyID: symKeyID,
|
|
|
|
})
|
|
|
|
s.NoError(err)
|
2018-06-15 15:12:31 +00:00
|
|
|
s.NotNil(hash)
|
2018-07-04 09:30:57 +00:00
|
|
|
s.Contains(api.service.tracker.cache, common.BytesToHash(hash))
|
2018-06-15 15:12:31 +00:00
|
|
|
|
2018-07-04 09:30:57 +00:00
|
|
|
// Send a request without a symmetric key. In this case,
|
|
|
|
// a public key extracted from MailServerPeer will be used.
|
|
|
|
hash, err = api.RequestMessages(context.TODO(), MessagesRequest{
|
|
|
|
MailServerPeer: mailNode.Server().Self().String(),
|
|
|
|
})
|
|
|
|
s.NoError(err)
|
|
|
|
s.NotNil(hash)
|
2018-06-15 15:12:31 +00:00
|
|
|
s.Contains(api.service.tracker.cache, common.BytesToHash(hash))
|
2018-04-26 05:56:19 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 13:27:17 +00:00
|
|
|
func (s *ShhExtSuite) TestDebugPostSync() {
|
|
|
|
mock := newHandlerMock(1)
|
|
|
|
s.services[0].tracker.handler = mock
|
|
|
|
symID, err := s.whisper[0].GenerateSymKey()
|
|
|
|
s.NoError(err)
|
|
|
|
s.nodes[0].Server().AddPeer(s.nodes[1].Server().Self())
|
|
|
|
client, err := s.nodes[0].Attach()
|
|
|
|
s.NoError(err)
|
|
|
|
var hash common.Hash
|
|
|
|
|
|
|
|
var testCases = []struct {
|
|
|
|
name string
|
|
|
|
msg whisper.NewMessage
|
|
|
|
postSyncTimeout time.Duration
|
|
|
|
expectedErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "timeout",
|
|
|
|
msg: whisper.NewMessage{
|
|
|
|
SymKeyID: symID,
|
|
|
|
PowTarget: whisper.DefaultMinimumPoW,
|
|
|
|
PowTime: 200,
|
|
|
|
Topic: whisper.TopicType{0x01, 0x01, 0x01, 0x01},
|
|
|
|
Payload: []byte("hello"),
|
|
|
|
},
|
|
|
|
postSyncTimeout: postSyncTimeout,
|
|
|
|
expectedErr: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid message",
|
|
|
|
msg: whisper.NewMessage{
|
|
|
|
PowTarget: whisper.DefaultMinimumPoW,
|
|
|
|
PowTime: 200,
|
|
|
|
Topic: whisper.TopicType{0x01, 0x01, 0x01, 0x01},
|
|
|
|
Payload: []byte("hello"),
|
|
|
|
},
|
|
|
|
postSyncTimeout: postSyncTimeout,
|
|
|
|
expectedErr: whisper.ErrSymAsym,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "context deadline exceeded",
|
|
|
|
msg: whisper.NewMessage{
|
|
|
|
SymKeyID: symID,
|
|
|
|
PowTarget: whisper.DefaultMinimumPoW,
|
|
|
|
PowTime: 10,
|
|
|
|
Topic: whisper.TopicType{0x01, 0x01, 0x01, 0x01},
|
|
|
|
TTL: 100,
|
|
|
|
Payload: []byte("hello"),
|
|
|
|
},
|
|
|
|
postSyncTimeout: 1 * time.Millisecond,
|
|
|
|
expectedErr: errors.New("context deadline exceeded"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
s.T().Run(tc.name, func(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), tc.postSyncTimeout)
|
|
|
|
defer cancel()
|
|
|
|
err := client.CallContext(ctx, &hash, "debug_postSync", tc.msg)
|
|
|
|
|
|
|
|
if tc.expectedErr != nil {
|
|
|
|
s.Equal(tc.expectedErr.Error(), err.Error())
|
|
|
|
} else {
|
|
|
|
s.NoError(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ShhExtSuite) TestEnvelopeExpiredOnDebugPostSync() {
|
|
|
|
mock := newHandlerMock(1)
|
|
|
|
s.services[0].tracker.handler = mock
|
|
|
|
symID, err := s.whisper[0].GenerateSymKey()
|
|
|
|
s.NoError(err)
|
|
|
|
client, err := s.nodes[0].Attach()
|
|
|
|
s.NoError(err)
|
|
|
|
var hash common.Hash
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), postSyncTimeout)
|
|
|
|
defer cancel()
|
|
|
|
err = client.CallContext(ctx, &hash, "debug_postSync", whisper.NewMessage{
|
|
|
|
SymKeyID: symID,
|
|
|
|
PowTarget: whisper.DefaultMinimumPoW,
|
|
|
|
PowTime: 200,
|
|
|
|
Topic: whisper.TopicType{0x01, 0x01, 0x01, 0x01},
|
|
|
|
Payload: []byte("hello"),
|
|
|
|
TTL: 1,
|
|
|
|
})
|
|
|
|
|
|
|
|
s.Equal(errEnvelopeExpired.Error(), err.Error())
|
|
|
|
}
|
|
|
|
|
2018-04-11 15:41:51 +00:00
|
|
|
func (s *ShhExtSuite) TearDown() {
|
|
|
|
for _, n := range s.nodes {
|
|
|
|
s.NoError(n.Stop())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
testHash = common.Hash{0x01}
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTrackerSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(TrackerSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type TrackerSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
|
|
|
tracker *tracker
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TrackerSuite) SetupTest() {
|
|
|
|
s.tracker = &tracker{
|
2018-04-13 05:52:22 +00:00
|
|
|
cache: map[common.Hash]EnvelopeState{},
|
2018-04-11 15:41:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TrackerSuite) TestConfirmed() {
|
|
|
|
s.tracker.Add(testHash)
|
|
|
|
s.Contains(s.tracker.cache, testHash)
|
|
|
|
s.Equal(EnvelopePosted, s.tracker.cache[testHash])
|
|
|
|
s.tracker.handleEvent(whisper.EnvelopeEvent{
|
|
|
|
Event: whisper.EventEnvelopeSent,
|
|
|
|
Hash: testHash,
|
|
|
|
})
|
|
|
|
s.Contains(s.tracker.cache, testHash)
|
|
|
|
s.Equal(EnvelopeSent, s.tracker.cache[testHash])
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TrackerSuite) TestIgnored() {
|
|
|
|
s.tracker.handleEvent(whisper.EnvelopeEvent{
|
|
|
|
Event: whisper.EventEnvelopeSent,
|
|
|
|
Hash: testHash,
|
|
|
|
})
|
|
|
|
s.NotContains(s.tracker.cache, testHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TrackerSuite) TestRemoved() {
|
|
|
|
s.tracker.Add(testHash)
|
|
|
|
s.Contains(s.tracker.cache, testHash)
|
|
|
|
s.tracker.handleEvent(whisper.EnvelopeEvent{
|
|
|
|
Event: whisper.EventEnvelopeExpired,
|
|
|
|
Hash: testHash,
|
|
|
|
})
|
|
|
|
s.NotContains(s.tracker.cache, testHash)
|
|
|
|
}
|
2018-06-15 15:12:31 +00:00
|
|
|
|
|
|
|
func (s *TrackerSuite) TestRequestCompleted() {
|
2018-07-02 07:38:10 +00:00
|
|
|
mock := newHandlerMock(1)
|
|
|
|
s.tracker.handler = mock
|
2018-06-15 15:12:31 +00:00
|
|
|
s.tracker.AddRequest(testHash, time.After(defaultRequestTimeout*time.Second))
|
|
|
|
s.Contains(s.tracker.cache, testHash)
|
|
|
|
s.Equal(MailServerRequestSent, s.tracker.cache[testHash])
|
|
|
|
s.tracker.handleEvent(whisper.EnvelopeEvent{
|
|
|
|
Event: whisper.EventMailServerRequestCompleted,
|
|
|
|
Hash: testHash,
|
2018-07-02 07:38:10 +00:00
|
|
|
Data: &whisper.MailServerResponse{},
|
2018-06-15 15:12:31 +00:00
|
|
|
})
|
2018-07-02 07:38:10 +00:00
|
|
|
select {
|
|
|
|
case requestID := <-mock.requestsCompleted:
|
|
|
|
s.Equal(testHash, requestID)
|
|
|
|
s.NotContains(s.tracker.cache, testHash)
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
s.Fail("timed out while waiting for a request to be completed")
|
|
|
|
}
|
2018-06-15 15:12:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TrackerSuite) TestRequestExpiration() {
|
|
|
|
mock := newHandlerMock(1)
|
|
|
|
s.tracker.handler = mock
|
|
|
|
c := make(chan time.Time)
|
|
|
|
s.tracker.AddRequest(testHash, c)
|
|
|
|
s.Contains(s.tracker.cache, testHash)
|
|
|
|
s.Equal(MailServerRequestSent, s.tracker.cache[testHash])
|
|
|
|
s.tracker.handleEvent(whisper.EnvelopeEvent{
|
|
|
|
Event: whisper.EventMailServerRequestExpired,
|
|
|
|
Hash: testHash,
|
|
|
|
})
|
|
|
|
select {
|
|
|
|
case requestID := <-mock.requestsExpired:
|
|
|
|
s.Equal(testHash, requestID)
|
|
|
|
s.NotContains(s.tracker.cache, testHash)
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
s.Fail("timed out while waiting for request expiration")
|
|
|
|
}
|
|
|
|
}
|