github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/orderer/common/channelparticipation/validator_test.go (about)

     1  /*
     2  Copyright hechain. 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  	"github.com/hechain20/hechain/bccsp"
    15  	"github.com/hechain20/hechain/orderer/common/channelparticipation"
    16  	"github.com/hechain20/hechain/protoutil"
    17  	cb "github.com/hyperledger/fabric-protos-go/common"
    18  	"github.com/stretchr/testify/require"
    19  )
    20  
    21  func TestValidateJoinBlock(t *testing.T) {
    22  	tests := []struct {
    23  		testName             string
    24  		joinBlock            *cb.Block
    25  		expectedChannelID    string
    26  		expectedIsAppChannel bool
    27  		expectedErr          error
    28  	}{
    29  		{
    30  			testName: "Valid system channel join block",
    31  			joinBlock: blockWithGroups(
    32  				map[string]*cb.ConfigGroup{
    33  					"Consortiums": {},
    34  				},
    35  				"my-channel",
    36  			),
    37  			expectedChannelID:    "my-channel",
    38  			expectedIsAppChannel: false,
    39  			expectedErr:          nil,
    40  		},
    41  		{
    42  			testName: "Valid application channel join block",
    43  			joinBlock: blockWithGroups(
    44  				map[string]*cb.ConfigGroup{
    45  					"Application": {},
    46  				},
    47  				"my-channel",
    48  			),
    49  			expectedChannelID:    "my-channel",
    50  			expectedIsAppChannel: true,
    51  			expectedErr:          nil,
    52  		},
    53  		{
    54  			testName:             "Join block not a config block",
    55  			joinBlock:            nonConfigBlock(),
    56  			expectedChannelID:    "",
    57  			expectedIsAppChannel: false,
    58  			expectedErr:          errors.New("block is not a config block"),
    59  		},
    60  		{
    61  			testName: "block ChannelID not valid",
    62  			joinBlock: blockWithGroups(
    63  				map[string]*cb.ConfigGroup{
    64  					"Consortiums": {},
    65  				},
    66  				"My-Channel",
    67  			),
    68  			expectedChannelID:    "",
    69  			expectedIsAppChannel: false,
    70  			expectedErr:          errors.New("initializing configtx manager failed: bad channel ID: 'My-Channel' contains illegal characters"),
    71  		},
    72  		{
    73  			testName: "Invalid bundle",
    74  			joinBlock: blockWithGroups(
    75  				map[string]*cb.ConfigGroup{
    76  					"InvalidGroup": {},
    77  				},
    78  				"my-channel",
    79  			),
    80  			expectedChannelID:    "",
    81  			expectedIsAppChannel: false,
    82  			expectedErr:          errors.New("initializing channelconfig failed: Disallowed channel group: "),
    83  		},
    84  		{
    85  			testName: "Join block has no application or consortiums group",
    86  			joinBlock: blockWithGroups(
    87  				map[string]*cb.ConfigGroup{},
    88  				"my-channel",
    89  			),
    90  			expectedChannelID:    "",
    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  			channelID, isAppChannel, err := channelparticipation.ValidateJoinBlock(test.joinBlock)
    99  			require.Equal(t, test.expectedChannelID, channelID)
   100  			require.Equal(t, test.expectedIsAppChannel, isAppChannel)
   101  			if test.expectedErr != nil {
   102  				require.EqualError(t, err, test.expectedErr.Error())
   103  			} else {
   104  				require.NoError(t, err)
   105  			}
   106  		})
   107  	}
   108  }
   109  
   110  func blockWithGroups(groups map[string]*cb.ConfigGroup, channelID string) *cb.Block {
   111  	return &cb.Block{
   112  		Data: &cb.BlockData{
   113  			Data: [][]byte{
   114  				protoutil.MarshalOrPanic(&cb.Envelope{
   115  					Payload: protoutil.MarshalOrPanic(&cb.Payload{
   116  						Data: protoutil.MarshalOrPanic(&cb.ConfigEnvelope{
   117  							Config: &cb.Config{
   118  								ChannelGroup: &cb.ConfigGroup{
   119  									Groups: groups,
   120  									Values: map[string]*cb.ConfigValue{
   121  										"HashingAlgorithm": {
   122  											Value: protoutil.MarshalOrPanic(&cb.HashingAlgorithm{
   123  												Name: bccsp.SHA256,
   124  											}),
   125  										},
   126  										"BlockDataHashingStructure": {
   127  											Value: protoutil.MarshalOrPanic(&cb.BlockDataHashingStructure{
   128  												Width: math.MaxUint32,
   129  											}),
   130  										},
   131  										"OrdererAddresses": {
   132  											Value: protoutil.MarshalOrPanic(&cb.OrdererAddresses{
   133  												Addresses: []string{"localhost"},
   134  											}),
   135  										},
   136  									},
   137  								},
   138  							},
   139  						}),
   140  						Header: &cb.Header{
   141  							ChannelHeader: protoutil.MarshalOrPanic(&cb.ChannelHeader{
   142  								Type:      int32(cb.HeaderType_CONFIG),
   143  								ChannelId: channelID,
   144  							}),
   145  						},
   146  					}),
   147  				}),
   148  			},
   149  		},
   150  	}
   151  }
   152  
   153  func nonConfigBlock() *cb.Block {
   154  	return &cb.Block{
   155  		Data: &cb.BlockData{
   156  			Data: [][]byte{
   157  				protoutil.MarshalOrPanic(&cb.Envelope{
   158  					Payload: protoutil.MarshalOrPanic(&cb.Payload{
   159  						Header: &cb.Header{
   160  							ChannelHeader: protoutil.MarshalOrPanic(&cb.ChannelHeader{
   161  								Type: int32(cb.HeaderType_ENDORSER_TRANSACTION),
   162  							}),
   163  						},
   164  					}),
   165  				}),
   166  			},
   167  		},
   168  	}
   169  }