2021-08-09 19:21:48 +02:00
|
|
|
package client
|
2021-06-14 16:57:18 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-03-16 16:46:17 +01:00
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-06-14 16:57:18 +02:00
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
|
|
|
"google.golang.org/grpc"
|
2021-12-21 10:02:25 +01:00
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
2021-06-15 16:08:46 +02:00
|
|
|
"google.golang.org/grpc/keepalive"
|
|
|
|
"google.golang.org/grpc/metadata"
|
2023-03-16 16:46:17 +01:00
|
|
|
|
|
|
|
sigProto "github.com/netbirdio/netbird/signal/proto"
|
|
|
|
"github.com/netbirdio/netbird/signal/server"
|
2021-06-14 16:57:18 +02:00
|
|
|
)
|
|
|
|
|
2022-01-18 16:44:58 +01:00
|
|
|
var _ = Describe("GrpcClient", func() {
|
2021-06-14 16:57:18 +02:00
|
|
|
|
|
|
|
var (
|
|
|
|
addr string
|
|
|
|
listener net.Listener
|
|
|
|
server *grpc.Server
|
|
|
|
)
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
|
|
|
server, listener = startSignal()
|
|
|
|
addr = listener.Addr().String()
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
AfterEach(func() {
|
|
|
|
server.Stop()
|
|
|
|
listener.Close()
|
|
|
|
})
|
|
|
|
|
2021-06-15 16:08:46 +02:00
|
|
|
Describe("Exchanging messages", func() {
|
2021-11-14 19:41:17 +01:00
|
|
|
Context("between connected peers", func() {
|
2021-06-15 16:08:46 +02:00
|
|
|
It("should be successful", func() {
|
|
|
|
|
|
|
|
var msgReceived sync.WaitGroup
|
|
|
|
msgReceived.Add(2)
|
|
|
|
|
2023-03-16 16:46:17 +01:00
|
|
|
var payloadReceivedOnA string
|
|
|
|
var payloadReceivedOnB string
|
|
|
|
var featuresSupportedReceivedOnA []uint32
|
|
|
|
var featuresSupportedReceivedOnB []uint32
|
2021-06-15 16:08:46 +02:00
|
|
|
|
|
|
|
// connect PeerA to Signal
|
|
|
|
keyA, _ := wgtypes.GenerateKey()
|
|
|
|
clientA := createSignalClient(addr, keyA)
|
2021-11-06 15:00:13 +01:00
|
|
|
go func() {
|
|
|
|
err := clientA.Receive(func(msg *sigProto.Message) error {
|
2023-03-16 16:46:17 +01:00
|
|
|
payloadReceivedOnA = msg.GetBody().GetPayload()
|
|
|
|
featuresSupportedReceivedOnA = msg.GetBody().GetFeaturesSupported()
|
2021-11-06 15:00:13 +01:00
|
|
|
msgReceived.Done()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
clientA.WaitStreamConnected()
|
2021-06-15 16:08:46 +02:00
|
|
|
|
|
|
|
// connect PeerB to Signal
|
|
|
|
keyB, _ := wgtypes.GenerateKey()
|
|
|
|
clientB := createSignalClient(addr, keyB)
|
2021-11-06 15:00:13 +01:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
err := clientB.Receive(func(msg *sigProto.Message) error {
|
2023-03-16 16:46:17 +01:00
|
|
|
payloadReceivedOnB = msg.GetBody().GetPayload()
|
|
|
|
featuresSupportedReceivedOnB = msg.GetBody().GetFeaturesSupported()
|
2021-11-06 15:00:13 +01:00
|
|
|
err := clientB.Send(&sigProto.Message{
|
|
|
|
Key: keyB.PublicKey().String(),
|
|
|
|
RemoteKey: keyA.PublicKey().String(),
|
|
|
|
Body: &sigProto.Body{Payload: "pong"},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
Fail("failed sending a message to PeerA")
|
|
|
|
}
|
|
|
|
msgReceived.Done()
|
|
|
|
return nil
|
2021-06-15 16:08:46 +02:00
|
|
|
})
|
2021-06-15 16:20:39 +02:00
|
|
|
if err != nil {
|
2021-11-06 15:00:13 +01:00
|
|
|
return
|
2021-06-15 16:20:39 +02:00
|
|
|
}
|
2021-11-06 15:00:13 +01:00
|
|
|
}()
|
|
|
|
|
|
|
|
clientB.WaitStreamConnected()
|
2021-06-15 16:08:46 +02:00
|
|
|
|
|
|
|
// PeerA initiates ping-pong
|
2021-06-15 16:20:39 +02:00
|
|
|
err := clientA.Send(&sigProto.Message{
|
2021-06-15 16:08:46 +02:00
|
|
|
Key: keyA.PublicKey().String(),
|
|
|
|
RemoteKey: keyB.PublicKey().String(),
|
2023-03-16 16:46:17 +01:00
|
|
|
Body: &sigProto.Body{Payload: "ping", FeaturesSupported: []uint32{DirectCheck}},
|
2021-06-15 16:08:46 +02:00
|
|
|
})
|
2021-06-15 16:20:39 +02:00
|
|
|
if err != nil {
|
|
|
|
Fail("failed sending a message to PeerB")
|
|
|
|
}
|
2021-06-15 16:08:46 +02:00
|
|
|
|
2021-06-15 18:58:47 +02:00
|
|
|
if waitTimeout(&msgReceived, 3*time.Second) {
|
|
|
|
Fail("test timed out on waiting for peers to exchange messages")
|
|
|
|
}
|
2021-06-15 16:08:46 +02:00
|
|
|
|
2023-03-16 16:46:17 +01:00
|
|
|
Expect(payloadReceivedOnA).To(BeEquivalentTo("pong"))
|
|
|
|
Expect(payloadReceivedOnB).To(BeEquivalentTo("ping"))
|
|
|
|
Expect(featuresSupportedReceivedOnA).To(BeNil())
|
|
|
|
Expect(featuresSupportedReceivedOnB).To(ContainElements([]uint32{DirectCheck}))
|
2021-06-15 16:08:46 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-06-14 16:57:18 +02:00
|
|
|
Describe("Connecting to the Signal stream channel", func() {
|
|
|
|
Context("with a signal client", func() {
|
|
|
|
It("should be successful", func() {
|
|
|
|
|
|
|
|
key, _ := wgtypes.GenerateKey()
|
|
|
|
client := createSignalClient(addr, key)
|
2021-11-06 15:00:13 +01:00
|
|
|
go func() {
|
|
|
|
err := client.Receive(func(msg *sigProto.Message) error {
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
client.WaitStreamConnected()
|
2021-06-14 16:57:18 +02:00
|
|
|
Expect(client).NotTo(BeNil())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-08-20 22:33:43 +02:00
|
|
|
Context("with a raw client and no Id header", func() {
|
2021-06-14 16:57:18 +02:00
|
|
|
It("should fail", func() {
|
|
|
|
|
|
|
|
client := createRawSignalClient(addr)
|
|
|
|
stream, err := client.ConnectStream(context.Background())
|
2021-06-15 16:31:45 +02:00
|
|
|
if err != nil {
|
|
|
|
Fail("error connecting to stream")
|
|
|
|
}
|
2021-06-14 16:57:18 +02:00
|
|
|
|
2021-06-15 16:08:46 +02:00
|
|
|
_, err = stream.Recv()
|
|
|
|
|
|
|
|
Expect(stream).NotTo(BeNil())
|
2021-06-14 16:57:18 +02:00
|
|
|
Expect(err).NotTo(BeNil())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-08-20 22:33:43 +02:00
|
|
|
Context("with a raw client and with an Id header", func() {
|
2021-06-15 16:08:46 +02:00
|
|
|
It("should be successful", func() {
|
|
|
|
|
|
|
|
md := metadata.New(map[string]string{sigProto.HeaderId: "peer"})
|
|
|
|
ctx := metadata.NewOutgoingContext(context.Background(), md)
|
|
|
|
|
|
|
|
client := createRawSignalClient(addr)
|
|
|
|
stream, err := client.ConnectStream(ctx)
|
|
|
|
|
|
|
|
Expect(stream).NotTo(BeNil())
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-06-14 16:57:18 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
})
|
|
|
|
|
2023-03-16 16:46:17 +01:00
|
|
|
func TestParseFeaturesSupported(t *testing.T) {
|
|
|
|
expectedOnEmptyOrUnsupported := FeaturesSupport{DirectCheck: false}
|
|
|
|
expectedWithDirectCheck := FeaturesSupport{DirectCheck: true}
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
input []uint32
|
|
|
|
expected FeaturesSupport
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Should Return DirectCheck Supported",
|
|
|
|
input: []uint32{DirectCheck},
|
|
|
|
expected: expectedWithDirectCheck,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Should Return DirectCheck Unsupported When Nil",
|
|
|
|
input: nil,
|
|
|
|
expected: expectedOnEmptyOrUnsupported,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Should Return DirectCheck Unsupported When Not Known Feature",
|
|
|
|
input: []uint32{9999},
|
|
|
|
expected: expectedOnEmptyOrUnsupported,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
|
|
result := ParseFeaturesSupported(testCase.input)
|
|
|
|
if result.DirectCheck != testCase.expected.DirectCheck {
|
|
|
|
t.Errorf("Direct check feature should match: Expected: %t, Got: %t", testCase.expected.DirectCheck, result.DirectCheck)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-18 16:44:58 +01:00
|
|
|
func createSignalClient(addr string, key wgtypes.Key) *GrpcClient {
|
2021-08-01 12:54:35 +02:00
|
|
|
var sigTLSEnabled = false
|
2021-08-09 19:21:48 +02:00
|
|
|
client, err := NewClient(context.Background(), addr, key, sigTLSEnabled)
|
2021-06-14 16:57:18 +02:00
|
|
|
if err != nil {
|
|
|
|
Fail("failed creating signal client")
|
|
|
|
}
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
|
|
|
func createRawSignalClient(addr string) sigProto.SignalExchangeClient {
|
|
|
|
ctx := context.Background()
|
2021-12-21 10:02:25 +01:00
|
|
|
conn, err := grpc.DialContext(ctx, addr,
|
|
|
|
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
2021-06-15 16:08:46 +02:00
|
|
|
grpc.WithBlock(),
|
|
|
|
grpc.WithKeepaliveParams(keepalive.ClientParameters{
|
|
|
|
Time: 3 * time.Second,
|
|
|
|
Timeout: 2 * time.Second,
|
|
|
|
}))
|
2021-06-14 16:57:18 +02:00
|
|
|
if err != nil {
|
|
|
|
Fail("failed creating raw signal client")
|
|
|
|
}
|
|
|
|
|
|
|
|
return sigProto.NewSignalExchangeClient(conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func startSignal() (*grpc.Server, net.Listener) {
|
|
|
|
lis, err := net.Listen("tcp", ":0")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
s := grpc.NewServer()
|
2021-08-09 19:21:48 +02:00
|
|
|
sigProto.RegisterSignalExchangeServer(s, server.NewServer())
|
2021-06-14 16:57:18 +02:00
|
|
|
go func() {
|
|
|
|
if err := s.Serve(lis); err != nil {
|
|
|
|
log.Fatalf("failed to serve: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return s, lis
|
|
|
|
}
|
2021-06-15 18:58:47 +02:00
|
|
|
|
|
|
|
// waitTimeout waits for the waitgroup for the specified max timeout.
|
|
|
|
// Returns true if waiting timed out.
|
|
|
|
func waitTimeout(wg *sync.WaitGroup, timeout time.Duration) bool {
|
|
|
|
c := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
defer close(c)
|
|
|
|
wg.Wait()
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case <-c:
|
|
|
|
return false // completed normally
|
|
|
|
case <-time.After(timeout):
|
|
|
|
return true // timed out
|
|
|
|
}
|
|
|
|
}
|