blackbox_test.go 2.38 KB
Newer Older
1
/*
2
Copyright IBM Corp. All Rights Reserved.
3

4
SPDX-License-Identifier: Apache-2.0
5
6
7
8
9
10
*/

package protolator_test

import (
	"bytes"
11
	"os"
12
13
	"testing"

14
	"github.com/hyperledger/fabric/common/tools/configtxgen/configtxgentest"
15
	"github.com/hyperledger/fabric/common/tools/configtxgen/encoder"
16
	genesisconfig "github.com/hyperledger/fabric/common/tools/configtxgen/localconfig"
17
	. "github.com/hyperledger/fabric/common/tools/protolator"
18
	cb "github.com/hyperledger/fabric/protos/common"
19
	"github.com/hyperledger/fabric/protos/msp"
20
	"github.com/hyperledger/fabric/protos/utils"
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

	"github.com/golang/protobuf/proto"
	"github.com/stretchr/testify/assert"
)

func bidirectionalMarshal(t *testing.T, doc proto.Message) {
	var buffer bytes.Buffer

	assert.NoError(t, DeepMarshalJSON(&buffer, doc))

	newRoot := proto.Clone(doc)
	newRoot.Reset()
	assert.NoError(t, DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newRoot))

	// Note, we cannot do an equality check between newRoot and sampleDoc
	// because of the nondeterministic nature of binary proto marshaling
	// So instead we re-marshal to JSON which is a deterministic marshaling
	// and compare equality there instead

	//t.Log(doc)
	//t.Log(newRoot)

	var remarshaled bytes.Buffer
	assert.NoError(t, DeepMarshalJSON(&remarshaled, newRoot))
	assert.Equal(t, string(buffer.Bytes()), string(remarshaled.Bytes()))
	//t.Log(string(buffer.Bytes()))
	//t.Log(string(remarshaled.Bytes()))
}

func TestConfigUpdate(t *testing.T) {
51
	cg, err := encoder.NewChannelGroup(configtxgentest.Load(genesisconfig.SampleSingleMSPSoloProfile))
52
53
	assert.NoError(t, err)

54
55
56
57
58
59
	bidirectionalMarshal(t, &cb.ConfigUpdateEnvelope{
		ConfigUpdate: utils.MarshalOrPanic(&cb.ConfigUpdate{
			ReadSet:  cg,
			WriteSet: cg,
		}),
	})
60
61
}

62
63
64
65
66
67
68
69
70
func TestIdemix(t *testing.T) {
	bidirectionalMarshal(t, &msp.MSPConfig{
		Type: 1,
		Config: utils.MarshalOrPanic(&msp.IdemixMSPConfig{
			Name: "fooo",
		}),
	})
}

71
func TestGenesisBlock(t *testing.T) {
72
	p := encoder.New(configtxgentest.Load(genesisconfig.SampleSingleMSPSoloProfile))
73
74
75
76
	gb := p.GenesisBlockForChannel("foo")

	bidirectionalMarshal(t, gb)
}
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

func TestEmitDefaultsBug(t *testing.T) {
	block := &cb.Block{
		Header: &cb.BlockHeader{
			PreviousHash: []byte("foo"),
		},
		Data: &cb.BlockData{
			Data: [][]byte{
				utils.MarshalOrPanic(&cb.Envelope{
					Signature: []byte("bar"),
				}),
			},
		},
	}

	err := DeepMarshalJSON(os.Stdout, block)
	assert.NoError(t, err)
}