mirror of
https://github.com/logos-messaging/logos-messaging-go.git
synced 2026-01-04 06:53:06 +00:00
chore: add test utils string generators (#879)
* Chore(filter v2) test updates (#811) * test: Test incorrect protocol identifiers * fix: return errors in FilterSubscribeOption * test: Test incorrect push identifier added - test incorrect subscribe identifier separated * test: Test Ping failure after unsubscription * test: Test PubSub with single content topic * test: Simplify test PubSub with single content topic * test: Test with single pubsub and multiple content topics * test: Test with multiple PubSub and multiple contentTopic * test: Test with multiple overlaping contentTopics - test contentTopics limit * test: refactor tests to fix concurrent run errors * test: Test subscription refresh * test: Test error handling for subscribe * test: Test subscription to multiple full nodes * update test to fix #804 * Update waku/v2/protocol/filter/filter_test.go Combine log messages Co-authored-by: richΛrd <info@richardramos.me> * Update waku/v2/protocol/filter/filter_test.go Delete commented - temporary code Co-authored-by: richΛrd <info@richardramos.me> * Update waku/v2/protocol/filter/filter_test.go fmt.Sprintf instead of "+" suffix => more performance and beauty Co-authored-by: richΛrd <info@richardramos.me> * Update waku/v2/protocol/filter/filter_test.go Adjust comment with code Co-authored-by: richΛrd <info@richardramos.me> * Update waku/v2/protocol/filter/filter_test.go Combine multiple related log entries into one. Co-authored-by: richΛrd <info@richardramos.me> * Update waku/v2/protocol/filter/filter_test.go Align comment with the code Co-authored-by: richΛrd <info@richardramos.me> * Update waku/v2/protocol/filter/filter_test.go Use fmt.Sprintf() instead of "+" for more beauty and speed Co-authored-by: richΛrd <info@richardramos.me> * test: refactor tests with prepareData() * test: Test incorrect protocol identifiers * chore: rebase onto latest master * test: Test incorrect push identifier added - test incorrect subscribe identifier separated * test: Test Ping failure after unsubscription * test: Test PubSub with single content topic * test: Simplify test PubSub with single content topic * test: Test with single pubsub and multiple content topics * test: Test with multiple PubSub and multiple contentTopic * test: Test with multiple overlaping contentTopics - test contentTopics limit * test: refactor tests to fix concurrent run errors * test: Test subscription refresh * test: Test error handling for subscribe * test: Test subscription to multiple full nodes * update test to fix #804 * Update waku/v2/protocol/filter/filter_test.go Combine log messages Co-authored-by: richΛrd <info@richardramos.me> * Update waku/v2/protocol/filter/filter_test.go Delete commented - temporary code Co-authored-by: richΛrd <info@richardramos.me> * Update waku/v2/protocol/filter/filter_test.go fmt.Sprintf instead of "+" suffix => more performance and beauty Co-authored-by: richΛrd <info@richardramos.me> * Update waku/v2/protocol/filter/filter_test.go Adjust comment with code Co-authored-by: richΛrd <info@richardramos.me> * Update waku/v2/protocol/filter/filter_test.go Combine multiple related log entries into one. Co-authored-by: richΛrd <info@richardramos.me> * Update waku/v2/protocol/filter/filter_test.go Align comment with the code Co-authored-by: richΛrd <info@richardramos.me> * Update waku/v2/protocol/filter/filter_test.go Use fmt.Sprintf() instead of "+" for more beauty and speed Co-authored-by: richΛrd <info@richardramos.me> * test: refactor tests with prepareData() * test: Test incorrect protocol identifiers * fix: return errors in FilterSubscribeOption * test: Test incorrect push identifier added - test incorrect subscribe identifier separated * test: Test Ping failure after unsubscription * test: Test PubSub with single content topic * test: Simplify test PubSub with single content topic * test: Test with single pubsub and multiple content topics * test: Test with multiple PubSub and multiple contentTopic * test: Test with multiple overlaping contentTopics - test contentTopics limit * test: refactor tests to fix concurrent run errors * test: Test subscription refresh * test: Test error handling for subscribe * test: Test subscription to multiple full nodes * update test to fix #804 * Update waku/v2/protocol/filter/filter_test.go Combine log messages Co-authored-by: richΛrd <info@richardramos.me> * Update waku/v2/protocol/filter/filter_test.go Delete commented - temporary code Co-authored-by: richΛrd <info@richardramos.me> * Update waku/v2/protocol/filter/filter_test.go fmt.Sprintf instead of "+" suffix => more performance and beauty Co-authored-by: richΛrd <info@richardramos.me> * Update waku/v2/protocol/filter/filter_test.go Adjust comment with code Co-authored-by: richΛrd <info@richardramos.me> * Update waku/v2/protocol/filter/filter_test.go Combine multiple related log entries into one. Co-authored-by: richΛrd <info@richardramos.me> * Update waku/v2/protocol/filter/filter_test.go Align comment with the code Co-authored-by: richΛrd <info@richardramos.me> * Update waku/v2/protocol/filter/filter_test.go Use fmt.Sprintf() instead of "+" for more beauty and speed Co-authored-by: richΛrd <info@richardramos.me> * test: refactor tests with prepareData() * Fix error during rebase * Sync filter tests with latest master * Refactor context initialization for test * test: Incorrect Subscribe Identifier refactored with custom subscribe * test: refactor into multiple files * test: Subscribe with multiple light nodes to one full node * test: shared mode for full node creation - test preview Subscribe fullNode to fullNode * test: test Subscribe fullNode to fullNode --------- Co-authored-by: Richard Ramos <info@richardramos.me> Co-authored-by: Prem Chaitanya Prathi <chaitanyaprem@gmail.com> * string generators for testing * fix CodeQL findings * merge variants of UTF8 String generator into one --------- Co-authored-by: Richard Ramos <info@richardramos.me> Co-authored-by: Prem Chaitanya Prathi <chaitanyaprem@gmail.com>
This commit is contained in:
parent
5aa4d578aa
commit
24879b2a0b
46
tests/string_generators_test.go
Normal file
46
tests/string_generators_test.go
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
package tests
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"github.com/waku-org/go-waku/waku/v2/utils"
|
||||||
|
"go.uber.org/zap"
|
||||||
|
"strconv"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestStringGenerators(t *testing.T) {
|
||||||
|
|
||||||
|
log := utils.Logger()
|
||||||
|
|
||||||
|
// Generate string and print out to console
|
||||||
|
for i := 0; i < 1000; i++ {
|
||||||
|
x, err := GenerateRandomASCIIString(1, 4097)
|
||||||
|
require.NoError(t, err)
|
||||||
|
log.Info("Generated random ASCII string", zap.String(strconv.Itoa(i), x))
|
||||||
|
|
||||||
|
x, err = GenerateRandomUTF8String(1, 4097, false)
|
||||||
|
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()
|
||||||
|
require.NoError(t, err)
|
||||||
|
log.Info("Generated random JSON string", zap.String(strconv.Itoa(i), x))
|
||||||
|
|
||||||
|
x, err = GenerateRandomBase64String(1025)
|
||||||
|
require.NoError(t, err)
|
||||||
|
log.Info("Generated random Base64 string", zap.String(strconv.Itoa(i), x))
|
||||||
|
|
||||||
|
x, err = GenerateRandomURLEncodedString(2049)
|
||||||
|
require.NoError(t, err)
|
||||||
|
log.Info("Generated random URL encoded string", zap.String(strconv.Itoa(i), x))
|
||||||
|
|
||||||
|
x, err = GenerateRandomSQLInsert()
|
||||||
|
require.NoError(t, err)
|
||||||
|
log.Info("Generated random SQL insert string", zap.String(strconv.Itoa(i), x))
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
169
tests/utils.go
169
tests/utils.go
@ -1,16 +1,23 @@
|
|||||||
package tests
|
package tests
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"crypto/ecdsa"
|
"crypto/ecdsa"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
|
"encoding/base64"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"math"
|
"math"
|
||||||
|
"math/big"
|
||||||
"net"
|
"net"
|
||||||
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"unicode/utf8"
|
||||||
|
|
||||||
gcrypto "github.com/ethereum/go-ethereum/crypto"
|
gcrypto "github.com/ethereum/go-ethereum/crypto"
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
@ -220,3 +227,165 @@ func ExtractIP(addr multiaddr.Multiaddr) (*net.TCPAddr, error) {
|
|||||||
Port: port,
|
Port: port,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func RandomInt(min, max int) (int, error) {
|
||||||
|
n, err := rand.Int(rand.Reader, big.NewInt(int64(max-min+1)))
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return min + int(n.Int64()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func RandomBytes(n int) ([]byte, error) {
|
||||||
|
b := make([]byte, n)
|
||||||
|
_, err := rand.Read(b)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return b, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GenerateRandomASCIIString(minLength int, maxLength int) (string, error) {
|
||||||
|
length, err := rand.Int(rand.Reader, big.NewInt(int64(maxLength-minLength+1)))
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
length.SetInt64(length.Int64() + int64(minLength))
|
||||||
|
|
||||||
|
const chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||||
|
result := make([]byte, length.Int64())
|
||||||
|
for i := range result {
|
||||||
|
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(chars))))
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
result[i] = chars[num.Int64()]
|
||||||
|
}
|
||||||
|
|
||||||
|
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)))
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
length.SetInt64(length.Int64() + int64(minLength))
|
||||||
|
|
||||||
|
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 (~)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; int64(i) < length.Int64(); i++ {
|
||||||
|
randNum, err := rand.Int(rand.Reader, big.NewInt(int64(end-start+1)))
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
char := rune(start + int(randNum.Int64()))
|
||||||
|
if !utf8.ValidRune(char) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
runes = append(runes, char)
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(runes), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GenerateRandomJSONString() (string, error) {
|
||||||
|
// With 5 key-value pairs
|
||||||
|
m := make(map[string]interface{})
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
|
key, err := GenerateRandomASCIIString(1, 20)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
value, err := GenerateRandomASCIIString(1, 4097)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
m[key] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
// Marshal the map into a JSON string
|
||||||
|
var buf bytes.Buffer
|
||||||
|
encoder := json.NewEncoder(&buf)
|
||||||
|
encoder.SetEscapeHTML(false)
|
||||||
|
err := encoder.Encode(m)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf.String(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GenerateRandomBase64String(length int) (string, error) {
|
||||||
|
bytes, err := RandomBytes(length)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return base64.StdEncoding.EncodeToString(bytes), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GenerateRandomURLEncodedString(length int) (string, error) {
|
||||||
|
randomString, err := GenerateRandomASCIIString(1, 4097)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
// URL-encode the random string
|
||||||
|
return url.QueryEscape(randomString), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GenerateRandomSQLInsert() (string, error) {
|
||||||
|
// Random table name
|
||||||
|
tableName, err := GenerateRandomASCIIString(1, 10)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Random column names
|
||||||
|
columnCount, err := RandomInt(3, 6)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
columnNames := make([]string, columnCount)
|
||||||
|
for i := 0; i < columnCount; i++ {
|
||||||
|
columnName, err := GenerateRandomASCIIString(1, 20)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
columnNames[i] = columnName
|
||||||
|
}
|
||||||
|
|
||||||
|
// Random values
|
||||||
|
values := make([]string, columnCount)
|
||||||
|
for i := 0; i < columnCount; i++ {
|
||||||
|
value, err := GenerateRandomASCIIString(1, 100)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
values[i] = "'" + value + "'"
|
||||||
|
}
|
||||||
|
|
||||||
|
query := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s);",
|
||||||
|
tableName,
|
||||||
|
strings.Join(columnNames, ", "),
|
||||||
|
strings.Join(values, ", "))
|
||||||
|
|
||||||
|
return query, nil
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user