2019-09-04 06:25:33 +00:00
|
|
|
package mailservers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-10-12 19:21:49 +00:00
|
|
|
|
2020-01-02 09:10:19 +00:00
|
|
|
"github.com/status-im/status-go/appdatabase"
|
2021-01-14 22:15:13 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2023-11-15 15:58:15 +00:00
|
|
|
"github.com/status-im/status-go/protocol/common/shard"
|
2024-02-20 15:49:39 +00:00
|
|
|
"github.com/status-im/status-go/protocol/sqlite"
|
2021-01-14 22:15:13 +00:00
|
|
|
"github.com/status-im/status-go/protocol/transport"
|
2023-08-11 11:28:45 +00:00
|
|
|
"github.com/status-im/status-go/t/helpers"
|
2019-09-04 06:25:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func setupTestDB(t *testing.T) (*Database, func()) {
|
2023-08-11 11:28:45 +00:00
|
|
|
db, cleanup, err := helpers.SetupTestSQLDB(appdatabase.DbInitializer{}, "maliservers-tests-")
|
2019-09-04 06:25:33 +00:00
|
|
|
require.NoError(t, err)
|
2024-02-20 15:49:39 +00:00
|
|
|
err = sqlite.Migrate(db) // migrate default
|
|
|
|
require.NoError(t, err)
|
2023-08-11 11:28:45 +00:00
|
|
|
return NewDB(db), func() { require.NoError(t, cleanup()) }
|
2019-09-04 06:25:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddGetDeleteMailserver(t *testing.T) {
|
|
|
|
db, close := setupTestDB(t)
|
|
|
|
defer close()
|
|
|
|
api := &API{db: db}
|
|
|
|
testMailserver := Mailserver{
|
|
|
|
ID: "mailserver001",
|
|
|
|
Name: "My Mailserver",
|
|
|
|
Address: "enode://...",
|
2022-01-31 10:33:56 +00:00
|
|
|
Custom: true,
|
2020-02-20 13:05:05 +00:00
|
|
|
Fleet: "prod",
|
2019-09-04 06:25:33 +00:00
|
|
|
}
|
|
|
|
testMailserverWithPassword := testMailserver
|
|
|
|
testMailserverWithPassword.ID = "mailserver002"
|
|
|
|
testMailserverWithPassword.Password = "test-pass"
|
|
|
|
|
|
|
|
err := api.AddMailserver(context.Background(), testMailserver)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = api.AddMailserver(context.Background(), testMailserverWithPassword)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
mailservers, err := api.GetMailservers(context.Background())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.EqualValues(t, []Mailserver{testMailserver, testMailserverWithPassword}, mailservers)
|
|
|
|
|
|
|
|
err = api.DeleteMailserver(context.Background(), testMailserver.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
// Verify they was deleted.
|
|
|
|
mailservers, err = api.GetMailservers(context.Background())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.EqualValues(t, []Mailserver{testMailserverWithPassword}, mailservers)
|
|
|
|
// Delete non-existing mailserver.
|
|
|
|
err = api.DeleteMailserver(context.Background(), "other-id")
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2019-09-04 10:04:17 +00:00
|
|
|
|
2021-01-14 22:15:13 +00:00
|
|
|
func TestTopic(t *testing.T) {
|
|
|
|
db, close := setupTestDB(t)
|
|
|
|
defer close()
|
|
|
|
topicA := "0x61000000"
|
|
|
|
topicD := "0x64000000"
|
2023-11-15 15:58:15 +00:00
|
|
|
topic1 := MailserverTopic{PubsubTopic: shard.DefaultShardPubsubTopic(), ContentTopic: topicA, LastRequest: 1}
|
|
|
|
topic2 := MailserverTopic{PubsubTopic: shard.DefaultShardPubsubTopic(), ContentTopic: "0x6200000", LastRequest: 2}
|
|
|
|
topic3 := MailserverTopic{PubsubTopic: shard.DefaultShardPubsubTopic(), ContentTopic: "0x6300000", LastRequest: 3}
|
2021-01-14 22:15:13 +00:00
|
|
|
|
|
|
|
require.NoError(t, db.AddTopic(topic1))
|
|
|
|
require.NoError(t, db.AddTopic(topic2))
|
|
|
|
require.NoError(t, db.AddTopic(topic3))
|
|
|
|
|
|
|
|
topics, err := db.Topics()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, topics, 3)
|
|
|
|
|
|
|
|
filters := []*transport.Filter{
|
|
|
|
// Existing topic, is not updated
|
2023-05-22 21:38:02 +00:00
|
|
|
{
|
2023-11-15 15:58:15 +00:00
|
|
|
PubsubTopic: shard.DefaultShardPubsubTopic(),
|
2023-05-22 21:38:02 +00:00
|
|
|
ContentTopic: types.BytesToTopic([]byte{0x61}),
|
|
|
|
},
|
2021-01-14 22:15:13 +00:00
|
|
|
// Non existing topic is not inserted
|
|
|
|
{
|
2023-05-22 21:38:02 +00:00
|
|
|
Discovery: true,
|
|
|
|
Negotiated: true,
|
2023-11-15 15:58:15 +00:00
|
|
|
PubsubTopic: shard.DefaultShardPubsubTopic(),
|
2023-05-22 21:38:02 +00:00
|
|
|
ContentTopic: types.BytesToTopic([]byte{0x64}),
|
2021-01-14 22:15:13 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, db.SetTopics(filters))
|
|
|
|
|
|
|
|
topics, err = db.Topics()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, topics, 2)
|
2023-05-22 21:38:02 +00:00
|
|
|
require.Equal(t, topics[0].ContentTopic, topicA)
|
2021-01-14 22:15:13 +00:00
|
|
|
require.Equal(t, topics[0].LastRequest, 1)
|
|
|
|
|
2023-05-22 21:38:02 +00:00
|
|
|
require.Equal(t, topics[0].ContentTopic, topicA)
|
2021-01-14 22:15:13 +00:00
|
|
|
require.Equal(t, topics[0].LastRequest, 1)
|
|
|
|
|
2023-05-22 21:38:02 +00:00
|
|
|
require.Equal(t, topics[1].ContentTopic, topicD)
|
2021-01-14 22:15:13 +00:00
|
|
|
require.NotEmpty(t, topics[1].LastRequest)
|
|
|
|
require.True(t, topics[1].Negotiated)
|
|
|
|
require.True(t, topics[1].Discovery)
|
|
|
|
}
|
|
|
|
|
2019-09-04 10:04:17 +00:00
|
|
|
func TestAddGetDeleteMailserverRequestGap(t *testing.T) {
|
|
|
|
db, close := setupTestDB(t)
|
|
|
|
defer close()
|
|
|
|
chatID1 := "chat-id-1"
|
|
|
|
chatID2 := "chat-id-2"
|
|
|
|
|
|
|
|
api := &API{db: db}
|
|
|
|
gap1 := MailserverRequestGap{ID: "1", ChatID: chatID1, From: 1, To: 2}
|
|
|
|
gap2 := MailserverRequestGap{ID: "2", ChatID: chatID2, From: 1, To: 2}
|
|
|
|
gap3 := MailserverRequestGap{ID: "3", ChatID: chatID2, From: 1, To: 2}
|
|
|
|
|
|
|
|
gaps := []MailserverRequestGap{
|
|
|
|
gap1,
|
|
|
|
gap2,
|
|
|
|
gap3,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := api.AddMailserverRequestGaps(context.Background(), gaps)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
actualGaps, err := api.GetMailserverRequestGaps(context.Background(), chatID1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.EqualValues(t, []MailserverRequestGap{gap1}, actualGaps)
|
|
|
|
|
|
|
|
actualGaps, err = api.GetMailserverRequestGaps(context.Background(), chatID2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.EqualValues(t, []MailserverRequestGap{gap2, gap3}, actualGaps)
|
|
|
|
|
|
|
|
err = api.DeleteMailserverRequestGaps(context.Background(), []string{gap1.ID, gap2.ID})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Verify it was deleted.
|
|
|
|
actualGaps, err = api.GetMailserverRequestGaps(context.Background(), chatID1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, actualGaps, 0)
|
|
|
|
|
|
|
|
actualGaps, err = api.GetMailserverRequestGaps(context.Background(), chatID2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, actualGaps, 1)
|
|
|
|
|
|
|
|
err = api.DeleteMailserverRequestGapsByChatID(context.Background(), chatID2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Verify it was deleted.
|
|
|
|
actualGaps, err = api.GetMailserverRequestGaps(context.Background(), chatID2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, actualGaps, 0)
|
|
|
|
}
|
2019-09-04 18:23:17 +00:00
|
|
|
|
|
|
|
func TestAddGetDeleteMailserverTopics(t *testing.T) {
|
|
|
|
db, close := setupTestDB(t)
|
|
|
|
defer close()
|
|
|
|
api := &API{db: db}
|
|
|
|
testTopic := MailserverTopic{
|
2023-11-15 15:58:15 +00:00
|
|
|
PubsubTopic: shard.DefaultShardPubsubTopic(),
|
2023-05-22 21:38:02 +00:00
|
|
|
ContentTopic: "topic-001",
|
|
|
|
ChatIDs: []string{"chatID01", "chatID02"},
|
|
|
|
LastRequest: 10,
|
2019-09-04 18:23:17 +00:00
|
|
|
}
|
|
|
|
err := api.AddMailserverTopic(context.Background(), testTopic)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Verify topics were added.
|
|
|
|
topics, err := api.GetMailserverTopics(context.Background())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.EqualValues(t, []MailserverTopic{testTopic}, topics)
|
|
|
|
|
2023-11-15 15:58:15 +00:00
|
|
|
err = api.DeleteMailserverTopic(context.Background(), shard.DefaultShardPubsubTopic(), testTopic.ContentTopic)
|
2019-09-04 18:23:17 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
topics, err = api.GetMailserverTopics(context.Background())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.EqualValues(t, ([]MailserverTopic)(nil), topics)
|
|
|
|
|
|
|
|
// Delete non-existing topic.
|
2023-11-15 15:58:15 +00:00
|
|
|
err = api.DeleteMailserverTopic(context.Background(), shard.DefaultShardPubsubTopic(), "non-existing-topic")
|
2019-09-04 18:23:17 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2019-09-06 13:02:31 +00:00
|
|
|
|
|
|
|
func TestAddGetDeleteChatRequestRanges(t *testing.T) {
|
|
|
|
db, close := setupTestDB(t)
|
|
|
|
defer close()
|
|
|
|
api := &API{db: db}
|
|
|
|
chatRequestRange1 := ChatRequestRange{
|
|
|
|
ChatID: "chat-id-001",
|
|
|
|
LowestRequestFrom: 123,
|
|
|
|
HighestRequestTo: 456,
|
|
|
|
}
|
|
|
|
chatRequestRange2 := chatRequestRange1
|
|
|
|
chatRequestRange2.ChatID = "chat-id-002"
|
|
|
|
|
|
|
|
err := api.AddChatRequestRange(context.Background(), chatRequestRange1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = api.AddChatRequestRange(context.Background(), chatRequestRange2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Verify topics were added.
|
|
|
|
ranges, err := api.GetChatRequestRanges(context.Background())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.EqualValues(t, []ChatRequestRange{chatRequestRange1, chatRequestRange2}, ranges)
|
|
|
|
|
|
|
|
err = api.DeleteChatRequestRange(context.Background(), chatRequestRange1.ChatID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
ranges, err = api.GetChatRequestRanges(context.Background())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.EqualValues(t, []ChatRequestRange{chatRequestRange2}, ranges)
|
|
|
|
|
|
|
|
// Delete non-existing topic.
|
|
|
|
err = api.DeleteChatRequestRange(context.Background(), "non-existing-chat-id")
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|