Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: filter v2 tests push valid payload #904

Merged
merged 3 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions tests/string_generators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,15 @@ func TestStringGenerators(t *testing.T) {

// Generate string and print out to console
for i := 0; i < 1000; i++ {
x, err := GenerateRandomASCIIString(1, 4097)
x, err := GenerateRandomASCIIString(4097)
require.NoError(t, err)
log.Info("Generated random ASCII string", zap.String(strconv.Itoa(i), x))

x, err = GenerateRandomUTF8String(1, 4097, false)
x, err = GenerateRandomUTF8String(4097)
require.NoError(t, err)
log.Info("Generated random UTF8 string", zap.String(strconv.Itoa(i), x))

x, err = GenerateRandomUTF8String(1, 4097, true)
require.NoError(t, err)
log.Info("Generated uncommon UTF8 string", zap.String(strconv.Itoa(i), x))

x, err = GenerateRandomJSONString()
x, err = GenerateRandomJSONString(4099)
require.NoError(t, err)
log.Info("Generated random JSON string", zap.String(strconv.Itoa(i), x))

Expand All @@ -38,7 +34,7 @@ func TestStringGenerators(t *testing.T) {
require.NoError(t, err)
log.Info("Generated random URL encoded string", zap.String(strconv.Itoa(i), x))

x, err = GenerateRandomSQLInsert()
x, err = GenerateRandomSQLInsert(4096)
require.NoError(t, err)
log.Info("Generated random SQL insert string", zap.String(strconv.Itoa(i), x))
}
Expand Down
48 changes: 22 additions & 26 deletions tests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import (
"go.uber.org/zap"
)

type StringGenerator func(maxLength int) (string, error)

// GetHostAddress returns the first listen address used by a host
func GetHostAddress(ha host.Host) multiaddr.Multiaddr {
return ha.Addrs()[0]
Expand Down Expand Up @@ -247,12 +249,12 @@ func RandomBytes(n int) ([]byte, error) {
return b, nil
}

func GenerateRandomASCIIString(minLength int, maxLength int) (string, error) {
length, err := rand.Int(rand.Reader, big.NewInt(int64(maxLength-minLength+1)))
func GenerateRandomASCIIString(maxLength int) (string, error) {
length, err := rand.Int(rand.Reader, big.NewInt(int64(maxLength)))
if err != nil {
return "", err
}
length.SetInt64(length.Int64() + int64(minLength))
length.SetInt64(length.Int64() + 1)

const chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
result := make([]byte, length.Int64())
Expand All @@ -267,27 +269,21 @@ func GenerateRandomASCIIString(minLength int, maxLength int) (string, error) {
return string(result), nil
}

func GenerateRandomUTF8String(minLength int, maxLength int, withUncommon bool) (string, error) {
length, err := rand.Int(rand.Reader, big.NewInt(int64(maxLength-minLength+1)))
func GenerateRandomUTF8String(maxLength int) (string, error) {
length, err := rand.Int(rand.Reader, big.NewInt(int64(maxLength)))
if err != nil {
return "", err
}
length.SetInt64(length.Int64() + int64(minLength))
length.SetInt64(length.Int64() + 1)

var (
runes []rune
start, end int
)

if withUncommon {
// Unicode range for uncommon or unprintable characters, the Private Use Area (E000–F8FF)
start = 0xE000
end = 0xF8FF
} else {
// Define unicode range
start = 0x0020 // Space character
end = 0x007F // Tilde (~)
}
// Define unicode range
start = 0x0020 // Space character
end = 0x007F // Tilde (~)

for i := 0; int64(i) < length.Int64(); i++ {
randNum, err := rand.Int(rand.Reader, big.NewInt(int64(end-start+1)))
Expand All @@ -304,15 +300,15 @@ func GenerateRandomUTF8String(minLength int, maxLength int, withUncommon bool) (
return string(runes), nil
}

func GenerateRandomJSONString() (string, error) {
func GenerateRandomJSONString(maxLength int) (string, error) {
// With 5 key-value pairs
m := make(map[string]interface{})
for i := 0; i < 5; i++ {
key, err := GenerateRandomASCIIString(1, 20)
key, err := GenerateRandomASCIIString(20)
if err != nil {
return "", err
}
value, err := GenerateRandomASCIIString(1, 4097)
value, err := GenerateRandomASCIIString(maxLength)
if err != nil {
return "", err
}
Expand All @@ -332,17 +328,17 @@ func GenerateRandomJSONString() (string, error) {
return buf.String(), nil
}

func GenerateRandomBase64String(length int) (string, error) {
bytes, err := RandomBytes(length)
func GenerateRandomBase64String(maxLength int) (string, error) {
bytes, err := RandomBytes(maxLength)
if err != nil {
return "", err
}

return base64.StdEncoding.EncodeToString(bytes), nil
}

func GenerateRandomURLEncodedString(length int) (string, error) {
randomString, err := GenerateRandomASCIIString(1, 4097)
func GenerateRandomURLEncodedString(maxLength int) (string, error) {
randomString, err := GenerateRandomASCIIString(maxLength)
if err != nil {
return "", err
}
Expand All @@ -351,9 +347,9 @@ func GenerateRandomURLEncodedString(length int) (string, error) {
return url.QueryEscape(randomString), nil
}

func GenerateRandomSQLInsert() (string, error) {
func GenerateRandomSQLInsert(maxLength int) (string, error) {
// Random table name
tableName, err := GenerateRandomASCIIString(1, 10)
tableName, err := GenerateRandomASCIIString(10)
if err != nil {
return "", err
}
Expand All @@ -365,7 +361,7 @@ func GenerateRandomSQLInsert() (string, error) {
}
columnNames := make([]string, columnCount)
for i := 0; i < columnCount; i++ {
columnName, err := GenerateRandomASCIIString(1, 20)
columnName, err := GenerateRandomASCIIString(maxLength)
if err != nil {
return "", err
}
Expand All @@ -375,7 +371,7 @@ func GenerateRandomSQLInsert() (string, error) {
// Random values
values := make([]string, columnCount)
for i := 0; i < columnCount; i++ {
value, err := GenerateRandomASCIIString(1, 100)
value, err := GenerateRandomASCIIString(maxLength)
if err != nil {
return "", err
}
Expand Down
143 changes: 143 additions & 0 deletions waku/v2/protocol/filter/filter_push_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package filter

import (
"context"
"github.com/waku-org/go-waku/tests"
"go.uber.org/zap"
"strconv"
"time"
)

func (s *FilterTestSuite) TestValidPayloadsASCII() {

// Subscribe
s.subDetails = s.subscribe(s.testTopic, s.testContentTopic, s.fullNodeHost.ID())

// Prepare data
messages := prepareData(100, false, false, true, tests.GenerateRandomASCIIString)

// All messages should be received
s.waitForMessages(func() {
s.publishMessages(messages)
}, s.subDetails, messages)

_, err := s.lightNode.UnsubscribeAll(s.ctx)
s.Require().NoError(err)

}

func (s *FilterTestSuite) TestValidPayloadsUTF8() {

// Subscribe
s.subDetails = s.subscribe(s.testTopic, s.testContentTopic, s.fullNodeHost.ID())

// Prepare data
messages := prepareData(100, false, false, true, tests.GenerateRandomUTF8String)

// All messages should be received
s.waitForMessages(func() {
s.publishMessages(messages)
}, s.subDetails, messages)

_, err := s.lightNode.UnsubscribeAll(s.ctx)
s.Require().NoError(err)

}

func (s *FilterTestSuite) TestValidPayloadsBase64() {

// Subscribe
s.subDetails = s.subscribe(s.testTopic, s.testContentTopic, s.fullNodeHost.ID())

// Prepare data
messages := prepareData(100, false, false, true, tests.GenerateRandomBase64String)

// All messages should be received
s.waitForMessages(func() {
s.publishMessages(messages)
}, s.subDetails, messages)

_, err := s.lightNode.UnsubscribeAll(s.ctx)
s.Require().NoError(err)

}

func (s *FilterTestSuite) TestValidPayloadsJSON() {

// Subscribe
s.subDetails = s.subscribe(s.testTopic, s.testContentTopic, s.fullNodeHost.ID())

// Prepare data
messages := prepareData(100, false, false, true, tests.GenerateRandomJSONString)

// All messages should be received
s.waitForMessages(func() {
s.publishMessages(messages)
}, s.subDetails, messages)

_, err := s.lightNode.UnsubscribeAll(s.ctx)
s.Require().NoError(err)

}

func (s *FilterTestSuite) TestValidPayloadsURLEncoded() {

// Subscribe
s.subDetails = s.subscribe(s.testTopic, s.testContentTopic, s.fullNodeHost.ID())

// Prepare data
messages := prepareData(100, false, false, true, tests.GenerateRandomURLEncodedString)

// All messages should be received
s.waitForMessages(func() {
s.publishMessages(messages)
}, s.subDetails, messages)

_, err := s.lightNode.UnsubscribeAll(s.ctx)
s.Require().NoError(err)

}

func (s *FilterTestSuite) TestValidPayloadsSQL() {

// Subscribe
s.subDetails = s.subscribe(s.testTopic, s.testContentTopic, s.fullNodeHost.ID())

// Prepare data
messages := prepareData(100, false, false, true, tests.GenerateRandomSQLInsert)

// All messages should be received
s.waitForMessages(func() {
s.publishMessages(messages)
}, s.subDetails, messages)

_, err := s.lightNode.UnsubscribeAll(s.ctx)
s.Require().NoError(err)

}

func (s *FilterTestSuite) TestLargePayloadsUTF8() {

s.ctx, s.ctxCancel = context.WithTimeout(context.Background(), 20*time.Second)

// Subscribe
s.subDetails = s.subscribe(s.testTopic, s.testContentTopic, s.fullNodeHost.ID())

// Prepare basic data
messages := prepareData(10, false, false, false, nil)

// Generate large string
for i := range messages {
messages[i].payload, _ = tests.GenerateRandomUTF8String(1048576)
s.log.Info("Generated payload with ", zap.String("length", strconv.Itoa(len(messages[i].payload))))
}

// All messages should be received
s.waitForMessages(func() {
s.publishMessages(messages)
}, s.subDetails, messages)

_, err := s.lightNode.UnsubscribeAll(s.ctx)
s.Require().NoError(err)

}
12 changes: 6 additions & 6 deletions waku/v2/protocol/filter/filter_subscribe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (s *FilterTestSuite) TestPubSubMultiContentTopic() {
// Create test context
s.ctx, s.ctxCancel = context.WithTimeout(context.Background(), 20*time.Second) // Test can't exceed 10 seconds

messages := prepareData(3, false, true, false)
messages := prepareData(3, false, true, false, nil)

// Subscribe
for _, m := range messages {
Expand Down Expand Up @@ -126,7 +126,7 @@ func (s *FilterTestSuite) TestMultiPubSubMultiContentTopic() {
err := s.lightNodeHost.Peerstore().AddProtocols(s.fullNodeHost.ID(), FilterSubscribeID_v20beta1)
s.Require().NoError(err)

messages := prepareData(2, true, true, false)
messages := prepareData(2, true, true, false, nil)

// Subscribe
for _, m := range messages {
Expand Down Expand Up @@ -156,7 +156,7 @@ func (s *FilterTestSuite) TestPubSubMultiOverlapContentTopic() {
// Create test context
s.ctx, s.ctxCancel = context.WithTimeout(context.Background(), 20*time.Second) // Test can't exceed 20 seconds

messages := prepareData(10, false, true, true)
messages := prepareData(10, false, true, true, nil)

// Subscribe
for _, m := range messages {
Expand All @@ -175,7 +175,7 @@ func (s *FilterTestSuite) TestPubSubMultiOverlapContentTopic() {

func (s *FilterTestSuite) TestSubscriptionRefresh() {

messages := prepareData(2, false, false, true)
messages := prepareData(2, false, false, true, nil)

// Initial subscribe
s.subDetails = s.subscribe(s.testTopic, s.testContentTopic, s.fullNodeHost.ID())
Expand Down Expand Up @@ -207,7 +207,7 @@ func (s *FilterTestSuite) TestContentTopicsLimit() {
}
}

messages := prepareData(maxContentTopics+1, false, true, true)
messages := prepareData(maxContentTopics+1, false, true, true, nil)

// Subscribe
for _, m := range messages[:len(messages)-1] {
Expand Down Expand Up @@ -311,7 +311,7 @@ func (s *FilterTestSuite) TestSubscribeMultipleLightNodes() {
// Connect node2
lightNode2.h.Peerstore().AddAddr(s.fullNodeHost.ID(), tests.GetHostAddress(s.fullNodeHost), peerstore.PermanentAddrTTL)

messages := prepareData(2, true, true, true)
messages := prepareData(2, true, true, true, nil)

// Subscribe separately: light node 1 -> full node
contentFilter := protocol.ContentFilter{PubsubTopic: messages[0].pubSubTopic, ContentTopics: protocol.NewContentTopicSet(messages[0].contentTopic)}
Expand Down
Loading