Skip to content

Commit

Permalink
Merge pull request #13 from shubhexists/4-print-success-messsges-also
Browse files Browse the repository at this point in the history
feat(test): introduced tests
  • Loading branch information
shubhexists authored Sep 5, 2024
2 parents 2157718 + 352a744 commit 9a5b038
Show file tree
Hide file tree
Showing 9 changed files with 10,683 additions and 12 deletions.
2 changes: 1 addition & 1 deletion core/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/target
/target
137 changes: 137 additions & 0 deletions core/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ categories = ["development-tools", "command-line-utilities", "parsing", "os", "h
keywords = ["https", "certificates", "localhost", "local-development", "root-ca"]
readme = "README.md"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
openssl = "0.10"
clap = { version = "4.4.8", features = ["derive"] }
dirs = "5.0"
sha2 = "0.10"
lazy_static = "1.4"
colored = "2.0"
colored = "2.0"
base64 = "0.21"

[dev-dependencies]
tempfile = "3.3"
5 changes: 2 additions & 3 deletions core/src/commands/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use crate::{
trust_stores::{
firefox::FirefoxTrustStore, nss::NSSValue, nss_profile::NSSProfile,
utils::check_if_firefox_exists, CAValue,
},
x509::{ca_req::CAReq, distinguished_name::DistinguishedName, leaf_cert::LeafCert},
}, utils::get_unique_hash, x509::{ca_req::CAReq, distinguished_name::DistinguishedName, leaf_cert::LeafCert}
};
use colored::*;
use openssl::{
Expand All @@ -20,8 +19,8 @@ pub fn generate_install(cert: X509) -> Result<(), Box<dyn Error>> {
let ca_value_object: CAValue = CAValue { certificate: cert };
ca_value_object.install_certificate()?;
let nss_profile_object: NSSProfile = NSSProfile::new();
let ca_unique_name: String = "vanish-root-test-123456-shubham-brr".to_string();
let caroot: String = "/home/jerry/.local/share/vanish/ca_cert.pem".to_string();
let ca_unique_name: String = get_unique_hash(&caroot)?;
let mkcert: NSSValue =
NSSValue::new(nss_profile_object, ca_unique_name.clone(), caroot.clone());
let success: bool = mkcert.install_nss();
Expand Down
2 changes: 2 additions & 0 deletions core/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ mod x509;
use clap::{Parser, Subcommand};
use commands::generate::generate;
use std::env;
#[cfg(test)]
mod utils_tests;

#[derive(Parser)]
#[clap(
Expand Down
4 changes: 2 additions & 2 deletions core/src/trust_stores/nss_profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::path::{Path, PathBuf};
use std::process::Command;

pub struct NSSProfile {
pub has_nss: bool,
pub _has_nss: bool,
pub has_certutil: bool,
pub certutil_path: Option<String>,
pub nss_dbs: Vec<String>,
Expand Down Expand Up @@ -60,7 +60,7 @@ impl NSSProfile {
}

Self {
has_nss,
_has_nss: has_nss,
has_certutil,
certutil_path,
nss_dbs,
Expand Down
31 changes: 28 additions & 3 deletions core/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,25 @@ use crate::{
errors::{CertKeyPairError, CertKeyResult, SerialNumberError, SerialNumberResult},
x509::{self, ca_cert::CACert},
};
use base64::engine::general_purpose::URL_SAFE;
use base64::Engine;
use colored::*;
use openssl::{
asn1::Asn1Integer,
bn::BigNum,
error::ErrorStack,
pkey::{PKey, Private},
rsa::Rsa,
sha::Sha256,
x509::X509,
};
use std::{error, fs, io, path::Path, process::Output};
use std::{
error,
fs::{self, File},
io::{self, Read},
path::Path,
process::Output,
};
use std::{path::PathBuf, process::Command};

pub fn generate_cert_key_pair() -> CertKeyResult<(Rsa<Private>, PKey<Private>)> {
Expand Down Expand Up @@ -149,13 +158,29 @@ pub fn save_generated_cert_key_files(
}
}

pub fn _path_exists(path: &str) -> bool {
#[allow(dead_code)]
pub fn path_exists(path: &str) -> bool {
Path::new(path).exists()
}

pub fn _binary_exists(binary: &str) -> bool {
#[allow(dead_code)]
pub fn binary_exists(binary: &str) -> bool {
Command::new(binary)
.output()
.map(|output: Output| output.status.success())
.unwrap_or(false)
}

#[allow(dead_code)]
pub fn get_unique_hash(csr_path: &str) -> Result<String, io::Error> {
let mut file: File = File::open(csr_path)?;
let mut csr_contents: Vec<u8> = Vec::new();
file.read_to_end(&mut csr_contents)?;
let mut hasher: Sha256 = Sha256::new();
hasher.update(&csr_contents);
let result: [u8; 32] = hasher.finish();
let mut unique_name: String = URL_SAFE.encode(result);
unique_name = unique_name.trim_end_matches('=').to_string();

Ok(unique_name)
}
Loading

0 comments on commit 9a5b038

Please sign in to comment.