@@ -35,6 +35,9 @@ import (
35
35
"google.golang.org/grpc/credentials"
36
36
"google.golang.org/grpc/grpclog"
37
37
38
+ "io/ioutil"
39
+ "path/filepath"
40
+
38
41
"github.com/golang/protobuf/proto"
39
42
"github.com/golang/protobuf/ptypes/timestamp"
40
43
"github.com/hyperledger/fabric/common/ledger/testutil"
@@ -47,6 +50,8 @@ import (
47
50
"github.com/hyperledger/fabric/msp"
48
51
"github.com/hyperledger/fabric/msp/mgmt"
49
52
"github.com/hyperledger/fabric/msp/mgmt/testtools"
53
+ "github.com/hyperledger/fabric/protos/common"
54
+ msp2 "github.com/hyperledger/fabric/protos/msp"
50
55
pb "github.com/hyperledger/fabric/protos/peer"
51
56
"github.com/hyperledger/fabric/protos/utils"
52
57
"github.com/spf13/viper"
@@ -110,7 +115,7 @@ func (a *Adapter) Disconnected(err error) {
110
115
}
111
116
}
112
117
113
- func createRegisterEvent (timestamp * timestamp.Timestamp , cert * x509.Certificate ) (* pb.Event , error ) {
118
+ func createRegisterEvent (timestamp * timestamp.Timestamp , tlsCert * x509.Certificate ) (* pb.Event , error ) {
114
119
events := make ([]* pb.Interest , 2 )
115
120
events [0 ] = & pb.Interest {
116
121
EventType : pb .EventType_BLOCK ,
@@ -129,8 +134,8 @@ func createRegisterEvent(timestamp *timestamp.Timestamp, cert *x509.Certificate)
129
134
Creator : signerSerialized ,
130
135
Timestamp : timestamp ,
131
136
}
132
- if cert != nil {
133
- evt .TlsCertHash = util .ComputeSHA256 (cert .Raw )
137
+ if tlsCert != nil {
138
+ evt .TlsCertHash = util .ComputeSHA256 (tlsCert .Raw )
134
139
}
135
140
return evt , nil
136
141
}
@@ -157,11 +162,24 @@ func corrupt(bytes []byte) {
157
162
bytes [r .Int31n (int32 (len (bytes )))]--
158
163
}
159
164
165
+ func createExpiredIdentity (t * testing.T ) []byte {
166
+ certBytes , err := ioutil .ReadFile (filepath .Join ("testdata" , "expiredCert.pem" ))
167
+ assert .NoError (t , err )
168
+ sId := & msp2.SerializedIdentity {
169
+ IdBytes : certBytes ,
170
+ }
171
+ serializedIdentity , err := proto .Marshal (sId )
172
+ assert .NoError (t , err )
173
+ return serializedIdentity
174
+ }
175
+
160
176
func TestSignedEvent (t * testing.T ) {
161
177
recvChan := make (chan * streamEvent )
162
178
sendChan := make (chan * pb.Event )
163
179
stream := & mockEventStream {recvChan : recvChan , sendChan : sendChan }
164
180
mockHandler := & handler {ChatStream : stream }
181
+ backupSerializedIdentity := signerSerialized
182
+ signerSerialized = createExpiredIdentity (t )
165
183
// get a test event
166
184
evt , err := createRegisterEvent (nil , nil )
167
185
if err != nil {
@@ -176,6 +194,29 @@ func TestSignedEvent(t *testing.T) {
176
194
return
177
195
}
178
196
197
+ // validate it. Expected to fail because the identity expired
198
+ _ , err = mockHandler .validateEventMessage (sEvt )
199
+ assert .Equal (t , err .Error (), "identity expired" )
200
+ if err == nil {
201
+ t .Fatalf ("validateEventMessage succeeded but should have failed" )
202
+ return
203
+ }
204
+
205
+ // Restore the original legit serialized identity
206
+ signerSerialized = backupSerializedIdentity
207
+ evt , err = createRegisterEvent (nil , nil )
208
+ if err != nil {
209
+ t .Fatalf ("createEvent failed, err %s" , err )
210
+ return
211
+ }
212
+
213
+ // sign it
214
+ sEvt , err = utils .GetSignedEvent (evt , signer )
215
+ if err != nil {
216
+ t .Fatalf ("GetSignedEvent failed, err %s" , err )
217
+ return
218
+ }
219
+
179
220
// validate it. Expected to succeed
180
221
_ , err = mockHandler .validateEventMessage (sEvt )
181
222
if err != nil {
@@ -440,6 +481,67 @@ func TestRegister_MutualTLS(t *testing.T) {
440
481
}
441
482
}
442
483
484
+ func TestRegister_ExpiredIdentity (t * testing.T ) {
485
+ m := newMockEventhub ()
486
+ defer close (m .recvChan )
487
+
488
+ go ehServer .Chat (m )
489
+
490
+ publishBlock := func () {
491
+ gEventProcessor .eventChannel <- & pb.Event {
492
+ Event : & pb.Event_Block {
493
+ Block : & common.Block {
494
+ Header : & common.BlockHeader {
495
+ Number : 100 ,
496
+ },
497
+ },
498
+ },
499
+ }
500
+ }
501
+
502
+ expireSessions := func () {
503
+ gEventProcessor .RLock ()
504
+ handlerList := gEventProcessor .eventConsumers [pb .EventType_BLOCK ].(* genericHandlerList )
505
+ handlerList .RLock ()
506
+ for k := range handlerList .handlers {
507
+ // Artificially move the session end time a minute into the past
508
+ k .sessionEndTime = time .Now ().Add (- 1 * time .Minute )
509
+ }
510
+ handlerList .RUnlock ()
511
+ gEventProcessor .RUnlock ()
512
+ }
513
+
514
+ sEvt , err := createSignedRegisterEvent (util .CreateUtcTimestamp (), nil )
515
+ assert .NoError (t , err )
516
+ m .recvChan <- & streamEvent {event : sEvt }
517
+
518
+ // Wait for register Ack
519
+ select {
520
+ case <- m .sendChan :
521
+ case <- time .After (time .Millisecond * 500 ):
522
+ assert .Fail (t , "Didn't receive back a register ack on time" )
523
+ }
524
+
525
+ // Publish a block and make sure we receive it
526
+ publishBlock ()
527
+ select {
528
+ case resp := <- m .sendChan :
529
+ assert .Equal (t , uint64 (100 ), resp .GetBlock ().Header .Number )
530
+ case <- time .After (time .Millisecond * 500 ):
531
+ assert .Fail (t , "Didn't receive the block on time, but should have" )
532
+ }
533
+
534
+ // Expire the sessions, and publish a block again
535
+ expireSessions ()
536
+ publishBlock ()
537
+ // Make sure we don't receive it
538
+ select {
539
+ case resp := <- m .sendChan :
540
+ t .Fatalf ("Received a block (%v) but wasn't supposed to" , resp .GetBlock ())
541
+ case <- time .After (time .Millisecond * 500 ):
542
+ }
543
+ }
544
+
443
545
func resetEventProcessor (useMutualTLS bool ) {
444
546
extract := func (msg proto.Message ) []byte {
445
547
evt , isEvent := msg .(* pb.Event )
0 commit comments