go-waku/waku/v2/protocol/relay/validators_test.go

99 lines
2.8 KiB
Go
Raw Normal View History

package relay
import (
"context"
"encoding/hex"
"testing"
"time"
"github.com/ethereum/go-ethereum/crypto"
pubsub "github.com/libp2p/go-libp2p-pubsub"
pubsub_pb "github.com/libp2p/go-libp2p-pubsub/pb"
"github.com/stretchr/testify/require"
"github.com/waku-org/go-waku/waku/v2/protocol/pb"
"google.golang.org/protobuf/proto"
)
type FakeTimesource struct {
now time.Time
}
func NewFakeTimesource(now time.Time) FakeTimesource {
return FakeTimesource{now}
}
func (f FakeTimesource) Start(ctx context.Context) error {
return nil
}
func (f FakeTimesource) Stop() {}
func (f FakeTimesource) Now() time.Time {
return f.now
}
type Timesource interface {
Now() time.Time
}
func TestMsgHash(t *testing.T) {
privKeyB, _ := hex.DecodeString("5526a8990317c9b7b58d07843d270f9cd1d9aaee129294c1c478abf7261dd9e6")
prvKey, _ := crypto.ToECDSA(privKeyB)
payload, _ := hex.DecodeString("1A12E077D0E89F9CAC11FBBB6A676C86120B5AD3E248B1F180E98F15EE43D2DFCF62F00C92737B2FF6F59B3ABA02773314B991C41DC19ADB0AD8C17C8E26757B")
contentTopic := "content-topic"
ephemeral := true
timestamp := time.Unix(0, 1683208172339052800)
msg := &pb.WakuMessage{
Payload: payload,
ContentTopic: contentTopic,
Timestamp: timestamp.UnixNano(),
Ephemeral: ephemeral,
}
err := SignMessage(prvKey, msg)
require.NoError(t, err)
// expectedSignature, _ := hex.DecodeString("127FA211B2514F0E974A055392946DC1A14052182A6ABEFB8A6CD7C51DA1BF2E40595D28EF1A9488797C297EED3AAC45430005FB3A7F037BDD9FC4BD99F59E63")
// require.True(t, bytes.Equal(expectedSignature, msg.Meta))
msgData, _ := proto.Marshal(msg)
address := crypto.PubkeyToAddress(prvKey.PublicKey)
//expectedMessageHash, _ := hex.DecodeString("662F8C20A335F170BD60ABC1F02AD66F0C6A6EE285DA2A53C95259E7937C0AE9")
//messageHash := MsgHash(pubsubTopic, msg)
//require.True(t, bytes.Equal(expectedMessageHash, messageHash))
myValidator, err := validatorFnBuilder(NewFakeTimesource(timestamp), address)
require.NoError(t, err)
result := myValidator(context.Background(), "", &pubsub.Message{
Message: &pubsub_pb.Message{
Data: msgData,
},
})
require.True(t, result)
// Exceed 5m window in both directions
now5m1sInPast := timestamp.Add(-5 * time.Minute).Add(-1 * time.Second)
myValidator, err = validatorFnBuilder(NewFakeTimesource(now5m1sInPast), address)
require.NoError(t, err)
result = myValidator(context.Background(), "", &pubsub.Message{
Message: &pubsub_pb.Message{
Data: msgData,
},
})
require.False(t, result)
now5m1sInFuture := timestamp.Add(5 * time.Minute).Add(1 * time.Second)
myValidator, err = validatorFnBuilder(NewFakeTimesource(now5m1sInFuture), address)
require.NoError(t, err)
result = myValidator(context.Background(), "", &pubsub.Message{
Message: &pubsub_pb.Message{
Data: msgData,
},
})
require.False(t, result)
}