Skip to content
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
90 changes: 83 additions & 7 deletions fleetspeak/src/client/https/https_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ package https

import (
"context"
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
cryptorand "crypto/rand"
"crypto/x509"
"io"
"net/http"
"net/http/httptest"
Expand All @@ -24,8 +29,14 @@ import (
"testing"
"time"

"google.golang.org/protobuf/proto"

"github.com/google/fleetspeak/fleetspeak/src/client/comms"
"github.com/google/fleetspeak/fleetspeak/src/client/stats"
"github.com/google/fleetspeak/fleetspeak/src/common"

clpb "github.com/google/fleetspeak/fleetspeak/src/client/proto/fleetspeak_client"
fspb "github.com/google/fleetspeak/fleetspeak/src/common/proto/fleetspeak"
)

type testStatsCollector struct {
Expand All @@ -39,22 +50,87 @@ func (c *testStatsCollector) AfterGetFileRequest(_, _, _ string, didFetch bool,
}
}

// testCommsContext is a minimal comms.Context for tests that run a Communicator
// or StreamingCommunicator without a client.Client.
type testCommsContext struct {
comms.Context
stats stats.Collector
clientLabels []string
id common.ClientID
priv *ecdsa.PrivateKey
clientLabels []string
si comms.ServerInfo
conf *clpb.CommunicatorConfig
outbox <-chan comms.MessageInfo
beacon <-chan struct{}
stats stats.Collector
processContactData func(ctx context.Context, cd *fspb.ContactData, streaming bool) error
}

func (c *testCommsContext) Outbox() <-chan comms.MessageInfo { return c.outbox }
func (c *testCommsContext) ProcessingBeacon() <-chan struct{} { return c.beacon }
func (c *testCommsContext) CurrentID() common.ClientID { return c.id }
func (c *testCommsContext) CurrentIdentity() (comms.ClientIdentity, error) {
var pub crypto.PublicKey
if c.priv != nil {
pub = c.priv.Public()
}
return comms.ClientIdentity{ID: c.id, Private: c.priv, Public: pub, Labels: c.clientLabels}, nil
}
func (c *testCommsContext) ServerInfo() (comms.ServerInfo, error) { return c.si, nil }
func (c *testCommsContext) CommunicatorConfig() *clpb.CommunicatorConfig { return c.conf }
func (c *testCommsContext) ChainRevoked([]*x509.Certificate) bool { return false }
func (c *testCommsContext) Stats() stats.Collector {
return c.stats
if c.stats != nil {
return c.stats
}
return stats.NoopCollector{}
}
func (c *testCommsContext) MakeContactData(msgs []*fspb.Message, _ map[string]uint64) (*fspb.WrappedContactData, map[string]uint64, error) {
b, err := proto.Marshal(&fspb.ContactData{Messages: msgs})
if err != nil {
return nil, nil, err
}
return &fspb.WrappedContactData{ContactData: b}, nil, nil
}
func (c *testCommsContext) ProcessContactData(ctx context.Context, cd *fspb.ContactData, streaming bool) error {
if c.processContactData != nil {
return c.processContactData(ctx, cd, streaming)
}
return nil
}

func (c *testCommsContext) CurrentIdentity() (comms.ClientIdentity, error) {
return comms.ClientIdentity{Labels: c.clientLabels}, nil
func newTestIdentity(t *testing.T) (common.ClientID, *ecdsa.PrivateKey) {
t.Helper()
priv, err := ecdsa.GenerateKey(elliptic.P256(), cryptorand.Reader)
if err != nil {
t.Fatalf("GenerateKey() failed: %v", err)
}
id, err := common.MakeClientID(priv.Public())
if err != nil {
t.Fatalf("MakeClientID() failed: %v", err)
}
return id, priv
}

func newTestCommsContext(t *testing.T, si comms.ServerInfo, conf *clpb.CommunicatorConfig) *testCommsContext {
t.Helper()
id, priv := newTestIdentity(t)
return &testCommsContext{
id: id,
priv: priv,
si: si,
conf: conf,
}
}

func (c *testCommsContext) ServerInfo() (comms.ServerInfo, error) {
return comms.ServerInfo{}, nil
func startCommunicator(t *testing.T, c comms.Communicator, cctx comms.Context) {
t.Helper()
if err := c.Setup(cctx); err != nil {
t.Fatalf("Setup() failed: %v", err)
}
if err := c.Start(); err != nil {
t.Fatalf("Start() failed: %v", err)
}
t.Cleanup(c.Stop)
}

func createFakeServer(lastModified time.Time, blockedLabels ...string) (*httptest.Server, []string) {
Expand Down
50 changes: 24 additions & 26 deletions fleetspeak/src/client/https/polling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"google.golang.org/protobuf/proto"

"github.com/google/fleetspeak/fleetspeak/src/client"
"github.com/google/fleetspeak/fleetspeak/src/client/comms"
"github.com/google/fleetspeak/fleetspeak/src/client/config"
"github.com/google/fleetspeak/fleetspeak/src/client/service"
"github.com/google/fleetspeak/fleetspeak/src/common"
Expand Down Expand Up @@ -735,47 +736,44 @@ func TestCertificateRevoked(t *testing.T) {
}

func TestReset(t *testing.T) {
var c Communicator
conf := config.Configuration{
Servers: []string{"localhost:1234"},
CommunicatorConfig: &clpb.CommunicatorConfig{
t.Parallel()
dialCalls := make(chan struct{}, 10)
c := Communicator{
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
dialCalls <- struct{}{}
<-ctx.Done()
return nil, ctx.Err()
},
}
cctx := newTestCommsContext(
t,
comms.ServerInfo{Servers: []string{"localhost:1234"}},
&clpb.CommunicatorConfig{
MaxPollDelaySeconds: 10,
MinFailureDelaySeconds: 10,
},
}
dialCalls := make(chan struct{}, 10)
c.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
dialCalls <- struct{}{}
<-ctx.Done()
return nil, ctx.Err()
}
)
startCommunicator(t, &c, cctx)

cl, err := client.New(
conf,
client.Components{
Communicator: &c,
})
if err != nil {
t.Fatalf("unable to create client: %v", err)
}
defer cl.Stop()

// Wait for first dial attempt
select {
case <-dialCalls:
case <-time.After(5 * time.Second):
t.Fatal("Timed out waiting for first dial attempt")
}

// Call Reset
time.Sleep(100 * time.Millisecond)
select {
case <-dialCalls:
t.Fatal("DialContext() called a second time before Reset()")
case <-time.After(100 * time.Millisecond):
}

c.Reset()

// Wait for second dial attempt (should be immediate, not waiting 10s)
// Reset cancels the in-flight poll and retries without waiting 10s.
select {
case <-dialCalls:
case <-time.After(2 * time.Second):
t.Fatal("Timed out waiting for second dial attempt after Reset")
t.Fatal("Timed out waiting for second dial attempt after Reset()")
}
}

Expand Down
Loading
Loading