Skip to content
Permalink
Browse files

Integrationo tests for witness & basic networking test

Signed-off-by: dkijania <dariusz.kijania@iohk.io>
  • Loading branch information...
dkijania committed May 14, 2019
1 parent dfe0ac7 commit f91128766924ce7b92b3fab53f37b673bb75fc57
@@ -89,7 +89,7 @@ impl NodeConfig {

fn get_available_port() -> u16 {
let available_port = loop {
let port = rand::thread_rng().gen_range(8000, 9999);
let port = rand::thread_rng().gen_range(6000, 9999);
if port_is_available(port) {
break port;
}
@@ -2,7 +2,7 @@

extern crate mktemp;

use std::fs::File;
use std::fs;
use std::io::Write;
use std::path::PathBuf;

@@ -40,15 +40,24 @@ pub fn get_temp_folder() -> PathBuf {
/// Creates file in temporary folder
pub fn create_file_in_temp(file_name: &str, content: &str) -> PathBuf {
let path = get_path_in_temp(&file_name);
let mut file = File::create(&path).unwrap();
let mut file = std::fs::File::create(&path).unwrap();
file.write_all(content.as_bytes())
.expect(&format!("cannot write to file {:?}", path));
path
}

/// Creates file with content
pub fn create_file_with_content(path: &PathBuf, content: &str) -> () {
let mut file = File::create(&path).unwrap();
let mut file = std::fs::File::create(&path).unwrap();
file.write_all(content.as_bytes())
.expect(&format!("cannot write to file {:?}", path));
}

pub fn make_readonly(path: &PathBuf) {
if !path.exists() {
std::fs::File::create(&path).unwrap();
}
let mut perms = fs::metadata(path.as_os_str()).unwrap().permissions();
perms.set_readonly(true);
fs::set_permissions(path.as_os_str(), perms).expect("cannot set permissions");
}
@@ -16,9 +16,9 @@ use std::path::PathBuf;

#[derive(Debug)]
pub struct JCLITransactionWrapper {
staging_file_path: PathBuf,
pub staging_file_path: PathBuf,
commands: TransactionCommands,
genesis_hash: String,
pub genesis_hash: String,
}

impl JCLITransactionWrapper {
@@ -158,6 +158,14 @@ impl JCLITransactionWrapper {
self
}

pub fn assert_add_account_fail(&self, account_addr: &str, amount: &i32, expected_msg: &str) {
process_assert::assert_process_failed_and_matches_message(
self.commands
.get_add_account_command(&account_addr, &amount, &self.staging_file_path),
expected_msg,
);
}

pub fn assert_add_account_from_legacy<'a>(
&'a mut self,
fund: &Fund,
@@ -259,6 +267,20 @@ impl JCLITransactionWrapper {
self
}

pub fn assert_make_witness_fails(&self, witness: &Witness, expected_msg: &str) {
process_assert::assert_process_failed_and_matches_message(
self.commands.get_make_witness_command(
&witness.block_hash,
&witness.transaction_id,
&witness.addr_type,
&witness.spending_account_counter,
&witness.file,
&witness.private_key_path,
),
&expected_msg,
);
}

pub fn create_witness_from_key(&self, private_key: &str, addr_type: &str) -> Witness {
let transaction_id = self.get_transaction_id();
let witness = Witness::new(
@@ -278,22 +300,11 @@ impl JCLITransactionWrapper {

pub fn assert_add_witness_fail<'a>(&'a mut self, witness: &Witness, expected_part: &str) -> () {
println!("Runing add transaction witness command...");

let output = process_utils::run_process_and_get_output(
process_assert::assert_process_failed_and_matches_message(
self.commands
.get_add_witness_command(&witness.file, &self.staging_file_path),
expected_part,
);
let actual = output.err_as_single_line();

assert_eq!(
actual.contains(expected_part),
true,
"message : '{}' does not contain expected part '{}'",
&actual,
&expected_part
);

process_assert::assert_process_failed(output);
}

pub fn assert_add_witness<'a>(
@@ -332,6 +343,15 @@ impl JCLITransactionWrapper {
content
}

pub fn assert_transaction_to_message_fails(&self, expected_msg: &str) {
println!("Runing transaction to message command...");
process_assert::assert_process_failed_and_matches_message(
self.commands
.get_transaction_message_to_command(&self.staging_file_path),
expected_msg,
);
}

pub fn get_transaction_id(&self) -> String {
println!("Runing get transaction id command...");

@@ -1,43 +1,11 @@
#![allow(dead_code)]
#![cfg(feature = "integration-test")]

use common::configuration::genesis_model::Fund;
use common::jcli_wrapper;
use common::jcli_wrapper::jcli_transaction_wrapper::JCLITransactionWrapper;
use common::startup;

#[test]
#[cfg(feature = "integration-test")]
pub fn test_utxo_transation_with_more_than_one_witness_per_input_is_rejected() {
let sender = startup::create_new_utxo_address();
let reciever = startup::create_new_utxo_address();
let mut config = startup::from_initial_funds(vec![Fund {
address: sender.address.clone(),
value: 100,
}]);

let jormungandr_rest_address = config.get_node_address();
let _jormungandr = startup::start_jormungandr_node_as_leader(&mut config);
let utxo = startup::get_utxo_for_address(&sender, &jormungandr_rest_address);
let block0_hash = jcli_wrapper::assert_genesis_hash(&config.genesis_block_path);

let mut transaction_wrapper = JCLITransactionWrapper::new_transaction(&block0_hash);
transaction_wrapper
.assert_add_input_from_utxo(&utxo)
.assert_add_output(&reciever.address, &utxo.out_value)
.assert_finalize();

let witness1 = transaction_wrapper.create_witness_default("utxo");
let witness2 = transaction_wrapper.create_witness_default("utxo");

transaction_wrapper
.assert_make_witness(&witness1)
.assert_add_witness(&witness1)
.assert_make_witness(&witness2)
.assert_add_witness_fail(&witness2, "cannot add anymore witnesses");
}

#[test]
#[cfg(feature = "integration-test")]
pub fn test_two_correct_utxo_to_utxo_transactions_are_accepted_by_node() {
let sender = startup::create_new_utxo_address();
let middle_man = startup::create_new_utxo_address();
@@ -81,7 +49,6 @@ pub fn test_two_correct_utxo_to_utxo_transactions_are_accepted_by_node() {
}

#[test]
#[cfg(feature = "integration-test")]
pub fn test_correct_utxo_transaction_is_accepted_by_node() {
let sender = startup::create_new_utxo_address();
let reciever = startup::create_new_utxo_address();
@@ -107,7 +74,6 @@ pub fn test_correct_utxo_transaction_is_accepted_by_node() {
}

#[test]
#[cfg(feature = "integration-test")]
pub fn test_transaction_from_utxo_to_account_is_accepted_by_node() {
let sender = startup::create_new_utxo_address();
let reciever = startup::create_new_account_address();
@@ -133,7 +99,6 @@ pub fn test_transaction_from_utxo_to_account_is_accepted_by_node() {
}

#[test]
#[cfg(feature = "integration-test")]
pub fn test_transaction_from_account_to_account_is_accepted_by_node() {
let sender = startup::create_new_account_address();
let reciever = startup::create_new_account_address();
@@ -156,3 +121,52 @@ pub fn test_transaction_from_account_to_account_is_accepted_by_node() {

jcli_wrapper::assert_transaction_post_accepted(&transaction_message, &jormungandr_rest_address);
}

#[test]
pub fn test_transaction_from_account_to_delegation_is_accepted_by_node() {
let sender = startup::create_new_account_address();
let reciever = startup::create_new_delegation_address();
let transfer_amount = 100;
let mut config = startup::from_initial_funds(vec![Fund {
address: sender.address.clone(),
value: transfer_amount.clone(),
}]);

let jormungandr_rest_address = config.get_node_address();
let _jormungandr = startup::start_jormungandr_node_as_leader(&mut config);
let block0_hash = jcli_wrapper::assert_genesis_hash(&config.genesis_block_path);

let transaction_message = JCLITransactionWrapper::new_transaction(&block0_hash)
.assert_add_account(&sender.address, &transfer_amount)
.assert_add_output(&reciever.address, &transfer_amount)
.assert_finalize()
.seal_with_witness_deafult(&sender.private_key, "account")
.assert_transaction_to_message();

jcli_wrapper::assert_transaction_post_accepted(&transaction_message, &jormungandr_rest_address);
}

#[test]
pub fn test_transaction_from_utxo_to_delegation_is_accepted_by_node() {
let sender = startup::create_new_utxo_address();
let reciever = startup::create_new_delegation_address();
let transfer_amount = 100;
let mut config = startup::from_initial_funds(vec![Fund {
address: sender.address.clone(),
value: transfer_amount.clone(),
}]);

let jormungandr_rest_address = config.get_node_address();
let _jormungandr = startup::start_jormungandr_node_as_leader(&mut config);
let block0_hash = jcli_wrapper::assert_genesis_hash(&config.genesis_block_path);
let utxo = startup::get_utxo_for_address(&sender, &jormungandr_rest_address);

let transaction_message = JCLITransactionWrapper::new_transaction(&block0_hash)
.assert_add_input_from_utxo(&utxo)
.assert_add_output(&reciever.address, &transfer_amount)
.assert_finalize()
.seal_with_witness_deafult(&sender.private_key, "utxo")
.assert_transaction_to_message();

jcli_wrapper::assert_transaction_post_accepted(&transaction_message, &jormungandr_rest_address);
}
@@ -1,6 +1,7 @@
#![allow(dead_code)]
#![cfg(feature = "integration-test")]

use common::configuration::genesis_model::Fund;
use common::file_utils;
use common::jcli_wrapper;
use common::jcli_wrapper::jcli_transaction_wrapper::JCLITransactionWrapper;
use common::startup;
@@ -10,7 +11,6 @@ const FAKE_INPUT_TRANSACTION_ID: &str =
const FAKE_GENESIS_HASH: &str = "19c9852ca0a68f15d0f7de5d1a26acd67a3a3251640c6066bdb91d22e2000193";

#[test]
#[cfg(feature = "integration-test")]
pub fn test_cannot_create_input_with_negative_amount() {
JCLITransactionWrapper::new_transaction(FAKE_GENESIS_HASH).assert_add_input_fail(
&FAKE_INPUT_TRANSACTION_ID,
@@ -21,7 +21,6 @@ pub fn test_cannot_create_input_with_negative_amount() {
}

#[test]
#[cfg(feature = "integration-test")]
pub fn test_cannot_create_input_with_too_big_utxo_amount() {
JCLITransactionWrapper::new_transaction(FAKE_GENESIS_HASH).assert_add_input_fail(
&FAKE_INPUT_TRANSACTION_ID,
@@ -32,7 +31,6 @@ pub fn test_cannot_create_input_with_too_big_utxo_amount() {
}

#[test]
#[cfg(feature = "integration-test")]
pub fn test_unbalanced_output_utxo_transation_is_not_finalized() {
let reciever = startup::create_new_utxo_address();

@@ -43,7 +41,40 @@ pub fn test_unbalanced_output_utxo_transation_is_not_finalized() {
}

#[test]
#[cfg(feature = "integration-test")]
pub fn test_add_account_for_utxo_address_fails() {
let sender = startup::create_new_utxo_address();

JCLITransactionWrapper::new_transaction(FAKE_GENESIS_HASH).assert_add_account_fail(
&sender.address,
&100,
"Invalid input account, this is a UTxO address",
);
}

#[test]
pub fn test_cannot_create_input_when_staging_file_is_readonly() {
let mut transaction_wrapper = JCLITransactionWrapper::new_transaction(FAKE_GENESIS_HASH);
file_utils::make_readonly(&transaction_wrapper.staging_file_path);
transaction_wrapper.assert_add_input_fail(
&FAKE_INPUT_TRANSACTION_ID,
&0,
"100",
"Access is denied",
);
}

#[test]
pub fn test_add_account_for_utxo_delegation_address_fails() {
let sender = startup::create_new_delegation_address();

JCLITransactionWrapper::new_transaction(FAKE_GENESIS_HASH).assert_add_account_fail(
&sender.address,
&100,
"Invalid input account, this is a UTxO address",
);
}

#[test]
pub fn test_transaction_with_input_address_equal_to_output_is_accepted_by_node() {
let sender = startup::create_new_utxo_address();
let mut config = startup::from_initial_funds(vec![Fund {
@@ -68,7 +99,6 @@ pub fn test_transaction_with_input_address_equal_to_output_is_accepted_by_node()
}

#[test]
#[cfg(feature = "integration-test")]
pub fn test_input_with_smaller_value_than_initial_utxo_is_rejected_by_node() {
let sender = startup::create_new_utxo_address();
let reciever = startup::create_new_utxo_address();
@@ -98,7 +128,6 @@ pub fn test_input_with_smaller_value_than_initial_utxo_is_rejected_by_node() {
}

#[test]
#[cfg(feature = "integration-test")]
pub fn test_input_with_no_spending_utxo_is_accepted_by_node() {
let sender = startup::create_new_utxo_address();
let reciever = startup::create_new_utxo_address();
@@ -124,7 +153,6 @@ pub fn test_input_with_no_spending_utxo_is_accepted_by_node() {
}

#[test]
#[cfg(feature = "integration-test")]
pub fn test_transaction_with_non_existing_id_should_be_rejected_by_node() {
let sender = startup::create_new_utxo_address();
let reciever = startup::create_new_utxo_address();
@@ -1,2 +1,3 @@
pub mod e2e_transaction_tests;
pub mod input_output_tests;
pub mod witness_tests;
Oops, something went wrong.

0 comments on commit f911287

Please sign in to comment.
You can’t perform that action at this time.