-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Import unit tests for Owned, OpsManaged and SafeMath #73
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
pragma solidity ^0.4.17; | ||
|
||
// Copyright 2017 OpenST Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// ---------------------------------------------------------------------------- | ||
// Common: SafeMath Library Implementation | ||
// | ||
// http://www.simpletoken.org/ | ||
// | ||
// Based on the SafeMath library by the OpenZeppelin team. | ||
// Copyright (c) 2016 Smart Contract Solutions, Inc. | ||
// https://github.com/OpenZeppelin/zeppelin-solidity | ||
// The MIT License. | ||
// ---------------------------------------------------------------------------- | ||
|
||
import "./SafeMath.sol"; | ||
|
||
|
||
contract SafeMathMock { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need "is SafeMath" to inherit SafeMath.sol functionality here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the idea is to have this contract call on SafeMath.sol (and inheriting it may tell the compiler to optimise it out) |
||
uint256 public result; | ||
|
||
function multiply(uint256 a, uint256 b) public { | ||
result = SafeMath.mul(a, b); | ||
} | ||
|
||
function subtract(uint256 a, uint256 b) public { | ||
result = SafeMath.sub(a, b); | ||
} | ||
|
||
function add(uint256 a, uint256 b) public { | ||
result = SafeMath.add(a, b); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
// Copyright 2017 OpenST Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// ---------------------------------------------------------------------------- | ||
// Test: OpsManaged.js | ||
// | ||
// http://www.simpletoken.org/ | ||
// | ||
// ---------------------------------------------------------------------------- | ||
|
||
const BigNumber = require('bignumber.js') | ||
|
||
const Utils = require('./lib/utils.js') | ||
const OpsManagedUtils = require('./OpsManaged_utils.js'); | ||
|
||
const OpsManaged = artifacts.require("./OpsManaged.sol") | ||
|
||
|
||
// | ||
// Basic properties | ||
// owner | ||
// adminAddress | ||
// opsAddress | ||
// | ||
// setAdminAddress | ||
// setAdminAddress to owner | ||
// setAdminAddress to this | ||
// setAdminAddress to ops address | ||
// setAdminAddress to account[1] | ||
// setAdminAddress to 0 | ||
// | ||
// setOpsAddress | ||
// setOpsAddress to owner | ||
// setOpsAddress to this | ||
// setOpsAddress to ops address | ||
// setOpsAddress to account[1] | ||
// setOpsAddress to 0 | ||
// | ||
|
||
contract('OpsManaged', (accounts) => { | ||
|
||
|
||
async function createOpsManaged() { | ||
return await OpsManaged.new() | ||
} | ||
|
||
|
||
describe('Basic properties', async () => { | ||
|
||
var instance = null | ||
|
||
before(async () => { | ||
instance = await createOpsManaged() | ||
}) | ||
|
||
|
||
it("owner", async () => { | ||
assert.equal(await instance.owner.call(), accounts[0]) | ||
}) | ||
|
||
it("adminAddress", async () => { | ||
assert.equal(await instance.adminAddress.call(), 0) | ||
}) | ||
|
||
it("opsAddress", async () => { | ||
assert.equal(await instance.opsAddress.call(), 0) | ||
}) | ||
}) | ||
|
||
|
||
describe('setAdminAddress', async () => { | ||
|
||
var instance = null | ||
|
||
before(async () => { | ||
instance = await createOpsManaged() | ||
}) | ||
|
||
|
||
it("to the owner", async () => { | ||
const owner = await instance.owner.call() | ||
await Utils.expectThrow(instance.setAdminAddress.call(owner)) | ||
}) | ||
|
||
it("to 'this'", async () => { | ||
await Utils.expectThrow(instance.setAdminAddress.call(instance.address)) | ||
}) | ||
|
||
it("to ops address", async () => { | ||
assert.equal(await instance.setOpsAddress.call(accounts[2]), true) | ||
await instance.setOpsAddress(accounts[2]) | ||
|
||
await Utils.expectThrow(instance.setAdminAddress.call(accounts[2])) | ||
}) | ||
|
||
it("to accounts[1]", async () => { | ||
assert.equal(await instance.adminAddress.call(), 0) | ||
assert.equal(await instance.setAdminAddress.call(accounts[1]), true) | ||
OpsManagedUtils.checkAdminAddressChangedEventGroup(await instance.setAdminAddress(accounts[1]), accounts[1]) | ||
assert.equal(await instance.adminAddress.call(), accounts[1]) | ||
}) | ||
|
||
it("to 0", async () => { | ||
assert.equal(await instance.adminAddress.call(), accounts[1]) | ||
assert.equal(await instance.setAdminAddress.call(0), true) | ||
OpsManagedUtils.checkAdminAddressChangedEventGroup(await instance.setAdminAddress(0), 0) | ||
assert.equal(await instance.adminAddress.call(), 0) | ||
}) | ||
}) | ||
|
||
|
||
describe('setOpsAddress', async () => { | ||
|
||
var instance = null | ||
|
||
before(async () => { | ||
instance = await createOpsManaged() | ||
}) | ||
|
||
|
||
it("to the owner", async () => { | ||
const owner = await instance.owner.call() | ||
await Utils.expectThrow(instance.setOpsAddress.call(owner)) | ||
}) | ||
|
||
it("to 'this'", async () => { | ||
await Utils.expectThrow(instance.setOpsAddress.call(instance.address)) | ||
}) | ||
|
||
it("to ops address", async () => { | ||
assert.equal(await instance.setAdminAddress.call(accounts[3]), true) | ||
await instance.setAdminAddress(accounts[3]) | ||
|
||
await Utils.expectThrow(instance.setOpsAddress.call(accounts[3])) | ||
}) | ||
|
||
it("to accounts[1]", async () => { | ||
assert.equal(await instance.opsAddress.call(), 0) | ||
assert.equal(await instance.setOpsAddress.call(accounts[1]), true) | ||
OpsManagedUtils.checkOpsAddressChangedEventGroup(await instance.setOpsAddress(accounts[1]), accounts[1]) | ||
assert.equal(await instance.opsAddress.call(), accounts[1]) | ||
}) | ||
|
||
it("to 0", async () => { | ||
assert.equal(await instance.opsAddress.call(), accounts[1]) | ||
assert.equal(await instance.setAdminAddress.call(0), true) | ||
OpsManagedUtils.checkOpsAddressChangedEventGroup(await instance.setOpsAddress(0), 0) | ||
assert.equal(await instance.opsAddress.call(), 0) | ||
}) | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright 2017 OpenST Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// ---------------------------------------------------------------------------- | ||
// Test: OpsManaged_utils.js | ||
// | ||
// http://www.simpletoken.org/ | ||
// | ||
// ---------------------------------------------------------------------------- | ||
|
||
module.exports.checkAdminAddressChangedEventGroup = (result, _newAddress) => { | ||
assert.equal(result.logs.length, 1) | ||
|
||
const event = result.logs[0] | ||
|
||
assert.equal(event.event, "AdminAddressChanged") | ||
assert.equal(event.args._newAddress, _newAddress) | ||
} | ||
|
||
module.exports.checkOpsAddressChangedEventGroup = (result, _newAddress) => { | ||
assert.equal(result.logs.length, 1) | ||
|
||
const event = result.logs[0] | ||
|
||
assert.equal(event.event, "OpsAddressChanged") | ||
assert.equal(event.args._newAddress, _newAddress) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
// Copyright 2017 OpenST Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// ---------------------------------------------------------------------------- | ||
// Test: Owned.js | ||
// | ||
// http://www.simpletoken.org/ | ||
// | ||
// ---------------------------------------------------------------------------- | ||
|
||
const Utils = require('./lib/utils.js') | ||
const OwnedUtils = require('./Owned_utils.js') | ||
|
||
const BigNumber = require('bignumber.js') | ||
|
||
const Owned = artifacts.require("./Owned.sol") | ||
|
||
|
||
// | ||
// Basic properties | ||
// owner | ||
// | ||
// initiateOwnershipTransfer | ||
// to 0 | ||
// to this | ||
// to current owner | ||
// to accounts[1] as non-owner | ||
// to accounts[1] | ||
// | ||
// completeOwnershipTransfer | ||
// from accounts[2] | ||
// from current owner | ||
// from accounts[1] | ||
// | ||
|
||
contract('OpsManaged', (accounts) => { | ||
|
||
|
||
async function createOwned() { | ||
return await Owned.new() | ||
} | ||
|
||
|
||
describe('Basic properties', async () => { | ||
|
||
var instance = null | ||
|
||
before(async () => { | ||
instance = await createOwned() | ||
}) | ||
|
||
|
||
it("owner", async () => { | ||
assert.equal(await instance.owner.call(), accounts[0]) | ||
}) | ||
|
||
it("proposedOwner", async () => { | ||
assert.equal(await instance.proposedOwner.call(), 0) | ||
}) | ||
}) | ||
|
||
|
||
describe('initiateOwnershipTransfer', async () => { | ||
|
||
var instance = null | ||
|
||
before(async () => { | ||
instance = await createOwned() | ||
}) | ||
|
||
it("to 0", async () => { | ||
assert.equal(await instance.initiateOwnershipTransfer.call(0), true) | ||
OwnedUtils.checkOwnershipTransferInitiatedEventGroup(await instance.initiateOwnershipTransfer(0), 0) | ||
}) | ||
|
||
it("to this", async () => { | ||
assert.equal(await instance.initiateOwnershipTransfer.call(instance.address), true) | ||
OwnedUtils.checkOwnershipTransferInitiatedEventGroup(await instance.initiateOwnershipTransfer(instance.address), instance.address) | ||
}) | ||
|
||
it("to current owner", async () => { | ||
const owner = await instance.owner.call() | ||
assert.equal(await instance.initiateOwnershipTransfer.call(owner), true) | ||
OwnedUtils.checkOwnershipTransferInitiatedEventGroup(await instance.initiateOwnershipTransfer(owner), owner) | ||
}) | ||
|
||
it("to accounts[1] as non-owner", async () => { | ||
await Utils.expectThrow(instance.initiateOwnershipTransfer.call(accounts[1], { from: accounts[2] })) | ||
}) | ||
|
||
it("to accounts[1]", async () => { | ||
assert.equal(await instance.initiateOwnershipTransfer.call(accounts[1]), true) | ||
OwnedUtils.checkOwnershipTransferInitiatedEventGroup(await instance.initiateOwnershipTransfer(accounts[1]), accounts[1]) | ||
}) | ||
}) | ||
|
||
|
||
describe('completeOwnershipTransfer', async () => { | ||
|
||
var instance = null | ||
|
||
before(async () => { | ||
instance = await createOwned() | ||
}) | ||
|
||
|
||
it("from accounts[2]", async () => { | ||
await Utils.expectThrow(instance.completeOwnershipTransfer.call({ from: accounts[2] })) | ||
}) | ||
|
||
it("from current owner", async () => { | ||
const owner = await instance.owner.call() | ||
await Utils.expectThrow(instance.completeOwnershipTransfer.call({ from: owner })) | ||
}) | ||
|
||
it("from account[1]", async () => { | ||
assert.equal(await instance.owner.call(), accounts[0]) | ||
assert.equal(await instance.proposedOwner.call(), 0) | ||
await instance.initiateOwnershipTransfer(accounts[1]) | ||
assert.equal(await instance.proposedOwner.call(), accounts[1]) | ||
|
||
assert.equal(await instance.completeOwnershipTransfer.call({ from: accounts[1] }), true) | ||
OwnedUtils.checkOwnershipTransferCompletedEventGroup(await instance.completeOwnershipTransfer({ from: accounts[1] }), accounts[1]) | ||
|
||
assert.equal(await instance.owner.call(), accounts[1]) | ||
assert.equal(await instance.proposedOwner.call(), 0) | ||
}) | ||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any specific reason for SafeMatchMock contract? Can't we use SafeMatch contract in SafeMath.js directly?