2020-01-20 20:56:06 +00:00
|
|
|
package wakuext
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"math"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-02-18 11:21:01 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2020-01-20 20:56:06 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/storage"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/node"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
2020-07-22 07:41:40 +00:00
|
|
|
"github.com/status-im/status-go/appdatabase"
|
2020-01-20 20:56:06 +00:00
|
|
|
gethbridge "github.com/status-im/status-go/eth-node/bridge/geth"
|
|
|
|
"github.com/status-im/status-go/eth-node/crypto"
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2020-11-25 11:39:01 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts"
|
2020-01-20 20:56:06 +00:00
|
|
|
"github.com/status-im/status-go/params"
|
|
|
|
"github.com/status-im/status-go/services/ext"
|
2022-09-27 20:27:20 +00:00
|
|
|
"github.com/status-im/status-go/sqlite"
|
2020-01-20 20:56:06 +00:00
|
|
|
"github.com/status-im/status-go/t/helpers"
|
|
|
|
"github.com/status-im/status-go/waku"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRequestMessagesErrors(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
waku := gethbridge.NewGethWakuWrapper(waku.New(nil, nil))
|
|
|
|
aNode, err := node.New(&node.Config{
|
|
|
|
P2P: p2p.Config{
|
|
|
|
MaxPeers: math.MaxInt32,
|
|
|
|
NoDiscovery: true,
|
|
|
|
},
|
|
|
|
NoUSB: true,
|
|
|
|
}) // in-memory node as no data dir
|
|
|
|
require.NoError(t, err)
|
2021-07-09 13:19:33 +00:00
|
|
|
w := gethbridge.GetGethWakuFrom(waku)
|
|
|
|
aNode.RegisterLifecycle(w)
|
|
|
|
aNode.RegisterAPIs(w.APIs())
|
|
|
|
aNode.RegisterProtocols(w.Protocols())
|
2020-01-20 20:56:06 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = aNode.Start()
|
|
|
|
require.NoError(t, err)
|
2021-07-09 13:19:33 +00:00
|
|
|
defer func() { require.NoError(t, aNode.Close()) }()
|
2020-01-20 20:56:06 +00:00
|
|
|
|
|
|
|
handler := ext.NewHandlerMock(1)
|
2022-01-12 16:02:01 +00:00
|
|
|
config := params.NodeConfig{
|
|
|
|
ShhextConfig: params.ShhextConfig{
|
|
|
|
InstallationID: "1",
|
|
|
|
BackupDisabledDataDir: os.TempDir(),
|
|
|
|
PFSEnabled: true,
|
|
|
|
},
|
2020-01-20 20:56:06 +00:00
|
|
|
}
|
|
|
|
nodeWrapper := ext.NewTestNodeWrapper(nil, waku)
|
2022-06-02 12:17:52 +00:00
|
|
|
service := New(config, nodeWrapper, nil, handler, nil)
|
2020-01-20 20:56:06 +00:00
|
|
|
api := NewPublicAPI(service)
|
|
|
|
|
|
|
|
const mailServerPeer = "enode://b7e65e1bedc2499ee6cbd806945af5e7df0e59e4070c96821570bd581473eade24a489f5ec95d060c0db118c879403ab88d827d3766978f28708989d35474f87@[::]:51920"
|
|
|
|
|
|
|
|
var hash []byte
|
|
|
|
|
|
|
|
// invalid MailServer enode address
|
|
|
|
hash, err = api.RequestMessages(context.TODO(), ext.MessagesRequest{MailServerPeer: "invalid-address"})
|
|
|
|
require.Nil(t, hash)
|
|
|
|
require.EqualError(t, err, "invalid mailServerPeer value: invalid URL scheme, want \"enode\"")
|
|
|
|
|
|
|
|
// non-existent symmetric key
|
|
|
|
hash, err = api.RequestMessages(context.TODO(), ext.MessagesRequest{
|
|
|
|
MailServerPeer: mailServerPeer,
|
|
|
|
SymKeyID: "invalid-sym-key-id",
|
|
|
|
})
|
|
|
|
require.Nil(t, hash)
|
|
|
|
require.EqualError(t, err, "invalid symKeyID value: non-existent key ID")
|
|
|
|
|
|
|
|
// with a symmetric key
|
|
|
|
symKeyID, symKeyErr := waku.AddSymKeyFromPassword("some-pass")
|
|
|
|
require.NoError(t, symKeyErr)
|
|
|
|
hash, err = api.RequestMessages(context.TODO(), ext.MessagesRequest{
|
|
|
|
MailServerPeer: mailServerPeer,
|
|
|
|
SymKeyID: symKeyID,
|
|
|
|
})
|
|
|
|
require.Nil(t, hash)
|
|
|
|
require.Contains(t, err.Error(), "could not find peer with ID")
|
|
|
|
|
|
|
|
// from is greater than to
|
|
|
|
hash, err = api.RequestMessages(context.TODO(), ext.MessagesRequest{
|
|
|
|
From: 10,
|
|
|
|
To: 5,
|
|
|
|
})
|
|
|
|
require.Nil(t, hash)
|
|
|
|
require.Contains(t, err.Error(), "Query range is invalid: from > to (10 > 5)")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInitProtocol(t *testing.T) {
|
2023-01-03 14:40:11 +00:00
|
|
|
directory, err := os.MkdirTemp("", "status-go-testing")
|
2020-01-20 20:56:06 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-01-12 16:02:01 +00:00
|
|
|
config := params.NodeConfig{
|
|
|
|
ShhextConfig: params.ShhextConfig{
|
|
|
|
InstallationID: "2",
|
|
|
|
BackupDisabledDataDir: directory,
|
|
|
|
PFSEnabled: true,
|
|
|
|
MailServerConfirmations: true,
|
|
|
|
ConnectionTarget: 10,
|
|
|
|
},
|
2020-01-20 20:56:06 +00:00
|
|
|
}
|
|
|
|
db, err := leveldb.Open(storage.NewMemStorage(), nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
waku := gethbridge.NewGethWakuWrapper(waku.New(nil, nil))
|
|
|
|
privateKey, err := crypto.GenerateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
nodeWrapper := ext.NewTestNodeWrapper(nil, waku)
|
2022-06-02 12:17:52 +00:00
|
|
|
service := New(config, nodeWrapper, nil, nil, db)
|
2020-01-20 20:56:06 +00:00
|
|
|
|
2023-01-03 14:40:11 +00:00
|
|
|
tmpdir, err := os.MkdirTemp("", "test-shhext-service-init-protocol")
|
2020-01-20 20:56:06 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-09-27 20:27:20 +00:00
|
|
|
sqlDB, err := appdatabase.InitializeDB(fmt.Sprintf("%s/db.sql", tmpdir), "password", sqlite.ReducedKDFIterationsNumber)
|
2020-01-20 20:56:06 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-11-24 13:13:46 +00:00
|
|
|
tmpfile, err := ioutil.TempFile("", "multi-accounts-tests-")
|
|
|
|
require.NoError(t, err)
|
|
|
|
multiAccounts, err := multiaccounts.InitializeDB(tmpfile.Name())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-12-15 18:00:31 +00:00
|
|
|
acc := &multiaccounts.Account{KeyUID: "0xdeadbeef"}
|
2020-12-15 15:06:59 +00:00
|
|
|
|
Check token funds when handling community requests to join
This adds checks to `HandleCommunityRequestToJoin` and
`AcceptRequestToJoinCommunity` that ensure a given user's revealed
wallet addresses own the token funds required by a community.
When community has token permissions of type `BECOME_MEMBER`, the
following happens when the owner receives a request:
1. Upon verifying provided wallet addresses by the requester, the owner
node accumulates all token funds related to the given wallets that
match the token criteria in the configured permissions
2. If the requester does not meet the necessary requirements, the
request to join will be declined. If the requester does have the
funds, he'll either be automatically accepted to the community, or
enters the next stage where an owner needs to manually accept the
request.
3. The the community does not automatically accept users, then the funds
check will happen again, when the owner tries to manually accept the
request. If the necessary funds do not exist at this stage, the
request will be declined
4. Upon accepting, whether automatically or manually, the owner adds the
requester's wallet addresses to the `CommunityDescription`, such that
they can be retrieved later when doing periodic checks or when
permissions have changed.
2023-03-16 14:35:33 +00:00
|
|
|
err = service.InitProtocol("Test", privateKey, sqlDB, nil, multiAccounts, acc, nil, nil, zap.NewNop())
|
2020-01-20 20:56:06 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShhExtSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(ShhExtSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type ShhExtSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
|
|
|
dir string
|
|
|
|
nodes []*node.Node
|
|
|
|
wakus []types.Waku
|
|
|
|
services []*Service
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ShhExtSuite) createAndAddNode() {
|
|
|
|
idx := len(s.nodes)
|
|
|
|
|
|
|
|
// create a node
|
|
|
|
cfg := &node.Config{
|
|
|
|
Name: strconv.Itoa(idx),
|
|
|
|
P2P: p2p.Config{
|
|
|
|
MaxPeers: math.MaxInt32,
|
|
|
|
NoDiscovery: true,
|
|
|
|
ListenAddr: ":0",
|
|
|
|
},
|
|
|
|
NoUSB: true,
|
|
|
|
}
|
|
|
|
stack, err := node.New(cfg)
|
|
|
|
s.NoError(err)
|
|
|
|
w := waku.New(nil, nil)
|
2021-07-09 13:19:33 +00:00
|
|
|
stack.RegisterLifecycle(w)
|
|
|
|
stack.RegisterAPIs(w.APIs())
|
|
|
|
stack.RegisterProtocols(w.Protocols())
|
2020-01-20 20:56:06 +00:00
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
// set up protocol
|
2022-01-12 16:02:01 +00:00
|
|
|
config := params.NodeConfig{
|
|
|
|
ShhextConfig: params.ShhextConfig{
|
|
|
|
InstallationID: "1",
|
|
|
|
BackupDisabledDataDir: s.dir,
|
|
|
|
PFSEnabled: true,
|
|
|
|
MailServerConfirmations: true,
|
|
|
|
ConnectionTarget: 10,
|
|
|
|
},
|
2020-01-20 20:56:06 +00:00
|
|
|
}
|
|
|
|
db, err := leveldb.Open(storage.NewMemStorage(), nil)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
nodeWrapper := ext.NewTestNodeWrapper(nil, gethbridge.NewGethWakuWrapper(w))
|
2022-06-02 12:17:52 +00:00
|
|
|
service := New(config, nodeWrapper, nil, nil, db)
|
2022-09-27 20:27:20 +00:00
|
|
|
sqlDB, err := appdatabase.InitializeDB(fmt.Sprintf("%s/%d", s.dir, idx), "password", sqlite.ReducedKDFIterationsNumber)
|
2020-01-20 20:56:06 +00:00
|
|
|
s.Require().NoError(err)
|
2020-11-24 13:13:46 +00:00
|
|
|
|
|
|
|
tmpfile, err := ioutil.TempFile("", "multi-accounts-tests-")
|
|
|
|
s.Require().NoError(err)
|
|
|
|
multiAccounts, err := multiaccounts.InitializeDB(tmpfile.Name())
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2020-01-20 20:56:06 +00:00
|
|
|
privateKey, err := crypto.GenerateKey()
|
|
|
|
s.NoError(err)
|
2020-11-24 13:13:46 +00:00
|
|
|
|
2020-12-15 18:00:31 +00:00
|
|
|
acc := &multiaccounts.Account{KeyUID: "0xdeadbeef"}
|
2020-12-15 15:06:59 +00:00
|
|
|
|
Check token funds when handling community requests to join
This adds checks to `HandleCommunityRequestToJoin` and
`AcceptRequestToJoinCommunity` that ensure a given user's revealed
wallet addresses own the token funds required by a community.
When community has token permissions of type `BECOME_MEMBER`, the
following happens when the owner receives a request:
1. Upon verifying provided wallet addresses by the requester, the owner
node accumulates all token funds related to the given wallets that
match the token criteria in the configured permissions
2. If the requester does not meet the necessary requirements, the
request to join will be declined. If the requester does have the
funds, he'll either be automatically accepted to the community, or
enters the next stage where an owner needs to manually accept the
request.
3. The the community does not automatically accept users, then the funds
check will happen again, when the owner tries to manually accept the
request. If the necessary funds do not exist at this stage, the
request will be declined
4. Upon accepting, whether automatically or manually, the owner adds the
requester's wallet addresses to the `CommunityDescription`, such that
they can be retrieved later when doing periodic checks or when
permissions have changed.
2023-03-16 14:35:33 +00:00
|
|
|
err = service.InitProtocol("Test", privateKey, sqlDB, nil, multiAccounts, acc, nil, nil, zap.NewNop())
|
2020-01-20 20:56:06 +00:00
|
|
|
s.NoError(err)
|
2020-11-24 13:13:46 +00:00
|
|
|
|
2021-07-09 13:19:33 +00:00
|
|
|
stack.RegisterLifecycle(service)
|
|
|
|
stack.RegisterAPIs(service.APIs())
|
|
|
|
stack.RegisterProtocols(service.Protocols())
|
|
|
|
|
2020-01-20 20:56:06 +00:00
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
// start the node
|
|
|
|
err = stack.Start()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
// store references
|
|
|
|
s.nodes = append(s.nodes, stack)
|
|
|
|
s.wakus = append(s.wakus, gethbridge.NewGethWakuWrapper(w))
|
|
|
|
s.services = append(s.services, service)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ShhExtSuite) SetupTest() {
|
|
|
|
var err error
|
2023-01-03 14:40:11 +00:00
|
|
|
s.dir, err = os.MkdirTemp("", "status-go-testing")
|
2020-01-20 20:56:06 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ShhExtSuite) TearDownTest() {
|
|
|
|
for _, n := range s.nodes {
|
2021-07-09 13:19:33 +00:00
|
|
|
s.NoError(n.Close())
|
2020-01-20 20:56:06 +00:00
|
|
|
}
|
|
|
|
s.nodes = nil
|
|
|
|
s.wakus = nil
|
|
|
|
s.services = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ShhExtSuite) TestRequestMessagesSuccess() {
|
|
|
|
// two nodes needed: client and mailserver
|
|
|
|
s.createAndAddNode()
|
|
|
|
s.createAndAddNode()
|
|
|
|
|
|
|
|
waitErr := helpers.WaitForPeerAsync(s.nodes[0].Server(), s.nodes[1].Server().Self().URLv4(), p2p.PeerEventTypeAdd, time.Second)
|
|
|
|
s.nodes[0].Server().AddPeer(s.nodes[1].Server().Self())
|
|
|
|
s.Require().NoError(<-waitErr)
|
|
|
|
|
|
|
|
api := NewPublicAPI(s.services[0])
|
|
|
|
|
|
|
|
_, err := api.RequestMessages(context.Background(), ext.MessagesRequest{
|
|
|
|
MailServerPeer: s.nodes[1].Server().Self().URLv4(),
|
|
|
|
Topics: []types.TopicType{{1}},
|
|
|
|
})
|
|
|
|
s.NoError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ShhExtSuite) TestMultipleRequestMessagesWithoutForce() {
|
|
|
|
// two nodes needed: client and mailserver
|
|
|
|
s.createAndAddNode()
|
|
|
|
s.createAndAddNode()
|
|
|
|
|
|
|
|
waitErr := helpers.WaitForPeerAsync(s.nodes[0].Server(), s.nodes[1].Server().Self().URLv4(), p2p.PeerEventTypeAdd, time.Second)
|
|
|
|
s.nodes[0].Server().AddPeer(s.nodes[1].Server().Self())
|
|
|
|
s.Require().NoError(<-waitErr)
|
|
|
|
|
|
|
|
api := NewPublicAPI(s.services[0])
|
|
|
|
|
|
|
|
_, err := api.RequestMessages(context.Background(), ext.MessagesRequest{
|
|
|
|
MailServerPeer: s.nodes[1].Server().Self().URLv4(),
|
|
|
|
Topics: []types.TopicType{{1}},
|
|
|
|
})
|
|
|
|
s.NoError(err)
|
|
|
|
_, err = api.RequestMessages(context.Background(), ext.MessagesRequest{
|
|
|
|
MailServerPeer: s.nodes[1].Server().Self().URLv4(),
|
|
|
|
Topics: []types.TopicType{{2}},
|
|
|
|
})
|
|
|
|
s.NoError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ShhExtSuite) TestFailedRequestWithUnknownMailServerPeer() {
|
|
|
|
s.createAndAddNode()
|
|
|
|
|
|
|
|
api := NewPublicAPI(s.services[0])
|
|
|
|
|
|
|
|
_, err := api.RequestMessages(context.Background(), ext.MessagesRequest{
|
|
|
|
MailServerPeer: "enode://19872f94b1e776da3a13e25afa71b47dfa99e658afd6427ea8d6e03c22a99f13590205a8826443e95a37eee1d815fc433af7a8ca9a8d0df7943d1f55684045b7@0.0.0.0:30305",
|
|
|
|
Topics: []types.TopicType{{1}},
|
|
|
|
})
|
|
|
|
s.EqualError(err, "could not find peer with ID: 10841e6db5c02fc331bf36a8d2a9137a1696d9d3b6b1f872f780e02aa8ec5bba")
|
|
|
|
}
|