|
| 1 | +#[cfg(any(feature = "test_go_interop", feature = "test_js_interop"))] |
| 2 | +pub use common::ForeignNode; |
| 3 | + |
| 4 | +#[cfg(all(not(feature = "test_go_interop"), not(feature = "test_js_interop")))] |
| 5 | +pub struct ForeignNode; |
| 6 | + |
| 7 | +#[cfg(any(feature = "test_go_interop", feature = "test_js_interop"))] |
| 8 | +pub mod common { |
| 9 | + use anyhow; |
| 10 | + use libp2p::{core::PublicKey, Multiaddr, PeerId}; |
| 11 | + use rand::prelude::*; |
| 12 | + use serde::Deserialize; |
| 13 | + use std::time::Duration; |
| 14 | + use std::{ |
| 15 | + env, fs, |
| 16 | + path::PathBuf, |
| 17 | + process::{Child, Command, Stdio}, |
| 18 | + thread, |
| 19 | + }; |
| 20 | + |
| 21 | + // this environment variable should point to the location of the foreign ipfs binary |
| 22 | + #[cfg(feature = "test_go_interop")] |
| 23 | + pub const ENV_IPFS_PATH: &str = "GO_IPFS_PATH"; |
| 24 | + #[cfg(feature = "test_js_interop")] |
| 25 | + pub const ENV_IPFS_PATH: &str = "JS_IPFS_PATH"; |
| 26 | + |
| 27 | + pub struct ForeignNode { |
| 28 | + pub dir: PathBuf, |
| 29 | + pub daemon: Child, |
| 30 | + pub id: PeerId, |
| 31 | + pub pk: PublicKey, |
| 32 | + pub addrs: Vec<Multiaddr>, |
| 33 | + } |
| 34 | + |
| 35 | + impl ForeignNode { |
| 36 | + pub fn new() -> ForeignNode { |
| 37 | + let binary_path = env::vars() |
| 38 | + .find(|(key, _val)| key == ENV_IPFS_PATH) |
| 39 | + .unwrap_or_else(|| { |
| 40 | + panic!("the {} environment variable was not found", ENV_IPFS_PATH) |
| 41 | + }) |
| 42 | + .1; |
| 43 | + |
| 44 | + let mut tmp_dir = env::temp_dir(); |
| 45 | + let mut rng = rand::thread_rng(); |
| 46 | + tmp_dir.push(&format!("ipfs_test_{}", rng.gen::<u64>())); |
| 47 | + let _ = fs::create_dir(&tmp_dir); |
| 48 | + |
| 49 | + Command::new(&binary_path) |
| 50 | + .env("IPFS_PATH", &tmp_dir) |
| 51 | + .arg("init") |
| 52 | + .arg("-p") |
| 53 | + .arg("test") |
| 54 | + .arg("--bits") |
| 55 | + .arg("2048") |
| 56 | + .stdout(Stdio::null()) |
| 57 | + .status() |
| 58 | + .unwrap(); |
| 59 | + |
| 60 | + let daemon = Command::new(&binary_path) |
| 61 | + .env("IPFS_PATH", &tmp_dir) |
| 62 | + .arg("daemon") |
| 63 | + .stdout(Stdio::null()) |
| 64 | + .spawn() |
| 65 | + .unwrap(); |
| 66 | + |
| 67 | + // give the daemon a little bit of time to start |
| 68 | + thread::sleep(Duration::from_secs(1)); |
| 69 | + |
| 70 | + let node_id = Command::new(&binary_path) |
| 71 | + .env("IPFS_PATH", &tmp_dir) |
| 72 | + .arg("id") |
| 73 | + .output() |
| 74 | + .unwrap() |
| 75 | + .stdout; |
| 76 | + |
| 77 | + let node_id_stdout = String::from_utf8_lossy(&node_id); |
| 78 | + let ForeignNodeId { |
| 79 | + id, |
| 80 | + addresses, |
| 81 | + public_key, |
| 82 | + .. |
| 83 | + } = serde_json::de::from_str(&node_id_stdout).unwrap(); |
| 84 | + |
| 85 | + let id = id.parse().unwrap(); |
| 86 | + let pk = PublicKey::from_protobuf_encoding( |
| 87 | + &base64::decode(public_key.into_bytes()).unwrap(), |
| 88 | + ) |
| 89 | + .unwrap(); |
| 90 | + |
| 91 | + ForeignNode { |
| 92 | + dir: tmp_dir, |
| 93 | + daemon, |
| 94 | + id, |
| 95 | + pk, |
| 96 | + addrs: addresses, |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + #[allow(dead_code)] |
| 101 | + pub async fn identity(&self) -> Result<(PublicKey, Vec<Multiaddr>), anyhow::Error> { |
| 102 | + Ok((self.pk.clone(), self.addrs.clone())) |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + impl Drop for ForeignNode { |
| 107 | + fn drop(&mut self) { |
| 108 | + let _ = self.daemon.kill(); |
| 109 | + let _ = fs::remove_dir_all(&self.dir); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + #[derive(Deserialize, Debug)] |
| 114 | + #[cfg_attr(feature = "test_go_interop", serde(rename_all = "PascalCase"))] |
| 115 | + #[cfg_attr(feature = "test_js_interop", serde(rename_all = "camelCase"))] |
| 116 | + pub struct ForeignNodeId { |
| 117 | + #[cfg_attr(feature = "test_go_interop", serde(rename = "ID"))] |
| 118 | + pub id: String, |
| 119 | + pub public_key: String, |
| 120 | + pub addresses: Vec<Multiaddr>, |
| 121 | + #[serde(skip)] |
| 122 | + agent_version: String, |
| 123 | + #[serde(skip)] |
| 124 | + protocol_version: String, |
| 125 | + } |
| 126 | +} |
0 commit comments