Skip to content

Commit

Permalink
contrib/google.golang.org/grpc: isolate more tests using an independe…
Browse files Browse the repository at this point in the history
…nt rig instance instead of a shared one
  • Loading branch information
darccio committed Oct 5, 2023
1 parent 8a5def5 commit bbc77a4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 26 deletions.
54 changes: 33 additions & 21 deletions contrib/google.golang.org/grpc/appsec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,21 @@ func TestAppSec(t *testing.T) {
t.Skip("appsec disabled")
}

rig, err := newRig(false)
require.NoError(t, err)
defer rig.Close()
setup := func() (FixtureClient, mocktracer.Tracer, func()) {
rig, err := newRig(false)
require.NoError(t, err)

client := rig.client
mt := mocktracer.Start()

return rig.client, mt, func() {
rig.Close()
mt.Stop()
}
}

t.Run("unary", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()
client, mt, cleanup := setup()
defer cleanup()

// Send a XSS attack in the payload along with the canary value in the RPC metadata
ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs("dd-canary", "dd-test-scanner-log"))
Expand All @@ -58,8 +64,8 @@ func TestAppSec(t *testing.T) {
})

t.Run("stream", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()
client, mt, cleanup := setup()
defer cleanup()

// Send a XSS attack in the payload along with the canary value in the RPC metadata
ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs("dd-canary", "dd-test-scanner-log"))
Expand Down Expand Up @@ -110,15 +116,21 @@ func TestBlocking(t *testing.T) {
t.Skip("appsec disabled")
}

rig, err := newRig(false)
require.NoError(t, err)
defer rig.Close()
setup := func() (FixtureClient, mocktracer.Tracer, func()) {
rig, err := newRig(false)
require.NoError(t, err)

mt := mocktracer.Start()

client := rig.client
return rig.client, mt, func() {
rig.Close()
mt.Stop()
}
}

t.Run("unary-block", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()
client, mt, cleanup := setup()
defer cleanup()

// Send a XSS attack in the payload along with the canary value in the RPC metadata
ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs("dd-canary", "dd-test-scanner-log", "x-client-ip", "1.2.3.4"))
Expand All @@ -136,8 +148,8 @@ func TestBlocking(t *testing.T) {
})

t.Run("unary-no-block", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()
client, _, cleanup := setup()
defer cleanup()

// Send a XSS attack in the payload along with the canary value in the RPC metadata
ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs("dd-canary", "dd-test-scanner-log", "x-client-ip", "1.2.3.5"))
Expand All @@ -148,8 +160,8 @@ func TestBlocking(t *testing.T) {
})

t.Run("stream-block", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()
client, mt, cleanup := setup()
defer cleanup()

ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs("dd-canary", "dd-test-scanner-log", "x-client-ip", "1.2.3.4"))
stream, err := client.StreamPing(ctx)
Expand All @@ -168,8 +180,8 @@ func TestBlocking(t *testing.T) {
})

t.Run("stream-no-block", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()
client, _, cleanup := setup()
defer cleanup()

ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs("dd-canary", "dd-test-scanner-log", "x-client-ip", "1.2.3.5"))
stream, err := client.StreamPing(ctx)
Expand Down Expand Up @@ -280,7 +292,7 @@ func TestUserBlocking(t *testing.T) {
})

t.Run("stream-no-block", func(t *testing.T) {
client, mt, cleanup := setup()
client, _, cleanup := setup()
defer cleanup()

ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs("user-id", "legit user"))
Expand Down
10 changes: 5 additions & 5 deletions contrib/google.golang.org/grpc/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ import (
func TestUnary(t *testing.T) {
assert := assert.New(t)

rig, err := newRig(true, WithServiceName("grpc"), WithRequestTags())
require.NoError(t, err, "error setting up rig")
defer rig.Close()
client := rig.client

for name, tt := range map[string]struct {
message string
error bool
Expand All @@ -67,6 +62,11 @@ func TestUnary(t *testing.T) {
},
} {
t.Run(name, func(t *testing.T) {
rig, err := newRig(true, WithServiceName("grpc"), WithRequestTags())
require.NoError(t, err, "error setting up rig")
defer rig.Close()
client := rig.client

mt := mocktracer.Start()
defer mt.Stop()

Expand Down

0 comments on commit bbc77a4

Please sign in to comment.