Example #1
0
 @Test
 public void toStringReturnsCorrectRepresentationWhenStatusCodeIsUnknown() {
   // Assert:
   Assert.assertThat(
       "Http Status Code -123: badness",
       IsEqual.equalTo(new ErrorResponse(new TimeInstant(4), "badness", -123).toString()));
   Assert.assertThat(
       "Http Status Code -123",
       IsEqual.equalTo(new ErrorResponse(new TimeInstant(4), null, -123).toString()));
 }
Example #2
0
  @Test
  public void canBeCreatedAroundMessage() {
    // Arrange:
    final ErrorResponse response = new ErrorResponse(new TimeInstant(29), "badness", 500);

    // Assert:
    Assert.assertThat(response.getTimeStamp(), IsEqual.equalTo(new TimeInstant(29)));
    Assert.assertThat(response.getError(), IsEqual.equalTo("Internal Server Error"));
    Assert.assertThat(response.getMessage(), IsEqual.equalTo("badness"));
    Assert.assertThat(response.getStatus(), IsEqual.equalTo(500));
  }
Example #3
0
  @Test
  public void responseCanBeRoundTripped() {
    // Act:
    final ErrorResponse response =
        createRoundTrippedResponse(new ErrorResponse(new TimeInstant(18), "badness", 500));

    // Assert:
    Assert.assertThat(response.getTimeStamp(), IsEqual.equalTo(new TimeInstant(18)));
    Assert.assertThat(response.getError(), IsEqual.equalTo("Internal Server Error"));
    Assert.assertThat(response.getMessage(), IsEqual.equalTo("badness"));
    Assert.assertThat(response.getStatus(), IsEqual.equalTo(500));
  }
Example #4
0
  @Test
  public void responseWithoutDescriptionsCanBeRoundTripped() {
    // Act:
    final ErrorResponse response =
        createRoundTrippedResponse(new ErrorResponse(new TimeInstant(54), null, 890));

    // Assert:
    Assert.assertThat(response.getTimeStamp(), IsEqual.equalTo(new TimeInstant(54)));
    Assert.assertThat(response.getError(), IsNull.nullValue());
    Assert.assertThat(response.getMessage(), IsNull.nullValue());
    Assert.assertThat(response.getStatus(), IsEqual.equalTo(890));
  }
Example #5
0
  @Test
  public void canBeCreatedAroundUnknownHttpStatus() {
    // Arrange:
    final ErrorResponse response =
        new ErrorResponse(new TimeInstant(18), "exception message", -123);

    // Assert:
    Assert.assertThat(response.getTimeStamp(), IsEqual.equalTo(new TimeInstant(18)));
    Assert.assertThat(response.getError(), IsNull.nullValue());
    Assert.assertThat(response.getMessage(), IsEqual.equalTo("exception message"));
    Assert.assertThat(response.getStatus(), IsEqual.equalTo(-123));
  }
Example #6
0
  @Test
  public void canBeCreatedAroundException() {
    // Arrange:
    final ErrorResponse response =
        new ErrorResponse(
            new TimeInstant(18), new RuntimeException("exception message"), HttpStatus.NOT_FOUND);

    // Assert:
    Assert.assertThat(response.getTimeStamp(), IsEqual.equalTo(new TimeInstant(18)));
    Assert.assertThat(response.getError(), IsEqual.equalTo("Not Found"));
    Assert.assertThat(response.getMessage(), IsEqual.equalTo("exception message"));
    Assert.assertThat(response.getStatus(), IsEqual.equalTo(404));
  }
    @Test
    public void addressConstantIsConsistentWithNemesisBlock() {
      // Arrange:
      final Block block = this.loadNemesisBlock();
      final Address blockAddress = block.getSigner().getAddress();

      // Assert:
      Assert.assertThat(blockAddress, IsEqual.equalTo(NEMESIS_BLOCK_INFO.getAddress()));
      Assert.assertThat(
          blockAddress.getPublicKey(),
          IsEqual.equalTo(NEMESIS_BLOCK_INFO.getAddress().getPublicKey()));
      Assert.assertThat(blockAddress.getPublicKey(), IsNull.notNullValue());
    }
  @Test
  public void deleteDeletesUnderlyingStorableEntityFile() throws IOException {
    // Arrange:
    final File file = new File(TEST_FILE_DIRECTORY, "to-be-deleted.bar");
    final TEntityFileDescriptor descriptor = this.createDescriptor(file);
    Assert.assertThat(file.createNewFile(), IsEqual.equalTo(true));

    // Act:
    descriptor.delete();

    // Assert:
    Assert.assertThat(file.exists(), IsEqual.equalTo(false));
  }
  @Test
  public void hashCodesAreEqualForEquivalentObjects() {
    // Arrange:
    final GroupElement g1 = MathUtils.getRandomGroupElement();
    final GroupElement g2 = MathUtils.toRepresentation(g1, GroupElement.Representation.P2);
    final GroupElement g3 = MathUtils.toRepresentation(g1, GroupElement.Representation.P1P1);
    final GroupElement g4 = MathUtils.getRandomGroupElement();

    // Assert
    Assert.assertThat(g2.hashCode(), IsEqual.equalTo(g1.hashCode()));
    Assert.assertThat(g3.hashCode(), IsEqual.equalTo(g1.hashCode()));
    Assert.assertThat(g1.hashCode(), IsNot.not(IsEqual.equalTo(g4.hashCode())));
    Assert.assertThat(g2.hashCode(), IsNot.not(IsEqual.equalTo(g4.hashCode())));
    Assert.assertThat(g3.hashCode(), IsNot.not(IsEqual.equalTo(g4.hashCode())));
  }
  @Test
  public void canChangeWalletNameAndPasswordIfWalletIsAlreadyOpen() {
    // Arrange:
    final WalletNamePasswordPair pair2 = createPair("n2", "p2");
    final WalletDescriptor descriptor2 = createDescriptor("n2");
    final TestContext context = new TestContext();
    Mockito.when(context.descriptorFactory.createNew(pair2, FILE_EXTENSION))
        .thenReturn(descriptor2);

    // Act:
    context.walletServices.open(context.pair);
    context.walletServices.move(context.pair, pair2);

    // Assert:
    // - the original is opened and the target is created
    Mockito.verify(context.descriptorFactory, Mockito.times(2))
        .openExisting(context.pair, FILE_EXTENSION);
    Mockito.verify(context.descriptorFactory, Mockito.times(1)).createNew(pair2, FILE_EXTENSION);

    // - the original is loaded
    Mockito.verify(context.repository, Mockito.times(2)).load(context.descriptor);

    // - the target is saved
    final Wallet updatedWallet = context.walletServices.get(new WalletName("n2"));
    Assert.assertThat(updatedWallet.getName(), IsEqual.equalTo(new WalletName("n2")));
    // TODO BR: Fix this
    // Mockito.verify(context.repository, Mockito.times(1)).save(descriptor2, updatedWallet);
    Mockito.verify(context.repository, Mockito.times(1)).save(Mockito.any(), Mockito.any());

    // - the original wallet is deleted
    Mockito.verify(context.descriptor, Mockito.times(1)).delete();
  }
  @Test
  public void copyToCopiesBytesToGivenOutputStream() {
    // Arrange:
    final TestContext context = new TestContext();
    final WalletNamePasswordPair pair = createPair("n", "p");
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    Mockito.when(context.descriptor.openRead(StorableEntityReadMode.Raw))
        .thenReturn(new ByteArrayInputStream("test".getBytes()));

    // Act:
    context.walletServices.copyTo(pair, outputStream);

    // Assert:
    Assert.assertThat(outputStream.size(), IsEqual.equalTo(4));
    Assert.assertThat(outputStream.toString(), IsEqual.equalTo("test"));
  }
Example #12
0
  @Test
  public void responseCanBeSerialized() {
    // Arrange:
    final JsonSerializer serializer = new JsonSerializer();
    final ErrorResponse response = new ErrorResponse(new TimeInstant(18), "badness", 500);

    // Act:
    response.serialize(serializer);
    final JSONObject jsonObject = serializer.getObject();

    // Assert:
    Assert.assertThat(jsonObject.get("timeStamp"), IsEqual.equalTo(18));
    Assert.assertThat(jsonObject.get("error"), IsEqual.equalTo("Internal Server Error"));
    Assert.assertThat(jsonObject.get("message"), IsEqual.equalTo("badness"));
    Assert.assertThat(jsonObject.get("status"), IsEqual.equalTo(500));
  }
  @Test
  public void descriptorCanBeSerialized() {
    // Arrange:
    final File file =
        new File(Paths.get(TEST_FILE_DIRECTORY.toString(), "blah").toString(), "foo.bar");
    final TEntityFileDescriptor descriptor = this.createDescriptor(file);

    // Act:
    final JSONObject jsonObject = JsonSerializer.serializeToJson(descriptor);

    // Assert:
    Assert.assertThat(jsonObject.size(), IsEqual.equalTo(2));
    Assert.assertThat(jsonObject.get(descriptor.getName().getLabel()), IsEqual.equalTo("foo"));
    Assert.assertThat(
        jsonObject.get("location"),
        IsEqual.equalTo(Paths.get(TEST_FILE_DIRECTORY.toString(), "blah", "foo.bar").toString()));
  }
  @Test
  public void descriptorCanBeCreatedAroundStorableEntityWithMixedCaseExtension() {
    // Arrange:
    final File file =
        new File(Paths.get(TEST_FILE_DIRECTORY.toString(), "BlAh").toString(), "FoO.BaR");

    // Act:
    final TEntityFileDescriptor descriptor = this.createDescriptor(file);

    // Assert:
    Assert.assertThat(descriptor.getName(), IsEqual.equalTo(this.createEntityName("FoO")));
    Assert.assertThat(
        descriptor.getFileExtension(), IsEqual.equalTo(this.createEntityFileExtension(".BaR")));
    Assert.assertThat(
        descriptor.getStorableEntityLocation(),
        IsEqual.equalTo(Paths.get(TEST_FILE_DIRECTORY.toString(), "BlAh", "FoO.BaR").toString()));
  }
    @Test
    public void nemesisBlockIsVerifiable() {
      // Arrange:
      final Block block = this.loadNemesisBlock();

      // Assert:
      Assert.assertThat(block.verify(), IsEqual.equalTo(true));
    }
    @Test
    public void generationHashConstantIsConsistentWithNemesisBlock() {
      // Arrange:
      final Block block = this.loadNemesisBlock();

      // Assert:
      Assert.assertThat(
          block.getGenerationHash(), IsEqual.equalTo(NEMESIS_BLOCK_INFO.getGenerationHash()));
    }
  @Test
  public void isOnCurveReturnsTrueForPointsOnTheCurve() {
    for (int i = 0; i < 100; i++) {
      // Arrange:
      final GroupElement g = MathUtils.getRandomGroupElement();

      // Assert:
      Assert.assertThat(g.isOnCurve(), IsEqual.equalTo(true));
    }
  }
    @Test
    public void nemesisTransactionsAreVerifiable() {
      // Arrange:
      final Block block = this.loadNemesisBlock();

      // Assert:
      for (final Transaction transaction : block.getTransactions()) {
        Assert.assertThat(transaction.verify(), IsEqual.equalTo(true));
      }
    }
  @Test
  public void scalarMultiplyBasePointWithZeroReturnsNeutralElement() {
    // Arrange:
    final GroupElement basePoint = ed25519.getB();

    // Act:
    final GroupElement g = basePoint.scalarMultiply(curve.getField().ZERO.toByteArray());

    // Assert:
    Assert.assertThat(curve.getZero(GroupElement.Representation.P3), IsEqual.equalTo(g));
  }
  @Test
  public void scalarMultiplyBasePointWithOneReturnsBasePoint() {
    // Arrange:
    final GroupElement basePoint = ed25519.getB();

    // Act:
    final GroupElement g = basePoint.scalarMultiply(curve.getField().ONE.toByteArray());

    // Assert:
    Assert.assertThat(basePoint, IsEqual.equalTo(g));
  }
  @Test
  public void descriptorCanBeCreatedAroundUrlEncodedStorableEntityWithValidExtension() {
    // Arrange:
    final File file =
        new File(
            Paths.get(TEST_FILE_DIRECTORY.toString(), "blah").toString(),
            "%C3%B6%C3%A4%C3%BC%40.bar");

    // Act:
    final TEntityFileDescriptor descriptor = this.createDescriptor(file);

    // Assert:
    Assert.assertThat(descriptor.getName(), IsEqual.equalTo(this.createEntityName("öäü@")));
    Assert.assertThat(
        descriptor.getFileExtension(), IsEqual.equalTo(this.createEntityFileExtension(".bar")));
    Assert.assertThat(
        descriptor.getStorableEntityLocation(),
        IsEqual.equalTo(
            Paths.get(TEST_FILE_DIRECTORY.toString(), "blah", "%C3%B6%C3%A4%C3%BC%40.bar")
                .toString()));
  }
  @Test
  public void isOnCurveReturnsFalseForPointsNotOnTheCurve() {
    for (int i = 0; i < 100; i++) {
      // Arrange:
      final GroupElement g = MathUtils.getRandomGroupElement();
      final GroupElement h =
          GroupElement.p2(curve, g.getX(), g.getY(), g.getZ().multiply(curve.getField().TWO));

      // Assert (can only fail for 5*Z^2=1):
      Assert.assertThat(h.isOnCurve(), IsEqual.equalTo(false));
    }
  }
  @Test
  public void getCanReturnOpenWallet() {
    // Arrange:
    final TestContext context = new TestContext();

    // Act:
    final Wallet wallet1 = context.walletServices.open(context.pair);
    final Wallet wallet2 = context.walletServices.get(context.pair.getName());

    // Assert:
    Assert.assertThat(wallet2, IsEqual.equalTo(wallet1));
  }
  @Test
  public void addingNeutralGroupElementDoesNotChangeGroupElement() {
    final GroupElement neutral =
        GroupElement.p3(
            curve,
            curve.getField().ZERO,
            curve.getField().ONE,
            curve.getField().ONE,
            curve.getField().ZERO);
    for (int i = 0; i < 1000; i++) {
      // Arrange:
      final GroupElement g = MathUtils.getRandomGroupElement();

      // Act:
      final GroupElement h1 = g.add(neutral.toCached());
      final GroupElement h2 = neutral.add(g.toCached());

      // Assert:
      Assert.assertThat(g, IsEqual.equalTo(h1));
      Assert.assertThat(g, IsEqual.equalTo(h2));
    }
  }
  @Test
  public void getStorableEntityLocationReturnsAbsolutePathToStorableEntity() {
    // Arrange:
    final File file =
        new File(Paths.get(TEST_FILE_DIRECTORY.toString(), "blah").toString(), "foo.bar");
    final TEntityFileDescriptor descriptor = this.createDescriptor(file);

    // Act:
    final String path = descriptor.getStorableEntityLocation();

    // Assert:
    Assert.assertThat(path, IsEqual.equalTo(file.getAbsolutePath()));
  }
    @Test
    public void amountConstantIsConsistentWithNemesisBlock() {
      // Act:
      Amount totalAmount = Amount.ZERO;
      final Block block = this.loadNemesisBlock();
      for (final Transaction transaction : block.getTransactions()) {
        if (transaction instanceof TransferTransaction) {
          totalAmount = totalAmount.add(((TransferTransaction) transaction).getAmount());
        }
      }

      // Assert:
      Assert.assertThat(totalAmount, IsEqual.equalTo(NEMESIS_BLOCK_INFO.getAmount()));
    }
    @Test
    public void nemesisTransactionsHaveCorrectFees() {
      // Arrange:
      final Block block = this.loadNemesisBlock();

      // Assert:
      for (final Transaction transaction : block.getTransactions()) {
        final Amount expectedFee =
            TransactionTypes.TRANSFER == transaction.getType()
                ? Amount.ZERO
                : NemGlobals.getTransactionFeeCalculator().calculateMinimumFee(transaction);
        Assert.assertThat(transaction.getFee(), IsEqual.equalTo(expectedFee));
      }
    }
  @Test
  public void dblPrecomputedTableContainsExpectedGroupElements() {
    // Arrange:
    GroupElement g = ed25519.getB();
    GroupElement h = MathUtils.addGroupElements(g, g);

    // Act + Assert:
    for (int i = 0; i < 8; i++) {
      Assert.assertThat(
          MathUtils.toRepresentation(g, GroupElement.Representation.PRECOMP),
          IsEqual.equalTo(ed25519.getB().dblPrecmp[i]));
      g = MathUtils.addGroupElements(g, h);
    }
  }
  @Test
  public void dblReturnsExpectedResult() {
    for (int i = 0; i < 1000; i++) {
      // Arrange:
      final GroupElement g = MathUtils.getRandomGroupElement();

      // Act:
      final GroupElement h1 = g.dbl();
      final GroupElement h2 = MathUtils.doubleGroupElement(g);

      // Assert:
      Assert.assertThat(h2, IsEqual.equalTo(h1));
    }
  }
  // This test is slow (~6s) due to math utils using an inferior algorithm to calculate the result.
  @Test
  public void scalarMultiplyBasePointReturnsExpectedResult() {
    for (int i = 0; i < 100; i++) {
      // Arrange:
      final GroupElement basePoint = ed25519.getB();
      final FieldElement f = MathUtils.getRandomFieldElement();

      // Act:
      final GroupElement g = basePoint.scalarMultiply(f.toByteArray());
      final GroupElement h = MathUtils.scalarMultiplyGroupElement(basePoint, f);

      // Assert:
      Assert.assertThat(g, IsEqual.equalTo(h));
    }
  }