2020-01-21 07:11:24 +00:00
|
|
|
// In order to run these tests, you must run a PostgreSQL database.
|
|
|
|
//
|
|
|
|
// Using Docker:
|
2021-09-01 12:02:18 +00:00
|
|
|
// docker run -e POSTGRES_HOST_AUTH_METHOD=trust -d -p 5432:5432 postgres:9.6-alpine
|
2020-01-21 07:11:24 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
package mailserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-09-01 12:02:18 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
2020-02-10 16:52:43 +00:00
|
|
|
|
2020-01-21 07:11:24 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2021-09-01 12:02:18 +00:00
|
|
|
|
2020-01-21 07:11:24 +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"
|
2021-09-01 12:02:18 +00:00
|
|
|
"github.com/status-im/status-go/postgres"
|
2021-05-14 10:55:42 +00:00
|
|
|
waku "github.com/status-im/status-go/waku/common"
|
2020-01-21 07:11:24 +00:00
|
|
|
)
|
|
|
|
|
2021-09-01 12:02:18 +00:00
|
|
|
func TestMailServerPostgresDBSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(MailServerPostgresDBSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type MailServerPostgresDBSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MailServerPostgresDBSuite) SetupSuite() {
|
|
|
|
// ResetDefaultTestPostgresDB Required to completely reset the Postgres DB
|
|
|
|
err := postgres.ResetDefaultTestPostgresDB()
|
|
|
|
s.NoError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MailServerPostgresDBSuite) TestPostgresDB_BuildIteratorWithBloomFilter() {
|
2020-01-21 07:11:24 +00:00
|
|
|
topic := []byte{0xaa, 0xbb, 0xcc, 0xdd}
|
|
|
|
|
2021-09-01 12:02:18 +00:00
|
|
|
db, err := NewPostgresDB(postgres.DefaultTestURI)
|
|
|
|
s.NoError(err)
|
|
|
|
defer db.Close()
|
2020-01-21 07:11:24 +00:00
|
|
|
|
|
|
|
envelope, err := newTestEnvelope(topic)
|
2021-09-01 12:02:18 +00:00
|
|
|
s.NoError(err)
|
2020-01-21 07:11:24 +00:00
|
|
|
err = db.SaveEnvelope(envelope)
|
2021-09-01 12:02:18 +00:00
|
|
|
s.NoError(err)
|
2020-01-21 07:11:24 +00:00
|
|
|
|
|
|
|
iter, err := db.BuildIterator(CursorQuery{
|
|
|
|
start: NewDBKey(uint32(time.Now().Add(-time.Hour).Unix()), types.BytesToTopic(topic), types.Hash{}).Bytes(),
|
|
|
|
end: NewDBKey(uint32(time.Now().Add(time.Second).Unix()), types.BytesToTopic(topic), types.Hash{}).Bytes(),
|
|
|
|
bloom: types.TopicToBloom(types.BytesToTopic(topic)),
|
|
|
|
limit: 10,
|
|
|
|
})
|
2021-09-01 12:02:18 +00:00
|
|
|
s.NoError(err)
|
2020-01-21 07:11:24 +00:00
|
|
|
hasNext := iter.Next()
|
2021-09-01 12:02:18 +00:00
|
|
|
s.True(hasNext)
|
2021-08-24 11:51:22 +00:00
|
|
|
rawValue, err := iter.GetEnvelopeByBloomFilter(nil)
|
2021-09-01 12:02:18 +00:00
|
|
|
s.NoError(err)
|
|
|
|
s.NotEmpty(rawValue)
|
2021-05-14 10:55:42 +00:00
|
|
|
var receivedEnvelope waku.Envelope
|
2020-01-21 07:11:24 +00:00
|
|
|
err = rlp.DecodeBytes(rawValue, &receivedEnvelope)
|
2021-09-01 12:02:18 +00:00
|
|
|
s.NoError(err)
|
|
|
|
s.EqualValues(waku.BytesToTopic(topic), receivedEnvelope.Topic)
|
2020-01-21 07:11:24 +00:00
|
|
|
|
|
|
|
err = iter.Release()
|
2021-09-01 12:02:18 +00:00
|
|
|
s.NoError(err)
|
|
|
|
s.NoError(iter.Error())
|
2020-01-21 07:11:24 +00:00
|
|
|
}
|
|
|
|
|
2021-09-01 12:02:18 +00:00
|
|
|
func (s *MailServerPostgresDBSuite) TestPostgresDB_BuildIteratorWithTopic() {
|
2020-01-21 07:11:24 +00:00
|
|
|
topic := []byte{0x01, 0x02, 0x03, 0x04}
|
|
|
|
|
2021-09-01 12:02:18 +00:00
|
|
|
db, err := NewPostgresDB(postgres.DefaultTestURI)
|
|
|
|
s.NoError(err)
|
|
|
|
defer db.Close()
|
2020-01-21 07:11:24 +00:00
|
|
|
|
|
|
|
envelope, err := newTestEnvelope(topic)
|
2021-09-01 12:02:18 +00:00
|
|
|
s.NoError(err)
|
2020-01-21 07:11:24 +00:00
|
|
|
err = db.SaveEnvelope(envelope)
|
2021-09-01 12:02:18 +00:00
|
|
|
s.NoError(err)
|
2020-01-21 07:11:24 +00:00
|
|
|
|
|
|
|
iter, err := db.BuildIterator(CursorQuery{
|
|
|
|
start: NewDBKey(uint32(time.Now().Add(-time.Hour).Unix()), types.BytesToTopic(topic), types.Hash{}).Bytes(),
|
|
|
|
end: NewDBKey(uint32(time.Now().Add(time.Second).Unix()), types.BytesToTopic(topic), types.Hash{}).Bytes(),
|
|
|
|
topics: [][]byte{topic},
|
|
|
|
limit: 10,
|
|
|
|
})
|
2021-09-01 12:02:18 +00:00
|
|
|
s.NoError(err)
|
2020-01-21 07:11:24 +00:00
|
|
|
hasNext := iter.Next()
|
2021-09-01 12:02:18 +00:00
|
|
|
s.True(hasNext)
|
2021-08-24 11:51:22 +00:00
|
|
|
rawValue, err := iter.GetEnvelopeByBloomFilter(nil)
|
2021-09-01 12:02:18 +00:00
|
|
|
s.NoError(err)
|
|
|
|
s.NotEmpty(rawValue)
|
2021-05-14 10:55:42 +00:00
|
|
|
var receivedEnvelope waku.Envelope
|
2020-01-21 07:11:24 +00:00
|
|
|
err = rlp.DecodeBytes(rawValue, &receivedEnvelope)
|
2021-09-01 12:02:18 +00:00
|
|
|
s.NoError(err)
|
|
|
|
s.EqualValues(waku.BytesToTopic(topic), receivedEnvelope.Topic)
|
2020-01-21 07:11:24 +00:00
|
|
|
|
|
|
|
err = iter.Release()
|
2021-09-01 12:02:18 +00:00
|
|
|
s.NoError(err)
|
|
|
|
s.NoError(iter.Error())
|
2020-01-21 07:11:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newTestEnvelope(topic []byte) (types.Envelope, error) {
|
|
|
|
privateKey, err := crypto.GenerateKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-14 10:55:42 +00:00
|
|
|
params := waku.MessageParams{
|
2020-01-21 07:11:24 +00:00
|
|
|
TTL: 10,
|
|
|
|
PoW: 2.0,
|
|
|
|
Payload: []byte("hello world"),
|
|
|
|
WorkTime: 1,
|
2021-05-14 10:55:42 +00:00
|
|
|
Topic: waku.BytesToTopic(topic),
|
2020-01-21 07:11:24 +00:00
|
|
|
Dst: &privateKey.PublicKey,
|
|
|
|
}
|
2021-05-14 10:55:42 +00:00
|
|
|
message, err := waku.NewSentMessage(¶ms)
|
2020-01-21 07:11:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
now := time.Now()
|
|
|
|
envelope, err := message.Wrap(¶ms, now)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-09 10:16:04 +00:00
|
|
|
return gethbridge.NewWakuEnvelope(envelope), nil
|
2020-01-21 07:11:24 +00:00
|
|
|
}
|