//! This module tests the library's analysis capabilities on the
//! `SaleClockAuction` contract`.
#![cfg(test)]

use storage_layout_extractor::{
    tc::abi::{AbiType, StructElement},
    watchdog::LazyWatchdog,
};

mod common;

/// Tests the library on the bytecode of the SaleClockAuction contract
/// deployed [here](https://etherscan.io/address/0xb1690C08E213a35Ed9bAb7B318DE14420FB57d8C).
#[test]
fn correctly_generates_a_layout() -> anyhow::Result<()> {
    // Create the extractor
    let bytecode = "0x6060604052600436106100e25763ffffffff60e060020a60003504166327ebe40a81146100e75780633f4ba83a14610114578063454a2ab31461013b578063484eccb4146101465780635c975abb1461016e5780635fd8c7101461018157806378bd79351461019457806383b5ff8b146101e55780638456cb59146101f857806385b861881461020b578063878eb3681461021e5780638a98a9cc146102345780638da5cb5b1461024757806396b5a75514610276578063c55d0f561461028c578063dd1b7a0f146102a2578063eac9d94c146102b5578063f2fde38b146102c8575b600080fd5b34156100f257600080fd5b610112600435602435604435606435600160a060020a03608435166102e7565b005b341561011f57600080fd5b6101276103bf565b604051901515815260200160405180910390f35b610112600435610443565b341561015157600080fd5b61015c6004356104ad565b60405190815260200160405180910390f35b341561017957600080fd5b6101276104c1565b341561018c57600080fd5b6101126104d1565b341561019f57600080fd5b6101aa600435610547565b604051600160a060020a03909516855260208501939093526040808501929092526060840152608083019190915260a0909101905180910390f35b34156101f057600080fd5b61015c6105d4565b341561020357600080fd5b6101276105da565b341561021657600080fd5b610127610663565b341561022957600080fd5b61011260043561066c565b341561023f57600080fd5b61015c6106dd565b341561025257600080fd5b61025a6106e3565b604051600160a060020a03909116815260200160405180910390f35b341561028157600080fd5b6101126004356106f2565b341561029757600080fd5b61015c60043561073b565b34156102ad57600080fd5b61025a61076d565b34156102c057600080fd5b61015c61077c565b34156102d357600080fd5b610112600160a060020a03600435166107b0565b6102ef610d2b565b6001608060020a038516851461030457600080fd5b6001608060020a038416841461031957600080fd5b67ffffffffffffffff8316831461032f57600080fd5b60015433600160a060020a0390811691161461034a57600080fd5b6103548287610806565b60a06040519081016040528083600160a060020a03168152602001866001608060020a03168152602001856001608060020a031681526020018467ffffffffffffffff1681526020014267ffffffffffffffff1681525090506103b78682610881565b505050505050565b6000805433600160a060020a039081169116146103db57600080fd5b60005460a060020a900460ff1615156103f357600080fd5b6000805474ff0000000000000000000000000000000000000000191690557f7805862f689e2f13df9f062ff482ad3ad112aca9e0847911ed832e158c525b3360405160405180910390a150600190565b600081815260036020526040812054600160a060020a0316906104668334610a1c565b90506104723384610b4d565b600154600160a060020a03838116911614156104a857600580548291600691066005811061049c57fe5b01556005805460010190555b505050565b600681600581106104ba57fe5b0154905081565b60005460a060020a900460ff1681565b60015460008054600160a060020a039283169233811691161480610506575081600160a060020a031633600160a060020a0316145b151561051157600080fd5b81600160a060020a03166108fc30600160a060020a0316319081150290604051600060405180830381858888f150505050505050565b6000818152600360205260408120819081908190819061056681610ba3565b151561057157600080fd5b80546001820154600290920154600160a060020a03909116986001608060020a038084169950700100000000000000000000000000000000909304909216965067ffffffffffffffff808216965068010000000000000000909104169350915050565b60025481565b6000805433600160a060020a039081169116146105f657600080fd5b60005460a060020a900460ff161561060d57600080fd5b6000805474ff0000000000000000000000000000000000000000191660a060020a1790557f6985a02210a168e66602d3235cb6db0e70f92b3ba4d376a33c0f3d9434bff62560405160405180910390a150600190565b60045460ff1681565b6000805460a060020a900460ff16151561068557600080fd5b60005433600160a060020a039081169116146106a057600080fd5b5060008181526003602052604090206106b881610ba3565b15156106c357600080fd5b80546106d9908390600160a060020a0316610bc4565b5050565b60055481565b600054600160a060020a031681565b60008181526003602052604081209061070a82610ba3565b151561071557600080fd5b508054600160a060020a03908116903316811461073157600080fd5b6104a88382610bc4565b600081815260036020526040812061075281610ba3565b151561075d57600080fd5b61076681610c0e565b9392505050565b600154600160a060020a031681565b600080805b60058110156107a6576006816005811061079757fe5b01549190910190600101610781565b5060059004919050565b60005433600160a060020a039081169116146107cb57600080fd5b600160a060020a03811615610803576000805473ffffffffffffffffffffffffffffffffffffffff1916600160a060020a0383161790555b50565b600154600160a060020a03166323b872dd83308460405160e060020a63ffffffff8616028152600160a060020a0393841660048201529190921660248201526044810191909152606401600060405180830381600087803b151561086957600080fd5b6102c65a03f1151561087a57600080fd5b5050505050565b603c816060015167ffffffffffffffff16101561089d57600080fd5b600082815260036020526040902081908151815473ffffffffffffffffffffffffffffffffffffffff1916600160a060020a039190911617815560208201516001820180546fffffffffffffffffffffffffffffffff19166001608060020a039290921691909117905560408201516001820180546001608060020a03928316700100000000000000000000000000000000029216919091179055606082015160028201805467ffffffffffffffff191667ffffffffffffffff9290921691909117905560808201516002909101805467ffffffffffffffff9290921668010000000000000000026fffffffffffffffff000000000000000019909216919091179055507fa9c8dfcda5664a5a124c713e386da27de87432d5b668e79458501eb296389ba78260208301516001608060020a031683604001516001608060020a0316846060015167ffffffffffffffff166040518085815260200184815260200183815260200182815260200194505050505060405180910390a15050565b60008281526003602052604081208180808080610a3886610ba3565b1515610a4357600080fd5b610a4c86610c0e565b945084881015610a5b57600080fd5b8554600160a060020a03169350610a7189610c95565b6000851115610abb57610a8385610ce2565b92508285039150600160a060020a03841682156108fc0283604051600060405180830381858888f193505050501515610abb57600080fd5b50838703600160a060020a03331681156108fc0282604051600060405180830381858888f193505050501515610af057600080fd5b7f4fcc30d90a842164dd58501ab874a101a3749c3d4747139cefe7c876f4ccebd28986336040519283526020830191909152600160a060020a03166040808301919091526060909101905180910390a15092979650505050505050565b600154600160a060020a031663a9059cbb838360405160e060020a63ffffffff8516028152600160a060020a0390921660048301526024820152604401600060405180830381600087803b151561086957600080fd5b6002015460006801000000000000000090910467ffffffffffffffff161190565b610bcd82610c95565b610bd78183610b4d565b7f2809c7e17bf978fbc7194c0a694b638c4215e9140cacc6c38ca36010b45697df8260405190815260200160405180910390a15050565b6002810154600090819068010000000000000000900467ffffffffffffffff16421115610c545750600282015468010000000000000000900467ffffffffffffffff1642035b60018301546002840154610766916001608060020a0380821692700100000000000000000000000000000000909204169067ffffffffffffffff1684610cee565b6000908152600360205260408120805473ffffffffffffffffffffffffffffffffffffffff19168155600181019190915560020180546fffffffffffffffffffffffffffffffff19169055565b60025461271091020490565b6000808080858510610d0257869350610d20565b878703925085858402811515610d1457fe5b05915081880190508093505b505050949350505050565b60a06040519081016040908152600080835260208301819052908201819052606082018190526080820152905600a165627a7a72305820666ae68d08f6b7766b21d1f7ba24d5315f1eb94a6e0b2af3961e798edfbb27de0029";
    let extractor = common::new_extractor_from_bytecode(bytecode, LazyWatchdog.in_rc())?;

    // Get the final storage layout for the input contract
    let layout = extractor.analyze()?;

    // We should see 8 entries, but we only see 6 due to a lack of discovery of
    // fixed-length arrays, and a missing packed element
    assert_eq!(layout.slot_count(), 7);

    // `address`, but we infer `number160`
    assert!(layout.has_slot(0, 0, AbiType::Number { size: Some(160) }));

    // `bool`, packed
    assert!(layout.has_slot(0, 160, AbiType::Number { size: Some(8) }));

    // `address`
    assert!(layout.has_slot(1, 0, AbiType::Address));

    // `uint256` but we infer `numberUnknown`
    assert!(layout.has_slot(2, 0, AbiType::Number { size: None }));

    // `mapping(uint256 => struct(address, uint128, uint128, uint64, uint64)` but we
    // infer `mapping(bytes32 => struct(address, uint128, uint128, conflict,
    // uint64)`
    assert!(layout.has_slot(
        3,
        0,
        AbiType::Mapping {
            key_type:   Box::new(AbiType::Bytes { length: Some(32) }),
            value_type: Box::new(AbiType::Struct {
                elements: vec![
                    StructElement::new(0, AbiType::Address),
                    StructElement::new(256, AbiType::Number { size: Some(128) }),
                    StructElement::new(384, AbiType::UInt { size: Some(128) }),
                    StructElement::new(512, AbiType::conflict()),
                    StructElement::new(576, AbiType::UInt { size: Some(64) })
                ],
            }),
        }
    ));

    // `bool` but we infer `number8`
    assert!(layout.has_slot(4, 0, AbiType::Number { size: Some(8) }));

    // `uint256` but we infer `uintUnknown`
    assert!(layout.has_slot(5, 0, AbiType::UInt { size: None }));

    Ok(())
}
