Skip to content

Commit

Permalink
test: add basic unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Aug 20, 2024
1 parent 3ff0ef1 commit 286f863
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 61 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ jobs:

- name: Clippy
run: cargo clippy

- name: Unit tests
run: cargo test
118 changes: 59 additions & 59 deletions src/database/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,59 @@ use diesel::{AsChangeset, Associations, Identifiable, Insertable, Queryable, Sel
use serde::Serialize;
use std::fmt::{Display, Formatter};

#[derive(Queryable, Identifiable, Selectable, AsChangeset, Serialize, Debug, PartialEq, Clone)]
#[diesel(table_name = crate::database::schema::invoices)]
pub struct Invoice {
pub id: i64,
pub payment_hash: Vec<u8>,
pub preimage: Option<Vec<u8>>,
pub bolt11: String,
pub state: String,
pub created_at: chrono::NaiveDateTime,
}

#[derive(Insertable, Debug, PartialEq, Clone)]
#[diesel(table_name = crate::database::schema::invoices)]
pub struct InvoiceInsertable {
pub payment_hash: Vec<u8>,
pub bolt11: String,
pub state: String,
}

#[derive(
Queryable,
Identifiable,
Selectable,
Associations,
Insertable,
AsChangeset,
Serialize,
Debug,
PartialEq,
Clone,
)]
#[diesel(belongs_to(Invoice))]
#[diesel(table_name = crate::database::schema::htlcs)]
pub struct Htlc {
pub id: i64,
pub invoice_id: i64,
pub state: String,
pub scid: String,
pub channel_id: i64,
pub msat: i64,
pub created_at: chrono::NaiveDateTime,
}

#[derive(Insertable, Debug, PartialEq, Clone)]
#[diesel(table_name = crate::database::schema::htlcs)]
pub struct HtlcInsertable {
pub invoice_id: i64,
pub state: String,
pub scid: String,
pub channel_id: i64,
pub msat: i64,
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub enum InvoiceState {
Paid = 0,
Expand All @@ -17,12 +70,6 @@ impl Display for InvoiceState {
}
}

impl InvoiceState {
pub fn is_final(&self) -> bool {
*self == InvoiceState::Paid || *self == InvoiceState::Cancelled
}
}

impl From<InvoiceState> for String {
fn from(value: InvoiceState) -> Self {
match value {
Expand All @@ -49,6 +96,12 @@ impl TryFrom<&str> for InvoiceState {
}
}

impl InvoiceState {
pub fn is_final(&self) -> bool {
*self == InvoiceState::Paid || *self == InvoiceState::Cancelled
}
}

#[derive(Serialize, Clone, Debug)]
pub struct HoldInvoice {
pub invoice: Invoice,
Expand Down Expand Up @@ -78,56 +131,3 @@ impl HoldInvoice {
.any(|htlc| htlc.scid == scid && htlc.channel_id == id as i64)
}
}

#[derive(Queryable, Identifiable, Selectable, AsChangeset, Serialize, Debug, PartialEq, Clone)]
#[diesel(table_name = crate::database::schema::invoices)]
pub struct Invoice {
pub id: i64,
pub payment_hash: Vec<u8>,
pub preimage: Option<Vec<u8>>,
pub bolt11: String,
pub state: String,
pub created_at: chrono::NaiveDateTime,
}

#[derive(Insertable, Debug, PartialEq, Clone)]
#[diesel(table_name = crate::database::schema::invoices)]
pub struct InvoiceInsertable {
pub payment_hash: Vec<u8>,
pub bolt11: String,
pub state: String,
}

#[derive(
Queryable,
Identifiable,
Selectable,
Associations,
Insertable,
AsChangeset,
Serialize,
Debug,
PartialEq,
Clone,
)]
#[diesel(belongs_to(Invoice))]
#[diesel(table_name = crate::database::schema::htlcs)]
pub struct Htlc {
pub id: i64,
pub invoice_id: i64,
pub state: String,
pub scid: String,
pub channel_id: i64,
pub msat: i64,
pub created_at: chrono::NaiveDateTime,
}

#[derive(Insertable, Debug, PartialEq, Clone)]
#[diesel(table_name = crate::database::schema::htlcs)]
pub struct HtlcInsertable {
pub invoice_id: i64,
pub state: String,
pub scid: String,
pub channel_id: i64,
pub msat: i64,
}
94 changes: 94 additions & 0 deletions src/grpc/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,97 @@ fn generate_certificate(
Vec::from(cert.pem().as_bytes()),
))
}

#[cfg(test)]
mod test {
use crate::grpc::tls::{generate_certificate, generate_or_load_certificate, load_certificates};
use rcgen::{CertificateParams, KeyPair};
use std::fs;
use std::path::Path;

#[test]
fn test_load_certificates() {
let certs_dir = "test-certs-all";
assert_eq!(Path::new(certs_dir).exists(), false);

let (_, cert) = load_certificates(certs_dir.into()).unwrap();
assert_eq!(Path::new(certs_dir).exists(), true);

for file in vec!["ca", "client", "server"]
.iter()
.flat_map(|entry| vec![format!("{}.pem", entry), format!("{}-key.pem", entry)])
{
assert_eq!(Path::new(certs_dir).join(file).exists(), true);
}

let (_, cert_loaded) = load_certificates(certs_dir.into()).unwrap();
assert_eq!(cert.into_inner(), cert_loaded.into_inner());

fs::remove_dir_all(certs_dir).unwrap();
}

#[test]
fn test_generate_or_load_certificate() {
let certs_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("test-certs-load".to_string());
fs::create_dir(certs_dir.clone()).unwrap();

let (created_key, created_cert) =
generate_or_load_certificate("test", Path::new(&certs_dir), "ca", None).unwrap();
let (loaded_key, loaded_cert) =
generate_or_load_certificate("test", Path::new(&certs_dir), "ca", None).unwrap();

assert_eq!(created_key, loaded_key);
assert_eq!(created_cert, loaded_cert);

fs::remove_dir_all(certs_dir).unwrap();
}

#[test]
fn test_generate_certificate_ca() {
let certs_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("test-certs1".to_string());
fs::create_dir(certs_dir.clone()).unwrap();

let key_path = certs_dir.clone().join("key.pem");
let cert_path = certs_dir.clone().join("cert.pem");
let (key, cert) =
generate_certificate("test", key_path.clone(), cert_path.clone(), None).unwrap();

assert_eq!(key, fs::read(key_path).unwrap());
assert_eq!(cert, fs::read(cert_path).unwrap());

fs::remove_dir_all(certs_dir).unwrap();
}

#[test]
fn test_generate_certificate() {
let certs_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("test-certs2".to_string());
fs::create_dir(certs_dir.clone()).unwrap();

let (ca_key, ca_cert) = generate_certificate(
"test",
certs_dir.clone().join("ca-key.pem"),
certs_dir.clone().join("ca.pem"),
None,
)
.unwrap();

let ca_keypair = KeyPair::from_pem(&String::from_utf8_lossy(&ca_key)).unwrap();
let ca = (
&ca_keypair,
&CertificateParams::from_ca_cert_pem(&String::from_utf8_lossy(&ca_cert.clone()))
.unwrap()
.self_signed(&ca_keypair)
.unwrap(),
);

let key_path = certs_dir.clone().join("client-key.pem");
let cert_path = certs_dir.clone().join("client.pem");
let (client_key, client_cert) =
generate_certificate("test", key_path.clone(), cert_path.clone(), Some(ca)).unwrap();

assert_eq!(client_key, fs::read(key_path).unwrap());
assert_eq!(client_cert, fs::read(cert_path).unwrap());

fs::remove_dir_all(certs_dir).unwrap();
}
}
4 changes: 2 additions & 2 deletions src/grpc/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ impl From<HoldInvoice> for hold::Invoice {
fn from(value: HoldInvoice) -> Self {
hold::Invoice {
id: value.invoice.id,
payment_hash: vec![],
preimage: value.invoice.preimage,
bolt11: value.invoice.bolt11,
preimage: value.invoice.preimage,
payment_hash: value.invoice.payment_hash,
state: transform_invoice_state(
InvoiceState::try_from(value.invoice.state.as_str()).unwrap(),
),
Expand Down

0 comments on commit 286f863

Please sign in to comment.