public async Task PostTransaction_Success() { TransactionValidator validator = CreateValidator(defaultAccounts); ByteString mutation = CreateMutation("http://root/"); ByteString result = await validator.PostTransaction(mutation, new SignatureEvidence[0]); Assert.Equal(32, result.Value.Count); }
public async Task PostTransaction_InvalidNamespace() { TransactionValidator validator = CreateValidator(defaultAccounts); ByteString mutation = CreateMutation("http://wrong-root/"); TransactionInvalidException exception = await Assert.ThrowsAsync <TransactionInvalidException>( () => validator.PostTransaction(mutation, new SignatureEvidence[0])); Assert.Equal("InvalidNamespace", exception.Reason); }
public async Task PostTransaction_Success() { TransactionValidator validator = CreateValidator(defaultAccounts); ByteString mutation = CreateMutation(validNamespace); ByteString result = await validator.PostTransaction(mutation, new SignatureEvidence[0]); Assert.Equal(32, result.Value.Count); Assert.Equal(1, store.AddedTransactions.Count); }
public async Task PostTransaction_InvalidMutation() { TransactionValidator validator = CreateValidator(new Dictionary <string, long>()); ByteString mutation = ByteString.Parse("aa"); TransactionInvalidException exception = await Assert.ThrowsAsync <TransactionInvalidException>( () => validator.PostTransaction(mutation, new SignatureEvidence[0])); Assert.Equal("InvalidMutation", exception.Reason); }
public async Task PostTransaction_GenerateValidMutations() { Mutation generatedMutation = MessageSerializer.DeserializeMutation(CreateMutation(validNamespace)); TransactionValidator validator = CreateValidator(defaultAccounts, generatedMutation); ByteString mutation = CreateMutation(validNamespace); ByteString result = await validator.PostTransaction(mutation, new SignatureEvidence[0]); Assert.Equal(32, result.Value.Count); Assert.Equal(2, store.AddedTransactions.Count); }
public async Task Validate_ValidSignature() { TransactionValidator validator = CreateValidator(defaultAccounts); ByteString mutation = CreateMutation("http://root/"); SignatureEvidence signature = new SignatureEvidence( ByteString.Parse("0213b0006543d4ab6e79f49559fbfb18e9d73596d63f39e2f12ebc2c9d51e2eb06"), ByteString.Parse("304402200c7fba6b623efd7e52731a11e6d7b99c2ae752c0f950b7a444ef7fb80162498c02202b01c74a4a04fb120860494de09bd6848f088927a7b07e3c3925b3894c8c89d4")); ByteString result = await validator.PostTransaction(mutation, new[] { signature }); Assert.Equal(32, result.Value.Count); }
public async Task Validate_ValidSignature() { TransactionValidator validator = CreateValidator(defaultAccounts); ByteString mutation = CreateMutation(validNamespace); SignatureEvidence signature = new SignatureEvidence( ByteString.Parse("0213b0006543d4ab6e79f49559fbfb18e9d73596d63f39e2f12ebc2c9d51e2eb06"), ByteString.Parse("3045022100e2ecc27c2e0d19329a0c7ad37e20fde00e64be235b2e7e86d285c18ff9c1e5b102200efa46125e057136f5008f4aa15a07e5ae4a0fcb2d00aa37862e246abbee74ab")); ByteString result = await validator.PostTransaction(mutation, new[] { signature }); Assert.Equal(32, result.Value.Count); }
public async Task PostTransaction_GenerateInvalidMutations() { Mutation generatedMutation = MessageSerializer.DeserializeMutation(CreateMutation(invalidNamespace)); TransactionValidator validator = CreateValidator(defaultAccounts, generatedMutation); ByteString mutation = CreateMutation(validNamespace); TransactionInvalidException exception = await Assert.ThrowsAsync <TransactionInvalidException>( () => validator.PostTransaction(mutation, new SignatureEvidence[0])); Assert.Equal("InvalidNamespace", exception.Reason); Assert.Equal(null, store.AddedTransactions); }
public async Task PostTransaction_ValidationException() { TransactionValidator validator = new TransactionValidator( new TestStore(defaultAccounts, false), new TestValidator(true), "http://root/"); ByteString mutation = CreateMutation("http://root/"); TransactionInvalidException exception = await Assert.ThrowsAsync <TransactionInvalidException>( () => validator.PostTransaction(mutation, new SignatureEvidence[0])); Assert.Equal("Test", exception.Reason); }
public async Task PostTransaction_ConcurrencyException() { TransactionValidator validator = new TransactionValidator( new TestStore(defaultAccounts, true), new TestValidator(false), validNamespace); ByteString mutation = CreateMutation(validNamespace); TransactionInvalidException exception = await Assert.ThrowsAsync <TransactionInvalidException>( () => validator.PostTransaction(mutation, new SignatureEvidence[0])); Assert.Equal("OptimisticConcurrency", exception.Reason); }
public async Task Validate_InvalidSignature() { TransactionValidator validator = CreateValidator(defaultAccounts); ByteString mutation = CreateMutation(validNamespace); SignatureEvidence signature = new SignatureEvidence( ByteString.Parse("0013b0006543d4ab6e79f49559fbfb18e9d73596d63f39e2f12ebc2c9d51e2eb06"), ByteString.Parse("3045022100e2ecc27c2e0d19329a0c7ad37e20fde00e64be235b2e7e86d285c18ff9c1e5b102200efa46125e057136f5008f4aa15a07e5ae4a0fcb2d00aa37862e246abbee74ab")); TransactionInvalidException exception = await Assert.ThrowsAsync <TransactionInvalidException>( () => validator.PostTransaction(mutation, new[] { signature })); Assert.Equal("InvalidSignature", exception.Reason); Assert.Equal(null, store.AddedTransactions); }
public async Task PostTransaction_EmptyMutation() { Dictionary <string, long> accounts = new Dictionary <string, long>(); TransactionValidator validator = CreateValidator(accounts); Mutation mutation = new Mutation( validNamespace, new Record[0], ByteString.Empty); TransactionInvalidException exception = await Assert.ThrowsAsync <TransactionInvalidException>( () => validator.PostTransaction(new ByteString(MessageSerializer.SerializeMutation(mutation)), new SignatureEvidence[0])); Assert.Equal("InvalidMutation", exception.Reason); }
public async Task PostTransaction_ValidationException() { this.store = new TestStore(defaultAccounts, false); TransactionValidator validator = new TransactionValidator( this.store, new TestValidator(true), validNamespace); ByteString mutation = CreateMutation(validNamespace); TransactionInvalidException exception = await Assert.ThrowsAsync <TransactionInvalidException>( () => validator.PostTransaction(mutation, new SignatureEvidence[0])); Assert.Equal("Test", exception.Reason); Assert.Equal(null, store.AddedTransactions); }
public async Task PostTransaction_UnbalancedTransaction() { Dictionary <string, long> accounts = new Dictionary <string, long>() { ["/account/1/"] = 100, ["/account/2/"] = 110, }; TransactionValidator validator = CreateValidator(accounts); ByteString mutation = CreateMutation("http://root/"); TransactionInvalidException exception = await Assert.ThrowsAsync <TransactionInvalidException>( () => validator.PostTransaction(mutation, new SignatureEvidence[0])); Assert.Equal("UnbalancedTransaction", exception.Reason); }
public async Task PostTransaction_MaxKeySize() { Dictionary <string, long> accounts = new Dictionary <string, long>(); TransactionValidator validator = CreateValidator(accounts); Mutation mutation = new Mutation( new ByteString(Encoding.UTF8.GetBytes("http://root/")), new Record[] { new Record( ByteString.Parse(new string('a', 1025 * 2)), new ByteString(BitConverter.GetBytes(100L).Reverse()), ByteString.Empty) }, ByteString.Empty); TransactionInvalidException exception = await Assert.ThrowsAsync <TransactionInvalidException>( () => validator.PostTransaction(new ByteString(MessageSerializer.SerializeMutation(mutation)), new SignatureEvidence[0])); Assert.Equal("InvalidMutation", exception.Reason); }
public async Task PostTransaction_MaxKeySize() { Dictionary <string, long> accounts = new Dictionary <string, long>(); TransactionValidator validator = CreateValidator(accounts); Mutation mutation = new Mutation( validNamespace, new Record[] { new Record( new AccountKey(LedgerPath.Parse("/"), LedgerPath.Parse($"/{new string('a', 512)}/")).Key.ToBinary(), new ByteString(BitConverter.GetBytes(100L).Reverse()), ByteString.Empty) }, ByteString.Empty); TransactionInvalidException exception = await Assert.ThrowsAsync <TransactionInvalidException>( () => validator.PostTransaction(new ByteString(MessageSerializer.SerializeMutation(mutation)), new SignatureEvidence[0])); Assert.Equal("InvalidMutation", exception.Reason); Assert.Equal(null, store.AddedTransactions); }
public async Task <ActionResult> Post() { if (validator == null) { return(CreateErrorResponse("ValidationDisabled")); } JObject body; try { string bodyContent; using (StreamReader streamReader = new StreamReader(Request.Body)) bodyContent = await streamReader.ReadToEndAsync(); body = JObject.Parse(bodyContent); } catch (JsonReaderException) { return(BadRequest()); } ByteString parsedMutation; List <SignatureEvidence> authentication = new List <SignatureEvidence>(); if (!(body["mutation"] is JValue && body["signatures"] is JArray)) { return(BadRequest()); } try { parsedMutation = ByteString.Parse((string)body["mutation"]); foreach (JToken signatureItem in body["signatures"]) { JObject evidence = signatureItem as JObject; if (!(evidence != null && evidence["pub_key"] is JValue && evidence["signature"] is JValue)) { return(BadRequest()); } authentication.Add(new SignatureEvidence( ByteString.Parse((string)evidence["pub_key"]), ByteString.Parse((string)evidence["signature"]))); } } catch (FormatException) { return(BadRequest()); } ByteString transactionId; try { transactionId = await validator.PostTransaction(parsedMutation, authentication); } catch (TransactionInvalidException exception) { logger.LogInformation("Rejected transaction: {0}", exception.Message); return(CreateErrorResponse(exception.Reason)); } logger.LogInformation("Validated transaction {0}", transactionId.ToString()); return(Json(new { transaction_hash = transactionId.ToString(), mutation_hash = new ByteString(MessageSerializer.ComputeHash(parsedMutation.ToByteArray())).ToString() })); }
public async Task PostTransaction_ConcurrencyException() { this.store = new TestStore(defaultAccounts, true); TransactionValidator validator = new TransactionValidator( this.store, new TestValidator(false), validNamespace); ByteString mutation = CreateMutation(validNamespace); TransactionInvalidException exception = await Assert.ThrowsAsync<TransactionInvalidException>( () => validator.PostTransaction(mutation, new SignatureEvidence[0])); Assert.Equal("OptimisticConcurrency", exception.Reason); Assert.Equal(1, store.AddedTransactions.Count); }
public async Task PostTransaction_ValidationException() { TransactionValidator validator = new TransactionValidator( new TestStore(defaultAccounts, false), new TestValidator(true), validNamespace); ByteString mutation = CreateMutation(validNamespace); TransactionInvalidException exception = await Assert.ThrowsAsync<TransactionInvalidException>( () => validator.PostTransaction(mutation, new SignatureEvidence[0])); Assert.Equal("Test", exception.Reason); }