Fix TestJailWhisper e2e test (#442)
We have a test called TestJailWhisper that checks various Whisper communication scenarios (like sending a message signed with a symmetric key, asymmetric keys etc.). However, it was written in a bit cryptic way, so I also refactored it. Important changes: * Cleaner way of importing keys using AddKeyPair() method in the test suite, * Removed TestEncryptedAnonymousMessage because it is not needed anymore as "test 4" in TestJailWhisper was fixed, * Bumped PoW to 2.0. 0.01 used by status-react makes this test flaky.
This commit is contained in:
parent
c6e98b948b
commit
fb75054a35
4
Makefile
4
Makefile
|
@ -13,7 +13,7 @@ endif
|
||||||
CGO_CFLAGS=-I/$(JAVA_HOME)/include -I/$(JAVA_HOME)/include/darwin
|
CGO_CFLAGS=-I/$(JAVA_HOME)/include -I/$(JAVA_HOME)/include/darwin
|
||||||
GOBIN = build/bin
|
GOBIN = build/bin
|
||||||
GO ?= latest
|
GO ?= latest
|
||||||
networkid ?=
|
networkid ?=
|
||||||
|
|
||||||
# This is a code for automatic help generator.
|
# This is a code for automatic help generator.
|
||||||
# It supports ANSI colors and categories.
|
# It supports ANSI colors and categories.
|
||||||
|
@ -123,4 +123,4 @@ ci: lint mock-install mock test-unit test-e2e ##@tests Run all linters and tests
|
||||||
|
|
||||||
clean: ##@other Cleanup
|
clean: ##@other Cleanup
|
||||||
rm -fr build/bin/*
|
rm -fr build/bin/*
|
||||||
rm coverage.out coverage-all.out coverage.html
|
rm coverage.out coverage-all.out coverage.html
|
||||||
|
|
|
@ -1,14 +1,9 @@
|
||||||
package whisper
|
package whisper
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"crypto/rand"
|
|
||||||
"errors"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/accounts/keystore"
|
|
||||||
gethcommon "github.com/ethereum/go-ethereum/common"
|
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
whisper "github.com/ethereum/go-ethereum/whisper/whisperv5"
|
whisper "github.com/ethereum/go-ethereum/whisper/whisperv5"
|
||||||
"github.com/status-im/status-go/e2e"
|
"github.com/status-im/status-go/e2e"
|
||||||
|
@ -32,9 +27,7 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestWhisperJailTestSuite(t *testing.T) {
|
func TestWhisperJailTestSuite(t *testing.T) {
|
||||||
s := new(WhisperJailTestSuite)
|
suite.Run(t, new(WhisperJailTestSuite))
|
||||||
s.Timeout = time.Minute * 5
|
|
||||||
suite.Run(t, s)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type WhisperJailTestSuite struct {
|
type WhisperJailTestSuite struct {
|
||||||
|
@ -48,46 +41,33 @@ type WhisperJailTestSuite struct {
|
||||||
func (s *WhisperJailTestSuite) StartTestBackend(opts ...e2e.TestNodeOption) {
|
func (s *WhisperJailTestSuite) StartTestBackend(opts ...e2e.TestNodeOption) {
|
||||||
s.BackendTestSuite.StartTestBackend(opts...)
|
s.BackendTestSuite.StartTestBackend(opts...)
|
||||||
|
|
||||||
|
s.Timeout = time.Minute * 5
|
||||||
s.WhisperAPI = whisper.NewPublicWhisperAPI(s.WhisperService())
|
s.WhisperAPI = whisper.NewPublicWhisperAPI(s.WhisperService())
|
||||||
s.Jail = s.Backend.JailManager()
|
s.Jail = s.Backend.JailManager()
|
||||||
s.NotNil(s.Jail)
|
s.NotNil(s.Jail)
|
||||||
|
|
||||||
s.Jail.BaseJS(baseStatusJSCode)
|
s.Jail.BaseJS(baseStatusJSCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *WhisperJailTestSuite) GetAccountKey(account struct {
|
func (s *WhisperJailTestSuite) AddKeyPair(address, password string) (string, error) {
|
||||||
Address string
|
|
||||||
Password string
|
|
||||||
}) (*keystore.Key, string, error) {
|
|
||||||
accountManager := s.Backend.AccountManager()
|
accountManager := s.Backend.AccountManager()
|
||||||
|
|
||||||
_, accountKey1, err := accountManager.AddressToDecryptedAccount(account.Address, account.Password)
|
_, accountKey, err := accountManager.AddressToDecryptedAccount(address, password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, "", err
|
return "", err
|
||||||
}
|
|
||||||
accountKey1Hex := gethcommon.ToHex(crypto.FromECDSAPub(&accountKey1.PrivateKey.PublicKey))
|
|
||||||
|
|
||||||
_, err = s.WhisperService().AddKeyPair(accountKey1.PrivateKey)
|
|
||||||
if err != nil {
|
|
||||||
return nil, "", err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ok := s.WhisperAPI.HasKeyPair(context.Background(), accountKey1Hex); !ok {
|
return s.WhisperService().AddKeyPair(accountKey.PrivateKey)
|
||||||
return nil, "", errors.New("KeyPair should be injected in Whisper")
|
|
||||||
}
|
|
||||||
|
|
||||||
return accountKey1, accountKey1Hex, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(adamb) Uncomment when issue #336 is fixed.
|
func (s *WhisperJailTestSuite) TestJailWhisper() {
|
||||||
/*
|
|
||||||
func (s *WhisperJailTestSuite) DontTestJailWhisper() {
|
|
||||||
s.StartTestBackend()
|
s.StartTestBackend()
|
||||||
defer s.StopTestBackend()
|
defer s.StopTestBackend()
|
||||||
|
|
||||||
_, accountKey1Hex, err := s.GetAccountKey(TestConfig.Account1)
|
keyPairID1, err := s.AddKeyPair(TestConfig.Account1.Address, TestConfig.Account1.Password)
|
||||||
s.NoError(err)
|
s.NoError(err)
|
||||||
|
|
||||||
_, accountKey2Hex, err := s.GetAccountKey(TestConfig.Account2)
|
keyPairID2, err := s.AddKeyPair(TestConfig.Account2.Address, TestConfig.Account2.Password)
|
||||||
s.NoError(err)
|
s.NoError(err)
|
||||||
|
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
|
@ -108,14 +88,14 @@ func (s *WhisperJailTestSuite) DontTestJailWhisper() {
|
||||||
{
|
{
|
||||||
"test 1: encrypted signed message from us (From != nil && To != nil)",
|
"test 1: encrypted signed message from us (From != nil && To != nil)",
|
||||||
`
|
`
|
||||||
var identity1 = '` + accountKey1Hex + `';
|
var identity1 = '` + keyPairID1 + `';
|
||||||
if (!shh.hasKeyPair(identity1)) {
|
if (!shh.hasKeyPair(identity1)) {
|
||||||
throw 'idenitity "` + accountKey1Hex + `" not found in whisper';
|
throw 'identity "` + keyPairID1 + `" not found in whisper';
|
||||||
}
|
}
|
||||||
|
|
||||||
var identity2 = '` + accountKey2Hex + `';
|
var identity2 = '` + keyPairID2 + `';
|
||||||
if (!shh.hasKeyPair(identity2)) {
|
if (!shh.hasKeyPair(identity2)) {
|
||||||
throw 'identitity "` + accountKey2Hex + `" not found in whisper';
|
throw 'identitity "` + keyPairID2 + `" not found in whisper';
|
||||||
}
|
}
|
||||||
|
|
||||||
var topic = makeTopic();
|
var topic = makeTopic();
|
||||||
|
@ -123,19 +103,19 @@ func (s *WhisperJailTestSuite) DontTestJailWhisper() {
|
||||||
|
|
||||||
// start watching for messages
|
// start watching for messages
|
||||||
var filter = shh.newMessageFilter({
|
var filter = shh.newMessageFilter({
|
||||||
sig: identity1,
|
sig: shh.getPublicKey(identity1),
|
||||||
privateKeyID: identity2,
|
privateKeyID: identity2,
|
||||||
topics: [topic]
|
topics: [topic]
|
||||||
});
|
});
|
||||||
|
|
||||||
// post message
|
// post message
|
||||||
var message = {
|
var message = {
|
||||||
ttl: 20,
|
ttl: 10,
|
||||||
powTarget: 0.01,
|
powTarget: 1.0,
|
||||||
powTime: 20,
|
powTime: 20,
|
||||||
topic: topic,
|
topic: topic,
|
||||||
sig: identity1,
|
sig: shh.getPublicKey(identity1),
|
||||||
pubKey: identity2,
|
pubKey: shh.getPublicKey(identity2),
|
||||||
payload: web3.toHex(payload),
|
payload: web3.toHex(payload),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -149,9 +129,9 @@ func (s *WhisperJailTestSuite) DontTestJailWhisper() {
|
||||||
{
|
{
|
||||||
"test 2: signed (known sender) broadcast (From != nil && To == nil)",
|
"test 2: signed (known sender) broadcast (From != nil && To == nil)",
|
||||||
`
|
`
|
||||||
var identity = '` + accountKey1Hex + `';
|
var identity = '` + keyPairID1 + `';
|
||||||
if (!shh.hasKeyPair(identity)) {
|
if (!shh.hasKeyPair(identity)) {
|
||||||
throw 'idenitity "` + accountKey1Hex + `" not found in whisper';
|
throw 'identity "` + keyPairID1 + `" not found in whisper';
|
||||||
}
|
}
|
||||||
|
|
||||||
var topic = makeTopic();
|
var topic = makeTopic();
|
||||||
|
@ -165,18 +145,18 @@ func (s *WhisperJailTestSuite) DontTestJailWhisper() {
|
||||||
|
|
||||||
// start watching for messages
|
// start watching for messages
|
||||||
var filter = shh.newMessageFilter({
|
var filter = shh.newMessageFilter({
|
||||||
sig: identity,
|
sig: shh.getPublicKey(identity),
|
||||||
topics: [topic],
|
topics: [topic],
|
||||||
symKeyID: keyid
|
symKeyID: keyid
|
||||||
});
|
});
|
||||||
|
|
||||||
// post message
|
// post message
|
||||||
var message = {
|
var message = {
|
||||||
ttl: 20,
|
ttl: 10,
|
||||||
powTarget: 0.01,
|
powTarget: 1.0,
|
||||||
powTime: 20,
|
powTime: 20,
|
||||||
topic: topic,
|
topic: topic,
|
||||||
sig: identity,
|
sig: shh.getPublicKey(identity),
|
||||||
symKeyID: keyid,
|
symKeyID: keyid,
|
||||||
payload: web3.toHex(payload),
|
payload: web3.toHex(payload),
|
||||||
};
|
};
|
||||||
|
@ -208,8 +188,8 @@ func (s *WhisperJailTestSuite) DontTestJailWhisper() {
|
||||||
|
|
||||||
// post message
|
// post message
|
||||||
var message = {
|
var message = {
|
||||||
ttl: 20,
|
ttl: 10,
|
||||||
powTarget: 0.01,
|
powTarget: 1.0,
|
||||||
powTime: 20,
|
powTime: 20,
|
||||||
topic: topic,
|
topic: topic,
|
||||||
symKeyID: keyid,
|
symKeyID: keyid,
|
||||||
|
@ -223,71 +203,69 @@ func (s *WhisperJailTestSuite) DontTestJailWhisper() {
|
||||||
`,
|
`,
|
||||||
true,
|
true,
|
||||||
},
|
},
|
||||||
// @TODO(adam): quarantined as always failing. Check out TestEncryptedAnonymousMessage
|
{
|
||||||
// as an equivalent test in pure Go which passes. Bug in web3?
|
"test 4: encrypted anonymous message (From == nil && To != nil)",
|
||||||
// {
|
`
|
||||||
// "test 4: encrypted anonymous message (From == nil && To != nil)",
|
var identity = '` + keyPairID1 + `';
|
||||||
// `
|
if (!shh.hasKeyPair(identity)) {
|
||||||
// var identity = '` + accountKey2Hex + `';
|
throw 'identity "` + keyPairID1 + `" not found in whisper';
|
||||||
// if (!shh.hasKeyPair(identity)) {
|
}
|
||||||
// throw 'idenitity "` + accountKey2Hex + `" not found in whisper';
|
|
||||||
// }
|
|
||||||
|
|
||||||
// var topic = makeTopic();
|
var topic = makeTopic();
|
||||||
// var payload = '` + whisperMessage4 + `';
|
var payload = '` + whisperMessage4 + `';
|
||||||
|
|
||||||
// // start watching for messages
|
// start watching for messages
|
||||||
// var filter = shh.newMessageFilter({
|
var filter = shh.newMessageFilter({
|
||||||
// privateKeyID: identity,
|
privateKeyID: identity,
|
||||||
// topics: [topic],
|
topics: [topic],
|
||||||
// });
|
});
|
||||||
|
|
||||||
// // post message
|
// post message
|
||||||
// var message = {
|
var message = {
|
||||||
// ttl: 20,
|
ttl: 20,
|
||||||
// powTarget: 0.01,
|
powTarget: 0.01,
|
||||||
// powTime: 20,
|
powTime: 20,
|
||||||
// topic: topic,
|
topic: topic,
|
||||||
// pubKey: identity,
|
pubKey: shh.getPublicKey(identity),
|
||||||
// payload: web3.toHex(payload),
|
payload: web3.toHex(payload),
|
||||||
// };
|
};
|
||||||
|
|
||||||
// var sent = shh.post(message)
|
var sent = shh.post(message)
|
||||||
// if (!sent) {
|
if (!sent) {
|
||||||
// throw 'message not sent: ' + JSON.stringify(message);
|
throw 'message not sent: ' + JSON.stringify(message);
|
||||||
// }
|
}
|
||||||
// `,
|
`,
|
||||||
// true,
|
true,
|
||||||
// },
|
},
|
||||||
{
|
{
|
||||||
"test 5: encrypted signed response to us (From != nil && To != nil)",
|
"test 5: encrypted signed response to us (From != nil && To != nil)",
|
||||||
`
|
`
|
||||||
var identity1 = '` + accountKey1Hex + `';
|
var identity1 = '` + keyPairID1 + `';
|
||||||
if (!shh.hasKeyPair(identity1)) {
|
if (!shh.hasKeyPair(identity1)) {
|
||||||
throw 'idenitity "` + accountKey1Hex + `" not found in whisper';
|
throw 'identity "` + keyPairID1 + `" not found in whisper';
|
||||||
}
|
}
|
||||||
var identity2 = '` + accountKey2Hex + `';
|
var identity2 = '` + keyPairID2 + `';
|
||||||
if (!shh.hasKeyPair(identity2)) {
|
if (!shh.hasKeyPair(identity2)) {
|
||||||
throw 'idenitity "` + accountKey2Hex + `" not found in whisper';
|
throw 'identity "` + keyPairID2 + `" not found in whisper';
|
||||||
}
|
}
|
||||||
var topic = makeTopic();
|
var topic = makeTopic();
|
||||||
var payload = '` + whisperMessage5 + `';
|
var payload = '` + whisperMessage5 + `';
|
||||||
// start watching for messages
|
// start watching for messages
|
||||||
var filter = shh.newMessageFilter({
|
var filter = shh.newMessageFilter({
|
||||||
privateKeyID: identity1,
|
privateKeyID: identity1,
|
||||||
sig: identity2,
|
sig: shh.getPublicKey(identity2),
|
||||||
topics: [topic],
|
topics: [topic],
|
||||||
});
|
});
|
||||||
|
|
||||||
// post message
|
// post message
|
||||||
var message = {
|
var message = {
|
||||||
sig: identity2,
|
ttl: 10,
|
||||||
pubKey: identity1,
|
powTarget: 1.0,
|
||||||
topic: topic,
|
|
||||||
payload: web3.toHex(payload),
|
|
||||||
ttl: 20,
|
|
||||||
powTime: 20,
|
powTime: 20,
|
||||||
powTarget: 0.01,
|
sig: shh.getPublicKey(identity2),
|
||||||
|
pubKey: shh.getPublicKey(identity1),
|
||||||
|
topic: topic,
|
||||||
|
payload: web3.toHex(payload)
|
||||||
};
|
};
|
||||||
|
|
||||||
var sent = shh.post(message)
|
var sent = shh.post(message)
|
||||||
|
@ -299,27 +277,26 @@ func (s *WhisperJailTestSuite) DontTestJailWhisper() {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
};
|
||||||
|
`
|
||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
s.T().Log(tc.name)
|
|
||||||
|
|
||||||
chatID := crypto.Keccak256Hash([]byte(tc.name)).Hex()
|
chatID := crypto.Keccak256Hash([]byte(tc.name)).Hex()
|
||||||
|
s.Jail.Parse(chatID, makeTopicCode)
|
||||||
s.Jail.Parse(chatID, `
|
|
||||||
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;
|
|
||||||
};
|
|
||||||
`)
|
|
||||||
|
|
||||||
cell, err := s.Jail.Cell(chatID)
|
cell, err := s.Jail.Cell(chatID)
|
||||||
s.NoError(err, "cannot get VM")
|
s.NoError(err, "cannot get VM")
|
||||||
|
|
||||||
// Setup filters and post messages.
|
// Run JS code that setups filters and sends messages.
|
||||||
_, err = cell.Run(tc.code)
|
_, err = cell.Run(tc.code)
|
||||||
s.NoError(err)
|
s.NoError(err)
|
||||||
|
|
||||||
|
@ -337,24 +314,27 @@ func (s *WhisperJailTestSuite) DontTestJailWhisper() {
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
// Use polling because:
|
||||||
|
// (1) filterId is not assigned immediately,
|
||||||
|
// (2) messages propagate with some delay.
|
||||||
poll_loop:
|
poll_loop:
|
||||||
for {
|
for {
|
||||||
// Use polling because:
|
filter, err := cell.Get("filter")
|
||||||
// (1) filterId is not assigned immediately,
|
s.NoError(err, "cannot get filter")
|
||||||
// (2) messages propagate with some delay.
|
filterID, err := filter.Object().Get("filterId")
|
||||||
|
s.NoError(err, "cannot get filterId")
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-done:
|
case <-done:
|
||||||
|
ok, err := s.WhisperAPI.DeleteMessageFilter(filterID.String())
|
||||||
|
s.NoError(err)
|
||||||
|
s.True(ok)
|
||||||
break poll_loop
|
break poll_loop
|
||||||
case <-timedOut:
|
case <-timedOut:
|
||||||
s.FailNow("polling for messages timed out")
|
s.FailNow("polling for messages timed out")
|
||||||
case <-time.After(time.Second):
|
case <-time.After(time.Second):
|
||||||
}
|
}
|
||||||
|
|
||||||
filter, err := cell.Get("filter")
|
|
||||||
s.NoError(err, "cannot get filter")
|
|
||||||
filterID, err := filter.Object().Get("filterId")
|
|
||||||
s.NoError(err, "cannot get filterId")
|
|
||||||
|
|
||||||
// FilterID is not assigned yet.
|
// FilterID is not assigned yet.
|
||||||
if filterID.IsNull() {
|
if filterID.IsNull() {
|
||||||
continue
|
continue
|
||||||
|
@ -365,6 +345,7 @@ func (s *WhisperJailTestSuite) DontTestJailWhisper() {
|
||||||
|
|
||||||
messages, err := s.WhisperAPI.GetFilterMessages(filterID.String())
|
messages, err := s.WhisperAPI.GetFilterMessages(filterID.String())
|
||||||
s.NoError(err)
|
s.NoError(err)
|
||||||
|
|
||||||
for _, m := range messages {
|
for _, m := range messages {
|
||||||
s.Equal(payload.String(), string(m.Payload))
|
s.Equal(payload.String(), string(m.Payload))
|
||||||
close(done)
|
close(done)
|
||||||
|
@ -372,62 +353,3 @@ func (s *WhisperJailTestSuite) DontTestJailWhisper() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
func (s *WhisperJailTestSuite) TestEncryptedAnonymousMessage() {
|
|
||||||
s.StartTestBackend()
|
|
||||||
defer s.StopTestBackend()
|
|
||||||
|
|
||||||
accountKey2, accountKey2Hex, err := s.GetAccountKey(TestConfig.Account2)
|
|
||||||
s.NoError(err)
|
|
||||||
|
|
||||||
topicSlice := make([]byte, whisper.TopicLength)
|
|
||||||
_, err = rand.Read(topicSlice)
|
|
||||||
s.NoError(err)
|
|
||||||
|
|
||||||
topic := whisper.BytesToTopic(topicSlice)
|
|
||||||
|
|
||||||
filter, err := s.WhisperAPI.NewMessageFilter(whisper.Criteria{
|
|
||||||
PrivateKeyID: accountKey2Hex,
|
|
||||||
Topics: []whisper.TopicType{topic},
|
|
||||||
})
|
|
||||||
s.NoError(err)
|
|
||||||
|
|
||||||
ok, err := s.WhisperAPI.Post(context.Background(), whisper.NewMessage{
|
|
||||||
TTL: 20,
|
|
||||||
PowTarget: 0.01,
|
|
||||||
PowTime: 20,
|
|
||||||
Topic: topic,
|
|
||||||
PublicKey: crypto.FromECDSAPub(&accountKey2.PrivateKey.PublicKey),
|
|
||||||
Payload: []byte(whisperMessage4),
|
|
||||||
})
|
|
||||||
s.NoError(err)
|
|
||||||
s.True(ok)
|
|
||||||
|
|
||||||
done := make(chan struct{})
|
|
||||||
timedOut := make(chan struct{})
|
|
||||||
go func() {
|
|
||||||
select {
|
|
||||||
case <-done:
|
|
||||||
case <-time.After(s.Timeout):
|
|
||||||
close(timedOut)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-done:
|
|
||||||
return
|
|
||||||
case <-timedOut:
|
|
||||||
s.FailNow("polling for messages timed out")
|
|
||||||
case <-time.After(time.Second):
|
|
||||||
}
|
|
||||||
|
|
||||||
messages, err := s.WhisperAPI.GetFilterMessages(filter)
|
|
||||||
s.NoError(err)
|
|
||||||
for _, m := range messages {
|
|
||||||
s.Equal(whisperMessage4, string(m.Payload))
|
|
||||||
close(done)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue