Skip to content
This repository has been archived by the owner on Jun 1, 2022. It is now read-only.

Commit

Permalink
check to make sure we don't send too much (#334)
Browse files Browse the repository at this point in the history
* check to make sure we don't send too much
* need that 100%

Signed-off-by: Ian Simpson <[email protected]>
  • Loading branch information
theotherian authored and sappenin committed Oct 10, 2019
1 parent b6c2e44 commit b7d7a03
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ public PrepareAmounts getSendPacketAmounts(
public boolean auth(final PrepareAmounts prepareAmounts) {
Objects.requireNonNull(prepareAmounts);

if (is(amountLeftToSend.get()).lessThan(prepareAmounts.getAmountToSend())) {
if (is(sentAmount.get().plus(prepareAmounts.getAmountToSend())).greaterThan(amountToSend) ||
is(amountLeftToSend.get()).lessThan(prepareAmounts.getAmountToSend())) {
return false;
} else {
this.amountLeftToSend.getAndUpdate(sourceAmount -> sourceAmount.minus(prepareAmounts.getAmountToSend()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,19 @@ public void checkAllInteractions() {
assertThat(tracker.getDeliveredAmountInSenderUnits()).isEqualTo(UnsignedLong.valueOf(20));

}

@Test
public void authFailsWhenAmountLeftToSendLessThanPrepare() {
FixedSenderAmountPaymentTracker tracker = new FixedSenderAmountPaymentTracker(UnsignedLong.valueOf(12L),
new HalfsiesExchangeRateCalculator());

PrepareAmounts amounts = tracker.getSendPacketAmounts(UnsignedLong.valueOf(12), Denominations.XRP,
Optional.of(Denominations.XRP));
tracker.auth(amounts);
tracker.commit(amounts, UnsignedLong.valueOf(12));
assertThat(tracker.getOriginalAmountLeft()).isEqualTo(UnsignedLong.ZERO);
// check amountLeftToSend instead
tracker.setSentAmount(UnsignedLong.ZERO);
assertThat(tracker.auth(amounts)).isFalse();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,46 @@ private void allSoldierOnsFalse() {
assertThat(sendMoneyAggregator.soldierOn(true, true)).isFalse();
}

@Test
public void breakLoopToPreventOversend() throws Exception {
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(1);

PaymentTracker tracker = mock(PaymentTracker.class);
PrepareAmounts prepare = PrepareAmounts.builder()
.amountToSend(UnsignedLong.valueOf(10l))
.minimumAmountToAccept(UnsignedLong.ZERO)
.build();
when(tracker.getSendPacketAmounts(any(), any(), any())).thenReturn(prepare);
when(tracker.auth(any())).thenReturn(false);
when(tracker.getOriginalAmountMode()).thenReturn(SenderAmountMode.SENDER_AMOUNT);
when(tracker.moreToSend()).thenReturn(true);
when(tracker.getDeliveredAmountInReceiverUnits()).thenReturn(UnsignedLong.ZERO);
when(tracker.getDeliveredAmountInSenderUnits()).thenReturn(UnsignedLong.ZERO);
when(tracker.getOriginalAmount()).thenReturn(UnsignedLong.valueOf(10));
when(tracker.getOriginalAmountLeft()).thenReturn(UnsignedLong.valueOf(10));
when(tracker.successful()).thenReturn(false);

SendMoneyRequest request = SendMoneyRequest.builder()
.sharedSecret(sharedSecret)
.sourceAddress(sourceAddress)
.senderAmountMode(SenderAmountMode.SENDER_AMOUNT)
.destinationAddress(destinationAddress)
.amount(originalAmountToSend)
.timeout(Optional.of(Duration.ofSeconds(60)))
.denomination(Denominations.XRP)
.paymentTracker(tracker)
.build();
this.sendMoneyAggregator = new SendMoneyAggregator(
executor, streamConnectionMock, streamCodecContextMock, linkMock, congestionControllerMock,
streamEncryptionServiceMock, request);

setSoldierOnBooleans(false, false, true);
when(streamConnectionMock.nextSequence()).thenReturn(UnsignedLong.ONE);
sendMoneyAggregator.send().get();
verify(tracker, times(1)).auth(prepare);
verify(tracker, times(0)).commit(any(), any());
}

@Test
public void toEncrypted() throws Exception {
doThrow(new IOException()).when(streamCodecContextMock).write(any(), any());
Expand Down

0 comments on commit b7d7a03

Please sign in to comment.