forked from citadel-tech/coinswap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdns.rs
102 lines (87 loc) · 3.11 KB
/
dns.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
#![cfg(feature = "integration-test")]
use std::{io::Write, net::TcpStream, process::Command, thread, time::Duration};
mod test_framework;
use coinswap::protocol::DnsRequest;
use test_framework::{init_bitcoind, start_dns};
fn send_addresses(addresses: &[(&str, u32)]) {
for address in addresses {
let mut stream = TcpStream::connect(("127.0.0.1", 8080)).unwrap();
let request = DnsRequest::Dummy {
url: address.0.to_string(),
vout: address.1,
};
let buffer = serde_cbor::ser::to_vec(&request).unwrap();
let length = buffer.len() as u32;
stream.write_all(&length.to_be_bytes()).unwrap();
stream.write_all(&buffer).unwrap();
stream.flush().unwrap();
}
}
fn verify_addresses(addresses: &[(&str, u32)]) {
let output = Command::new("./target/debug/directory-cli")
.arg("list-addresses")
.output()
.unwrap();
let addresses_output = String::from_utf8(output.stdout).unwrap();
println!("{}", addresses_output);
assert!(
output.stderr.is_empty(),
"Error: {:?}",
String::from_utf8(output.stderr).unwrap()
);
// TODO add more through script checking
for (address, index) in addresses {
assert_eq!(
addresses_output.match_indices(&address.to_string()).count(),
1,
"Address {} not found or duplicate entries found",
address
);
assert_eq!(
addresses_output
.match_indices(&format!("vout: {}", index))
.count(),
1,
"OP index {} not found",
address
);
}
}
#[test]
fn test_dns() {
// Setup directory
let temp_dir = std::env::temp_dir().join("coinswap");
// Remove if previously existing
if temp_dir.exists() {
std::fs::remove_dir_all(&temp_dir).unwrap();
}
log::info!("temporary directory : {}", temp_dir.display());
let bitcoind = init_bitcoind(&temp_dir);
let data_dir = temp_dir.join("dns");
let mut process = start_dns(&data_dir, &bitcoind);
// The indexes denotes vout of an `OutPoint(deadbeefcafebabefeedc0ffee123456789abcdeffedcba9876543210ffeeddcc:vout)``
// So using the same index for different address, will replace the address.
let initial_addresses = vec![
("127.0.0.1:8080", 0),
("127.0.0.1:8081", 1),
("127.0.0.1:8082", 2),
];
send_addresses(&initial_addresses);
thread::sleep(Duration::from_secs(10));
verify_addresses(&initial_addresses);
// Replace address 8082 to 8083 registered for Bond index 2.
// Add a new entry with a new bond index
let additional_addresses = vec![("127.0.0.1:8083", 2), ("127.0.0.1:8084", 3)];
send_addresses(&additional_addresses);
thread::sleep(Duration::from_secs(10));
let all_addresses = vec![
("127.0.0.1:8080", 0),
("127.0.0.1:8081", 1),
("127.0.0.1:8083", 2),
("127.0.0.1:8084", 3),
];
verify_addresses(&all_addresses);
// Persistence check
process.kill().expect("Failed to kill directoryd process");
process.wait().unwrap();
}