github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/common/channelconfig/standardvalues_test.go (about) 1 /* 2 Copyright hechain. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package channelconfig 8 9 import ( 10 "testing" 11 12 "github.com/hechain20/hechain/protoutil" 13 cb "github.com/hyperledger/fabric-protos-go/common" 14 "github.com/stretchr/testify/require" 15 ) 16 17 type foo struct { 18 Msg1 *cb.Envelope 19 Msg2 *cb.Payload 20 } 21 22 type bar struct { 23 Msg3 *cb.Header 24 } 25 26 type conflict struct { 27 Msg1 *cb.Header 28 } 29 30 type nonProtos struct { 31 Msg *cb.Envelope 32 Wrong string 33 } 34 35 type unexported struct { 36 msg *cb.Envelope 37 } 38 39 func TestSingle(t *testing.T) { 40 fooVal := &foo{} 41 sv, err := NewStandardValues(fooVal) 42 require.NoError(t, err, "Valid non-nested structure provided") 43 require.NotNil(t, fooVal.Msg1, "Should have initialized Msg1") 44 require.NotNil(t, fooVal.Msg2, "Should have initialized Msg2") 45 46 msg1, err := sv.Deserialize("Msg1", protoutil.MarshalOrPanic(&cb.Envelope{})) 47 require.NoError(t, err, "Should have found map entry") 48 require.Equal(t, msg1, fooVal.Msg1, "Should be same entry") 49 50 msg2, err := sv.Deserialize("Msg2", protoutil.MarshalOrPanic(&cb.Payload{})) 51 require.NoError(t, err, "Should have found map entry") 52 require.Equal(t, msg2, fooVal.Msg2, "Should be same entry") 53 } 54 55 func TestPair(t *testing.T) { 56 fooVal := &foo{} 57 barVal := &bar{} 58 sv, err := NewStandardValues(fooVal, barVal) 59 require.NoError(t, err, "Valid non-nested structure provided") 60 require.NotNil(t, fooVal.Msg1, "Should have initialized Msg1") 61 require.NotNil(t, fooVal.Msg2, "Should have initialized Msg2") 62 require.NotNil(t, barVal.Msg3, "Should have initialized Msg3") 63 64 msg1, err := sv.Deserialize("Msg1", protoutil.MarshalOrPanic(&cb.Envelope{})) 65 require.NoError(t, err, "Should have found map entry") 66 require.Equal(t, msg1, fooVal.Msg1, "Should be same entry") 67 68 msg2, err := sv.Deserialize("Msg2", protoutil.MarshalOrPanic(&cb.Payload{})) 69 require.NoError(t, err, "Should have found map entry") 70 require.Equal(t, msg2, fooVal.Msg2, "Should be same entry") 71 72 msg3, err := sv.Deserialize("Msg3", protoutil.MarshalOrPanic(&cb.Header{})) 73 require.NoError(t, err, "Should have found map entry") 74 require.Equal(t, msg3, barVal.Msg3, "Should be same entry") 75 } 76 77 func TestPairConflict(t *testing.T) { 78 _, err := NewStandardValues(&foo{}, &conflict{}) 79 require.Error(t, err, "Conflicting keys provided") 80 } 81 82 func TestNonProtosStruct(t *testing.T) { 83 _, err := NewStandardValues(&nonProtos{}) 84 require.Error(t, err, "Structure with non-struct non-proto fields provided") 85 } 86 87 func TestUnexportedField(t *testing.T) { 88 _, err := NewStandardValues(&unexported{msg: nil}) 89 require.Error(t, err, "Structure with unexported fields") 90 } 91 92 func TestNonPointerParam(t *testing.T) { 93 _, err := NewStandardValues(foo{}) 94 require.Error(t, err, "Parameter must be pointer") 95 } 96 97 func TestPointerToNonStruct(t *testing.T) { 98 nonStruct := "foo" 99 _, err := NewStandardValues(&nonStruct) 100 require.Error(t, err, "Pointer must be to a struct") 101 }