Skip to content

Commit

Permalink
test: separate mocks for unstructured storage tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sohkai committed Aug 27, 2018
1 parent 719ff7f commit 816e166
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ contract KernelPinnedStorageMock is Kernel, FakeAppConstants {
// Testing this contract is a bit of a pain... we can't overload anything to make the contract check
// pass in the constructor, so we're forced to initialize this with a mocked Kernel that already
// sets a contract for the fake app.
contract AppStubPinnedStorage is AppProxyPinned, FakeAppConstants {
contract AppProxyPinnedStorageMock is AppProxyPinned, FakeAppConstants {
constructor(KernelPinnedStorageMock _mockKernel)
AppProxyPinned(IKernel(_mockKernel), FAKE_APP_ID, new bytes(0))
public // solium-disable-line visibility-first
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
pragma solidity 0.4.24;

import "../../apps/UnsafeAragonApp.sol";
import "../../apps/AppStorage.sol";


// Need to use UnsafeAragonApp to allow initialization
contract AppStubStorage is UnsafeAragonApp {
function initialize() onlyInit public {
initialized();
}

contract AppStorageMock is AppStorage {
function setKernelExt(IKernel _kernel) public {
setKernel(_kernel);
}
Expand All @@ -24,8 +19,4 @@ contract AppStubStorage is UnsafeAragonApp {
function getAppIdPosition() public pure returns (bytes32) {
return APP_ID_POSITION;
}

function getInitializationBlockPosition() public pure returns (bytes32) {
return INITIALIZATION_BLOCK_POSITION;
}
}
14 changes: 14 additions & 0 deletions contracts/test/mocks/InitializableStorageMock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pragma solidity 0.4.24;

import "../../common/Initializable.sol";


contract InitializableStorageMock is Initializable {
function initialize() onlyInit public {
initialized();
}

function getInitializationBlockPosition() public pure returns (bytes32) {
return INITIALIZATION_BLOCK_POSITION;
}
}
16 changes: 10 additions & 6 deletions test/keccak_constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,23 @@ contract('Constants', accounts => {
})

it('checks AppStorage unstructured storage constants', async () => {
const app = await getContract('AppStubStorage').new()
const appStorage = await getContract('AppStorageMock').new()

assert.equal(await app.getKernelPosition(), await keccakConstants.kernelPosition(), "kernelPosition doesn't match")
assert.equal(await app.getAppIdPosition(), await keccakConstants.appIdPosition(), "appIdPosition doesn't match")
assert.equal(await app.getInitializationBlockPosition(), await keccakConstants.initializationBlockPosition(), "initializationBlockPosition doesn't match")
assert.equal(await appStorage.getKernelPosition(), await keccakConstants.kernelPosition(), "kernelPosition doesn't match")
assert.equal(await appStorage.getAppIdPosition(), await keccakConstants.appIdPosition(), "appIdPosition doesn't match")
})

it('checks AppProxyPinned unstructured storage constants', async () => {
// Set up AppStubPinnedStorage
const fakeApp = await getContract('AppStub').new()
const kernelMock = await getContract('KernelPinnedStorageMock').new(fakeApp.address)
const app = await getContract('AppStubPinnedStorage').new(kernelMock.address)
const pinnedProxy = await getContract('AppProxyPinnedStorageMock').new(kernelMock.address)

assert.equal(await app.getPinnedCodePosition(), await keccakConstants.pinnedCodePosition(), "pinnedCodePosition doesn't match")
assert.equal(await pinnedProxy.getPinnedCodePosition(), await keccakConstants.pinnedCodePosition(), "pinnedCodePosition doesn't match")
})

it('checks Initializable unstructured storage constants', async () => {
const initializableMock = await getContract('InitializableStorageMock').new()
assert.equal(await initializableMock.getInitializationBlockPosition(), await keccakConstants.initializationBlockPosition(), "initializationBlockPosition doesn't match")
})
})
164 changes: 97 additions & 67 deletions test/unstructured_storage.js
Original file line number Diff line number Diff line change
@@ -1,85 +1,115 @@
const AppStub = artifacts.require('AppStub')
const AppStubStorage = artifacts.require('AppStubStorage')
const AppStubPinnedStorage = artifacts.require('AppStubPinnedStorage')
const Kernel = artifacts.require('Kernel')

// Mocks
const AppStorageMock = artifacts.require('AppStorageMock')
const AppProxyPinnedStorageMock = artifacts.require('AppProxyPinnedStorageMock')
const InitializableStorageMock = artifacts.require('InitializableStorageMock')
const KernelPinnedStorageMock = artifacts.require('KernelPinnedStorageMock')

contract('Unstructured storage', accounts => {
let app, kernel
context('> AppStorage', () => {
let appStorage

beforeEach(async () => {
app = await AppStubStorage.new()
kernel = await Kernel.new(true)
beforeEach(async () => {
appStorage = await AppStorageMock.new()
})

// Set up AppStubPinnedStorage
const fakeApp = await AppStub.new()
const kernelMock = await KernelPinnedStorageMock.new(fakeApp.address)
appPinned = await AppStubPinnedStorage.new(kernelMock.address)
})
it('tests Kernel storage', async () => {
const kernel = await Kernel.new(true)
await appStorage.setKernelExt(kernel.address)
//checks
assert.equal(
await web3.eth.getStorageAt(appStorage.address, (await appStorage.getKernelPosition())),
(await appStorage.kernel()).toString(),
'Kernel should match'
)
assert.equal(
await web3.eth.getStorageAt(appStorage.address, (await appStorage.getKernelPosition())),
kernel.address,
'Kernel original value should match'
)
})

it('tests init block', async () => {
// set values
await app.initialize()
const blockNumber = web3.eth.blockNumber
//checks
assert.equal(
parseInt(await web3.eth.getStorageAt(app.address, (await app.getInitializationBlockPosition())), 16),
blockNumber,
'Init block should match'
)
assert.equal(
parseInt(await web3.eth.getStorageAt(app.address, (await app.getInitializationBlockPosition())), 16),
(await app.getInitializationBlock()).toString(),
'Init block should match'
)
it('tests appID storage', async () => {
const appId = '0x1234000000000000000000000000000000000000000000000000000000000000'
await appStorage.setAppIdExt(appId)
//checks
assert.equal(
await web3.eth.getStorageAt(appStorage.address, (await appStorage.getAppIdPosition())),
(await appStorage.appId()).toString(),
'appId should match'
)
assert.equal(
await web3.eth.getStorageAt(appStorage.address, (await appStorage.getAppIdPosition())),
appId,
'appId original value should match'
)
})
})

it('tests Kernel storage', async () => {
await app.setKernelExt(kernel.address)
//checks
assert.equal(
await web3.eth.getStorageAt(app.address, (await app.getKernelPosition())),
(await app.kernel()).toString(),
'Kernel should match'
)
assert.equal(
await web3.eth.getStorageAt(app.address, (await app.getKernelPosition())),
kernel.address,
'Kernel original value should match'
)
context('> AppProxyPinned', () => {
let appPinned
beforeEach(async () => {
// Set up AppStubPinnedStorage
const fakeApp = await AppStub.new()
const kernelMock = await KernelPinnedStorageMock.new(fakeApp.address)
appPinned = await AppProxyPinnedStorageMock.new(kernelMock.address)
})

it('tests pinnedCode storage', async () => {
const pinnedCode = '0x1200000000000000000000000000000000005678'
await appPinned.setPinnedCodeExt(pinnedCode)
//checks
assert.equal(
await web3.eth.getStorageAt(appPinned.address, (await appPinned.getPinnedCodePosition())),
(await appPinned.pinnedCodeExt()).toString(),
'Pinned Code should match'
)
assert.equal(
await web3.eth.getStorageAt(appPinned.address, (await appPinned.getPinnedCodePosition())),
pinnedCode,
'Pinned Code original value should match'
)
})
})

it('tests appID storage', async () => {
const appId = '0x1234000000000000000000000000000000000000000000000000000000000000'
await app.setAppIdExt(appId)
//checks
assert.equal(
await web3.eth.getStorageAt(app.address, (await app.getAppIdPosition())),
(await app.appId()).toString(),
'appId should match'
)
assert.equal(
await web3.eth.getStorageAt(app.address, (await app.getAppIdPosition())),
appId,
'appId original value should match'
)
})

it('tests pinnedCode storage', async () => {
const pinnedCode = '0x1200000000000000000000000000000000005678'
await appPinned.setPinnedCodeExt(pinnedCode)
//checks
assert.equal(
await web3.eth.getStorageAt(appPinned.address, (await appPinned.getPinnedCodePosition())),
(await appPinned.pinnedCodeExt()).toString(),
'Pinned Code should match'
)
assert.equal(
await web3.eth.getStorageAt(appPinned.address, (await appPinned.getPinnedCodePosition())),
pinnedCode,
'Pinned Code original value should match'
)
context('> Initializable', () => {
let initializableMock

beforeEach(async () => {
initializableMock = await InitializableStorageMock.new()
})

it('tests init block', async () => {
// set values
await initializableMock.initialize()
const blockNumber = web3.eth.blockNumber
//checks
assert.equal(
parseInt(
await web3.eth.getStorageAt(
initializableMock.address,
(await initializableMock.getInitializationBlockPosition())
),
16
),
blockNumber,
'Init block should match'
)
assert.equal(
parseInt(
await web3.eth.getStorageAt(
initializableMock.address,
(await initializableMock.getInitializationBlockPosition())
),
16
),
(await initializableMock.getInitializationBlock()).toString(),
'Init block should match'
)
})
})
})

0 comments on commit 816e166

Please sign in to comment.