@@ -71,13 +71,16 @@ func (id *mockIdentity) Serialize() ([]byte, error) {
71
71
return id .idBytes , nil
72
72
}
73
73
74
- func toSignedData (data [][]byte , identities [][]byte , signatures [][]byte ) ([]* cb. SignedData , []bool ) {
75
- signedData := make ([]* cb. SignedData , len (data ))
74
+ func toSignedData (data [][]byte , identities [][]byte , signatures [][]byte , deserializer msp. IdentityDeserializer ) ([]IdentityAndSignature , []bool ) {
75
+ signedData := make ([]IdentityAndSignature , len (data ))
76
76
for i := range signedData {
77
- signedData [i ] = & cb.SignedData {
78
- Data : data [i ],
79
- Identity : identities [i ],
80
- Signature : signatures [i ],
77
+ signedData [i ] = & deserializeAndVerify {
78
+ signedData : & cb.SignedData {
79
+ Data : data [i ],
80
+ Identity : identities [i ],
81
+ Signature : signatures [i ],
82
+ },
83
+ deserializer : deserializer ,
81
84
}
82
85
}
83
86
return signedData , make ([]bool , len (signedData ))
@@ -111,13 +114,13 @@ func TestSimpleSignature(t *testing.T) {
111
114
t .Fatalf ("Could not create a new SignaturePolicyEvaluator using the given policy, crypto-helper: %s" , err )
112
115
}
113
116
114
- if ! spe (toSignedData ([][]byte {nil }, [][]byte {signers [0 ]}, [][]byte {validSignature })) {
117
+ if ! spe (toSignedData ([][]byte {nil }, [][]byte {signers [0 ]}, [][]byte {validSignature }, & mockDeserializer {} )) {
115
118
t .Errorf ("Expected authentication to succeed with valid signatures" )
116
119
}
117
- if spe (toSignedData ([][]byte {nil }, [][]byte {signers [0 ]}, [][]byte {invalidSignature })) {
120
+ if spe (toSignedData ([][]byte {nil }, [][]byte {signers [0 ]}, [][]byte {invalidSignature }, & mockDeserializer {} )) {
118
121
t .Errorf ("Expected authentication to fail given the invalid signature" )
119
122
}
120
- if spe (toSignedData ([][]byte {nil }, [][]byte {signers [1 ]}, [][]byte {validSignature })) {
123
+ if spe (toSignedData ([][]byte {nil }, [][]byte {signers [1 ]}, [][]byte {validSignature }, & mockDeserializer {} )) {
121
124
t .Errorf ("Expected authentication to fail because signers[1] is not authorized in the policy, despite his valid signature" )
122
125
}
123
126
}
@@ -130,13 +133,13 @@ func TestMultipleSignature(t *testing.T) {
130
133
t .Fatalf ("Could not create a new SignaturePolicyEvaluator using the given policy, crypto-helper: %s" , err )
131
134
}
132
135
133
- if ! spe (toSignedData (msgs , signers , [][]byte {validSignature , validSignature })) {
136
+ if ! spe (toSignedData (msgs , signers , [][]byte {validSignature , validSignature }, & mockDeserializer {} )) {
134
137
t .Errorf ("Expected authentication to succeed with valid signatures" )
135
138
}
136
- if spe (toSignedData (msgs , signers , [][]byte {validSignature , invalidSignature })) {
139
+ if spe (toSignedData (msgs , signers , [][]byte {validSignature , invalidSignature }, & mockDeserializer {} )) {
137
140
t .Errorf ("Expected authentication to fail given one of two invalid signatures" )
138
141
}
139
- if spe (toSignedData (msgs , [][]byte {signers [0 ], signers [0 ]}, [][]byte {validSignature , validSignature })) {
142
+ if spe (toSignedData (msgs , [][]byte {signers [0 ], signers [0 ]}, [][]byte {validSignature , validSignature }, & mockDeserializer {} )) {
140
143
t .Errorf ("Expected authentication to fail because although there were two valid signatures, one was duplicated" )
141
144
}
142
145
}
@@ -149,19 +152,19 @@ func TestComplexNestedSignature(t *testing.T) {
149
152
t .Fatalf ("Could not create a new SignaturePolicyEvaluator using the given policy, crypto-helper: %s" , err )
150
153
}
151
154
152
- if ! spe (toSignedData (moreMsgs , append (signers , [][]byte {[]byte ("signer0" )}... ), [][]byte {validSignature , validSignature , validSignature })) {
155
+ if ! spe (toSignedData (moreMsgs , append (signers , [][]byte {[]byte ("signer0" )}... ), [][]byte {validSignature , validSignature , validSignature }, & mockDeserializer {} )) {
153
156
t .Errorf ("Expected authentication to succeed with valid signatures" )
154
157
}
155
- if ! spe (toSignedData (moreMsgs , [][]byte {[]byte ("signer0" ), []byte ("signer0" ), []byte ("signer0" )}, [][]byte {validSignature , validSignature , validSignature })) {
158
+ if ! spe (toSignedData (moreMsgs , [][]byte {[]byte ("signer0" ), []byte ("signer0" ), []byte ("signer0" )}, [][]byte {validSignature , validSignature , validSignature }, & mockDeserializer {} )) {
156
159
t .Errorf ("Expected authentication to succeed with valid signatures" )
157
160
}
158
- if spe (toSignedData (msgs , signers , [][]byte {validSignature , validSignature })) {
161
+ if spe (toSignedData (msgs , signers , [][]byte {validSignature , validSignature }, & mockDeserializer {} )) {
159
162
t .Errorf ("Expected authentication to fail with too few signatures" )
160
163
}
161
- if spe (toSignedData (moreMsgs , append (signers , [][]byte {[]byte ("signer0" )}... ), [][]byte {validSignature , invalidSignature , validSignature })) {
164
+ if spe (toSignedData (moreMsgs , append (signers , [][]byte {[]byte ("signer0" )}... ), [][]byte {validSignature , invalidSignature , validSignature }, & mockDeserializer {} )) {
162
165
t .Errorf ("Expected authentication failure as the signature of signer[1] was invalid" )
163
166
}
164
- if spe (toSignedData (moreMsgs , append (signers , [][]byte {[]byte ("signer1" )}... ), [][]byte {validSignature , validSignature , validSignature })) {
167
+ if spe (toSignedData (moreMsgs , append (signers , [][]byte {[]byte ("signer1" )}... ), [][]byte {validSignature , validSignature , validSignature }, & mockDeserializer {} )) {
165
168
t .Errorf ("Expected authentication failure as there was a signature from signer[0] missing" )
166
169
}
167
170
}
@@ -184,46 +187,109 @@ func TestNilSignaturePolicyEnvelope(t *testing.T) {
184
187
}
185
188
186
189
func TestDeduplicate (t * testing.T ) {
187
- ids := []* cb.SignedData {
188
- {
189
- Identity : []byte ("id1" ),
190
- },
191
- {
192
- Identity : []byte ("id2" ),
193
- },
194
- {
195
- Identity : []byte ("id3" ),
196
- },
197
- }
198
-
199
190
t .Run ("Empty" , func (t * testing.T ) {
200
- result := deduplicate ([]* cb. SignedData {}, & mockDeserializer {})
201
- assert .Equal (t , []* cb. SignedData {}, result , "Should have no identities" )
191
+ result := deduplicate ([]IdentityAndSignature {})
192
+ assert .Equal (t , []IdentityAndSignature {}, result , "Should have no identities" )
202
193
})
203
194
204
195
t .Run ("NoDuplication" , func (t * testing.T ) {
205
- result := deduplicate (ids , & mockDeserializer {})
196
+ md := & mockDeserializer {}
197
+ ids := []IdentityAndSignature {
198
+ & deserializeAndVerify {
199
+ signedData : & cb.SignedData {
200
+ Identity : []byte ("id1" ),
201
+ },
202
+ deserializer : md ,
203
+ },
204
+ & deserializeAndVerify {
205
+ signedData : & cb.SignedData {
206
+ Identity : []byte ("id2" ),
207
+ },
208
+ deserializer : md ,
209
+ },
210
+ & deserializeAndVerify {
211
+ signedData : & cb.SignedData {
212
+ Identity : []byte ("id3" ),
213
+ },
214
+ deserializer : md ,
215
+ },
216
+ }
217
+ result := deduplicate (ids )
206
218
assert .Equal (t , ids , result , "No identities should have been removed" )
207
219
})
208
220
209
221
t .Run ("AllDuplication" , func (t * testing.T ) {
210
- result := deduplicate ([]* cb.SignedData {ids [0 ], ids [0 ], ids [0 ]}, & mockDeserializer {})
211
- assert .Equal (t , []* cb.SignedData {ids [0 ]}, result , "All but the first identity should have been removed" )
222
+ md := & mockDeserializer {}
223
+ ids := []IdentityAndSignature {
224
+ & deserializeAndVerify {
225
+ signedData : & cb.SignedData {
226
+ Identity : []byte ("id1" ),
227
+ },
228
+ deserializer : md ,
229
+ },
230
+ }
231
+ result := deduplicate ([]IdentityAndSignature {ids [0 ], ids [0 ], ids [0 ]})
232
+ assert .Equal (t , []IdentityAndSignature {ids [0 ]}, result , "All but the first identity should have been removed" )
212
233
})
213
234
214
235
t .Run ("DuplicationPreservesOrder" , func (t * testing.T ) {
215
- result := deduplicate ([]* cb.SignedData {ids [1 ], ids [0 ], ids [0 ]}, & mockDeserializer {})
216
- assert .Equal (t , []* cb.SignedData {ids [1 ], ids [0 ]}, result , "The third identity should have been dropped" )
236
+ md := & mockDeserializer {}
237
+ ids := []IdentityAndSignature {
238
+ & deserializeAndVerify {
239
+ signedData : & cb.SignedData {
240
+ Identity : []byte ("id1" ),
241
+ },
242
+ deserializer : md ,
243
+ },
244
+ & deserializeAndVerify {
245
+ signedData : & cb.SignedData {
246
+ Identity : []byte ("id2" ),
247
+ },
248
+ deserializer : md ,
249
+ },
250
+ }
251
+ result := deduplicate ([]IdentityAndSignature {ids [1 ], ids [0 ], ids [0 ]})
252
+ assert .Equal (t , result , []IdentityAndSignature {ids [1 ], ids [0 ]}, "The third identity should have been dropped" )
217
253
})
218
254
219
255
t .Run ("ComplexDuplication" , func (t * testing.T ) {
220
- result := deduplicate ([]* cb.SignedData {ids [1 ], ids [0 ], ids [0 ], ids [1 ], ids [2 ], ids [0 ], ids [2 ], ids [1 ]}, & mockDeserializer {})
221
- assert .Equal (t , []* cb.SignedData {ids [1 ], ids [0 ], ids [2 ]}, result , "Expected only three non-duplicate identities" )
256
+ md := & mockDeserializer {}
257
+ ids := []IdentityAndSignature {
258
+ & deserializeAndVerify {
259
+ signedData : & cb.SignedData {
260
+ Identity : []byte ("id1" ),
261
+ },
262
+ deserializer : md ,
263
+ },
264
+ & deserializeAndVerify {
265
+ signedData : & cb.SignedData {
266
+ Identity : []byte ("id2" ),
267
+ },
268
+ deserializer : md ,
269
+ },
270
+ & deserializeAndVerify {
271
+ signedData : & cb.SignedData {
272
+ Identity : []byte ("id3" ),
273
+ },
274
+ deserializer : md ,
275
+ },
276
+ }
277
+ result := deduplicate ([]IdentityAndSignature {ids [1 ], ids [0 ], ids [0 ], ids [1 ], ids [2 ], ids [0 ], ids [2 ], ids [1 ]})
278
+ assert .Equal (t , []IdentityAndSignature {ids [1 ], ids [0 ], ids [2 ]}, result , "Expected only three non-duplicate identities" )
222
279
})
223
280
224
281
t .Run ("BadIdentity" , func (t * testing.T ) {
225
- result := deduplicate ([]* cb.SignedData {ids [1 ]}, & mockDeserializer {fail : errors .New ("error" )})
226
- assert .Equal (t , []* cb.SignedData {}, result , "No valid identities" )
282
+ md := & mockDeserializer {fail : errors .New ("error" )}
283
+ ids := []IdentityAndSignature {
284
+ & deserializeAndVerify {
285
+ signedData : & cb.SignedData {
286
+ Identity : []byte ("id1" ),
287
+ },
288
+ deserializer : md ,
289
+ },
290
+ }
291
+ result := deduplicate ([]IdentityAndSignature {ids [0 ]})
292
+ assert .Equal (t , []IdentityAndSignature {}, result , "No valid identities" )
227
293
})
228
294
}
229
295
@@ -292,7 +358,7 @@ func TestDeserializeIdentityError(t *testing.T) {
292
358
cauthdslLogger = logger
293
359
294
360
// Call
295
- signedData , used := toSignedData ([][]byte {nil }, [][]byte {nil }, [][]byte {nil })
361
+ signedData , used := toSignedData ([][]byte {nil }, [][]byte {nil }, [][]byte {nil }, & mockDeserializer { fail : errors . New ( "myError" )} )
296
362
ret := spe (signedData , used )
297
363
298
364
// Check result (ret and log)
0 commit comments