From c196c118609f99ffef2e8ded60db39f199f3105f Mon Sep 17 00:00:00 2001 From: Igor Katson Date: Wed, 14 Aug 2024 10:25:05 +0100 Subject: [PATCH] trying to make tests work with new metadata --- Makefile | 4 +++ crates/librqbit/examples/custom_storage.rs | 7 ++-- crates/librqbit/src/create_torrent_file.rs | 5 +-- crates/librqbit/src/session.rs | 40 +++++++++++----------- crates/librqbit/src/tests/e2e.rs | 10 +++--- crates/librqbit_core/src/magnet.rs | 8 +++++ 6 files changed, 45 insertions(+), 29 deletions(-) diff --git a/Makefile b/Makefile index 373536f..53bd9b1 100644 --- a/Makefile +++ b/Makefile @@ -44,6 +44,10 @@ install: build-release $(MAKE) sign-release install target/release/rqbit "$(HOME)/bin/" +@PHONY: test +test: + ulimit -n unlimited && cargo test + @PHONY: release-macos-universal release-macos-universal: cargo build --target aarch64-apple-darwin --profile release-github diff --git a/crates/librqbit/examples/custom_storage.rs b/crates/librqbit/examples/custom_storage.rs index 74e3949..f85fd37 100644 --- a/crates/librqbit/examples/custom_storage.rs +++ b/crates/librqbit/examples/custom_storage.rs @@ -1,3 +1,4 @@ +use bytes::Bytes; use librqbit::{ storage::{StorageFactory, StorageFactoryExt, TorrentStorage}, SessionOptions, @@ -79,9 +80,9 @@ async fn main() -> anyhow::Result<()> { .await?; let handle = s .add_torrent( - librqbit::AddTorrent::TorrentFileBytes( - include_bytes!("../resources/ubuntu-21.04-live-server-amd64.iso.torrent").into(), - ), + librqbit::AddTorrent::TorrentFileBytes(Bytes::from_static(include_bytes!( + "../resources/ubuntu-21.04-live-server-amd64.iso.torrent" + ))), Some(librqbit::AddTorrentOptions { storage_factory: Some(CustomStorageFactory::default().boxed()), paused: false, diff --git a/crates/librqbit/src/create_torrent_file.rs b/crates/librqbit/src/create_torrent_file.rs index fe78ca0..dcd6e58 100644 --- a/crates/librqbit/src/create_torrent_file.rs +++ b/crates/librqbit/src/create_torrent_file.rs @@ -6,6 +6,7 @@ use std::path::Path; use anyhow::Context; use bencode::bencode_serialize_to_writer; use buffers::ByteBufOwned; +use bytes::Bytes; use librqbit_core::torrent_metainfo::{TorrentMetaV1File, TorrentMetaV1Info, TorrentMetaV1Owned}; use librqbit_core::Id20; use sha1w::{ISha1, Sha1}; @@ -185,10 +186,10 @@ impl CreateTorrentResult { self.meta.info_hash } - pub fn as_bytes(&self) -> anyhow::Result> { + pub fn as_bytes(&self) -> anyhow::Result { let mut b = Vec::new(); bencode_serialize_to_writer(&self.meta, &mut b).context("error serializing torrent")?; - Ok(b) + Ok(b.into()) } } diff --git a/crates/librqbit/src/session.rs b/crates/librqbit/src/session.rs index 29f1c3d..8947c40 100644 --- a/crates/librqbit/src/session.rs +++ b/crates/librqbit/src/session.rs @@ -421,7 +421,7 @@ pub fn read_local_file_including_stdin(filename: &str) -> anyhow::Result pub enum AddTorrent<'a> { Url(Cow<'a, str>), - TorrentFileBytes(Cow<'a, [u8]>), + TorrentFileBytes(Bytes), TorrentInfo(Box, Bytes), } @@ -439,7 +439,7 @@ impl<'a> AddTorrent<'a> { Self::Url(url.into()) } - pub fn from_bytes(bytes: impl Into>) -> Self { + pub fn from_bytes(bytes: impl Into) -> Self { Self::TorrentFileBytes(bytes.into()) } @@ -448,13 +448,13 @@ impl<'a> AddTorrent<'a> { pub fn from_local_filename(filename: &str) -> anyhow::Result { let file = read_local_file_including_stdin(filename) .with_context(|| format!("error reading local file {filename:?}"))?; - Ok(Self::TorrentFileBytes(Cow::Owned(file))) + Ok(Self::TorrentFileBytes(file.into())) } - pub fn into_bytes(self) -> Vec { + pub fn into_bytes(self) -> Bytes { match self { - Self::Url(s) => s.into_owned().into_bytes(), - Self::TorrentFileBytes(b) => b.into_owned(), + Self::Url(s) => s.into_owned().into_bytes().into(), + Self::TorrentFileBytes(b) => b, Self::TorrentInfo(..) => unimplemented!(), } } @@ -1012,16 +1012,22 @@ impl Session { announce_port, opts.force_tracker_interval, )?; + let initial_peers_stream = opts + .initial_peers + .clone() + .and_then(|v| if v.is_empty() { None } else { Some(v) }) + .map(futures::stream::iter); + let peer_rx = merge_two_optional_streams(peer_rx, initial_peers_stream); let peer_rx = match peer_rx { Some(peer_rx) => peer_rx, - None => bail!("can't find peers: DHT disabled and no trackers in magnet"), + None => bail!("can't find peers: DHT is disabled, no trackers in magnet, and no initial peers provided"), }; debug!(?info_hash, "querying DHT"); match read_metainfo_from_peer_receiver( self.peer_id, info_hash, - opts.initial_peers.clone().unwrap_or_default(), + Default::default(), peer_rx, Some(self.merge_peer_opts(opts.peer_opts)), self.connector.clone(), @@ -1066,18 +1072,12 @@ impl Session { url ) } - AddTorrent::TorrentFileBytes(bytes) => { - let bytes = match bytes { - Cow::Borrowed(b) => ::bytes::Bytes::copy_from_slice(b), - Cow::Owned(v) => ::bytes::Bytes::from(v), - }; - ( - torrent_from_bytes(&bytes) - .map(|t| t.clone_to_owned(Some(&bytes))) - .context("error decoding torrent")?, - ByteBufOwned(bytes), - ) - } + AddTorrent::TorrentFileBytes(bytes) => ( + torrent_from_bytes(&bytes) + .map(|t| t.clone_to_owned(Some(&bytes))) + .context("error decoding torrent")?, + ByteBufOwned(bytes), + ), AddTorrent::TorrentInfo(t, bytes) => (*t, bytes.into()), }; diff --git a/crates/librqbit/src/tests/e2e.rs b/crates/librqbit/src/tests/e2e.rs index 2b81d15..dedc2e8 100644 --- a/crates/librqbit/src/tests/e2e.rs +++ b/crates/librqbit/src/tests/e2e.rs @@ -1,11 +1,11 @@ use std::{ - borrow::Cow, net::{Ipv4Addr, SocketAddr}, time::Duration, }; use anyhow::bail; use futures::{stream::FuturesUnordered, StreamExt}; +use librqbit_core::magnet::Magnet; use rand::Rng; use tokio::{ spawn, @@ -84,7 +84,7 @@ async fn test_e2e_download() { let handle = session .add_torrent( - crate::AddTorrent::TorrentFileBytes(Cow::Owned(torrent_file_bytes)), + crate::AddTorrent::TorrentFileBytes(torrent_file_bytes), Some(AddTorrentOptions { overwrite: true, output_folder: Some(tempdir.to_str().unwrap().to_owned()), @@ -139,6 +139,8 @@ async fn test_e2e_download() { .and_then(|v| v.parse().ok()) .unwrap_or(1usize); + let magnet = Magnet::from_id20(torrent_file.info_hash(), Vec::new()).to_string(); + // 3. Start a client with the initial peers, and download the file. for _ in 0..client_iters { let outdir = tempfile::TempDir::with_prefix("rqbit_e2e_client").unwrap(); @@ -163,7 +165,7 @@ async fn test_e2e_download() { let (id, handle) = { let r = session .add_torrent( - crate::AddTorrent::TorrentFileBytes(Cow::Owned(torrent_file_bytes.clone())), + crate::AddTorrent::Url((&magnet).into()), Some(AddTorrentOptions { initial_peers: Some(peers.clone()), // only_files: Some(vec![0]), @@ -235,7 +237,7 @@ async fn test_e2e_download() { // 4. After downloading, recheck its integrity. let handle = session .add_torrent( - crate::AddTorrent::TorrentFileBytes(Cow::Owned(torrent_file_bytes.clone())), + crate::AddTorrent::TorrentFileBytes(torrent_file_bytes.clone()), Some(AddTorrentOptions { paused: true, overwrite: true, diff --git a/crates/librqbit_core/src/magnet.rs b/crates/librqbit_core/src/magnet.rs index f0942bb..d456c35 100644 --- a/crates/librqbit_core/src/magnet.rs +++ b/crates/librqbit_core/src/magnet.rs @@ -20,6 +20,14 @@ impl Magnet { self.id32 } + pub fn from_id20(id20: Id20, trackers: Vec) -> Self { + Self { + id20: Some(id20), + id32: None, + trackers, + } + } + /// Parse a magnet link. pub fn parse(url: &str) -> anyhow::Result { let url = url::Url::parse(url).context("magnet link must be a valid URL")?;