Skip to content

Instantly share code, notes, and snippets.

@DonGuillotine
Last active October 26, 2024 15:54
Show Gist options
  • Save DonGuillotine/fcd10c5af311ef908d53bfe969d3968b to your computer and use it in GitHub Desktop.
Save DonGuillotine/fcd10c5af311ef908d53bfe969d3968b to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: SEE LICENSE IN LICENSE
pragma solidity ^0.8.13;

import "../src/Challenge/Challenge2.sol";
import {Test} from "forge-std/Test.sol";

contract Challenge2Test is Test {
    ChallengeTwo challengeContract;
    address admin = makeAddr('admin');
    address tester = makeAddr('tester');

    // Calculate storage slots for private mappings
    bytes32 constant LEVEL1_SLOT = 0; // First mapping slot
    bytes32 constant LEVEL2_SLOT = bytes32(uint256(1));

    function setUp() public {
        vm.createSelectFork("https://rpc2.sepolia.org");
        vm.startPrank(admin);
        challengeContract = new ChallengeTwo();
        vm.stopPrank();
    }

    // Helper function to set level1 mapping value
    function setLevel1(address _user, bool value) internal {
        bytes32 slot = keccak256(abi.encode(_user, LEVEL1_SLOT));
        vm.store(address(challengeContract), slot, bytes32(uint256(value ? 1 : 0)));
    }

    // Helper function to read level1 mapping value
    function getLevel1(address _user) internal view returns (bool) {
        bytes32 slot = keccak256(abi.encode(_user, LEVEL1_SLOT));
        return vm.load(address(challengeContract), slot) != bytes32(0);
    }

    // Helper function to read level2 mapping value
    function getLevel2(address _user) internal view returns (bool) {
        bytes32 slot = keccak256(abi.encode(_user, LEVEL2_SLOT));
        return vm.load(address(challengeContract), slot) != bytes32(0);
    }

    function test_GetEnoughPoint_without_solving_level1() public {
        vm.startPrank(tester);

        vm.expectRevert("go back and complete level 1");
        challengeContract.getENoughPoint("Levi");

        vm.stopPrank();
    }

    function test_GetEnoughPoint_already_solved() public {
        vm.startPrank(tester);
        
        // Set level1 to true and level2 to true
        setLevel1(tester, true);

        bytes32 slot = keccak256(abi.encode(tester, LEVEL2_SLOT));
        vm.store(address(challengeContract), slot, bytes32(uint256(1)));

        vm.expectRevert("already solved");
        challengeContract.getENoughPoint("Levi");

        vm.stopPrank();
    }

    function testPassKey_with_correct_key() public {
        // The correct key is 42069
        vm.startPrank(tester);
        
        // Pass the correct key
        challengeContract.passKey(uint16(2524));

        // After state check
        assertTrue(getLevel1(tx.origin));
        
        vm.stopPrank();
    }

    function testPassKey_failure() public {
        uint16 incorrectKey = 4321; // An arbitrary incorrect key

        // Start acting as the test user
        vm.startPrank(tester);

        // Expect revert with message "invalid key"
        vm.expectRevert("invalid key");
        challengeContract.passKey(incorrectKey);

        // Stop the prank
        vm.stopPrank();
    }
}
// SPDX-License-Identifier: SEE LICENSE IN LICENSE
pragma solidity ^0.8.13;

import "../src/Challenge/Challenge2.sol";
import {Test} from "forge-std/Test.sol";

contract Challenge2Test is Test {
    ChallengeTwo challengeContract;
    address admin = makeAddr('admin');
    address tester = makeAddr('tester');

    // Calculate storage slots for private mappings
    bytes32 constant LEVEL1_SLOT = 0; // First mapping slot
    bytes32 constant LEVEL2_SLOT = bytes32(uint256(1));

    function setUp() public {
        vm.createSelectFork("https://rpc2.sepolia.org");
        vm.startPrank(admin);
        challengeContract = new ChallengeTwo();
        vm.stopPrank();
    }

    // Helper function to set level1 mapping value
    function setLevel1(address _user, bool value) internal {
        bytes32 slot = keccak256(abi.encode(_user, LEVEL1_SLOT));
        vm.store(address(challengeContract), slot, bytes32(uint256(value ? 1 : 0)));
    }

    // Helper function to read level1 mapping value
    function getLevel1(address _user) internal view returns (bool) {
        bytes32 slot = keccak256(abi.encode(_user, LEVEL1_SLOT));
        return vm.load(address(challengeContract), slot) != bytes32(0);
    }

    // Helper function to read level2 mapping value
    function getLevel2(address _user) internal view returns (bool) {
        bytes32 slot = keccak256(abi.encode(_user, LEVEL2_SLOT));
        return vm.load(address(challengeContract), slot) != bytes32(0);
    }

    function test_GetEnoughPoint_without_solving_level1() public {
        vm.startPrank(tester);

        vm.expectRevert("go back and complete level 1");
        challengeContract.getENoughPoint("Levi");

        vm.stopPrank();
    }

    function test_GetEnoughPoint_already_solved() public {
        vm.startPrank(tester);
        
        setLevel1(tester, true);

        bytes32 slot = keccak256(abi.encode(tester, LEVEL2_SLOT));
        vm.store(address(challengeContract), slot, bytes32(uint256(1)));

        vm.expectRevert("already solved");
        challengeContract.getENoughPoint("Levi");

        vm.stopPrank();
    }

    function testPassKey_with_correct_key() public {
        vm.startPrank(tester);
        
        challengeContract.passKey(uint16(2524));

        assertTrue(getLevel1(tx.origin));
        
        vm.stopPrank();
    }

    function testPassKey_failure() public {
        uint16 incorrectKey = 4321;

        vm.startPrank(tester);

        vm.expectRevert("invalid key");
        challengeContract.passKey(incorrectKey);

        vm.stopPrank();
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment