2017-10-11 14:20:51 +00:00
|
|
|
package whisper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2018-03-02 09:25:30 +00:00
|
|
|
whisper "github.com/ethereum/go-ethereum/whisper/whisperv6"
|
2018-03-01 16:48:30 +00:00
|
|
|
"github.com/status-im/status-go/geth/jail"
|
2017-10-11 14:20:51 +00:00
|
|
|
"github.com/status-im/status-go/static"
|
2018-02-08 12:52:47 +00:00
|
|
|
e2e "github.com/status-im/status-go/t/e2e"
|
|
|
|
. "github.com/status-im/status-go/t/utils"
|
2017-10-11 14:20:51 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-10-23 10:41:41 +00:00
|
|
|
//nolint: unused, varcheck
|
2017-10-11 14:20:51 +00:00
|
|
|
whisperMessage1 = `test message 1 (K1 -> K2, signed+encrypted, from us)`
|
|
|
|
whisperMessage2 = `test message 3 (K1 -> "", signed broadcast)`
|
|
|
|
whisperMessage3 = `test message 4 ("" -> "", anon broadcast)`
|
|
|
|
whisperMessage4 = `test message 5 ("" -> K1, encrypted anon broadcast)`
|
|
|
|
whisperMessage5 = `test message 6 (K2 -> K1, signed+encrypted, to us)`
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
baseStatusJSCode = string(static.MustAsset("testdata/jail/status.js"))
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestWhisperJailTestSuite(t *testing.T) {
|
2017-11-06 16:04:45 +00:00
|
|
|
suite.Run(t, new(WhisperJailTestSuite))
|
2017-10-11 14:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type WhisperJailTestSuite struct {
|
|
|
|
e2e.BackendTestSuite
|
|
|
|
|
|
|
|
Timeout time.Duration
|
|
|
|
WhisperAPI *whisper.PublicWhisperAPI
|
2018-03-01 16:48:30 +00:00
|
|
|
Jail jail.Manager
|
2017-10-11 14:20:51 +00:00
|
|
|
}
|
|
|
|
|
2017-10-23 17:18:09 +00:00
|
|
|
func (s *WhisperJailTestSuite) StartTestBackend(opts ...e2e.TestNodeOption) {
|
|
|
|
s.BackendTestSuite.StartTestBackend(opts...)
|
2017-10-11 14:20:51 +00:00
|
|
|
|
2017-11-06 16:04:45 +00:00
|
|
|
s.Timeout = time.Minute * 5
|
2017-10-11 14:20:51 +00:00
|
|
|
s.WhisperAPI = whisper.NewPublicWhisperAPI(s.WhisperService())
|
|
|
|
s.Jail = s.Backend.JailManager()
|
2018-02-09 15:39:16 +00:00
|
|
|
s.Require().NotNil(s.Jail)
|
2017-11-07 17:36:42 +00:00
|
|
|
s.Jail.SetBaseJS(baseStatusJSCode)
|
2017-10-11 14:20:51 +00:00
|
|
|
}
|
|
|
|
|
2017-11-06 16:04:45 +00:00
|
|
|
func (s *WhisperJailTestSuite) AddKeyPair(address, password string) (string, error) {
|
2017-10-11 14:20:51 +00:00
|
|
|
accountManager := s.Backend.AccountManager()
|
|
|
|
|
2017-11-06 16:04:45 +00:00
|
|
|
_, accountKey, err := accountManager.AddressToDecryptedAccount(address, password)
|
2017-10-11 14:20:51 +00:00
|
|
|
if err != nil {
|
2017-11-06 16:04:45 +00:00
|
|
|
return "", err
|
2017-10-11 14:20:51 +00:00
|
|
|
}
|
|
|
|
|
2017-11-06 16:04:45 +00:00
|
|
|
return s.WhisperService().AddKeyPair(accountKey.PrivateKey)
|
2017-10-11 14:20:51 +00:00
|
|
|
}
|
|
|
|
|
2017-11-06 16:04:45 +00:00
|
|
|
func (s *WhisperJailTestSuite) TestJailWhisper() {
|
2017-10-23 16:03:07 +00:00
|
|
|
s.StartTestBackend()
|
2017-10-11 14:20:51 +00:00
|
|
|
defer s.StopTestBackend()
|
|
|
|
|
2018-02-09 15:39:16 +00:00
|
|
|
r := s.Require()
|
|
|
|
|
2017-11-06 16:04:45 +00:00
|
|
|
keyPairID1, err := s.AddKeyPair(TestConfig.Account1.Address, TestConfig.Account1.Password)
|
2018-02-09 15:39:16 +00:00
|
|
|
r.NoError(err)
|
2017-10-11 14:20:51 +00:00
|
|
|
|
2017-11-06 16:04:45 +00:00
|
|
|
keyPairID2, err := s.AddKeyPair(TestConfig.Account2.Address, TestConfig.Account2.Password)
|
2018-02-09 15:39:16 +00:00
|
|
|
r.NoError(err)
|
2017-10-11 14:20:51 +00:00
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
code string
|
|
|
|
useFilter bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"test 0: ensure correct version of Whisper is used",
|
|
|
|
`
|
2018-03-02 09:25:30 +00:00
|
|
|
var expectedVersion = '6.0';
|
2017-10-11 14:20:51 +00:00
|
|
|
if (web3.version.whisper != expectedVersion) {
|
|
|
|
throw 'unexpected shh version, expected: ' + expectedVersion + ', got: ' + web3.version.whisper;
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"test 1: encrypted signed message from us (From != nil && To != nil)",
|
|
|
|
`
|
2017-11-06 16:04:45 +00:00
|
|
|
var identity1 = '` + keyPairID1 + `';
|
2017-10-11 14:20:51 +00:00
|
|
|
if (!shh.hasKeyPair(identity1)) {
|
2017-11-06 16:04:45 +00:00
|
|
|
throw 'identity "` + keyPairID1 + `" not found in whisper';
|
2017-10-11 14:20:51 +00:00
|
|
|
}
|
|
|
|
|
2017-11-06 16:04:45 +00:00
|
|
|
var identity2 = '` + keyPairID2 + `';
|
2017-10-11 14:20:51 +00:00
|
|
|
if (!shh.hasKeyPair(identity2)) {
|
2017-11-06 16:04:45 +00:00
|
|
|
throw 'identitity "` + keyPairID2 + `" not found in whisper';
|
2017-10-11 14:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var topic = makeTopic();
|
|
|
|
var payload = '` + whisperMessage1 + `';
|
|
|
|
|
|
|
|
// start watching for messages
|
|
|
|
var filter = shh.newMessageFilter({
|
2017-11-06 16:04:45 +00:00
|
|
|
sig: shh.getPublicKey(identity1),
|
2017-10-11 14:20:51 +00:00
|
|
|
privateKeyID: identity2,
|
|
|
|
topics: [topic]
|
|
|
|
});
|
|
|
|
|
|
|
|
// post message
|
|
|
|
var message = {
|
2017-11-06 16:04:45 +00:00
|
|
|
ttl: 10,
|
2018-03-02 09:25:30 +00:00
|
|
|
powTarget: 0.001,
|
|
|
|
powTime: 2,
|
2017-10-11 14:20:51 +00:00
|
|
|
topic: topic,
|
2017-11-06 16:04:45 +00:00
|
|
|
sig: shh.getPublicKey(identity1),
|
|
|
|
pubKey: shh.getPublicKey(identity2),
|
2018-03-02 18:43:08 +00:00
|
|
|
payload: web3.toHex(payload),
|
2017-10-11 14:20:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var sent = shh.post(message)
|
|
|
|
if (!sent) {
|
|
|
|
throw 'message not sent: ' + JSON.stringify(message);
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"test 2: signed (known sender) broadcast (From != nil && To == nil)",
|
|
|
|
`
|
2017-11-06 16:04:45 +00:00
|
|
|
var identity = '` + keyPairID1 + `';
|
2017-10-11 14:20:51 +00:00
|
|
|
if (!shh.hasKeyPair(identity)) {
|
2017-11-06 16:04:45 +00:00
|
|
|
throw 'identity "` + keyPairID1 + `" not found in whisper';
|
2017-10-11 14:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var topic = makeTopic();
|
|
|
|
var payload = '` + whisperMessage2 + `';
|
|
|
|
|
|
|
|
// generate symmetric key
|
|
|
|
var keyid = shh.newSymKey();
|
|
|
|
if (!shh.hasSymKey(keyid)) {
|
|
|
|
throw new Error('key not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
// start watching for messages
|
|
|
|
var filter = shh.newMessageFilter({
|
2017-11-06 16:04:45 +00:00
|
|
|
sig: shh.getPublicKey(identity),
|
2017-10-11 14:20:51 +00:00
|
|
|
topics: [topic],
|
|
|
|
symKeyID: keyid
|
|
|
|
});
|
|
|
|
|
|
|
|
// post message
|
|
|
|
var message = {
|
2017-11-06 16:04:45 +00:00
|
|
|
ttl: 10,
|
|
|
|
powTarget: 1.0,
|
2017-10-11 14:20:51 +00:00
|
|
|
powTime: 20,
|
|
|
|
topic: topic,
|
2017-11-06 16:04:45 +00:00
|
|
|
sig: shh.getPublicKey(identity),
|
2017-10-11 14:20:51 +00:00
|
|
|
symKeyID: keyid,
|
2018-03-02 18:43:08 +00:00
|
|
|
payload: web3.toHex(payload),
|
2017-10-11 14:20:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var sent = shh.post(message)
|
|
|
|
if (!sent) {
|
|
|
|
throw 'message not sent: ' + JSON.stringify(message);
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"test 3: anonymous broadcast (From == nil && To == nil)",
|
|
|
|
`
|
|
|
|
var topic = makeTopic();
|
|
|
|
var payload = '` + whisperMessage3 + `';
|
|
|
|
|
|
|
|
// generate symmetric key
|
|
|
|
var keyid = shh.newSymKey();
|
|
|
|
if (!shh.hasSymKey(keyid)) {
|
|
|
|
throw new Error('key not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
// start watching for messages
|
|
|
|
var filter = shh.newMessageFilter({
|
|
|
|
topics: [topic],
|
|
|
|
symKeyID: keyid
|
|
|
|
});
|
2018-03-02 18:43:08 +00:00
|
|
|
// creating a filter is an async operation
|
|
|
|
setTimeout(function() {
|
|
|
|
// post message
|
|
|
|
var message = {
|
2017-11-06 16:04:45 +00:00
|
|
|
ttl: 10,
|
2018-03-02 09:25:30 +00:00
|
|
|
powTarget: 0.001,
|
|
|
|
powTime: 2,
|
2017-11-06 16:04:45 +00:00
|
|
|
topic: topic,
|
|
|
|
symKeyID: keyid,
|
2018-03-02 18:43:08 +00:00
|
|
|
payload: web3.toHex(payload),
|
|
|
|
};
|
2017-11-06 16:04:45 +00:00
|
|
|
|
2018-03-02 18:43:08 +00:00
|
|
|
var sent = shh.post(message)
|
|
|
|
if (!sent) {
|
2017-11-06 16:04:45 +00:00
|
|
|
throw 'message not sent: ' + JSON.stringify(message);
|
2018-03-02 18:43:08 +00:00
|
|
|
}
|
|
|
|
}, 0)
|
2017-11-06 16:04:45 +00:00
|
|
|
`,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"test 4: encrypted anonymous message (From == nil && To != nil)",
|
|
|
|
`
|
|
|
|
var identity = '` + keyPairID1 + `';
|
|
|
|
if (!shh.hasKeyPair(identity)) {
|
|
|
|
throw 'identity "` + keyPairID1 + `" not found in whisper';
|
|
|
|
}
|
|
|
|
|
|
|
|
var topic = makeTopic();
|
|
|
|
var payload = '` + whisperMessage4 + `';
|
|
|
|
|
|
|
|
// start watching for messages
|
|
|
|
var filter = shh.newMessageFilter({
|
|
|
|
privateKeyID: identity,
|
|
|
|
topics: [topic],
|
|
|
|
});
|
|
|
|
|
2017-10-11 14:20:51 +00:00
|
|
|
// post message
|
|
|
|
var message = {
|
|
|
|
ttl: 20,
|
|
|
|
powTarget: 0.01,
|
|
|
|
powTime: 20,
|
|
|
|
topic: topic,
|
2017-11-06 16:04:45 +00:00
|
|
|
pubKey: shh.getPublicKey(identity),
|
2018-03-02 18:43:08 +00:00
|
|
|
payload: web3.toHex(payload),
|
2017-10-11 14:20:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var sent = shh.post(message)
|
|
|
|
if (!sent) {
|
|
|
|
throw 'message not sent: ' + JSON.stringify(message);
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"test 5: encrypted signed response to us (From != nil && To != nil)",
|
|
|
|
`
|
2017-11-06 16:04:45 +00:00
|
|
|
var identity1 = '` + keyPairID1 + `';
|
2017-10-11 14:20:51 +00:00
|
|
|
if (!shh.hasKeyPair(identity1)) {
|
2017-11-06 16:04:45 +00:00
|
|
|
throw 'identity "` + keyPairID1 + `" not found in whisper';
|
2017-10-11 14:20:51 +00:00
|
|
|
}
|
2017-11-06 16:04:45 +00:00
|
|
|
var identity2 = '` + keyPairID2 + `';
|
2017-10-11 14:20:51 +00:00
|
|
|
if (!shh.hasKeyPair(identity2)) {
|
2017-11-06 16:04:45 +00:00
|
|
|
throw 'identity "` + keyPairID2 + `" not found in whisper';
|
2017-10-11 14:20:51 +00:00
|
|
|
}
|
|
|
|
var topic = makeTopic();
|
|
|
|
var payload = '` + whisperMessage5 + `';
|
|
|
|
// start watching for messages
|
|
|
|
var filter = shh.newMessageFilter({
|
|
|
|
privateKeyID: identity1,
|
2017-11-06 16:04:45 +00:00
|
|
|
sig: shh.getPublicKey(identity2),
|
2017-10-11 14:20:51 +00:00
|
|
|
topics: [topic],
|
|
|
|
});
|
|
|
|
|
|
|
|
// post message
|
|
|
|
var message = {
|
2017-11-06 16:04:45 +00:00
|
|
|
ttl: 10,
|
2018-03-02 09:25:30 +00:00
|
|
|
powTarget: 0.001,
|
|
|
|
powTime: 2,
|
2018-03-02 18:43:08 +00:00
|
|
|
sig: shh.getPublicKey(identity2),
|
|
|
|
pubKey: shh.getPublicKey(identity1),
|
|
|
|
topic: topic,
|
|
|
|
payload: web3.toHex(payload)
|
2017-10-11 14:20:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var sent = shh.post(message)
|
|
|
|
if (!sent) {
|
|
|
|
throw 'message not sent: ' + message;
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-11-06 16:04:45 +00:00
|
|
|
makeTopicCode := `
|
|
|
|
var shh = web3.shh;
|
|
|
|
// topic must be 4-byte long
|
|
|
|
var makeTopic = function () {
|
|
|
|
var topic = '0x';
|
|
|
|
for (var i = 0; i < 8; i++) {
|
|
|
|
topic += Math.floor(Math.random() * 16).toString(16);
|
|
|
|
}
|
|
|
|
return topic;
|
|
|
|
};
|
|
|
|
`
|
2017-10-11 14:20:51 +00:00
|
|
|
|
2017-11-06 16:04:45 +00:00
|
|
|
for _, tc := range testCases {
|
2017-10-11 14:20:51 +00:00
|
|
|
chatID := crypto.Keccak256Hash([]byte(tc.name)).Hex()
|
2017-11-07 17:36:42 +00:00
|
|
|
|
|
|
|
s.Jail.CreateAndInitCell(chatID, makeTopicCode)
|
2017-10-11 14:20:51 +00:00
|
|
|
|
2018-03-01 16:48:30 +00:00
|
|
|
jailCell, err := s.Jail.Cell(chatID)
|
2018-02-09 15:39:16 +00:00
|
|
|
r.NoError(err, "cannot get VM")
|
2017-10-11 14:20:51 +00:00
|
|
|
|
2018-03-01 16:48:30 +00:00
|
|
|
cell := jailCell.(*jail.Cell)
|
|
|
|
|
2017-11-06 16:04:45 +00:00
|
|
|
// Run JS code that setups filters and sends messages.
|
2017-10-11 14:20:51 +00:00
|
|
|
_, err = cell.Run(tc.code)
|
2018-02-09 15:39:16 +00:00
|
|
|
r.NoError(err)
|
2017-10-11 14:20:51 +00:00
|
|
|
|
|
|
|
if !tc.useFilter {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
done := make(chan struct{})
|
|
|
|
timedOut := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(s.Timeout):
|
|
|
|
close(timedOut)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-11-06 16:04:45 +00:00
|
|
|
// Use polling because:
|
|
|
|
// (1) filterId is not assigned immediately,
|
|
|
|
// (2) messages propagate with some delay.
|
2017-10-11 14:20:51 +00:00
|
|
|
poll_loop:
|
|
|
|
for {
|
2017-11-06 16:04:45 +00:00
|
|
|
filter, err := cell.Get("filter")
|
2018-02-09 15:39:16 +00:00
|
|
|
r.NoError(err, "cannot get filter")
|
2018-03-01 16:48:30 +00:00
|
|
|
filterID, err := cell.GetObjectValue(filter.Value(), "filterId")
|
2018-02-09 15:39:16 +00:00
|
|
|
r.NoError(err, "cannot get filterId")
|
2017-11-06 16:04:45 +00:00
|
|
|
|
2017-10-11 14:20:51 +00:00
|
|
|
select {
|
|
|
|
case <-done:
|
2018-03-01 16:48:30 +00:00
|
|
|
ok, err := s.WhisperAPI.DeleteMessageFilter(filterID.Value().String())
|
2018-02-09 15:39:16 +00:00
|
|
|
r.NoError(err)
|
|
|
|
r.True(ok)
|
2017-10-11 14:20:51 +00:00
|
|
|
break poll_loop
|
|
|
|
case <-timedOut:
|
2018-03-02 09:25:30 +00:00
|
|
|
s.FailNow("polling for messages timed out. Test case: " + tc.name)
|
2017-10-11 14:20:51 +00:00
|
|
|
case <-time.After(time.Second):
|
|
|
|
}
|
|
|
|
|
|
|
|
// FilterID is not assigned yet.
|
2018-03-01 16:48:30 +00:00
|
|
|
if filterID.Value().IsNull() {
|
2017-10-11 14:20:51 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
payload, err := cell.Get("payload")
|
2018-02-09 15:39:16 +00:00
|
|
|
r.NoError(err, "cannot get payload")
|
2017-10-11 14:20:51 +00:00
|
|
|
|
2018-03-01 16:48:30 +00:00
|
|
|
messages, err := s.WhisperAPI.GetFilterMessages(filterID.Value().String())
|
2018-02-09 15:39:16 +00:00
|
|
|
r.NoError(err)
|
2017-11-06 16:04:45 +00:00
|
|
|
|
2017-10-11 14:20:51 +00:00
|
|
|
for _, m := range messages {
|
2018-03-01 16:48:30 +00:00
|
|
|
r.Equal(payload.Value().String(), string(m.Payload))
|
2017-10-11 14:20:51 +00:00
|
|
|
close(done)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|