From 94303fdb83d91b2aadcbf65e6b3882a533aba435 Mon Sep 17 00:00:00 2001 From: i-stam Date: Sun, 3 May 2020 12:51:20 +0100 Subject: [PATCH] Add revert statement checks to all relevant tests --- test/controller/add_controller_test.go | 121 ++++----------------- test/controller/claim_test.go | 6 +- test/controller/controller_suite_test.go | 9 -- test/controller/remove_admin_test.go | 68 ++---------- test/controller/remove_controller_test.go | 54 +++------- test/controller/start_stop_test.go | 126 ++++++++++++---------- 6 files changed, 118 insertions(+), 266 deletions(-) diff --git a/test/controller/add_controller_test.go b/test/controller/add_controller_test.go index b8924455..31863215 100644 --- a/test/controller/add_controller_test.go +++ b/test/controller/add_controller_test.go @@ -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 @@ -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")) }) }) diff --git a/test/controller/claim_test.go b/test/controller/claim_test.go index b18560b3..be38a9fd 100644 --- a/test/controller/claim_test.go +++ b/test/controller/claim_test.go @@ -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")) }) }) @@ -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")) }) }) diff --git a/test/controller/controller_suite_test.go b/test/controller/controller_suite_test.go index eb2e0685..5164e8cf 100644 --- a/test/controller/controller_suite_test.go +++ b/test/controller/controller_suite_test.go @@ -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 -} diff --git a/test/controller/remove_admin_test.go b/test/controller/remove_admin_test.go index b6bde59d..74cb4eeb 100644 --- a/test/controller/remove_admin_test.go +++ b/test/controller/remove_admin_test.go @@ -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" @@ -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() { @@ -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() { @@ -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() { diff --git a/test/controller/remove_controller_test.go b/test/controller/remove_controller_test.go index 3a262375..c2a9a726 100644 --- a/test/controller/remove_controller_test.go +++ b/test/controller/remove_controller_test.go @@ -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" @@ -10,18 +9,17 @@ import ( var _ = Describe("removeController", func() { - When("controller owner calls RemoveController with a controller admin address", func() { + const gasLimit = 100000 - var tx *types.Transaction - const gasLimit = 100000 + When("controller owner calls RemoveController with a controller admin address", func() { BeforeEach(func() { - var err error - tx, err = ControllerContract.RemoveController(ControllerOwner.TransactOpts(ethertest.WithGasLimit(gasLimit)), ControllerAdmin.Address()) + tx, err := ControllerContract.RemoveController(ControllerOwner.TransactOpts(ethertest.WithGasLimit(gasLimit)), ControllerAdmin.Address()) Expect(err).ToNot(HaveOccurred()) Backend.Commit() - Expect(isGasExhausted(tx, gasLimit)).To(BeFalse()) Expect(isSuccessful(tx)).To(BeFalse()) + returnData, _ := ethCall(tx) + Expect(string(returnData[len(returnData)-64:])).To(ContainSubstring("provided account is not a controller")) }) It("should NOT decrease number of admins", func() { @@ -36,13 +34,6 @@ var _ = Describe("removeController", func() { Expect(it.Next()).To(BeFalse()) }) - It("should fail", func() { - Expect(isSuccessful(tx)).To(BeFalse()) - }) - - It("should not exaust gas", func() { - Expect(isGasExhausted(tx, gasLimit)).To(BeFalse()) - }) }) When("controller admin calls RemoveController with a controller address", func() { @@ -73,14 +64,13 @@ var _ = Describe("removeController", func() { When("controller admin calls RemoveController with a non controller address", func() { - var tx *types.Transaction - const gasLimit = 100000 - BeforeEach(func() { - var err error - tx, err = ControllerContract.RemoveController(ControllerAdmin.TransactOpts(ethertest.WithGasLimit(gasLimit)), RandomAccount.Address()) + tx, err := ControllerContract.RemoveController(ControllerAdmin.TransactOpts(ethertest.WithGasLimit(gasLimit)), RandomAccount.Address()) Expect(err).ToNot(HaveOccurred()) Backend.Commit() + Expect(isSuccessful(tx)).To(BeFalse()) + returnData, _ := ethCall(tx) + Expect(string(returnData[len(returnData)-64:])).To(ContainSubstring("provided account is not a controller")) }) It("should NOT decrease number of controllers", func() { @@ -95,34 +85,16 @@ var _ = Describe("removeController", func() { Expect(it.Next()).To(BeFalse()) }) - It("should fail", func() { - Expect(isSuccessful(tx)).To(BeFalse()) - }) - - It("should not exaust gas", func() { - Expect(isGasExhausted(tx, gasLimit)).To(BeFalse()) - }) - }) When("a Random address calls RemoveController with a controller admin address", func() { - - var tx *types.Transaction - const gasLimit = 100000 - - BeforeEach(func() { - var err error - tx, err = ControllerContract.RemoveController(RandomAccount.TransactOpts(ethertest.WithGasLimit(gasLimit)), ControllerAdmin.Address()) + It("should fail", func() { + tx, err := ControllerContract.RemoveController(RandomAccount.TransactOpts(ethertest.WithGasLimit(gasLimit)), ControllerAdmin.Address()) Expect(err).ToNot(HaveOccurred()) Backend.Commit() - }) - - It("should fail", 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 admin or owner")) }) }) diff --git a/test/controller/start_stop_test.go b/test/controller/start_stop_test.go index d1b4ffd6..7ee56fe1 100644 --- a/test/controller/start_stop_test.go +++ b/test/controller/start_stop_test.go @@ -42,7 +42,7 @@ var _ = Describe("Controller Stopping", func() { Expect(stopped).To(BeTrue()) }) - It("should emit Stopped event", func() { + It("should emit Stopped event", func() { it, err := ControllerContract.FilterStopped(nil) Expect(err).ToNot(HaveOccurred()) Expect(it.Next()).To(BeTrue()) @@ -53,34 +53,34 @@ var _ = Describe("Controller Stopping", func() { }) - When("a random address tries to start", func() { - It("Should fail", func() { - tx, err := ControllerContract.Start(RandomAccount.TransactOpts(ethertest.WithGasLimit(1000000))) - Expect(err).ToNot(HaveOccurred()) - Backend.Commit() - Expect(isSuccessful(tx)).To(BeFalse()) - }) - }) - - When("controller tries to start", func() { - It("Should fail", func() { - tx, err := ControllerContract.Start(Controller.TransactOpts(ethertest.WithGasLimit(1000000))) - Expect(err).ToNot(HaveOccurred()) - Backend.Commit() - Expect(isSuccessful(tx)).To(BeFalse()) - }) - }) - - When("controller admin tries to start", func() { - It("Should fail", func() { - tx, err := ControllerContract.Start(ControllerAdmin.TransactOpts(ethertest.WithGasLimit(1000000))) - Expect(err).ToNot(HaveOccurred()) - Backend.Commit() - Expect(isSuccessful(tx)).To(BeFalse()) - }) - }) - - When("the owner stops the controller", func() { + When("a random address tries to start", func() { + It("Should fail", func() { + tx, err := ControllerContract.Start(RandomAccount.TransactOpts(ethertest.WithGasLimit(1000000))) + Expect(err).ToNot(HaveOccurred()) + Backend.Commit() + Expect(isSuccessful(tx)).To(BeFalse()) + }) + }) + + When("controller tries to start", func() { + It("Should fail", func() { + tx, err := ControllerContract.Start(Controller.TransactOpts(ethertest.WithGasLimit(1000000))) + Expect(err).ToNot(HaveOccurred()) + Backend.Commit() + Expect(isSuccessful(tx)).To(BeFalse()) + }) + }) + + When("controller admin tries to start", func() { + It("Should fail", func() { + tx, err := ControllerContract.Start(ControllerAdmin.TransactOpts(ethertest.WithGasLimit(1000000))) + Expect(err).ToNot(HaveOccurred()) + Backend.Commit() + Expect(isSuccessful(tx)).To(BeFalse()) + }) + }) + + When("the owner stops the controller", func() { BeforeEach(func() { tx, err := ControllerContract.Stop(ControllerOwner.TransactOpts()) @@ -89,13 +89,13 @@ var _ = Describe("Controller Stopping", func() { Expect(isSuccessful(tx)).To(BeTrue()) }) - It("The controller should be stopped", func() { + It("The controller should be stopped", func() { stopped, err := ControllerContract.IsStopped(nil) Expect(err).ToNot(HaveOccurred()) Expect(stopped).To(BeTrue()) }) - It("should emit Stopped event", func() { + It("should emit Stopped event", func() { it, err := ControllerContract.FilterStopped(nil) Expect(err).ToNot(HaveOccurred()) Expect(it.Next()).To(BeTrue()) @@ -104,32 +104,46 @@ var _ = Describe("Controller Stopping", func() { Expect(evt.Sender).To(Equal(ControllerOwner.Address())) }) - When("owner tries to start", func() { - BeforeEach(func() { - tx, err := ControllerContract.Start(ControllerOwner.TransactOpts()) - Expect(err).ToNot(HaveOccurred()) - Backend.Commit() - Expect(isSuccessful(tx)).To(BeTrue()) - }) - - It("The controller should start again", func() { - stopped, err := ControllerContract.IsStopped(nil) - Expect(err).ToNot(HaveOccurred()) - Expect(stopped).To(BeFalse()) - }) - - It("should emit Started event", func() { - it, err := ControllerContract.FilterStarted(nil) - Expect(err).ToNot(HaveOccurred()) - Expect(it.Next()).To(BeTrue()) - evt := it.Event - Expect(it.Next()).To(BeFalse()) - Expect(evt.Sender).To(Equal(ControllerOwner.Address())) - }) - }) - - }) + It("should fail because it is stopped", func() { + tx, err := ControllerContract.AddAdmin(ControllerOwner.TransactOpts(ethertest.WithGasLimit(1000000)), RandomAccount.Address()) + Expect(err).ToNot(HaveOccurred()) + Backend.Commit() + Expect(isSuccessful(tx)).To(BeFalse()) + returnData, _ := ethCall(tx) + Expect(string(returnData[len(returnData)-64:])).To(ContainSubstring("controller is stopped")) + }) + When("owner tries to start", func() { + BeforeEach(func() { + tx, err := ControllerContract.Start(ControllerOwner.TransactOpts()) + Expect(err).ToNot(HaveOccurred()) + Backend.Commit() + Expect(isSuccessful(tx)).To(BeTrue()) + }) + + It("The controller should start again", func() { + stopped, err := ControllerContract.IsStopped(nil) + Expect(err).ToNot(HaveOccurred()) + Expect(stopped).To(BeFalse()) + }) + + It("should emit Started event", func() { + it, err := ControllerContract.FilterStarted(nil) + Expect(err).ToNot(HaveOccurred()) + Expect(it.Next()).To(BeTrue()) + evt := it.Event + Expect(it.Next()).To(BeFalse()) + Expect(evt.Sender).To(Equal(ControllerOwner.Address())) + }) + + It("should succeed", func() { + tx, err := ControllerContract.AddAdmin(ControllerOwner.TransactOpts(), RandomAccount.Address()) + Expect(err).ToNot(HaveOccurred()) + Backend.Commit() + Expect(isSuccessful(tx)).To(BeTrue()) + }) + }) + }) })