@@ -14,6 +14,7 @@ import (
14
14
"razor/cmd/mocks"
15
15
"razor/core"
16
16
"razor/core/types"
17
+ "razor/pkg/bindings"
17
18
"razor/utils"
18
19
mocks2 "razor/utils/mocks"
19
20
"reflect"
@@ -162,6 +163,46 @@ func TestHandleCommitState(t *testing.T) {
162
163
},
163
164
wantErr : nil ,
164
165
},
166
+ {
167
+ name : "Test 2: When there is an error in getting numActiveCollections" ,
168
+ args : args {
169
+ numActiveCollectionsErr : errors .New ("error in getting numActiveCollections" ),
170
+ },
171
+ want : types.CommitData {},
172
+ wantErr : errors .New ("error in getting numActiveCollections" ),
173
+ },
174
+ {
175
+ name : "Test 3: When there is an error in getting assignedCollections" ,
176
+ args : args {
177
+ numActiveCollections : 1 ,
178
+ assignedCollectionsErr : errors .New ("error in getting assignedCollections" ),
179
+ },
180
+ want : types.CommitData {},
181
+ wantErr : errors .New ("error in getting assignedCollections" ),
182
+ },
183
+ {
184
+ name : "Test 4: When there is an error in getting collectionId" ,
185
+ args : args {
186
+ numActiveCollections : 3 ,
187
+ assignedCollections : map [int ]bool {1 : true , 2 : true },
188
+ seqAllottedCollections : []* big.Int {big .NewInt (1 ), big .NewInt (2 )},
189
+ collectionIdErr : errors .New ("error in getting collectionId" ),
190
+ },
191
+ want : types.CommitData {},
192
+ wantErr : errors .New ("error in getting collectionId" ),
193
+ },
194
+ {
195
+ name : "Test 5: When there is an error in getting collectionData" ,
196
+ args : args {
197
+ numActiveCollections : 3 ,
198
+ assignedCollections : map [int ]bool {1 : true , 2 : true },
199
+ seqAllottedCollections : []* big.Int {big .NewInt (1 ), big .NewInt (2 )},
200
+ collectionId : 1 ,
201
+ collectionDataErr : errors .New ("error in getting collectionData" ),
202
+ },
203
+ want : types.CommitData {},
204
+ wantErr : errors .New ("error in getting collectionData" ),
205
+ },
165
206
}
166
207
for _ , tt := range tests {
167
208
t .Run (tt .name , func (t * testing.T ) {
@@ -192,3 +233,125 @@ func TestHandleCommitState(t *testing.T) {
192
233
})
193
234
}
194
235
}
236
+
237
+ func TestGetSalt (t * testing.T ) {
238
+ var client * ethclient.Client
239
+
240
+ type args struct {
241
+ epoch uint32
242
+ numProposedBlocks uint8
243
+ numProposedBlocksErr error
244
+ blockIndexedToBeConfirmed int8
245
+ blockIndexedToBeConfirmedErr error
246
+ saltFromBlockChain [32 ]byte
247
+ saltFromBlockChainErr error
248
+ blockId uint32
249
+ blockIdErr error
250
+ previousBlock bindings.StructsBlock
251
+ previousBlockErr error
252
+ salt [32 ]byte
253
+ }
254
+ tests := []struct {
255
+ name string
256
+ args args
257
+ want [32 ]byte
258
+ wantErr error
259
+ }{
260
+ {
261
+ name : "Test 1: When GetSalt() function executes successfully" ,
262
+ args : args {
263
+ epoch : 2 ,
264
+ numProposedBlocks : 1 ,
265
+ blockIndexedToBeConfirmed : 1 ,
266
+ blockId : 1 ,
267
+ previousBlock : bindings.StructsBlock {},
268
+ salt : [32 ]byte {},
269
+ },
270
+ want : [32 ]byte {},
271
+ wantErr : nil ,
272
+ },
273
+ {
274
+ name : "Test 2: When there is an error in getting numProposedBlocks" ,
275
+ args : args {
276
+ epoch : 2 ,
277
+ numProposedBlocksErr : errors .New ("error in getting numProposedBlocks" ),
278
+ },
279
+ want : [32 ]byte {},
280
+ wantErr : errors .New ("error in getting numProposedBlocks" ),
281
+ },
282
+ {
283
+ name : "Test 3: When there is an error in getting blockIndexedToBeConfirmed" ,
284
+ args : args {
285
+ epoch : 2 ,
286
+ numProposedBlocks : 1 ,
287
+ blockIndexedToBeConfirmedErr : errors .New ("error in getting blockIndexedToBeConfirmed" ),
288
+ },
289
+ want : [32 ]byte {},
290
+ wantErr : errors .New ("error in getting blockIndexedToBeConfirmed" ),
291
+ },
292
+ {
293
+ name : "Test 4: When numProposedBlock is zero" ,
294
+ args : args {
295
+ epoch : 2 ,
296
+ numProposedBlocks : 0 ,
297
+ saltFromBlockChain : [32 ]byte {},
298
+ },
299
+ want : [32 ]byte {},
300
+ wantErr : nil ,
301
+ },
302
+ {
303
+ name : "Test 5: When there is an error in getting blockId" ,
304
+ args : args {
305
+ epoch : 2 ,
306
+ numProposedBlocks : 1 ,
307
+ blockIndexedToBeConfirmed : 1 ,
308
+ blockIdErr : errors .New ("error" ),
309
+ },
310
+ want : [32 ]byte {},
311
+ wantErr : errors .New ("Error in getting blockId: error" ),
312
+ },
313
+ {
314
+ name : "Test 6: When there is an error in getting previousBlock" ,
315
+ args : args {
316
+ epoch : 2 ,
317
+ numProposedBlocks : 1 ,
318
+ blockIndexedToBeConfirmed : 1 ,
319
+ blockId : 1 ,
320
+ previousBlockErr : errors .New ("error" ),
321
+ },
322
+ want : [32 ]byte {},
323
+ wantErr : errors .New ("Error in getting previous block: error" ),
324
+ },
325
+ }
326
+ for _ , tt := range tests {
327
+ t .Run (tt .name , func (t * testing.T ) {
328
+ utilsPkgMock := new (mocks2.Utils )
329
+ utilsVoteManagerMock := new (mocks2.VoteManagerUtils )
330
+
331
+ utils .UtilsInterface = utilsPkgMock
332
+ utils .VoteManagerInterface = utilsVoteManagerMock
333
+
334
+ utilsPkgMock .On ("GetNumberOfProposedBlocks" , mock .AnythingOfType ("*ethclient.Client" ), mock .Anything ).Return (tt .args .numProposedBlocks , tt .args .numProposedBlocksErr )
335
+ utilsPkgMock .On ("GetBlockIndexToBeConfirmed" , mock .AnythingOfType ("*ethclient.Client" )).Return (tt .args .blockIndexedToBeConfirmed , tt .args .blockIndexedToBeConfirmedErr )
336
+ utilsVoteManagerMock .On ("GetSaltFromBlockchain" , mock .AnythingOfType ("*ethclient.Client" )).Return (tt .args .saltFromBlockChain , tt .args .saltFromBlockChainErr )
337
+ utilsPkgMock .On ("GetSortedProposedBlockId" , mock .AnythingOfType ("*ethclient.Client" ), mock .Anything , mock .Anything ).Return (tt .args .blockId , tt .args .blockIdErr )
338
+ utilsPkgMock .On ("GetProposedBlock" , mock .AnythingOfType ("*ethclient.Client" ), mock .Anything , mock .Anything ).Return (tt .args .previousBlock , tt .args .previousBlockErr )
339
+ utilsPkgMock .On ("CalculateSalt" , mock .Anything , mock .Anything ).Return (tt .args .salt )
340
+
341
+ ut := & UtilsStruct {}
342
+ got , err := ut .GetSalt (client , tt .args .epoch )
343
+ if ! reflect .DeepEqual (got , tt .want ) {
344
+ t .Errorf ("Data from GetSalt function, got = %v, want = %v" , got , tt .want )
345
+ }
346
+ if err == nil || tt .wantErr == nil {
347
+ if err != tt .wantErr {
348
+ t .Errorf ("Error from GetSalt function, got = %v, want = %v" , err , tt .wantErr )
349
+ }
350
+ } else {
351
+ if err .Error () != tt .wantErr .Error () {
352
+ t .Errorf ("Error from GetSalt function, got = %v, want = %v" , err , tt .wantErr )
353
+ }
354
+ }
355
+ })
356
+ }
357
+ }
0 commit comments