forked from InverterNetwork/contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathERC20Issuance_blacklist_v1.t.sol
911 lines (808 loc) · 32.6 KB
/
ERC20Issuance_blacklist_v1.t.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
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
215
216
217
218
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
// Internal
import {OZErrors} from "test/utils/errors/OZErrors.sol";
// Tests and Mocks
import {Test} from "forge-std/Test.sol";
import {ERC20Issuance_Blacklist_v1_Exposed} from
"test/external/ERC20Issuance_blacklist_v1_exposed.sol";
// System under testing
import {
ERC20Issuance_Blacklist_v1,
IERC20Issuance_Blacklist_v1
} from "@ex/token/ERC20Issuance_Blacklist_v1.sol";
/**
* @title ERC20Issuance_Blacklist_v1_Test
* @dev Test contract for ERC20Issuance_Blacklist_v1
* @author Zealynx Security
*/
contract ERC20Issuance_Blacklist_v1_Test is Test {
// ================================================================================
// Constants
uint constant BATCH_LIMIT = 200;
uint constant MAX_SUPPLY = type(uint).max - 1;
uint8 constant DECIMALS = 18;
string constant NAME = "Exposed Blacklist Token";
string constant SYMBOL = "EBLT";
// ================================================================================
// State
ERC20Issuance_Blacklist_v1_Exposed token;
// ================================================================================
// Setup
function setUp() public {
// Setup token
token = new ERC20Issuance_Blacklist_v1_Exposed(
NAME, SYMBOL, DECIMALS, MAX_SUPPLY, address(this), address(this)
);
}
// ================================================================================
// Test Init
function testInit() public {
assertEq(token.name(), NAME);
assertEq(token.symbol(), SYMBOL);
assertEq(token.decimals(), DECIMALS);
assertEq(token.cap(), MAX_SUPPLY);
assertEq(token.balanceOf(address(this)), 0);
assertEq(token.owner(), address(this));
assertEq(token.allowedMinters(address(this)), true);
assertEq(token.isBlacklistManager(address(this)), true);
}
// ================================================================================
// Test External (public + external)
// --------------------------------------------------------------------------------
// Feature: Authorization for Blacklist Modification
// Scenario: Verifying caller authorization for modifying the blacklist
/* Test: Function isBlacklisted()
├── Given the address is blacklisted
│ └── When the function isBlacklisted() is called
│ └── Then the function returns true
└── Given the address is not blacklisted
└── When the function isBlacklisted() is called
└── Then the function returns false
*/
function testIsBlacklisted_worksGivenAddressIsBlacklisted(
address toBeBlacklisted_
) public {
vm.assume(toBeBlacklisted_ != address(0));
// setup
_blacklistAddress(toBeBlacklisted_);
// assertion
assertTrue(
token.isBlacklisted(toBeBlacklisted_),
"Address should be blacklisted"
);
}
function testIsBlacklisted_worksGivenAddressIsNotBlacklisted() public {
// setup
address notBlacklisted = makeAddr("notBlacklisted");
// assertion
assertFalse(
token.isBlacklisted(notBlacklisted),
"Address should not be blacklisted"
);
}
/* Test: Function isBlacklistManager()
├── Given the caller is the blacklist manager
│ └── When the function isBlacklistManager() is called
│ └── Then the function should return true
└── Given the caller is not the blacklist manager
└── When the function isBlacklistManager() is called
└── Then the function should return false
*/
function testIsBlacklistManager_worksGivenAddressIsBlacklistManager(
address _toBeBlacklistManager
) public {
vm.assume(_toBeBlacklistManager != address(0));
// setup
token.setBlacklistManager(_toBeBlacklistManager, true);
// test
assertTrue(
token.isBlacklistManager(_toBeBlacklistManager),
"Address should be blacklist manager"
);
}
function testIsBlacklistManager_worksGivenAddressIsNotBlacklistManager()
public
{
// setup
address notBlacklistManager = makeAddr("notBlacklistManager");
// Assertion
assertFalse(
token.isBlacklistManager(notBlacklistManager),
"Address should not be blacklist manager"
);
}
// --------------------------------------------------------------------------------
// Feature: Individual Blacklist Address
// Scenario: Handling addition or removal of an address from the blacklist
/* Test: Function addToBlacklist()
├── Given the caller is not the blacklist manager
│ └── When the function addToBlacklist() is called
│ └── Then the function should revert (Modifier in place test)
└── Given the caller is a blacklist manager
├── And the address to blacklist is the zero address
│ └── When the function addToBlacklist() is called
│ └── Then the function should revert
└── And the address to blacklist is not the zero address
├── And the address is not blacklisted
│ └── When the function addToBlacklist() is called
│ └── Then it should emit an event
│ └── And it should add the address to the blacklist
└── And the address is blacklisted
└── When the function addToBlacklist() is called
└── Then no event should be emitted
└── And the address should remain blacklisted (idempotent)
*/
function testAddToBlacklist_worksGivenModifierInPlace(address unauthorized_)
public
{
// setup
vm.assume(unauthorized_ != address(0));
vm.assume(unauthorized_ != address(this));
// test modifier in place
vm.prank(unauthorized_);
vm.expectRevert(
IERC20Issuance_Blacklist_v1
.ERC20Issuance_Blacklist_NotBlacklistManager
.selector
);
token.addToBlacklist(unauthorized_);
}
function testAddToBlacklist_revertGivenAddressIsZeroAddress() public {
// setup
address zeroAddress = address(0);
// test
vm.expectRevert(
IERC20Issuance_Blacklist_v1
.ERC20Issuance_Blacklist_ZeroAddress
.selector
);
token.addToBlacklist(zeroAddress);
}
function testAddToBlacklist_worksGivenAddressIsNotBlacklisted(address user_)
public
{
// setup
vm.assume(user_ != address(0));
// test
vm.expectEmit(true, true, false, false);
emit IERC20Issuance_Blacklist_v1.AddedToBlacklist(user_, address(this));
token.addToBlacklist(user_);
// assertion
assertTrue(token.isBlacklisted(user_), "User should be blacklisted");
}
function testAddToBlacklist_revertGivenAddressAlreadyBlacklisted(
address user_
) public {
// setup
vm.assume(user_ != address(0));
// record logs
_blacklistAddress(user_);
// start recording logs
vm.recordLogs();
// assertion for pre condition
assertTrue(token.isBlacklisted(user_), "User should be blacklisted");
// test
token.addToBlacklist(user_);
// assertion
assertTrue(token.isBlacklisted(user_), "User should be blacklisted");
assertEq(vm.getRecordedLogs().length, 0, "There should be 0 log entry");
}
/* Test: Function removeFromBlacklist()
├── Given the caller is not the blacklist manager
│ └── When the function removeFromBlacklist() is called
│ └── Then the function should revert (Modifier in place test)
└── Given the caller is a blacklist manager
├── And the address is blacklisted
│ └── When the function removeFromBlacklist() is called
│ └── Then it should emit an event
│ └── And it should remove the address from the blacklist
└── And the address is not blacklisted
└── When the function removeFromBlacklist() is called
└── Then no event should be emitted
└── And the address should remain non-blacklisted (idempotent)
*/
function testRemoveFromBlacklist_worksGivenModifierInPlace(
address unauthorized_
) public {
// setup
vm.assume(unauthorized_ != address(this));
// test modifier in place
vm.prank(unauthorized_);
vm.expectRevert(
IERC20Issuance_Blacklist_v1
.ERC20Issuance_Blacklist_NotBlacklistManager
.selector
);
token.removeFromBlacklist(unauthorized_);
}
function testRemoveFromBlacklist_worksGivenAddressIsBlacklisted(
address user_
) public {
// Setup
vm.assume(user_ != address(0));
token.addToBlacklist(user_);
// Test
vm.expectEmit(true, true, false, false);
emit IERC20Issuance_Blacklist_v1.RemovedFromBlacklist(
user_, address(this)
);
token.removeFromBlacklist(user_);
// Assertion
assertFalse(
token.isBlacklisted(user_), "User should not be blacklisted"
);
}
function testRemoveFromBlacklist_worksGivenAddressIsNotBlacklisted(
address user_
) public {
// setup
vm.assume(user_ != address(0));
// start recording logs
vm.recordLogs();
// assertion for pre condition
assertFalse(
token.isBlacklisted(user_), "User should not be blacklisted"
);
// test
token.removeFromBlacklist(user_);
// assertion
assertFalse(
token.isBlacklisted(user_), "User should not be blacklisted"
);
assertEq(vm.getRecordedLogs().length, 0, "There should be 0 log entry");
}
// --------------------------------------------------------------------------------
// Feature: Batch Blacklist Address Management
// Scenario: Handling batch addition or removal of addresses from the blacklist
/* Test: Function addToBlacklistBatched()
├── Given the caller is not the blacklist manager
│ └── When the function addToBlacklistBatched() is called
│ └── Then the function should revert (Modifier in place test)
├── Given number of addresses is greater than BATCH_LIMIT
│ └── When addToBlacklistBatched() is called
│ └── Then it should revert
└── Given number of addresses is less than or equal to BATCH_LIMIT
├── And all addresses are not blacklisted
│ └── When addToBlacklistBatched() is called
│ └── Then it should emit an event for each address
│ └── And it should add each address to the blacklist
└── And the list contains not blacklisted and blacklisted addresses
└── Then it should add the addresses that are not blacklisted
└── And it should skip the addresses that are already blacklisted (idempotent)
*/
function testAddToBlacklistBatched_worksGivenModifierInPlace(
address unauthorized_
) public {
// setup
vm.assume(unauthorized_ != address(this));
address[] memory addresses = _generateAddresses(BATCH_LIMIT);
// test modifier in place
vm.prank(unauthorized_);
vm.expectRevert(
IERC20Issuance_Blacklist_v1
.ERC20Issuance_Blacklist_NotBlacklistManager
.selector
);
token.addToBlacklistBatched(addresses);
}
function testAddToBlacklistBatched_revertGivenBatchSizeExceedsLimit()
public
{
// Setup
address[] memory addresses = _generateAddresses(BATCH_LIMIT + 1);
// Test
vm.expectRevert(
abi.encodeWithSelector(
IERC20Issuance_Blacklist_v1
.ERC20Issuance_Blacklist_BatchLimitExceeded
.selector,
BATCH_LIMIT + 1,
BATCH_LIMIT
)
);
token.addToBlacklistBatched(addresses);
}
function testAddToBlacklistBatched_worksGivenAllAddressesAreNotBlacklisted(
uint numberOfAddresses_
) public {
// Setup
numberOfAddresses_ = bound(numberOfAddresses_, 2, BATCH_LIMIT);
address[] memory addresses = _generateAddresses(numberOfAddresses_);
// test
token.addToBlacklistBatched(addresses);
// assertion
for (uint i; i < addresses.length; ++i) {
assertTrue(
token.isBlacklisted(addresses[i]),
"Address should be blacklisted"
);
}
}
function testAddToBlacklistBatched_worksGivenSomeAddressesAlreadyBlacklisted(
uint numberOfAddresses_
) public {
// Setup
numberOfAddresses_ = bound(numberOfAddresses_, 2, BATCH_LIMIT);
address[] memory addresses = _generateAddresses(numberOfAddresses_);
// Get number of addresses to blacklist, given no address is blacklisted yet
uint numberOfBlacklistedAddresses = numberOfAddresses_ / 2;
// Blacklist subset of addresses
addresses =
_blacklistNumberOfAddresses(numberOfBlacklistedAddresses, addresses);
// Assert pre condition
for (uint i; i < numberOfBlacklistedAddresses; ++i) {
assertTrue(
token.isBlacklisted(addresses[i]),
"Address should be blacklisted"
);
}
// Test
token.addToBlacklistBatched(addresses);
// assertion
for (uint i; i < addresses.length; ++i) {
assertTrue(
token.isBlacklisted(addresses[i]),
"Address should be blacklisted"
);
}
}
/* Test: Function removeFromBlacklistBatched()
├── Given the caller is not the blacklist manager
│ └── When the function removeFromBlacklistBatched() is called
│ └── Then the function should revert (Modifier in place test)
├── Given number of addresses is greater than BATCH_LIMIT
│ └── When removeFromBlacklistBatched() is called
│ └── Then it should revert
└── Given number of addresses is less than or equal to BATCH_LIMIT
├── And all addresses are blacklisted
│ └── When removeFromBlacklistBatched() is called
│ └── Then it should emit an event for each address
│ └── And it should remove each address from the blacklist
└── And the list contains not blacklisted and blacklisted addresses
├── Then it should remove the addresses that are blacklisted
└── And it should skip the addresses that are already non-blacklisted (idempotent)
*/
function testRemoveFromBlacklistBatched_worksGivenModifierInPlace(
address unauthorized_
) public {
// setup
vm.assume(unauthorized_ != address(this));
address[] memory addresses = _generateAddresses(BATCH_LIMIT);
// test modifier in place
vm.prank(unauthorized_);
vm.expectRevert(
IERC20Issuance_Blacklist_v1
.ERC20Issuance_Blacklist_NotBlacklistManager
.selector
);
token.removeFromBlacklistBatched(addresses);
}
function testRemoveFromBlacklistBatched_revertGivenBatchSizeExceedsLimit()
public
{
// Setup
address[] memory addresses = _generateAddresses(BATCH_LIMIT + 1);
// Test
vm.expectRevert(
abi.encodeWithSelector(
IERC20Issuance_Blacklist_v1
.ERC20Issuance_Blacklist_BatchLimitExceeded
.selector,
BATCH_LIMIT + 1,
BATCH_LIMIT
)
);
token.removeFromBlacklistBatched(addresses);
}
function testRemoveFromBlacklistBatched_worksGivenAllAddressesAreBlacklisted(
uint numberOfAddresses_
) public {
// Setup
numberOfAddresses_ = bound(numberOfAddresses_, 2, BATCH_LIMIT);
address[] memory addresses = _generateAddresses(numberOfAddresses_);
token.addToBlacklistBatched(addresses);
// Assert pre condition
for (uint i; i < addresses.length; ++i) {
assertTrue(
token.isBlacklisted(addresses[i]),
"Address should be blacklisted"
);
}
// test
token.removeFromBlacklistBatched(addresses);
// assertion
for (uint i; i < addresses.length; ++i) {
assertFalse(
token.isBlacklisted(addresses[i]),
"Address should not be blacklisted"
);
}
}
function testRemoveFromBlacklistBatched_worksGivenSomeAddressesNotBlacklisted(
uint numberOfAddresses_
) public {
// Setup
numberOfAddresses_ = bound(numberOfAddresses_, 2, BATCH_LIMIT);
address[] memory addresses = _generateAddresses(numberOfAddresses_);
// Get number of addresses to blacklist, given no address is blacklisted yet
uint numberOfBlacklistedAddresses = numberOfAddresses_ / 2;
// Blacklist subset of addresses
addresses =
_blacklistNumberOfAddresses(numberOfBlacklistedAddresses, addresses);
// Assert pre condition
uint i = numberOfBlacklistedAddresses; // start from the first non-blacklisted address
for (i; i < numberOfAddresses_; ++i) {
assertFalse(
token.isBlacklisted(addresses[i]),
"Address should be not be blacklisted"
);
++i;
}
// Test
token.removeFromBlacklistBatched(addresses);
// assertion
for (uint j; j < addresses.length; ++j) {
assertFalse(
token.isBlacklisted(addresses[j]),
"Address should be not be blacklisted"
);
}
}
/* Test: Function setBlacklistManager()
├── Given the caller is not the owner
│ └── When the function setBlacklistManager() is called
│ └── Then the function should revert (Modifier in place test)
└── Given the caller is the owner
└── When the function setBlacklistManager() is called
└── Then it should update the blacklist manager status (conditions tested in internal function)
└── And it should emit an event
*/
function testSetBlacklistManager_worksGivenModifierInPlace(
address unauthorized_
) public {
// setup
vm.assume(unauthorized_ != address(this));
vm.prank(unauthorized_);
vm.expectRevert(
abi.encodeWithSelector(
OZErrors.Ownable__UnauthorizedAccount, unauthorized_
)
);
token.setBlacklistManager(unauthorized_, true);
}
function testSetBlacklistManager_worksGivenDirectsToInternalFunction(
address newManager_,
bool allowed_
) public {
// setup
vm.assume(newManager_ != address(0));
// set blacklist manager if we want to revoke the rights
if (false) {
token.setBlacklistManager(newManager_, allowed_);
}
// Test
vm.expectEmit(true, true, true, true);
emit IERC20Issuance_Blacklist_v1.BlacklistManagerUpdated(
newManager_, allowed_, address(this)
);
token.setBlacklistManager(newManager_, allowed_);
// Assertion
assertEq(token.isBlacklistManager(newManager_), allowed_);
}
// ================================================================================
// Test Internal
// --------------------------------------------------------------------------------
// Feature: Authorization for Blacklist Modification
// Scenario: Restricting setting blacklist manager to token owner
/* Test: Function _setBlacklistManager()
├── Given the new blacklist manager is the zero address
│ └── When the function _setBlacklistManager() is called
│ └── Then it should revert
└── Given the address is not the zero address
├── And the address should be approved as a blacklist manager
│ └── And the address is not yet approved as a blacklist manager
│ └── When the function setBlacklistManager() is called
│ └── Then it should set the new blacklist manager address
│ └── And it should emit an event
├── And the address should be approved as a blacklist manager
│ └── But the address is already approved as a blacklist manager
│ └── When the function setBlacklistManager() is called
│ └── Then the address should remain approved as a blacklist manager
│ └── And it should emit an event
├── And the address should be revoked as a blacklist manager
│ └── And the address is approved as a blacklist manager
│ └── When the function setBlacklistManager() is called
│ └── Then the address should be revoked as a blacklist manager
│ └── And it should emit an event
└── And the address should be revoked as a blacklist manager
└── But the address is not approved as a blacklist manager
└── When the function setBlacklistManager() is called
└── Then the address should remain revoked as a blacklist manager
└── And it should emit an event
*/
function testInternalSetBlacklistManager_revertGivenAddressIsZero()
public
{
vm.expectRevert(
abi.encodeWithSelector(
IERC20Issuance_Blacklist_v1
.ERC20Issuance_Blacklist_ZeroAddress
.selector
)
);
token.setBlacklistManager(address(0), true);
}
function testInternalSetBlacklistManager_worksGivenAddressNotYetApproved(
address newManager_
) public {
// Setup
vm.assume(newManager_ != address(0));
// Grant the rights
bool allowed_ = true;
// Test
vm.expectEmit(true, true, true, true);
emit IERC20Issuance_Blacklist_v1.BlacklistManagerUpdated(
newManager_, allowed_, address(this)
);
token.setBlacklistManager(newManager_, allowed_);
// Assertion
assertTrue(
token.isBlacklistManager(newManager_),
"Address should be approved as a blacklist manager"
);
}
function testInternalSetBlacklistManager_worksGivenAddressIsAlreadyApproved(
address newManager_
) public {
// Setup
vm.assume(newManager_ != address(0));
// Grant the rights
bool allowed_ = true;
token.setBlacklistManager(newManager_, true);
// Test
vm.expectEmit(true, true, true, true);
emit IERC20Issuance_Blacklist_v1.BlacklistManagerUpdated(
newManager_, allowed_, address(this)
);
token.setBlacklistManager(newManager_, allowed_);
// Assertion
assertTrue(
token.isBlacklistManager(newManager_),
"Address should be approved as a blacklist manager"
);
}
function testInternalSetBlacklistManager_worksGivenAddressNotYetRevoked(
address newManager_
) public {
// Setup
vm.assume(newManager_ != address(0));
// Revoke the rights
bool allowed_ = false;
token.setBlacklistManager(newManager_, true);
// Test
vm.expectEmit(true, true, true, true);
emit IERC20Issuance_Blacklist_v1.BlacklistManagerUpdated(
newManager_, allowed_, address(this)
);
token.setBlacklistManager(newManager_, allowed_);
// Assertion
assertFalse(
token.isBlacklistManager(newManager_),
"Address should not be approved as a blacklist manager"
);
}
function testInternalSetBlacklistManager_worksGivenAddressIsAlreadyRevoked(
address newManager_
) public {
// Setup
vm.assume(newManager_ != address(0));
bool allowed_ = false;
// Test
vm.expectEmit(true, true, true, true);
emit IERC20Issuance_Blacklist_v1.BlacklistManagerUpdated(
newManager_, allowed_, address(this)
);
token.setBlacklistManager(newManager_, allowed_);
// Assertion
assertFalse(
token.isBlacklistManager(newManager_),
"Address should not be approved as a blacklist manager"
);
}
// --------------------------------------------------------------------------------
// Feature: Blacklist-Restricted Actions
// Scenario: Restricting USP actions based on blacklist status
/* Test: Function _update()
├── Given the `from` address is blacklisted
│ └── And the `to` address is not blacklisted
│ └── When the function _update() is called
│ └── Then it should revert
├── Given the `from` address is blacklisted
│ └── And the `to` address is blacklisted
│ └── When the function _update() is called
│ └── Then it should revert
├── Given the `from` address is not blacklisted
│ └── And the `to` address is blacklisted
│ └── When the function _update() is called
│ └── Then it should revert
└── Given the `from` address is not blacklisted
└── And the `to` address is not blacklisted
└── When the function _update() is called
└── Then it should transfer the tokens
*/
function testInternalUpdate_revertGivenFromAddressIsBlacklistedAndToAddressIsNotBlacklisted(
address from_,
address to_,
uint amount_
) public {
// setup
vm.assume(from_ != address(0) && to_ != address(0));
vm.assume(from_ != to_);
amount_ = bound(amount_, 1, uint(type(uint).max - 1));
_fundAddress(from_, amount_);
_blacklistAddress(from_);
// pre-condition
assertTrue(
token.isBlacklisted(from_), "from_ address should be blacklisted"
);
assertFalse(
token.isBlacklisted(to_), "to_ address should not be blacklisted"
);
// test
vm.expectRevert(
abi.encodeWithSelector(
IERC20Issuance_Blacklist_v1
.ERC20Issuance_Blacklist_BlacklistedAddress
.selector,
from_
)
);
token.exposed_update(from_, to_, amount_);
}
function testInternalUpdate_revertGivenFromAndToAddressIsBlacklisted(
address from_,
address to_,
uint amount_
) public {
// setup
vm.assume(from_ != address(0) && to_ != address(0));
vm.assume(from_ != to_);
amount_ = bound(amount_, 1, uint(type(uint).max - 1));
_fundAddress(from_, amount_);
_blacklistAddress(from_);
_blacklistAddress(to_);
// pre-condition
assertTrue(
token.isBlacklisted(from_), "from_ address should be blacklisted"
);
assertTrue(
token.isBlacklisted(to_), "to_ address should be blacklisted"
);
// test
vm.expectRevert(
abi.encodeWithSelector(
IERC20Issuance_Blacklist_v1
.ERC20Issuance_Blacklist_BlacklistedAddress
.selector,
from_
)
);
token.exposed_update(from_, to_, amount_);
}
function testInternalUpdate_revertGivenFromAddressIsNotBlacklistedAndToAddressIsBlacklisted(
address from_,
address to_,
uint amount_
) public {
// setup
vm.assume(from_ != address(0) && to_ != address(0));
vm.assume(from_ != to_);
amount_ = bound(amount_, 1, uint(type(uint).max - 1));
_fundAddress(from_, amount_);
_blacklistAddress(to_);
// pre-condition
assertFalse(
token.isBlacklisted(from_),
"from_ address should not be blacklisted"
);
assertTrue(
token.isBlacklisted(to_), "to_ address should be blacklisted"
);
// test
vm.expectRevert(
abi.encodeWithSelector(
IERC20Issuance_Blacklist_v1
.ERC20Issuance_Blacklist_BlacklistedAddress
.selector,
to_
)
);
token.exposed_update(from_, to_, amount_);
}
function testInternalUpdate_revertGivenFromAndToAddressAreNotBlacklisted(
address from_,
address to_,
uint amount_
) public {
// setup
vm.assume(from_ != address(0) && to_ != address(0));
vm.assume(from_ != to_);
amount_ = bound(amount_, 1, uint(type(uint).max - 1));
_fundAddress(from_, amount_);
// pre-condition
assertFalse(
token.isBlacklisted(from_),
"from_ address should not be blacklisted"
);
assertFalse(
token.isBlacklisted(to_), "to_ address should not be blacklisted"
);
assertEq(token.balanceOf(to_), 0, "to_ has no balance");
// test
token.exposed_update(from_, to_, amount_);
// assertion
assertEq(token.balanceOf(to_), amount_, "to_ has correct balance");
}
/* Test: Function _isBlacklistManager()
├── Given the address is a blacklist manager
│ └── When the function _isBlacklistManager() is called
│ └── Then it should return true
└── Given the address is not a blacklist manager
└── When the function _isBlacklistManager() is called
└── Then it should return false
*/
function testInternalIsBlacklistManager_worksGivenAddressIsBlacklistManager(
address _user
) public {
// setup
vm.assume(_user != address(0));
// set blacklist manager
token.setBlacklistManager(_user, true);
// Assert
assertTrue(
token.isBlacklistManager(_user),
"Address should be a blacklist manager"
);
}
function testInternalIsBlacklistManager_worksGivenAddressNotBlacklistManager(
address _user
) public {
// setup
vm.assume(_user != address(0));
// set blacklist manager
token.setBlacklistManager(_user, false);
// Assert
assertFalse(
token.isBlacklistManager(_user),
"Address should not be a blacklist manager"
);
}
// ================================================================================
// Helper Functions
function _blacklistNumberOfAddresses(
uint numberOfAddresses_,
address[] memory addresses_
) internal returns (address[] memory) {
for (uint i; i < numberOfAddresses_; ++i) {
_blacklistAddress(addresses_[i]);
}
return addresses_;
}
function _blacklistAddress(address toBeBlacklisted_) internal {
token.addToBlacklist(toBeBlacklisted_);
}
function _fundAddress(address toBeFunded_, uint amount_) internal {
token.mint(toBeFunded_, amount_);
}
function _generateAddresses(uint count_)
internal
returns (address[] memory)
{
address[] memory addresses = new address[](count_);
for (uint i; i < count_; ++i) {
addresses[i] = makeAddr(string.concat("user", vm.toString(i)));
}
return addresses;
}
}