# Testing reentrance

**URL:** <https://forum.openzeppelin.com/t/testing-reentrance/38244>\
**Category:** Smart Contracts\
**Created:** [October 24, 2023, 4:31pm UTC](https://forum.openzeppelin.com/t/testing-reentrance/38244 "2023-10-24T16:31:48Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![madeindreams](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/madeindreams/32/23461_2.png) [@madeindreams](https://forum.openzeppelin.com/u/madeindreams)\
**Post date:** [October 24, 2023, 4:31pm UTC](https://forum.openzeppelin.com/t/testing-reentrance/38244/1 "2023-10-24T16:31:48Z")

</div>

When testing my reentrance, I'm not getting the appropriate revert message.

The function with guard;

```auto
function sellShares(uint256 amount) public nonReentrant {

   //...

   (bool sent, ) = _msgSender().call{value: returnedAmount}("");
        require(sent, "Failed to send Ether");

   //...
}

```

when reentering the contract, the call is reverted, but I'm getting the Failed to send Ether message

The thing is that even if I get this message and validate that the reentrance worked. I'm unable to make the test pass.

```auto
   await expect(await reentry.sellShares())
            .to.be.rejectedWith('Failed to send Ether');

```

The logs from the test

> Testing Reentry  
> Should revert on reentry:  
> Error: VM Exception while processing transaction: reverted with reason string 'Failed to send Ether'  
> at . (0x59b670e9fa9d0a427751af201d676719a970857b)  
> at . (0x322813fd9a801c5507c9de605d63cea4f2ce6c44)  
> at processTicksAndRejections (node:internal/process/task\_queues:96:5)  
> at HardhatNode.\_mineBlockWithPendingTxs (node\_modules/hardhat/src/internal/hardhat-network/provider/node.ts:1866:23)  
> at HardhatNode.mineBlock (node\_modules/hardhat/src/internal/hardhat-network/provider/node.ts:524:16)  
> at EthModule.\_sendTransactionAndReturnHash (node\_modules/hardhat/src/internal/hardhat-network/provider/modules/eth.ts:1482:18)  
> at HardhatNetworkProvider.request (node\_modules/hardhat/src/internal/hardhat-network/provider/provider.ts:124:18)  
> at HardhatEthersSigner.sendTransaction (node\_modules/@nomicfoundation/hardhat-ethers/src/signers.ts:125:18)  
> at send (node\_modules/ethers/src.ts/contract/contract.ts:313:20)

I'm not sure if the reentrancy guard should return a specific message but even if i expect this message the test don't pass.

---

<div class="post-metadata">

**Author:** ![barakman](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/barakman/32/855_2.png) [@barakman](https://forum.openzeppelin.com/u/barakman)\
**Post date:** [October 24, 2023, 7:37pm UTC](https://forum.openzeppelin.com/t/testing-reentrance/38244/2 "2023-10-24T19:37:05Z")

</div>

> [@madeindreams](#):
>
> ```auto
> await expect(await reentry.sellShares())
> .to.be.rejectedWith('Failed to send Ether');
> 
> ```

The inner `await` causes the transaction to get executed and throw an exception.

Subsequently, the `expect` function itself doesn't even get executed.

So you should get rid of that `await` and let the `expect` function deal with it.

Also, I'm pretty sure that you should replace `rejectedWith` with `revertedWith`.

---

<div class="post-metadata">

**Author:** ![madeindreams](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/madeindreams/32/23461_2.png) [@madeindreams](https://forum.openzeppelin.com/u/madeindreams)\
**Post date:** [October 24, 2023, 7:49pm UTC](https://forum.openzeppelin.com/t/testing-reentrance/38244/3 "2023-10-24T19:49:43Z")

</div>

Yes i have figured this out but i'm not able to get a reentrancy specific error. Not sure if i should. I'm using the latest OZ contract package.

---

<div class="post-metadata">

**Author:** ![barakman](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/barakman/32/855_2.png) [@barakman](https://forum.openzeppelin.com/u/barakman)\
**Post date:** [October 24, 2023, 7:52pm UTC](https://forum.openzeppelin.com/t/testing-reentrance/38244/4 "2023-10-24T19:52:16Z")

</div>

So why have you posted `await expect(await reentry.sellShares())` if this is NOT what you're doing?

---

<div class="post-metadata">

**Author:** ![madeindreams](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/madeindreams/32/23461_2.png) [@madeindreams](https://forum.openzeppelin.com/u/madeindreams)\
**Post date:** [October 24, 2023, 7:57pm UTC](https://forum.openzeppelin.com/t/testing-reentrance/38244/5 "2023-10-24T19:57:26Z")

</div>

This was indeed wrong, but i'm trying to get the revert error from the reentrance. This on is from the call.

---

<div class="post-metadata">

**Author:** ![barakman](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/barakman/32/855_2.png) [@barakman](https://forum.openzeppelin.com/u/barakman)\
**Post date:** [October 24, 2023, 8:01pm UTC](https://forum.openzeppelin.com/t/testing-reentrance/38244/6 "2023-10-24T20:01:38Z")

</div>

And what do you get after removing the inner `await`?

BTW, with `_msgSender().call{value: returnedAmount}("")`, you are calling the contract's `receive` function and not the `sellShares` function, so in either case, you are not testing reentrancy here.

---

<div class="post-metadata">

**Author:** ![madeindreams](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/madeindreams/32/23461_2.png) [@madeindreams](https://forum.openzeppelin.com/u/madeindreams)\
**Post date:** [October 24, 2023, 8:04pm UTC](https://forum.openzeppelin.com/t/testing-reentrance/38244/7 "2023-10-24T20:04:41Z")

</div>

The test succeed but only with the revert string 'Failed to send Ether'.

I was expecting a string more specific to reentrance

---

<div class="post-metadata">

**Author:** ![madeindreams](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/madeindreams/32/23461_2.png) [@madeindreams](https://forum.openzeppelin.com/u/madeindreams)\
**Post date:** [October 24, 2023, 8:05pm UTC](https://forum.openzeppelin.com/t/testing-reentrance/38244/8 "2023-10-24T20:05:50Z")

</div>

When the contract receive ether. it call for sellShares again.  
I have also tried with a fallback but it's the same behaviour.

---

<div class="post-metadata">

**Author:** ![barakman](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/barakman/32/855_2.png) [@barakman](https://forum.openzeppelin.com/u/barakman)\
**Post date:** [October 24, 2023, 8:07pm UTC](https://forum.openzeppelin.com/t/testing-reentrance/38244/9 "2023-10-24T20:07:18Z")

</div>

You've explicitly stated that string in the `require` statement, why would you expect anything else???

---

<div class="post-metadata">

**Author:** ![barakman](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/barakman/32/855_2.png) [@barakman](https://forum.openzeppelin.com/u/barakman)\
**Post date:** [October 24, 2023, 8:08pm UTC](https://forum.openzeppelin.com/t/testing-reentrance/38244/10 "2023-10-24T20:08:13Z")

</div>

> [@madeindreams](#):
>
> When the contract receive ether. it call for sellShares again.

Well then you should add that as part of your question, since it is **very much relevant**.

---

<div class="post-metadata">

**Author:** ![madeindreams](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/madeindreams/32/23461_2.png) [@madeindreams](https://forum.openzeppelin.com/u/madeindreams)\
**Post date:** [October 24, 2023, 8:09pm UTC](https://forum.openzeppelin.com/t/testing-reentrance/38244/11 "2023-10-24T20:09:13Z")

</div>

Because when of the modifier on that second call. Is it not getting triggered before it get's to the second call?

---

<div class="post-metadata">

**Author:** ![barakman](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/barakman/32/855_2.png) [@barakman](https://forum.openzeppelin.com/u/barakman)\
**Post date:** [October 24, 2023, 8:13pm UTC](https://forum.openzeppelin.com/t/testing-reentrance/38244/12 "2023-10-24T20:13:33Z")

</div>

But you've used `call`, which doesn't revert, but rather - returns a success status.

You even obtain that return-value explicitly in your code (using `bool sent`), so you are probably aware of the fact that `call` doesn't revert:

```auto
(bool sent, ) = _msgSender().call{value: returnedAmount}("");

```

And since this line doesn't revert, it is **the next line** which reverts:

```auto
require(sent, "Failed to send Ether");

```

And this line reverts with the error-message which you ultimately receive.

---

<div class="post-metadata">

**Author:** ![madeindreams](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/madeindreams/32/23461_2.png) [@madeindreams](https://forum.openzeppelin.com/u/madeindreams)\
**Post date:** [October 24, 2023, 8:13pm UTC](https://forum.openzeppelin.com/t/testing-reentrance/38244/13 "2023-10-24T20:13:56Z")

</div>

So if i remove the require statement i should get that message?

---

<div class="post-metadata">

**Author:** ![madeindreams](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/madeindreams/32/23461_2.png) [@madeindreams](https://forum.openzeppelin.com/u/madeindreams)\
**Post date:** [October 24, 2023, 8:15pm UTC](https://forum.openzeppelin.com/t/testing-reentrance/38244/14 "2023-10-24T20:15:17Z")

</div>

I see. I will experiment with this. thank you!

---

<div class="post-metadata">

**Author:** ![barakman](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/barakman/32/855_2.png) [@barakman](https://forum.openzeppelin.com/u/barakman)\
**Post date:** [October 24, 2023, 8:17pm UTC](https://forum.openzeppelin.com/t/testing-reentrance/38244/15 "2023-10-24T20:17:43Z")

</div>

NO, the `sellShares` function will just complete successfully!

If you change `call` to `transfer`, then the function will revert at the `nonReentrant` modifier, and you'll then receive the error-message specified in that modifier.
