auth.t.sol 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // This program is free software: you can redistribute it and/or modify
  2. // it under the terms of the GNU General Public License as published by
  3. // the Free Software Foundation, either version 3 of the License, or
  4. // (at your option) any later version.
  5. // This program is distributed in the hope that it will be useful,
  6. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. // GNU General Public License for more details.
  9. // You should have received a copy of the GNU General Public License
  10. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  11. pragma solidity >=0.4.23;
  12. import "ds-test/test.sol";
  13. import "./auth.sol";
  14. contract FakeVault is DSAuth {
  15. function access() public view auth {}
  16. }
  17. contract BooleanAuthority {
  18. bool yes;
  19. constructor(bool _yes) public {
  20. yes = _yes;
  21. }
  22. function canCall(
  23. address src, address dst, bytes4 sig
  24. ) public view returns (bool) {
  25. src; dst; sig; // silence warnings
  26. return yes;
  27. }
  28. }
  29. contract DSAuthTest is DSTest, DSAuthEvents {
  30. FakeVault vault;
  31. function setUp() public {
  32. vault = new FakeVault();
  33. }
  34. function test_owner() public {
  35. expectEventsExact(address(vault));
  36. vault.access();
  37. vault.setOwner(address(0));
  38. emit LogSetOwner(address(0));
  39. }
  40. function testFail_non_owner_1() public {
  41. vault.setOwner(address(0));
  42. vault.access();
  43. }
  44. function testFail_non_owner_2() public {
  45. vault.setOwner(address(0));
  46. vault.setOwner(address(0));
  47. }
  48. function test_accepting_authority() public {
  49. //hacky recasting to keep compatibility with <solc0.6.0
  50. vault.setAuthority(DSAuthority(address(new BooleanAuthority(true))));
  51. vault.setOwner(address(0));
  52. vault.access();
  53. }
  54. function testFail_rejecting_authority_1() public {
  55. vault.setAuthority(DSAuthority(address(new BooleanAuthority(false))));
  56. vault.setOwner(address(0));
  57. vault.access();
  58. }
  59. function testFail_rejecting_authority_2() public {
  60. vault.setAuthority(DSAuthority(address(new BooleanAuthority(false))));
  61. vault.setOwner(address(0));
  62. vault.setOwner(address(0));
  63. }
  64. }