-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathe2e.rs
396 lines (344 loc) · 15.7 KB
/
e2e.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#[cfg(feature = "_danger-local-https")]
mod e2e {
use std::env;
use std::process::Stdio;
use nix::sys::signal::{kill, Signal};
use nix::unistd::Pid;
use payjoin_test_utils::{init_bitcoind_sender_receiver, BoxError};
use tokio::fs;
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::process::Command;
fn sigint(child: &tokio::process::Child) -> nix::Result<()> {
let pid = child.id().expect("Failed to get child PID");
kill(Pid::from_raw(pid as i32), Signal::SIGINT)
}
const RECEIVE_SATS: &str = "54321";
#[cfg(not(feature = "v2"))]
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn send_receive_payjoin() -> Result<(), BoxError> {
let (bitcoind, _sender, _receiver) = init_bitcoind_sender_receiver(None, None)?;
let temp_dir = env::temp_dir();
let receiver_db_path = temp_dir.join("receiver_db");
let sender_db_path = temp_dir.join("sender_db");
let receiver_db_path_clone = receiver_db_path.clone();
let sender_db_path_clone = sender_db_path.clone();
let port = find_free_port()?;
let payjoin_sent = tokio::spawn(async move {
let receiver_rpchost = format!("http://{}/wallet/receiver", bitcoind.params.rpc_socket);
let sender_rpchost = format!("http://{}/wallet/sender", bitcoind.params.rpc_socket);
let cookie_file = &bitcoind.params.cookie_file;
let pj_endpoint = format!("https://localhost:{}", port);
let payjoin_cli = env!("CARGO_BIN_EXE_payjoin-cli");
let mut cli_receiver = Command::new(payjoin_cli)
.arg("--rpchost")
.arg(&receiver_rpchost)
.arg("--cookie-file")
.arg(cookie_file)
.arg("--db-path")
.arg(&receiver_db_path_clone)
.arg("receive")
.arg(RECEIVE_SATS)
.arg("--port")
.arg(port.to_string())
.arg("--pj-endpoint")
.arg(&pj_endpoint)
.stdout(Stdio::piped())
.stderr(Stdio::inherit())
.spawn()
.expect("Failed to execute payjoin-cli");
let stdout =
cli_receiver.stdout.take().expect("Failed to take stdout of child process");
let reader = BufReader::new(stdout);
let mut stdout = tokio::io::stdout();
let mut bip21 = String::new();
let mut lines = reader.lines();
while let Some(line) = lines.next_line().await.expect("Failed to read line from stdout")
{
// Write to stdout regardless
stdout
.write_all(format!("{}\n", line).as_bytes())
.await
.expect("Failed to write to stdout");
if line.to_ascii_uppercase().starts_with("BITCOIN") {
bip21 = line;
break;
}
}
log::debug!("Got bip21 {}", &bip21);
let mut cli_sender = Command::new(payjoin_cli)
.arg("--rpchost")
.arg(&sender_rpchost)
.arg("--cookie-file")
.arg(cookie_file)
.arg("--db-path")
.arg(&sender_db_path_clone)
.arg("send")
.arg(&bip21)
.arg("--fee-rate")
.arg("1")
.stdout(Stdio::piped())
.stderr(Stdio::inherit())
.spawn()
.expect("Failed to execute payjoin-cli");
let stdout = cli_sender.stdout.take().expect("Failed to take stdout of child process");
let reader = BufReader::new(stdout);
let (tx, mut rx) = tokio::sync::mpsc::channel(1);
let mut lines = reader.lines();
tokio::spawn(async move {
let mut stdout = tokio::io::stdout();
while let Some(line) =
lines.next_line().await.expect("Failed to read line from stdout")
{
stdout
.write_all(format!("{}\n", line).as_bytes())
.await
.expect("Failed to write to stdout");
if line.contains("Payjoin sent") {
let _ = tx.send(true).await;
break;
}
}
});
let timeout = tokio::time::Duration::from_secs(10);
let payjoin_sent = tokio::time::timeout(timeout, rx.recv())
.await
.unwrap_or(Some(false)) // timed out
.expect("rx channel closed prematurely"); // recv() returned None
sigint(&cli_receiver).expect("Failed to kill payjoin-cli");
sigint(&cli_sender).expect("Failed to kill payjoin-cli");
payjoin_sent
})
.await?;
cleanup_temp_file(&receiver_db_path).await;
cleanup_temp_file(&sender_db_path).await;
assert!(payjoin_sent, "Payjoin send was not detected");
fn find_free_port() -> Result<u16, BoxError> {
let listener = std::net::TcpListener::bind("127.0.0.1:0")?;
Ok(listener.local_addr()?.port())
}
Ok(())
}
#[cfg(feature = "v2")]
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn send_receive_payjoin() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
use std::path::PathBuf;
use payjoin_test_utils::{init_tracing, TestServices};
use tokio::process::Child;
type Result<T> = std::result::Result<T, BoxError>;
init_tracing();
let mut services = TestServices::initialize().await?;
let temp_dir = env::temp_dir();
let receiver_db_path = temp_dir.join("receiver_db");
let sender_db_path = temp_dir.join("sender_db");
let result: Result<()> = tokio::select! {
res = services.take_ohttp_relay_handle() => Err(format!("Ohttp relay is long running: {:?}", res).into()),
res = services.take_directory_handle() => Err(format!("Directory server is long running: {:?}", res).into()),
res = send_receive_cli_async(&services, receiver_db_path.clone(), sender_db_path.clone()) => res.map_err(|e| format!("send_receive failed: {:?}", e).into()),
};
cleanup_temp_file(&receiver_db_path).await;
cleanup_temp_file(&sender_db_path).await;
assert!(result.is_ok(), "{}", result.unwrap_err());
async fn send_receive_cli_async(
services: &TestServices,
receiver_db_path: PathBuf,
sender_db_path: PathBuf,
) -> Result<()> {
let (bitcoind, _sender, _receiver) = init_bitcoind_sender_receiver(None, None)?;
let temp_dir = env::temp_dir();
let cert_path = temp_dir.join("localhost.der");
tokio::fs::write(&cert_path, services.cert()).await?;
services.wait_for_services_ready().await?;
let ohttp_keys = services.fetch_ohttp_keys().await?;
let ohttp_keys_path = temp_dir.join("ohttp_keys");
tokio::fs::write(&ohttp_keys_path, ohttp_keys.encode()?).await?;
let receiver_rpchost = format!("http://{}/wallet/receiver", bitcoind.params.rpc_socket);
let sender_rpchost = format!("http://{}/wallet/sender", bitcoind.params.rpc_socket);
let cookie_file = &bitcoind.params.cookie_file;
let payjoin_cli = env!("CARGO_BIN_EXE_payjoin-cli");
let directory = &services.directory_url().to_string();
// Mock ohttp_relay since the ohttp_relay's http client doesn't have the certificate for the directory
let mock_ohttp_relay = directory;
let cli_receive_initiator = Command::new(payjoin_cli)
.arg("--rpchost")
.arg(&receiver_rpchost)
.arg("--cookie-file")
.arg(cookie_file)
.arg("--db-path")
.arg(&receiver_db_path)
.arg("--ohttp-relay")
.arg(mock_ohttp_relay)
.arg("receive")
.arg(RECEIVE_SATS)
.arg("--pj-directory")
.arg(directory)
.arg("--ohttp-keys")
.arg(&ohttp_keys_path)
.stdout(Stdio::piped())
.stderr(Stdio::inherit())
.spawn()
.expect("Failed to execute payjoin-cli");
let bip21 = get_bip21_from_receiver(cli_receive_initiator).await;
let cli_send_initiator = Command::new(payjoin_cli)
.arg("--rpchost")
.arg(&sender_rpchost)
.arg("--cookie-file")
.arg(cookie_file)
.arg("--db-path")
.arg(&sender_db_path)
.arg("--ohttp-relay")
.arg(mock_ohttp_relay)
.arg("send")
.arg(&bip21)
.arg("--fee-rate")
.arg("1")
.stdout(Stdio::piped())
.stderr(Stdio::inherit())
.spawn()
.expect("Failed to execute payjoin-cli");
send_until_request_timeout(cli_send_initiator).await?;
let cli_receive_resumer = Command::new(payjoin_cli)
.arg("--rpchost")
.arg(&receiver_rpchost)
.arg("--cookie-file")
.arg(cookie_file)
.arg("--db-path")
.arg(&receiver_db_path)
.arg("--ohttp-relay")
.arg(mock_ohttp_relay)
.arg("resume")
.stdout(Stdio::piped())
.stderr(Stdio::inherit())
.spawn()
.expect("Failed to execute payjoin-cli");
respond_with_payjoin(cli_receive_resumer).await?;
let cli_send_resumer = Command::new(payjoin_cli)
.arg("--rpchost")
.arg(&sender_rpchost)
.arg("--cookie-file")
.arg(cookie_file)
.arg("--db-path")
.arg(&sender_db_path)
.arg("--ohttp-relay")
.arg(mock_ohttp_relay)
.arg("send")
.arg(&bip21)
.arg("--fee-rate")
.arg("1")
.stdout(Stdio::piped())
.stderr(Stdio::inherit())
.spawn()
.expect("Failed to execute payjoin-cli");
check_payjoin_sent(cli_send_resumer).await?;
Ok(())
}
async fn get_bip21_from_receiver(mut cli_receiver: Child) -> String {
let stdout =
cli_receiver.stdout.take().expect("Failed to take stdout of child process");
let reader = BufReader::new(stdout);
let mut stdout = tokio::io::stdout();
let mut bip21 = String::new();
let mut lines = reader.lines();
while let Some(line) = lines.next_line().await.expect("Failed to read line from stdout")
{
// Write to stdout regardless
stdout
.write_all(format!("{}\n", line).as_bytes())
.await
.expect("Failed to write to stdout");
if line.to_ascii_uppercase().starts_with("BITCOIN") {
bip21 = line;
break;
}
}
log::debug!("Got bip21 {}", &bip21);
sigint(&cli_receiver).expect("Failed to kill payjoin-cli");
bip21
}
async fn send_until_request_timeout(mut cli_sender: Child) -> Result<()> {
let stdout = cli_sender.stdout.take().expect("Failed to take stdout of child process");
let reader = BufReader::new(stdout);
let (tx, mut rx) = tokio::sync::mpsc::channel(1);
let mut lines = reader.lines();
tokio::spawn(async move {
let mut stdout = tokio::io::stdout();
while let Some(line) =
lines.next_line().await.expect("Failed to read line from stdout")
{
stdout
.write_all(format!("{}\n", line).as_bytes())
.await
.expect("Failed to write to stdout");
if line.contains("No response yet.") {
let _ = tx.send(true).await;
break;
}
}
});
let timeout = tokio::time::Duration::from_secs(35);
let fallback_sent = tokio::time::timeout(timeout, rx.recv()).await?;
sigint(&cli_sender).expect("Failed to kill payjoin-cli initial sender");
assert!(fallback_sent.unwrap_or(false), "Fallback send was not detected");
Ok(())
}
async fn respond_with_payjoin(mut cli_receive_resumer: Child) -> Result<()> {
let stdout =
cli_receive_resumer.stdout.take().expect("Failed to take stdout of child process");
let reader = BufReader::new(stdout);
let (tx, mut rx) = tokio::sync::mpsc::channel(1);
let mut lines = reader.lines();
tokio::spawn(async move {
let mut stdout = tokio::io::stdout();
while let Some(line) =
lines.next_line().await.expect("Failed to read line from stdout")
{
stdout
.write_all(format!("{}\n", line).as_bytes())
.await
.expect("Failed to write to stdout");
if line.contains("Response successful") {
let _ = tx.send(true).await;
break;
}
}
});
let timeout = tokio::time::Duration::from_secs(10);
let response_successful = tokio::time::timeout(timeout, rx.recv()).await?;
sigint(&cli_receive_resumer).expect("Failed to kill payjoin-cli");
assert!(response_successful.unwrap_or(false), "Did not respond with Payjoin PSBT");
Ok(())
}
async fn check_payjoin_sent(mut cli_send_resumer: Child) -> Result<()> {
let stdout =
cli_send_resumer.stdout.take().expect("Failed to take stdout of child process");
let reader = BufReader::new(stdout);
let (tx, mut rx) = tokio::sync::mpsc::channel(1);
let mut lines = reader.lines();
tokio::spawn(async move {
let mut stdout = tokio::io::stdout();
while let Some(line) =
lines.next_line().await.expect("Failed to read line from stdout")
{
stdout
.write_all(format!("{}\n", line).as_bytes())
.await
.expect("Failed to write to stdout");
if line.contains("Payjoin sent") {
let _ = tx.send(true).await;
break;
}
}
});
let timeout = tokio::time::Duration::from_secs(10);
let payjoin_sent = tokio::time::timeout(timeout, rx.recv()).await?;
sigint(&cli_send_resumer).expect("Failed to kill payjoin-cli");
assert!(payjoin_sent.unwrap_or(false), "Payjoin send was not detected");
Ok(())
}
Ok(())
}
async fn cleanup_temp_file(path: &std::path::Path) {
if let Err(e) = fs::remove_dir_all(path).await {
eprintln!("Failed to remove {:?}: {}", path, e);
}
}
}