Skip to content
Permalink
Browse files

Certificate module integration test

Signed-off-by: dkijania <dariusz.kijania@iohk.io>
  • Loading branch information...
dkijania committed May 14, 2019
1 parent da1cb1d commit df0b7ca10191bd90e8dabf77b4cbe4a500c4a947
@@ -2,6 +2,7 @@

extern crate mktemp;

use std::fs;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
@@ -52,3 +53,8 @@ pub fn create_file_with_content(path: &PathBuf, content: &str) -> () {
file.write_all(content.as_bytes())
.expect(&format!("cannot write to file {:?}", path));
}

pub fn read_file(path: &PathBuf) -> String {
let contents = fs::read_to_string(path).expect("cannot read file");
contents
}
@@ -0,0 +1,93 @@
#![allow(dead_code)]

use std::path::PathBuf;
use std::process::Command;

use common::configuration;

#[derive(Debug)]
pub struct CertificateCommands {}

impl CertificateCommands {
pub fn new() -> CertificateCommands {
CertificateCommands {}
}

pub fn get_new_stake_delegation_command(
&self,
stake_pool_id: &str,
delegation_id: &str,
) -> Command {
let mut command = Command::new(configuration::get_jcli_app().as_os_str());
command
.arg("certificate")
.arg("new")
.arg("stake-delegation")
.arg(&stake_pool_id)
.arg(&delegation_id);
command
}

pub fn get_stake_key_registration_command(&self, delegation_key: &str) -> Command {
let mut command = Command::new(configuration::get_jcli_app().as_os_str());
command
.arg("certificate")
.arg("new")
.arg("stake-key-registration")
.arg(&delegation_key);
command
}

pub fn get_stake_pool_registration_command(
&self,
kes_key: &str,
serial_id: &str,
vrf_key: &str,
owner_public_key: &str,
) -> Command {
let mut command = Command::new(configuration::get_jcli_app().as_os_str());
command
.arg("certificate")
.arg("new")
.arg("stake-pool-registration")
.arg("--kes-key")
.arg(&kes_key)
.arg("--vrf-key")
.arg(&vrf_key)
.arg("--serial")
.arg(&serial_id)
.arg("--owner")
.arg(&owner_public_key);
command
}

pub fn get_stake_pool_id_command(
&self,
input_file: &PathBuf,
output_file: &PathBuf,
) -> Command {
let mut command = Command::new(configuration::get_jcli_app().as_os_str());
command
.arg("certificate")
.arg("get-stake-pool-id")
.arg(&input_file)
.arg(&output_file);
command
}

pub fn get_sign_command(
&self,
signing_key: &PathBuf,
input_file: &PathBuf,
output_file: &PathBuf,
) -> Command {
let mut command = Command::new(configuration::get_jcli_app().as_os_str());
command
.arg("certificate")
.arg("sign")
.arg(&signing_key.as_os_str())
.arg(&input_file.as_os_str())
.arg(&output_file.as_os_str());
command
}
}
@@ -0,0 +1,2 @@
pub mod commands;
pub mod wrapper;
@@ -0,0 +1,93 @@
use super::commands::CertificateCommands;

use common::file_assert;
use common::file_utils;
use common::process_assert;
use common::process_utils;
use common::process_utils::output_extensions::ProcessOutput;
use std::path::PathBuf;

#[derive(Debug)]
pub struct JCLICertificateWrapper {
commands: CertificateCommands,
}

impl JCLICertificateWrapper {
pub fn new() -> JCLICertificateWrapper {
JCLICertificateWrapper {
commands: CertificateCommands::new(),
}
}

pub fn assert_new_stake_delegation(&self, stake_pool_id: &str, delegation_id: &str) -> String {
println!("Running new stake delegation...");
let output = process_utils::run_process_and_get_output(
self.commands
.get_new_stake_delegation_command(&stake_pool_id, &delegation_id),
);
let certification = output.as_single_line();
process_assert::assert_process_exited_successfully(output);
certification
}

pub fn assert_new_stake_key_registration(&self, delegation_key: &str) -> String {
println!("Running new stake key registration...");
let output = process_utils::run_process_and_get_output(
self.commands
.get_stake_key_registration_command(&delegation_key),
);
let certification = output.as_single_line();
process_assert::assert_process_exited_successfully(output);
certification
}

pub fn assert_new_stake_pool_registration(
&self,
kes_key: &str,
serial_id: &str,
vrf_key: &str,
owner_public_key: &str,
) -> String {
println!("Running new stake pool registration...");
let output = process_utils::run_process_and_get_output(
self.commands.get_stake_pool_registration_command(
&kes_key,
&serial_id,
&vrf_key,
&owner_public_key,
),
);
let certification = output.as_single_line();
process_assert::assert_process_exited_successfully(output);
certification
}

pub fn assert_get_stake_pool_id(&self, input_file: &PathBuf, output_file: &PathBuf) -> String {
println!("Running get stake pool id...");
let output = process_utils::run_process_and_get_output(
self.commands
.get_stake_pool_id_command(&input_file, &output_file),
);
process_assert::assert_process_exited_successfully(output);
file_assert::assert_file_exists_and_not_empty(&output_file);
let certification = file_utils::read_file(&output_file);
certification
}

pub fn assert_sign(
&self,
signing_key: &PathBuf,
input_file: &PathBuf,
output_file: &PathBuf,
) -> String {
println!("Running sign certification...");
let output = process_utils::run_process_and_get_output(self.commands.get_sign_command(
&signing_key,
&input_file,
&output_file,
));
let certification = output.as_single_line();
process_assert::assert_process_exited_successfully(output);
certification
}
}
@@ -1,5 +1,6 @@
#![allow(dead_code)]

pub mod certificate;
pub mod jcli_commands;
pub mod jcli_transaction_wrapper;

@@ -0,0 +1 @@
pub mod new_certificate_test;
@@ -0,0 +1,45 @@
#![cfg(feature = "integration-test")]

use common::file_assert;
use common::file_utils;
use common::jcli_wrapper;
use common::jcli_wrapper::certificate::wrapper::JCLICertificateWrapper;

#[test]
pub fn test_create_and_sign_new_stake_delegation() {
let owner_private_key = jcli_wrapper::assert_key_generate_default();
let owner_public_key = jcli_wrapper::assert_key_to_public_default(&owner_private_key);

let kes_private_key = jcli_wrapper::assert_key_generate("FakeMMM");
let kes_public_key = jcli_wrapper::assert_key_to_public_default(&kes_private_key);

let vrf_private_key = jcli_wrapper::assert_key_generate("Curve25519_2HashDH");
let vrf_public_key = jcli_wrapper::assert_key_to_public_default(&vrf_private_key);

let serial_id = "13919597664319319060838570079442950054";

let certificate_wrapper = JCLICertificateWrapper::new();
let certificate = certificate_wrapper.assert_new_stake_pool_registration(
&kes_public_key,
&serial_id,
&vrf_public_key,
&owner_public_key,
);

let input_file = file_utils::create_file_in_temp("certificate", &certificate);
let output_file = file_utils::get_path_in_temp("pool_id");

let stake_pool_id = certificate_wrapper.assert_get_stake_pool_id(&input_file, &output_file);
let certificate =
certificate_wrapper.assert_new_stake_delegation(&stake_pool_id, &owner_public_key);

assert_ne!(certificate, "", "delegation cert is empty");

let signed_cert = file_utils::get_path_in_temp("signed_cert");
let owner_private_key_file =
file_utils::create_file_in_temp("owner.private", &owner_private_key);

certificate_wrapper.assert_sign(&owner_private_key_file, &input_file, &signed_cert);

file_assert::assert_file_exists_and_not_empty(&signed_cert);
}
@@ -1,4 +1,5 @@
pub mod common;
pub mod jcli_certificates;
pub mod jcli_transaction;

// The purpose of this file is to allow cargo correctly detect tests located in subfolders. It acts like lib.rs file.

0 comments on commit df0b7ca

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