@@ -13,6 +13,7 @@ import (
13
13
"github.com/lightningnetwork/lnd/macaroons"
14
14
"github.com/lightningnetwork/lnd/zpay32"
15
15
"github.com/stretchr/testify/require"
16
+ "google.golang.org/grpc/metadata"
16
17
"google.golang.org/protobuf/proto"
17
18
"gopkg.in/macaroon.v2"
18
19
)
@@ -223,6 +224,18 @@ func middlewareInterceptionTest(t *testing.T,
223
224
ctxc , cancel := context .WithTimeout (ctxb , defaultTimeout )
224
225
defer cancel ()
225
226
227
+ // Add some gRPC metadata pairs to the context that we use for one of
228
+ // the calls. This is so that we can test that the interceptor does
229
+ // properly receive the pairs via the interceptor request.
230
+ requestMetadata := metadata.MD {
231
+ "itest-metadata-key" : []string {"itest-metadata-value" },
232
+ "itest-metadata-key2" : []string {
233
+ "itest-metadata-value1" ,
234
+ "itest-metadata-value2" ,
235
+ },
236
+ }
237
+ ctxm := metadata .NewOutgoingContext (ctxc , requestMetadata )
238
+
226
239
// Create a client connection that we'll use to simulate user requests
227
240
// to lnd with.
228
241
cleanup , client := macaroonClient (t , node , userMac )
@@ -234,10 +247,11 @@ func middlewareInterceptionTest(t *testing.T,
234
247
req := & lnrpc.ListChannelsRequest {ActiveOnly : true }
235
248
go registration .interceptUnary (
236
249
"/lnrpc.Lightning/ListChannels" , req , nil , readOnly , false ,
250
+ requestMetadata ,
237
251
)
238
252
239
253
// Do the actual call now and wait for the interceptor to do its thing.
240
- resp , err := client .ListChannels (ctxc , req )
254
+ resp , err := client .ListChannels (ctxm , req )
241
255
require .NoError (t , err )
242
256
243
257
// Did we receive the correct intercept message?
@@ -252,7 +266,7 @@ func middlewareInterceptionTest(t *testing.T,
252
266
}
253
267
go registration .interceptUnary (
254
268
"/lnrpc.Lightning/ListChannels" , invalidReq , nil , readOnly ,
255
- false ,
269
+ false , nil ,
256
270
)
257
271
258
272
// Do the actual call now and wait for the interceptor to do its thing.
@@ -384,7 +398,7 @@ func middlewareResponseManipulationTest(t *testing.T,
384
398
req := & lnrpc.ListChannelsRequest {ActiveOnly : true }
385
399
go registration .interceptUnary (
386
400
"/lnrpc.Lightning/ListChannels" , req , replacementResponse ,
387
- readOnly , false ,
401
+ readOnly , false , nil ,
388
402
)
389
403
390
404
// Do the actual call now and wait for the interceptor to do its thing.
@@ -408,7 +422,7 @@ func middlewareResponseManipulationTest(t *testing.T,
408
422
}
409
423
go registration .interceptUnary (
410
424
"/lnrpc.Lightning/ListChannels" , invalidReq , betterError ,
411
- readOnly , false ,
425
+ readOnly , false , nil ,
412
426
)
413
427
414
428
// Do the actual call now and wait for the interceptor to do its thing.
@@ -500,7 +514,7 @@ func middlewareRequestManipulationTest(t *testing.T, node *node.HarnessNode,
500
514
}
501
515
go registration .interceptUnary (
502
516
"/lnrpc.Lightning/AddInvoice" , req , replacementRequest ,
503
- readOnly , true ,
517
+ readOnly , true , nil ,
504
518
)
505
519
506
520
// Do the actual call now and wait for the interceptor to do its thing.
@@ -717,12 +731,28 @@ func registerMiddleware(t *testing.T, node *node.HarnessNode,
717
731
// read from the response channel.
718
732
func (h * middlewareHarness ) interceptUnary (methodURI string ,
719
733
expectedRequest proto.Message , responseReplacement interface {},
720
- readOnly bool , replaceRequest bool ) {
734
+ readOnly bool , replaceRequest bool , expectedMetadata metadata. MD ) {
721
735
722
736
// Read intercept message and make sure it's for an RPC request.
723
737
reqIntercept , err := h .stream .Recv ()
724
738
require .NoError (h .t , err )
725
739
740
+ // Check that we have the expected metadata in the request.
741
+ if len (expectedMetadata ) > 0 {
742
+ require .GreaterOrEqual (
743
+ h .t , len (reqIntercept .MetadataPairs ),
744
+ len (expectedMetadata ),
745
+ )
746
+
747
+ for key := range expectedMetadata {
748
+ require .Contains (h .t , reqIntercept .MetadataPairs , key )
749
+ require .Equal (
750
+ h .t , expectedMetadata [key ],
751
+ reqIntercept .MetadataPairs [key ].Values ,
752
+ )
753
+ }
754
+ }
755
+
726
756
// Make sure the custom condition is populated correctly (if we're using
727
757
// a macaroon with a custom condition).
728
758
if ! readOnly {
0 commit comments