policy_test.go 7.06 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
11
12
13
*/

package policies

import (
	"testing"

	cb "github.com/hyperledger/fabric/protos/common"

14
15
16
	"fmt"
	"reflect"

17
18
	"strconv"

19
	"github.com/golang/protobuf/proto"
20
	"github.com/hyperledger/fabric/protos/msp"
21
	logging "github.com/op/go-logging"
22
	"github.com/stretchr/testify/assert"
23
24
25
26
27
28
29
30
)

func init() {
	logging.SetLevel(logging.DEBUG, "")
}

type mockProvider struct{}

31
32
func (mpp mockProvider) NewPolicy(data []byte) (Policy, proto.Message, error) {
	return nil, nil, nil
33
34
35
36
37
38
39
40
41
42
43
}

const mockType = int32(0)

func defaultProviders() map[int32]Provider {
	providers := make(map[int32]Provider)
	providers[mockType] = &mockProvider{}
	return providers
}

func TestUnnestedManager(t *testing.T) {
44
45
	config := &cb.ConfigGroup{
		Policies: map[string]*cb.ConfigPolicy{
46
47
48
			"1": {Policy: &cb.Policy{Type: mockType}},
			"2": {Policy: &cb.Policy{Type: mockType}},
			"3": {Policy: &cb.Policy{Type: mockType}},
49
		},
50
51
	}

52
53
54
	m, err := NewManagerImpl("test", defaultProviders(), config)
	assert.NoError(t, err)
	assert.NotNil(t, m)
55
56
57
58
59
60
61
62

	_, ok := m.Manager([]string{"subGroup"})
	assert.False(t, ok, "Should not have found a subgroup manager")

	r, ok := m.Manager([]string{})
	assert.True(t, ok, "Should have found the root manager")
	assert.Equal(t, m, r)

63
64
65
	assert.Len(t, m.policies, len(config.Policies))

	for policyName := range config.Policies {
66
67
68
69
70
71
		_, ok := m.GetPolicy(policyName)
		assert.True(t, ok, "Should have found policy %s", policyName)
	}
}

func TestNestedManager(t *testing.T) {
72
73
	config := &cb.ConfigGroup{
		Policies: map[string]*cb.ConfigPolicy{
74
75
76
			"n0a": {Policy: &cb.Policy{Type: mockType}},
			"n0b": {Policy: &cb.Policy{Type: mockType}},
			"n0c": {Policy: &cb.Policy{Type: mockType}},
77
78
		},
		Groups: map[string]*cb.ConfigGroup{
79
			"nest1": {
80
				Policies: map[string]*cb.ConfigPolicy{
81
82
83
					"n1a": {Policy: &cb.Policy{Type: mockType}},
					"n1b": {Policy: &cb.Policy{Type: mockType}},
					"n1c": {Policy: &cb.Policy{Type: mockType}},
84
85
				},
				Groups: map[string]*cb.ConfigGroup{
86
					"nest2a": {
87
						Policies: map[string]*cb.ConfigPolicy{
88
89
90
							"n2a_1": {Policy: &cb.Policy{Type: mockType}},
							"n2a_2": {Policy: &cb.Policy{Type: mockType}},
							"n2a_3": {Policy: &cb.Policy{Type: mockType}},
91
92
						},
					},
93
					"nest2b": {
94
						Policies: map[string]*cb.ConfigPolicy{
95
96
97
							"n2b_1": {Policy: &cb.Policy{Type: mockType}},
							"n2b_2": {Policy: &cb.Policy{Type: mockType}},
							"n2b_3": {Policy: &cb.Policy{Type: mockType}},
98
99
100
101
102
						},
					},
				},
			},
		},
103
104
	}

105
106
107
	m, err := NewManagerImpl("nest0", defaultProviders(), config)
	assert.NoError(t, err)
	assert.NotNil(t, m)
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126

	r, ok := m.Manager([]string{})
	assert.True(t, ok, "Should have found the root manager")
	assert.Equal(t, m, r)

	n1, ok := m.Manager([]string{"nest1"})
	assert.True(t, ok)
	n2a, ok := m.Manager([]string{"nest1", "nest2a"})
	assert.True(t, ok)
	n2b, ok := m.Manager([]string{"nest1", "nest2b"})
	assert.True(t, ok)

	n2as, ok := n1.Manager([]string{"nest2a"})
	assert.True(t, ok)
	assert.Equal(t, n2a, n2as)
	n2bs, ok := n1.Manager([]string{"nest2b"})
	assert.True(t, ok)
	assert.Equal(t, n2b, n2bs)

127
128
	absPrefix := PathSeparator + "nest0" + PathSeparator
	for policyName := range config.Policies {
129
130
		_, ok := m.GetPolicy(policyName)
		assert.True(t, ok, "Should have found policy %s", policyName)
131
132
133
134

		absName := absPrefix + policyName
		_, ok = m.GetPolicy(absName)
		assert.True(t, ok, "Should have found absolute policy %s", absName)
135
136
	}

137
	for policyName := range config.Groups["nest1"].Policies {
138
139
140
		_, ok := n1.GetPolicy(policyName)
		assert.True(t, ok, "Should have found policy %s", policyName)

141
142
		relPathFromBase := "nest1" + PathSeparator + policyName
		_, ok = m.GetPolicy(relPathFromBase)
143
		assert.True(t, ok, "Should have found policy %s", policyName)
144
145

		for i, abs := range []Manager{n1, m} {
146
			absName := absPrefix + relPathFromBase
147
148
149
			_, ok = abs.GetPolicy(absName)
			assert.True(t, ok, "Should have found absolutely policy for manager %d", i)
		}
150
151
	}

152
	for policyName := range config.Groups["nest1"].Groups["nest2a"].Policies {
153
154
155
		_, ok := n2a.GetPolicy(policyName)
		assert.True(t, ok, "Should have found policy %s", policyName)

156
157
		relPathFromN1 := "nest2a" + PathSeparator + policyName
		_, ok = n1.GetPolicy(relPathFromN1)
158
159
		assert.True(t, ok, "Should have found policy %s", policyName)

160
161
		relPathFromBase := "nest1" + PathSeparator + relPathFromN1
		_, ok = m.GetPolicy(relPathFromBase)
162
		assert.True(t, ok, "Should have found policy %s", policyName)
163
164

		for i, abs := range []Manager{n2a, n1, m} {
165
			absName := absPrefix + relPathFromBase
166
167
168
			_, ok = abs.GetPolicy(absName)
			assert.True(t, ok, "Should have found absolutely policy for manager %d", i)
		}
169
170
	}

171
	for policyName := range config.Groups["nest1"].Groups["nest2b"].Policies {
172
173
174
		_, ok := n2b.GetPolicy(policyName)
		assert.True(t, ok, "Should have found policy %s", policyName)

175
176
		relPathFromN1 := "nest2b" + PathSeparator + policyName
		_, ok = n1.GetPolicy(relPathFromN1)
177
178
		assert.True(t, ok, "Should have found policy %s", policyName)

179
180
		relPathFromBase := "nest1" + PathSeparator + relPathFromN1
		_, ok = m.GetPolicy(relPathFromBase)
181
		assert.True(t, ok, "Should have found policy %s", policyName)
182
183

		for i, abs := range []Manager{n2b, n1, m} {
184
			absName := absPrefix + relPathFromBase
185
186
187
			_, ok = abs.GetPolicy(absName)
			assert.True(t, ok, "Should have found absolutely policy for manager %d", i)
		}
188
189
	}
}
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214

func TestPrincipalUniqueSet(t *testing.T) {
	var principalSet PrincipalSet
	addPrincipal := func(i int) {
		principalSet = append(principalSet, &msp.MSPPrincipal{
			PrincipalClassification: msp.MSPPrincipal_Classification(i),
			Principal:               []byte(fmt.Sprintf("%d", i)),
		})
	}

	addPrincipal(1)
	addPrincipal(2)
	addPrincipal(2)
	addPrincipal(3)
	addPrincipal(3)
	addPrincipal(3)

	for principal, plurality := range principalSet.UniqueSet() {
		assert.Equal(t, int(principal.PrincipalClassification), plurality)
		assert.Equal(t, fmt.Sprintf("%d", plurality), string(principal.Principal))
	}

	v := reflect.Indirect(reflect.ValueOf(msp.MSPPrincipal{}))
	// Ensure msp.MSPPrincipal has only 2 fields.
	// This is essential for 'UniqueSet' to work properly
215
216
217
218
	// XXX This is a rather brittle check and brittle way to fix the test
	// There seems to be an assumption that the number of fields in the proto
	// struct matches the number of fields in the proto message
	assert.Equal(t, 5, v.NumField())
219
}
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244

func TestPrincipalSetContainingOnly(t *testing.T) {
	var principalSets PrincipalSets
	var principalSet PrincipalSet
	for j := 0; j < 3; j++ {
		for i := 0; i < 10; i++ {
			principalSet = append(principalSet, &msp.MSPPrincipal{
				PrincipalClassification: msp.MSPPrincipal_IDENTITY,
				Principal:               []byte(fmt.Sprintf("%d", j*10+i)),
			})
		}
		principalSets = append(principalSets, principalSet)
		principalSet = nil
	}

	between20And30 := func(principal *msp.MSPPrincipal) bool {
		n, _ := strconv.ParseInt(string(principal.Principal), 10, 32)
		return n >= 20 && n <= 29
	}

	principalSets = principalSets.ContainingOnly(between20And30)

	assert.Len(t, principalSets, 1)
	assert.True(t, principalSets[0].ContainingOnly(between20And30))
}