#![allow(dead_code)]
#![allow(unused_variables)]
#![allow(unused_imports)]
use crate::core::CoreBuilder;
use crate::setup::BootstrapConfig;
use crate::{Port, TcpConfig, TransportOpts, MIN_NETWORK_ID_LENGTH};
use super::*;
use futures::TryFutureExt;
use ini::Ini;
use std::fs;
use std::fs::File;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
pub fn setup_core_builder() -> CoreBuilder {
let config = BootstrapConfig::default().with_tcp(49158).with_udp(49159);
CoreBuilder::with_config(config)
}
const CUSTOM_TCP_PORT: Port = 49666;
const CUSTOM_UDP_PORT: Port = 49852;
fn create_test_ini_file(file_path: &str) {
let mut config = Ini::new();
config
.with_section(Some("ports"))
.set("tcp", CUSTOM_TCP_PORT.to_string())
.set("udp", CUSTOM_UDP_PORT.to_string());
config.with_section(Some("bootstrap")).set(
"boot_nodes",
"[12D3KooWGfbL6ZNGWqS11MoptH2A7DB1DG6u85FhXBUPXPVkVVRq:/ip4/192.168.1.205/tcp/1509]",
);
config.write_to_file(file_path).unwrap_or_default();
}
#[cfg(feature = "tokio-runtime")]
#[test]
fn node_default_behavior_works() {
use std::net::IpAddr;
use crate::{TcpConfig, TransportOpts, DEFAULT_NETWORK_ID, MAX_PORT, MIN_PORT};
let default_node = setup_core_builder();
assert_eq!(default_node.network_id, DEFAULT_NETWORK_ID);
assert_eq!(
default_node.transport,
TransportOpts::TcpQuic {
tcp_config: TcpConfig::Default
}
);
assert_eq!(default_node.keep_alive_duration, 60);
assert_eq!(
default_node.ip_address,
IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0))
);
let config = BootstrapConfig::default();
let default_state = ();
let builder = CoreBuilder::with_config(config);
assert_eq!(builder.tcp_udp_port, (MIN_PORT, MAX_PORT));
}
#[test]
fn node_custom_setup_works() {
let default_node = setup_core_builder();
let custom_network_id = "/custom-protocol/1.0".to_string();
let custom_transport = TransportOpts::TcpQuic {
tcp_config: TcpConfig::Custom {
ttl: 10,
nodelay: true,
backlog: 10,
},
};
let custom_keep_alive_duration = 20;
let custom_ip_address = IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
let custom_node = default_node
.with_network_id(custom_network_id.clone())
.with_transports(custom_transport.clone())
.with_idle_connection_timeout(custom_keep_alive_duration.clone())
.listen_on(custom_ip_address.clone());
assert_eq!(custom_node.network_id(), custom_network_id);
assert_eq!(custom_node.transport, custom_transport);
assert_eq!(custom_node.keep_alive_duration, custom_keep_alive_duration);
}
#[test]
fn node_custom_behavior_with_network_id_works() {
let custom_builder = setup_core_builder();
let custom_protocol: &str = "/custom-protocol/1.0";
let custom_builder = custom_builder.with_network_id(custom_protocol.to_string());
assert_eq!(
custom_builder.network_id().len() >= MIN_NETWORK_ID_LENGTH.into(),
true
);
assert!(custom_builder.network_id().starts_with("/"));
assert_eq!(custom_builder.network_id(), custom_protocol.to_string());
}
#[test]
#[should_panic(expected = "Could not parse provided network id")]
fn node_custom_behavior_with_network_id_fails() {
let custom_builder = setup_core_builder();
let invalid_protocol_1 = "/1.0".to_string();
let custom_builder = custom_builder.with_network_id(invalid_protocol_1);
let invalid_protocol_2 = "1.0".to_string();
custom_builder.with_network_id(invalid_protocol_2);
}
#[cfg(feature = "tokio-runtime")]
#[test]
fn node_save_keypair_offline_works_tokio() {
let default_node = setup_core_builder();
let result = tokio::runtime::Runtime::new()
.unwrap()
.block_on(async { default_node.build().await.unwrap() });
let file_path_1 = "saved_keys.ini";
create_test_ini_file(file_path_1);
let saved_1 = result.save_keypair_offline(&file_path_1);
assert_eq!(saved_1, true);
let file_path_2 = "test.ini";
let saved_2 = result.save_keypair_offline(file_path_2);
assert_eq!(saved_2, true);
fs::remove_file(file_path_1).unwrap_or_default();
fs::remove_file(file_path_2).unwrap_or_default();
}
#[cfg(feature = "async-std-runtime")]
#[test]
fn node_save_keypair_offline_works_async_std() {
let default_node = setup_core_builder();
let result = async_std::task::block_on(
default_node
.build()
.unwrap_or_else(|_| panic!("Could not build node")),
);
let file_path_1 = "saved_keys.ini";
create_test_ini_file(file_path_1);
let saved_1 = result.save_keypair_offline(file_path_1);
assert_eq!(saved_1, true);
let file_path_2 = "test.txt";
let saved_2 = result.save_keypair_offline(file_path_2);
assert_eq!(saved_2, true);
fs::remove_file(file_path_1).unwrap_or_default();
fs::remove_file(file_path_2).unwrap_or_default();
}