Skip to content

Commit

Permalink
Add test for TokenCancelAirdrop to fail fast with invalid parameters
Browse files Browse the repository at this point in the history
Signed-off-by: Kim Rader <[email protected]>
  • Loading branch information
kimbor committed Dec 23, 2024
1 parent fe64ed3 commit 0b7bf52
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
package com.hedera.node.app.service.token.impl.handlers;

import static com.hedera.hapi.node.base.ResponseCodeEnum.EMPTY_PENDING_AIRDROP_ID_LIST;
import static com.hedera.hapi.node.base.ResponseCodeEnum.INVALID_ACCOUNT_ID;
import static com.hedera.hapi.node.base.ResponseCodeEnum.INVALID_NFT_ID;
import static com.hedera.hapi.node.base.ResponseCodeEnum.INVALID_PENDING_AIRDROP_ID;
import static com.hedera.hapi.node.base.ResponseCodeEnum.INVALID_TOKEN_ID;
import static com.hedera.hapi.node.base.ResponseCodeEnum.INVALID_TOKEN_NFT_SERIAL_NUMBER;
import static com.hedera.hapi.node.base.ResponseCodeEnum.NOT_SUPPORTED;
Expand Down Expand Up @@ -78,7 +78,7 @@ public void preHandle(@NonNull final PreHandleContext context) throws PreCheckEx
final var op = txn.tokenCancelAirdropOrThrow();
final var allPendingAirdrops = op.pendingAirdrops();
for (final var airdrop : allPendingAirdrops) {
context.requireAliasedKeyOrThrow(airdrop.senderIdOrThrow(), INVALID_ACCOUNT_ID);
context.requireAliasedKeyOrThrow(airdrop.senderIdOrThrow(), INVALID_PENDING_AIRDROP_ID);

Check warning on line 81 in hedera-node/hedera-token-service-impl/src/main/java/com/hedera/node/app/service/token/impl/handlers/TokenCancelAirdropHandler.java

View check run for this annotation

Codecov / codecov/patch

hedera-node/hedera-token-service-impl/src/main/java/com/hedera/node/app/service/token/impl/handlers/TokenCancelAirdropHandler.java#L81

Added line #L81 was not covered by tests
}
}

Expand All @@ -93,8 +93,8 @@ public void pureChecks(@NonNull final TransactionBody txn) throws PreCheckExcept
if (!uniquePendingAirdrops.add(airdrop)) {
throw new PreCheckException(PENDING_AIRDROP_ID_REPEATED);
}
validateAccountID(airdrop.receiverId(), null);
validateAccountID(airdrop.senderId(), null);
validateAccountID(airdrop.receiverId(), INVALID_PENDING_AIRDROP_ID);
validateAccountID(airdrop.senderId(), INVALID_PENDING_AIRDROP_ID);

if (airdrop.hasFungibleTokenType()) {
final var tokenID = airdrop.fungibleTokenType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import static com.hedera.services.bdd.spec.transactions.token.TokenMovement.moving;
import static com.hedera.services.bdd.spec.transactions.token.TokenMovement.movingUnique;
import static com.hedera.services.bdd.spec.utilops.UtilVerbs.newKeyNamed;
import static com.hedera.services.bdd.spec.utilops.UtilVerbs.submitModified;
import static com.hedera.services.bdd.spec.utilops.mod.ModificationUtils.withSuccessivelyVariedBodyIds;
import static com.hedera.services.bdd.suites.HapiSuite.FIVE_HBARS;
import static com.hederahashgraph.api.proto.java.ResponseCodeEnum.ACCOUNT_HAS_PENDING_AIRDROPS;
import static com.hederahashgraph.api.proto.java.ResponseCodeEnum.EMPTY_PENDING_AIRDROP_ID_LIST;
Expand Down Expand Up @@ -78,6 +80,21 @@ static void beforeAll(@NonNull final TestLifecycle lifecycle) {
lifecycle.doAdhoc(setUpTokensAndAllReceivers());
}

@HapiTest
@DisplayName("fails gracefully with null parameters")
final Stream<DynamicTest> idVariantsTreatedAsExpected() {
final var account = "account";
return hapiTest(
cryptoCreate(account),
tokenAssociate(account, FUNGIBLE_TOKEN),
cryptoTransfer(moving(10, FUNGIBLE_TOKEN).between(OWNER, account)),
tokenAirdrop(moving(10, FUNGIBLE_TOKEN).between(account, RECEIVER_WITH_0_AUTO_ASSOCIATIONS))
.payingWith(account),
submitModified(withSuccessivelyVariedBodyIds(), () -> tokenCancelAirdrop(
pendingAirdrop(account, RECEIVER_WITH_0_AUTO_ASSOCIATIONS, FUNGIBLE_TOKEN))
.payingWith(account)));
}

@HapiTest
@DisplayName("not created NFT pending airdrop")
final Stream<DynamicTest> cancelNotCreatedNFTPendingAirdrop() {
Expand Down

0 comments on commit 0b7bf52

Please sign in to comment.