|
| 1 | +/* |
| 2 | +Copyright IBM Corp. All Rights Reserved. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: Apache-2.0 |
| 5 | +*/ |
| 6 | + |
| 7 | +package deliver |
| 8 | + |
| 9 | +import ( |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/hyperledger/fabric/protos/common" |
| 14 | + "github.com/hyperledger/fabric/protos/utils" |
| 15 | + "github.com/pkg/errors" |
| 16 | + "github.com/stretchr/testify/assert" |
| 17 | + "github.com/stretchr/testify/mock" |
| 18 | +) |
| 19 | + |
| 20 | +type mockACSupport struct { |
| 21 | + mock.Mock |
| 22 | +} |
| 23 | + |
| 24 | +func (s *mockACSupport) ExpiresAt(identityBytes []byte) time.Time { |
| 25 | + return s.Called().Get(0).(time.Time) |
| 26 | +} |
| 27 | + |
| 28 | +func (s *mockACSupport) Sequence() uint64 { |
| 29 | + return s.Called().Get(0).(uint64) |
| 30 | +} |
| 31 | + |
| 32 | +func createEnvelope() *common.Envelope { |
| 33 | + chHdr := utils.MakeChannelHeader(common.HeaderType_DELIVER_SEEK_INFO, 0, "mychannel", 0) |
| 34 | + siHdr := utils.MakeSignatureHeader(nil, nil) |
| 35 | + paylBytes := utils.MarshalOrPanic(&common.Payload{ |
| 36 | + Header: utils.MakePayloadHeader(chHdr, siHdr), |
| 37 | + }) |
| 38 | + |
| 39 | + return &common.Envelope{Payload: paylBytes} |
| 40 | +} |
| 41 | + |
| 42 | +type oneTimeInvoke struct { |
| 43 | + f func(*common.Envelope, string) error |
| 44 | + invoked bool |
| 45 | +} |
| 46 | + |
| 47 | +func (oti *oneTimeInvoke) invokeOnce() func(*common.Envelope, string) error { |
| 48 | + return func(env *common.Envelope, s string) error { |
| 49 | + if oti.invoked { |
| 50 | + panic("already invoked!") |
| 51 | + } |
| 52 | + oti.invoked = true |
| 53 | + return oti.f(env, s) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func oneTimeFunction(f func(*common.Envelope, string) error) func(*common.Envelope, string) error { |
| 58 | + oti := &oneTimeInvoke{f: f} |
| 59 | + return oti.invokeOnce() |
| 60 | +} |
| 61 | + |
| 62 | +func TestOneTimeFunction(t *testing.T) { |
| 63 | + acceptPolicyChecker := func(envelope *common.Envelope, channelID string) error { |
| 64 | + return nil |
| 65 | + } |
| 66 | + f := oneTimeFunction(acceptPolicyChecker) |
| 67 | + // First time no panic |
| 68 | + assert.NotPanics(t, func() { |
| 69 | + f(nil, "") |
| 70 | + }) |
| 71 | + |
| 72 | + // Second time we panic |
| 73 | + assert.Panics(t, func() { |
| 74 | + f(nil, "") |
| 75 | + }) |
| 76 | +} |
| 77 | + |
| 78 | +func TestAC(t *testing.T) { |
| 79 | + acceptPolicyChecker := func(envelope *common.Envelope, channelID string) error { |
| 80 | + return nil |
| 81 | + } |
| 82 | + |
| 83 | + denyPolicyChecker := func(envelope *common.Envelope, channelID string) error { |
| 84 | + return errors.New("forbidden") |
| 85 | + } |
| 86 | + |
| 87 | + sup := &mockACSupport{} |
| 88 | + // Scenario I: create empty header |
| 89 | + ac, err := newSessionAC(sup, &common.Envelope{}, nil, "mychannel", sup.ExpiresAt) |
| 90 | + assert.Nil(t, ac) |
| 91 | + assert.Contains(t, err.Error(), "Missing Header") |
| 92 | + |
| 93 | + // Scenario II: Identity has expired. |
| 94 | + sup = &mockACSupport{} |
| 95 | + sup.On("ExpiresAt").Return(time.Now().Add(-1 * time.Second)).Once() |
| 96 | + ac, err = newSessionAC(sup, createEnvelope(), oneTimeFunction(acceptPolicyChecker), "mychannel", sup.ExpiresAt) |
| 97 | + assert.NotNil(t, ac) |
| 98 | + assert.NoError(t, err) |
| 99 | + err = ac.evaluate() |
| 100 | + assert.Contains(t, err.Error(), "expired") |
| 101 | + |
| 102 | + // Scenario III: Identity hasn't expired, but is forbidden |
| 103 | + sup = &mockACSupport{} |
| 104 | + sup.On("ExpiresAt").Return(time.Now().Add(time.Second)).Once() |
| 105 | + sup.On("Sequence").Return(uint64(0)).Once() |
| 106 | + ac, err = newSessionAC(sup, createEnvelope(), oneTimeFunction(denyPolicyChecker), "mychannel", sup.ExpiresAt) |
| 107 | + assert.NoError(t, err) |
| 108 | + err = ac.evaluate() |
| 109 | + assert.Contains(t, err.Error(), "forbidden") |
| 110 | + |
| 111 | + // Scenario IV: Identity hasn't expired, and is allowed |
| 112 | + // We actually check 2 sub-cases, the first one is if the identity can expire, |
| 113 | + // and the second one is if the identity can't expire (i.e an idemix identity currently can't expire) |
| 114 | + for _, expirationTime := range []time.Time{time.Now().Add(time.Second), {}} { |
| 115 | + sup = &mockACSupport{} |
| 116 | + sup.On("ExpiresAt").Return(expirationTime).Once() |
| 117 | + sup.On("Sequence").Return(uint64(0)).Once() |
| 118 | + ac, err = newSessionAC(sup, createEnvelope(), oneTimeFunction(acceptPolicyChecker), "mychannel", sup.ExpiresAt) |
| 119 | + assert.NoError(t, err) |
| 120 | + err = ac.evaluate() |
| 121 | + assert.NoError(t, err) |
| 122 | + // Execute again. We should not evaluate the policy again. |
| 123 | + // If we do, the test fails with panic because the function can be invoked only once |
| 124 | + sup.On("Sequence").Return(uint64(0)).Once() |
| 125 | + err = ac.evaluate() |
| 126 | + assert.NoError(t, err) |
| 127 | + } |
| 128 | + |
| 129 | + // Scenario V: Identity hasn't expired, and is allowed at first, but afterwards there |
| 130 | + // is a config change and afterwards it isn't allowed |
| 131 | + sup = &mockACSupport{} |
| 132 | + sup.On("ExpiresAt").Return(time.Now().Add(time.Second)).Once() |
| 133 | + sup.On("Sequence").Return(uint64(0)).Once() |
| 134 | + sup.On("Sequence").Return(uint64(1)).Once() |
| 135 | + |
| 136 | + firstInvoke := true |
| 137 | + policyChecker := func(envelope *common.Envelope, channelID string) error { |
| 138 | + if firstInvoke { |
| 139 | + firstInvoke = false |
| 140 | + return nil |
| 141 | + } |
| 142 | + return errors.New("forbidden") |
| 143 | + } |
| 144 | + |
| 145 | + ac, err = newSessionAC(sup, createEnvelope(), policyChecker, "mychannel", sup.ExpiresAt) |
| 146 | + assert.NoError(t, err) |
| 147 | + err = ac.evaluate() // first time |
| 148 | + assert.NoError(t, err) |
| 149 | + err = ac.evaluate() // second time |
| 150 | + assert.Contains(t, err.Error(), "forbidden") |
| 151 | + |
| 152 | + // Scenario VI: Identity hasn't expired at first, but expires at a later time, |
| 153 | + // and then it shouldn't be allowed to be serviced |
| 154 | + sup = &mockACSupport{} |
| 155 | + sup.On("ExpiresAt").Return(time.Now().Add(time.Millisecond * 500)).Once() |
| 156 | + sup.On("Sequence").Return(uint64(0)).Times(3) |
| 157 | + ac, err = newSessionAC(sup, createEnvelope(), oneTimeFunction(acceptPolicyChecker), "mychannel", sup.ExpiresAt) |
| 158 | + assert.NoError(t, err) |
| 159 | + err = ac.evaluate() |
| 160 | + assert.NoError(t, err) |
| 161 | + err = ac.evaluate() |
| 162 | + assert.NoError(t, err) |
| 163 | + time.Sleep(time.Second) |
| 164 | + err = ac.evaluate() |
| 165 | + assert.Error(t, err) |
| 166 | + assert.Contains(t, err.Error(), "expired") |
| 167 | +} |
0 commit comments