Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixups for directory by listener #8

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions payjoin-directory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,10 @@ pub async fn listen_tcp_with_tls_on_free_port(
timeout: Duration,
cert_key: (Vec<u8>, Vec<u8>),
) -> Result<(u16, tokio::task::JoinHandle<Result<(), BoxError>>), BoxError> {
let listener = std::net::TcpListener::bind("0.0.0.0:0")?;
let listener = tokio::net::TcpListener::bind("[::]:0").await?;
let port = listener.local_addr()?.port();
println!("Directory server binding to port {}", port);
println!("Directory server binding to port {}", listener.local_addr()?);

let listener = tokio::net::TcpListener::from_std(listener)?;
println!("tokio listener created");
let handle = listen_tcp_with_tls_on_listener(listener, db_host, timeout, cert_key).await?;
println!("Directory server started");
Expand All @@ -54,7 +53,7 @@ pub async fn listen_tcp_with_tls_on_free_port(
// Helper function to avoid code duplication
async fn listen_tcp_with_tls_on_listener(
listener: tokio::net::TcpListener,
db_host: String,
db_host: String,
timeout: Duration,
tls_config: (Vec<u8>, Vec<u8>),
) -> Result<tokio::task::JoinHandle<Result<(), BoxError>>, BoxError> {
Expand Down Expand Up @@ -132,7 +131,7 @@ pub async fn listen_tcp(
pub async fn listen_tcp_with_tls(
port: u16,
db_host: String,
timeout: Duration,
timeout: Duration,
cert_key: (Vec<u8>, Vec<u8>),
) -> Result<tokio::task::JoinHandle<Result<(), BoxError>>, BoxError> {
let addr = format!("0.0.0.0:{}", port);
Expand Down
51 changes: 38 additions & 13 deletions payjoin/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ mod integration {
use payjoin::receive::v2::{PayjoinProposal, Receiver, UncheckedProposal};
use payjoin::{HpkeKeyPair, OhttpKeys, PjUri, UriExt};
use reqwest::{Client, ClientBuilder, Error, Response};
use testcontainers::Container;
use testcontainers_modules::redis::Redis;
use testcontainers_modules::testcontainers::clients::Cli;

Expand All @@ -200,7 +201,12 @@ mod integration {

let (cert, key) = local_cert_key();
dbg!("G");
let (port, directory_future) = init_directory((cert.clone(), key))

let docker: Cli = Cli::default();
let db = docker.run(Redis);
let db_host = format!("127.0.0.1:{}", db.get_host_port_ipv4(6379));

let (port, directory_future) = init_directory(db_host, (cert.clone(), key))
.await
.expect("Failed to init directory");
println!("Directory server started on port IN TEST FN {}", port);
Expand All @@ -212,7 +218,7 @@ mod integration {
eprintln!("Directory server error: {:?}", e);
}
});

tokio::select!(
_ = directory_task => panic!("Directory server is long running"),
res = try_request_with_bad_keys(directory, bad_ohttp_keys, cert) => {
Expand Down Expand Up @@ -250,7 +256,12 @@ mod integration {
let ohttp_relay_port = find_free_port();
let ohttp_relay =
Url::parse(&format!("http://localhost:{}", ohttp_relay_port)).unwrap();
let (directory_port, directory_handle) = init_directory((cert.clone(), key))

let docker: Cli = Cli::default();
let db = docker.run(Redis);
let db_host = format!("127.0.0.1:{}", db.get_host_port_ipv4(6379));

let (directory_port, directory_handle) = init_directory(db_host, (cert.clone(), key))
.await
.expect("Failed to init directory");
let directory = Url::parse(&format!("https://localhost:{}", directory_port)).unwrap();
Expand Down Expand Up @@ -321,7 +332,12 @@ mod integration {
let ohttp_relay_port = find_free_port();
let ohttp_relay =
Url::parse(&format!("http://localhost:{}", ohttp_relay_port)).unwrap();
let (directory_port, directory_future) = init_directory((cert.clone(), key))

let docker: Cli = Cli::default();
let db = docker.run(Redis);
let db_host = format!("127.0.0.1:{}", db.get_host_port_ipv4(6379));

let (directory_port, directory_future) = init_directory(db_host, (cert.clone(), key))
.await
.expect("Failed to init directory");
let directory = Url::parse(&format!("https://localhost:{}", directory_port)).unwrap();
Expand Down Expand Up @@ -455,7 +471,12 @@ mod integration {
let ohttp_relay_port = find_free_port();
let ohttp_relay =
Url::parse(&format!("http://localhost:{}", ohttp_relay_port)).unwrap();
let (directory_port, directory_future) = init_directory((cert.clone(), key))

let docker: Cli = Cli::default();
let db = docker.run(Redis);
let db_host = format!("127.0.0.1:{}", db.get_host_port_ipv4(6379));

let (directory_port, directory_future) = init_directory(db_host, (cert.clone(), key))
.await
.expect("Failed to init directory");
let directory = Url::parse(&format!("https://localhost:{}", directory_port)).unwrap();
Expand Down Expand Up @@ -675,7 +696,11 @@ mod integration {
let ohttp_relay_port = find_free_port();
let ohttp_relay =
Url::parse(&format!("http://localhost:{}", ohttp_relay_port)).unwrap();
let (directory_port, directory_future) = init_directory((cert.clone(), key))

let docker: Cli = Cli::default();
let db = docker.run(Redis);
let db_host = format!("127.0.0.1:{}", db.get_host_port_ipv4(6379));
let (directory_port, directory_future) = init_directory(db_host, (cert.clone(), key))
.await
.expect("Failed to init directory");
let directory = Url::parse(&format!("https://localhost:{}", directory_port)).unwrap();
Expand Down Expand Up @@ -803,15 +828,15 @@ mod integration {
}
}

async fn init_directory(
async fn init_directory<'a>(
db_host: String,
local_cert_key: (Vec<u8>, Vec<u8>),
) -> Result<(u16, tokio::task::JoinHandle<Result<(), BoxSendSyncError>>), BoxSendSyncError> {
let docker: Cli = Cli::default();
) -> Result<(u16, tokio::task::JoinHandle<Result<(), BoxSendSyncError>>), BoxSendSyncError>
{
println!("Database running on {}", db_host);
let timeout = Duration::from_secs(2);
let db = docker.run(Redis);
let db_host = format!("127.0.0.1:{}", db.get_host_port_ipv4(6379));
println!("Database running on {}", db.get_host_port_ipv4(6379));
payjoin_directory::listen_tcp_with_tls_on_free_port(db_host, timeout, local_cert_key).await
payjoin_directory::listen_tcp_with_tls_on_free_port(db_host, timeout, local_cert_key)
.await
}

// generates or gets a DER encoded localhost cert and key.
Expand Down
Loading