2018-04-11 15:41:51 +00:00
|
|
|
package whisper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
whisper "github.com/ethereum/go-ethereum/whisper/whisperv6"
|
|
|
|
"github.com/status-im/status-go/geth/node"
|
|
|
|
"github.com/status-im/status-go/geth/params"
|
2018-05-03 07:35:58 +00:00
|
|
|
"github.com/status-im/status-go/signal"
|
2018-04-11 15:41:51 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
)
|
|
|
|
|
2018-04-13 05:52:22 +00:00
|
|
|
func TestWhisperExtensionSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(WhisperExtensionSuite))
|
2018-04-11 15:41:51 +00:00
|
|
|
}
|
|
|
|
|
2018-04-13 05:52:22 +00:00
|
|
|
type WhisperExtensionSuite struct {
|
2018-04-11 15:41:51 +00:00
|
|
|
suite.Suite
|
|
|
|
|
|
|
|
nodes []*node.StatusNode
|
|
|
|
}
|
|
|
|
|
2018-04-13 05:52:22 +00:00
|
|
|
func (s *WhisperExtensionSuite) SetupTest() {
|
2018-04-11 15:41:51 +00:00
|
|
|
s.nodes = make([]*node.StatusNode, 2)
|
|
|
|
for i := range s.nodes {
|
|
|
|
dir, err := ioutil.TempDir("", "test-shhext-")
|
|
|
|
s.NoError(err)
|
|
|
|
// network id is irrelevant
|
2018-04-26 17:59:57 +00:00
|
|
|
cfg, err := params.NewNodeConfig(dir, "", 777)
|
2018-04-11 15:41:51 +00:00
|
|
|
cfg.LightEthConfig.Enabled = false
|
|
|
|
cfg.Name = fmt.Sprintf("test-shhext-%d", i)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.nodes[i] = node.New()
|
|
|
|
s.Require().NoError(s.nodes[i].Start(cfg))
|
|
|
|
}
|
2018-04-13 05:52:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WhisperExtensionSuite) TestSentSignal() {
|
2018-04-16 12:36:09 +00:00
|
|
|
node1 := s.nodes[0].GethNode()
|
|
|
|
s.NotNil(node1)
|
|
|
|
node2 := s.nodes[1].GethNode()
|
|
|
|
s.NotNil(node2)
|
2018-04-11 15:41:51 +00:00
|
|
|
node1.Server().AddPeer(node2.Server().Self())
|
|
|
|
confirmed := make(chan common.Hash, 1)
|
|
|
|
signal.SetDefaultNodeNotificationHandler(func(rawSignal string) {
|
|
|
|
var sg struct {
|
|
|
|
Type string
|
|
|
|
Event json.RawMessage
|
|
|
|
}
|
|
|
|
s.NoError(json.Unmarshal([]byte(rawSignal), &sg))
|
|
|
|
|
|
|
|
if sg.Type == signal.EventEnvelopeSent {
|
2018-05-03 07:35:58 +00:00
|
|
|
var event signal.EnvelopeSignal
|
2018-04-11 15:41:51 +00:00
|
|
|
s.NoError(json.Unmarshal(sg.Event, &event))
|
|
|
|
confirmed <- event.Hash
|
|
|
|
}
|
|
|
|
})
|
|
|
|
client := s.nodes[0].RPCClient()
|
|
|
|
s.NotNil(client)
|
|
|
|
var symID string
|
|
|
|
s.NoError(client.Call(&symID, "shh_newSymKey"))
|
|
|
|
msg := whisper.NewMessage{
|
|
|
|
SymKeyID: symID,
|
|
|
|
PowTarget: whisper.DefaultMinimumPoW,
|
|
|
|
PowTime: 200,
|
|
|
|
Topic: whisper.TopicType{0x01, 0x01, 0x01, 0x01},
|
|
|
|
Payload: []byte("hello"),
|
|
|
|
}
|
|
|
|
var hash common.Hash
|
|
|
|
s.NoError(client.Call(&hash, "shhext_post", msg))
|
|
|
|
s.NotEqual(common.Hash{}, hash)
|
|
|
|
select {
|
|
|
|
case conf := <-confirmed:
|
|
|
|
s.Equal(hash, conf)
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
s.Fail("timed out while waiting for confirmation")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-13 05:52:22 +00:00
|
|
|
func (s *WhisperExtensionSuite) TestExpiredSignal() {
|
|
|
|
expired := make(chan common.Hash, 1)
|
|
|
|
signal.SetDefaultNodeNotificationHandler(func(rawSignal string) {
|
|
|
|
var sg struct {
|
|
|
|
Type string
|
|
|
|
Event json.RawMessage
|
|
|
|
}
|
|
|
|
fmt.Println(string(rawSignal))
|
|
|
|
s.NoError(json.Unmarshal([]byte(rawSignal), &sg))
|
|
|
|
|
|
|
|
if sg.Type == signal.EventEnvelopeExpired {
|
2018-05-03 07:35:58 +00:00
|
|
|
var event signal.EnvelopeSignal
|
2018-04-13 05:52:22 +00:00
|
|
|
s.NoError(json.Unmarshal(sg.Event, &event))
|
|
|
|
expired <- event.Hash
|
|
|
|
}
|
|
|
|
})
|
|
|
|
client := s.nodes[0].RPCClient()
|
|
|
|
s.NotNil(client)
|
|
|
|
var symID string
|
|
|
|
s.NoError(client.Call(&symID, "shh_newSymKey"))
|
|
|
|
msg := whisper.NewMessage{
|
|
|
|
SymKeyID: symID,
|
|
|
|
PowTarget: whisper.DefaultMinimumPoW,
|
|
|
|
PowTime: 200,
|
|
|
|
TTL: 1,
|
|
|
|
Topic: whisper.TopicType{0x01, 0x01, 0x01, 0x01},
|
|
|
|
Payload: []byte("hello"),
|
|
|
|
}
|
|
|
|
var hash common.Hash
|
|
|
|
s.NoError(client.Call(&hash, "shhext_post", msg))
|
|
|
|
s.NotEqual(common.Hash{}, hash)
|
|
|
|
select {
|
|
|
|
case exp := <-expired:
|
|
|
|
s.Equal(hash, exp)
|
|
|
|
case <-time.After(3 * time.Second):
|
|
|
|
s.Fail("timed out while waiting for expiration")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WhisperExtensionSuite) TearDown() {
|
2018-04-11 15:41:51 +00:00
|
|
|
for _, n := range s.nodes {
|
2018-04-16 12:36:09 +00:00
|
|
|
cfg := n.Config()
|
|
|
|
s.NotNil(cfg)
|
2018-04-11 15:41:51 +00:00
|
|
|
s.NoError(n.Stop())
|
|
|
|
s.NoError(os.Remove(cfg.DataDir))
|
|
|
|
}
|
|
|
|
}
|