Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Commit

Permalink
Add revert statement checks to all relevant tests
Browse files Browse the repository at this point in the history
  • Loading branch information
i-stam committed May 3, 2020
1 parent 4a41561 commit 94303fd
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 266 deletions.
121 changes: 23 additions & 98 deletions test/controller/add_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (

var _ = Describe("AddController", func() {

const gasLimit = 100000

When("controller Admin calls AddController with a random address", func() {
var tx *types.Transaction

Expand Down Expand Up @@ -57,143 +59,66 @@ var _ = Describe("AddController", func() {
})

When("controller Admin calls AddController with it's own address", func() {

var tx *types.Transaction
const gasLimit = 100000

BeforeEach(func() {
var err error
tx, err = ControllerContract.AddController(ControllerAdmin.TransactOpts(ethertest.WithGasLimit(gasLimit)), ControllerAdmin.Address())
It("should fail at the already owner requirenment", func() {
tx, err := ControllerContract.AddController(ControllerAdmin.TransactOpts(ethertest.WithGasLimit(gasLimit)), ControllerAdmin.Address())
Expect(err).ToNot(HaveOccurred())
Backend.Commit()
})

It("should not succeed", func() {
Expect(isSuccessful(tx)).To(BeFalse())
returnData, _ := ethCall(tx)
Expect(string(returnData[len(returnData)-64:])).To(ContainSubstring("provided account is already an admin"))
})

It("should not exaust gas", func() {
Expect(isGasExhausted(tx, gasLimit)).To(BeFalse())
})

It("should fail at the already owner requirenment", func() {
Expect(TestRig.LastExecuted()).To(MatchRegexp(`require\(!_isAdmin\[_account\], "provided account is already an admin"\);`))
})

})

When("controller Admin calls AddController with Owner's address", func() {
var tx *types.Transaction
const gasLimit = 100000

BeforeEach(func() {
var err error
tx, err = ControllerContract.AddController(ControllerAdmin.TransactOpts(ethertest.WithGasLimit(gasLimit)), ControllerOwner.Address())
It("should fail at already controller requirenment", func() {
tx, err := ControllerContract.AddController(ControllerAdmin.TransactOpts(ethertest.WithGasLimit(gasLimit)), ControllerOwner.Address())
Expect(err).ToNot(HaveOccurred())
Backend.Commit()
})

It("should not succeed", func() {
Expect(isSuccessful(tx)).To(BeFalse())
})

It("should not exaust gas", func() {
Expect(isGasExhausted(tx, gasLimit)).To(BeFalse())
})

It("should fail at already controller requirenment", func() {
Expect(TestRig.LastExecuted()).To(MatchRegexp(`require\(!_isOwner\(_account\), "provided account is already the owner"\);`))
returnData, _ := ethCall(tx)
Expect(string(returnData[len(returnData)-64:])).To(ContainSubstring("provided account is already the owner"))
})
})

When("controller Admin calls AddController with controller's address", func() {
var tx *types.Transaction
const gasLimit = 100000

BeforeEach(func() {
var err error
tx, err = ControllerContract.AddController(ControllerAdmin.TransactOpts(ethertest.WithGasLimit(gasLimit)), Controller.Address())
It("should fail at already controller requirenment", func() {
tx, err := ControllerContract.AddController(ControllerAdmin.TransactOpts(ethertest.WithGasLimit(gasLimit)), Controller.Address())
Expect(err).ToNot(HaveOccurred())
Backend.Commit()
})

It("should not succeed", func() {
Expect(isSuccessful(tx)).To(BeFalse())
returnData, _ := ethCall(tx)
Expect(string(returnData[len(returnData)-64:])).To(ContainSubstring("provided account is already a controller"))
})

It("should not exaust gas", func() {
Expect(isGasExhausted(tx, gasLimit)).To(BeFalse())
})

It("should fail at already controller requirenment", func() {
Expect(TestRig.LastExecuted()).To(MatchRegexp(`require\(!_isController\[_account\], "provided account is already a controller"\);`))
})

})

When("controller Admin calls AddController with 0 address", func() {
var tx *types.Transaction
const gasLimit = 100000

BeforeEach(func() {
var err error
tx, err = ControllerContract.AddController(ControllerAdmin.TransactOpts(ethertest.WithGasLimit(gasLimit)), common.HexToAddress("0x0"))
tx, err := ControllerContract.AddController(ControllerAdmin.TransactOpts(ethertest.WithGasLimit(gasLimit)), common.HexToAddress("0x0"))
Expect(err).ToNot(HaveOccurred())
Backend.Commit()
})

It("should not succeed", func() {
Expect(isSuccessful(tx)).To(BeFalse())
})

It("should not exaust gas", func() {
Expect(isGasExhausted(tx, gasLimit)).To(BeFalse())
})

It("should fail at already controller requirenment", func() {
Expect(TestRig.LastExecuted()).To(MatchRegexp(`require\(_account != address\(0\), "provided account is the zero address"\);`))
returnData, _ := ethCall(tx)
Expect(string(returnData[len(returnData)-64:])).To(ContainSubstring("provided account is the zero address"))
})
})

When("Owner calls AddController with a random address", func() {
var tx *types.Transaction
const gasLimit = 100000

BeforeEach(func() {
var err error
tx, err = ControllerContract.AddController(ControllerOwner.TransactOpts(ethertest.WithGasLimit(gasLimit)), RandomAccount.Address())
It("should succeed", func() {
tx, err := ControllerContract.AddController(ControllerOwner.TransactOpts(), RandomAccount.Address())
Expect(err).ToNot(HaveOccurred())
Backend.Commit()
})

It("should succeed", func() {
Expect(isSuccessful(tx)).To(BeTrue())
})

})

When("controller calls AddController with a random address", func() {

var tx *types.Transaction
const gasLimit = 100000

BeforeEach(func() {
var err error
tx, err = ControllerContract.AddController(Controller.TransactOpts(ethertest.WithGasLimit(gasLimit)), RandomAccount.Address())
It("should fail at the notAdmin requirenment", func() {
tx, err := ControllerContract.AddController(Controller.TransactOpts(ethertest.WithGasLimit(gasLimit)), RandomAccount.Address())
Expect(err).ToNot(HaveOccurred())
Backend.Commit()
})

It("should not succeed", func() {
Expect(isSuccessful(tx)).To(BeFalse())
})

It("should not exaust gas", func() {
Expect(isGasExhausted(tx, gasLimit)).To(BeFalse())
})

It("should fail at the notAdmin requirenment", func() {
Expect(TestRig.LastExecuted()).To(MatchRegexp(`require\(\!isOwner\(msg.sender\) || isAdmin\(msg.sender\), "sender is not an admin"\);`))
returnData, _ := ethCall(tx)
Expect(string(returnData[len(returnData)-64:])).To(ContainSubstring("sender is not admin or owner"))
})
})

Expand Down
6 changes: 4 additions & 2 deletions test/controller/claim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ var _ = Describe("Controller claim", func() {
tx, err := ControllerContract.Claim(Controller.TransactOpts(ethertest.WithGasLimit(100000)), RandomAccount.Address(), ERC20Contract1Address, big.NewInt(222))
Expect(err).ToNot(HaveOccurred())
Backend.Commit()
Expect(isGasExhausted(tx, 100000)).To(BeFalse())
Expect(isSuccessful(tx)).To(BeFalse())
returnData, _ := ethCall(tx)
Expect(string(returnData[len(returnData)-64:])).To(ContainSubstring("sender is not admin"))
})
})

Expand All @@ -93,8 +94,9 @@ var _ = Describe("Controller claim", func() {
tx, err := ControllerContract.Claim(ControllerOwner.TransactOpts(ethertest.WithGasLimit(100000)), RandomAccount.Address(), ERC20Contract1Address, big.NewInt(222))
Expect(err).ToNot(HaveOccurred())
Backend.Commit()
Expect(isGasExhausted(tx, 100000)).To(BeFalse())
Expect(isSuccessful(tx)).To(BeFalse())
returnData, _ := ethCall(tx)
Expect(string(returnData[len(returnData)-64:])).To(ContainSubstring("sender is not admin"))
})
})

Expand Down
9 changes: 0 additions & 9 deletions test/controller/controller_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,3 @@ var _ = AfterEach(func() {
fmt.Fprintln(GinkgoWriter, TestRig.LastExecuted())
}
})

func isGasExhausted(tx *types.Transaction, gasLimit uint64) bool {
r, err := Backend.TransactionReceipt(context.Background(), tx.Hash())
Expect(err).ToNot(HaveOccurred())
if r.Status == types.ReceiptStatusSuccessful {
return false
}
return r.GasUsed == gasLimit
}
68 changes: 8 additions & 60 deletions test/controller/remove_admin_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package controller_test

import (
"github.com/ethereum/go-ethereum/core/types"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/tokencard/contracts/v3/test/shared"
Expand All @@ -10,6 +9,8 @@ import (

var _ = Describe("removeAdmin", func() {

const gasLimit = 100000

When("controller owner calls RemoveAdmin with a controller admin address", func() {

BeforeEach(func() {
Expand Down Expand Up @@ -37,56 +38,13 @@ var _ = Describe("removeAdmin", func() {
})

When("controller owner calls RemoveAdmin with a non controller admin address", func() {

var tx *types.Transaction
const gasLimit = 100000

BeforeEach(func() {
var err error
tx, err = ControllerContract.RemoveAdmin(ControllerOwner.TransactOpts(ethertest.WithGasLimit(gasLimit)), RandomAccount.Address())
tx, err := ControllerContract.RemoveAdmin(ControllerOwner.TransactOpts(ethertest.WithGasLimit(gasLimit)), RandomAccount.Address())
Expect(err).ToNot(HaveOccurred())
Backend.Commit()
})

It("should not succeed", func() {
Expect(isSuccessful(tx)).To(BeFalse())
})

It("should not exaust gas", func() {
Expect(isGasExhausted(tx, gasLimit)).To(BeFalse())
})

It("should NOT decrease number of admins", func() {
count, err := ControllerContract.AdminCount(nil)
Expect(err).ToNot(HaveOccurred())
Expect(count.String()).To(Equal("1"))
})

It("should NOT emit RemovedAdmin event", func() {
it, err := ControllerContract.FilterRemovedAdmin(nil)
Expect(err).ToNot(HaveOccurred())
Expect(it.Next()).To(BeFalse())
})
})

When("controller owner calls RemoveAdmin with a non controller admin address", func() {

var tx *types.Transaction
const gasLimit = 100000

BeforeEach(func() {
var err error
tx, err = ControllerContract.RemoveAdmin(ControllerOwner.TransactOpts(ethertest.WithGasLimit(gasLimit)), RandomAccount.Address())
Expect(err).ToNot(HaveOccurred())
Backend.Commit()
})

It("should not succeed", func() {
Expect(isSuccessful(tx)).To(BeFalse())
})

It("should not exaust gas", func() {
Expect(isGasExhausted(tx, gasLimit)).To(BeFalse())
returnData, _ := ethCall(tx)
Expect(string(returnData[len(returnData)-64:])).To(ContainSubstring("provided account is not an admin"))
})

It("should NOT decrease number of admins", func() {
Expand All @@ -103,23 +61,13 @@ var _ = Describe("removeAdmin", func() {
})

When("a Random address calls RemoveAdmin with a controller admin address", func() {

var tx *types.Transaction
const gasLimit = 100000

BeforeEach(func() {
var err error
tx, err = ControllerContract.RemoveAdmin(RandomAccount.TransactOpts(ethertest.WithGasLimit(gasLimit)), ControllerAdmin.Address())
tx, err := ControllerContract.RemoveAdmin(RandomAccount.TransactOpts(ethertest.WithGasLimit(gasLimit)), ControllerAdmin.Address())
Expect(err).ToNot(HaveOccurred())
Backend.Commit()
})

It("should not succeed", func() {
Expect(isSuccessful(tx)).To(BeFalse())
})

It("should not exaust gas", func() {
Expect(isGasExhausted(tx, gasLimit)).To(BeFalse())
returnData, _ := ethCall(tx)
Expect(string(returnData[len(returnData)-64:])).To(ContainSubstring("sender is not an owner"))
})

It("should NOT decrease number of admins", func() {
Expand Down
Loading

0 comments on commit 94303fd

Please sign in to comment.