github.com/osdi23p228/fabric@v0.0.0-20221218062954-77808885f5db/orderer/common/channelparticipation/validator_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package channelparticipation_test 8 9 import ( 10 "errors" 11 "math" 12 "testing" 13 14 cb "github.com/hyperledger/fabric-protos-go/common" 15 "github.com/osdi23p228/fabric/bccsp" 16 "github.com/osdi23p228/fabric/orderer/common/channelparticipation" 17 "github.com/osdi23p228/fabric/protoutil" 18 "github.com/stretchr/testify/assert" 19 ) 20 21 func TestValidateJoinBlock(t *testing.T) { 22 tests := []struct { 23 testName string 24 channelID string 25 joinBlock *cb.Block 26 expectedIsAppChannel bool 27 expectedErr error 28 }{ 29 { 30 testName: "Valid system channel join block", 31 channelID: "my-channel", 32 joinBlock: blockWithGroups( 33 map[string]*cb.ConfigGroup{ 34 "Consortiums": {}, 35 }, 36 "my-channel", 37 ), 38 expectedIsAppChannel: false, 39 expectedErr: nil, 40 }, 41 { 42 testName: "Valid application channel join block", 43 channelID: "my-channel", 44 joinBlock: blockWithGroups( 45 map[string]*cb.ConfigGroup{ 46 "Application": {}, 47 }, 48 "my-channel", 49 ), 50 expectedIsAppChannel: true, 51 expectedErr: nil, 52 }, 53 { 54 testName: "Join block not a config block", 55 channelID: "my-channel", 56 joinBlock: nonConfigBlock(), 57 expectedIsAppChannel: false, 58 expectedErr: errors.New("block is not a config block"), 59 }, 60 { 61 testName: "ChannelID does not match join blocks", 62 channelID: "not-my-channel", 63 joinBlock: blockWithGroups( 64 map[string]*cb.ConfigGroup{ 65 "Consortiums": {}, 66 }, 67 "my-channel", 68 ), 69 expectedIsAppChannel: false, 70 expectedErr: errors.New("config block channelID [my-channel] does not match passed channelID [not-my-channel]"), 71 }, 72 { 73 testName: "Invalid bundle", 74 channelID: "my-channel", 75 joinBlock: blockWithGroups( 76 map[string]*cb.ConfigGroup{ 77 "InvalidGroup": {}, 78 }, 79 "my-channel", 80 ), 81 expectedIsAppChannel: false, 82 expectedErr: nil, 83 }, 84 { 85 testName: "Join block has no application or consortiums group", 86 channelID: "my-channel", 87 joinBlock: blockWithGroups( 88 map[string]*cb.ConfigGroup{}, 89 "my-channel", 90 ), 91 expectedIsAppChannel: false, 92 expectedErr: errors.New("invalid config: must have at least one of application or consortiums"), 93 }, 94 } 95 96 for _, test := range tests { 97 t.Run(test.testName, func(t *testing.T) { 98 isAppChannel, err := channelparticipation.ValidateJoinBlock(test.channelID, test.joinBlock) 99 assert.Equal(t, isAppChannel, test.expectedIsAppChannel) 100 if test.expectedErr != nil { 101 assert.EqualError(t, err, test.expectedErr.Error()) 102 } 103 }) 104 } 105 } 106 107 func blockWithGroups(groups map[string]*cb.ConfigGroup, channelID string) *cb.Block { 108 return &cb.Block{ 109 Data: &cb.BlockData{ 110 Data: [][]byte{ 111 protoutil.MarshalOrPanic(&cb.Envelope{ 112 Payload: protoutil.MarshalOrPanic(&cb.Payload{ 113 Data: protoutil.MarshalOrPanic(&cb.ConfigEnvelope{ 114 Config: &cb.Config{ 115 ChannelGroup: &cb.ConfigGroup{ 116 Groups: groups, 117 Values: map[string]*cb.ConfigValue{ 118 "HashingAlgorithm": { 119 Value: protoutil.MarshalOrPanic(&cb.HashingAlgorithm{ 120 Name: bccsp.SHA256, 121 }), 122 }, 123 "BlockDataHashingStructure": { 124 Value: protoutil.MarshalOrPanic(&cb.BlockDataHashingStructure{ 125 Width: math.MaxUint32, 126 }), 127 }, 128 "OrdererAddresses": { 129 Value: protoutil.MarshalOrPanic(&cb.OrdererAddresses{ 130 Addresses: []string{"localhost"}, 131 }), 132 }, 133 }, 134 }, 135 }, 136 }), 137 Header: &cb.Header{ 138 ChannelHeader: protoutil.MarshalOrPanic(&cb.ChannelHeader{ 139 Type: int32(cb.HeaderType_CONFIG), 140 ChannelId: channelID, 141 }), 142 }, 143 }), 144 }), 145 }, 146 }, 147 } 148 } 149 150 func nonConfigBlock() *cb.Block { 151 return &cb.Block{ 152 Data: &cb.BlockData{ 153 Data: [][]byte{ 154 protoutil.MarshalOrPanic(&cb.Envelope{ 155 Payload: protoutil.MarshalOrPanic(&cb.Payload{ 156 Header: &cb.Header{ 157 ChannelHeader: protoutil.MarshalOrPanic(&cb.ChannelHeader{ 158 Type: int32(cb.HeaderType_ENDORSER_TRANSACTION), 159 }), 160 }, 161 }), 162 }), 163 }, 164 }, 165 } 166 }