From d1a3077b40ea6300a5abb58d20084c1026990ee3 Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Tue, 16 May 2023 14:00:39 +0100 Subject: [PATCH] chore: normalize infohash -> info_hash --- src/databases/database.rs | 8 +++--- src/databases/mysql.rs | 8 +++--- src/databases/sqlite.rs | 8 +++--- src/models/info_hash.rs | 14 +++++----- src/routes/torrent.rs | 22 ++++++++-------- tests/common/client.rs | 12 ++++----- tests/common/contexts/torrent/fixtures.rs | 2 +- tests/e2e/contexts/torrent/contract.rs | 32 +++++++++++------------ 8 files changed, 53 insertions(+), 53 deletions(-) diff --git a/src/databases/database.rs b/src/databases/database.rs index ccbd4bf6..2bc68865 100644 --- a/src/databases/database.rs +++ b/src/databases/database.rs @@ -172,8 +172,8 @@ pub trait Database: Sync + Send { ) -> Result; /// Get `Torrent` from `InfoHash`. - async fn get_torrent_from_infohash(&self, infohash: &InfoHash) -> Result { - let torrent_info = self.get_torrent_info_from_infohash(infohash).await?; + async fn get_torrent_from_info_hash(&self, info_hash: &InfoHash) -> Result { + let torrent_info = self.get_torrent_info_from_info_hash(info_hash).await?; let torrent_files = self.get_torrent_files_from_id(torrent_info.torrent_id).await?; @@ -205,7 +205,7 @@ pub trait Database: Sync + Send { async fn get_torrent_info_from_id(&self, torrent_id: i64) -> Result; /// Get torrent's info as `DbTorrentInfo` from torrent `InfoHash`. - async fn get_torrent_info_from_infohash(&self, info_hash: &InfoHash) -> Result; + async fn get_torrent_info_from_info_hash(&self, info_hash: &InfoHash) -> Result; /// Get all torrent's files as `Vec` from `torrent_id`. async fn get_torrent_files_from_id(&self, torrent_id: i64) -> Result, Error>; @@ -217,7 +217,7 @@ pub trait Database: Sync + Send { async fn get_torrent_listing_from_id(&self, torrent_id: i64) -> Result; /// Get `TorrentListing` from `InfoHash`. - async fn get_torrent_listing_from_infohash(&self, infohash: &InfoHash) -> Result; + async fn get_torrent_listing_from_info_hash(&self, info_hash: &InfoHash) -> Result; /// Get all torrents as `Vec`. async fn get_all_torrents_compact(&self) -> Result, Error>; diff --git a/src/databases/mysql.rs b/src/databases/mysql.rs index d566a1de..7985d752 100644 --- a/src/databases/mysql.rs +++ b/src/databases/mysql.rs @@ -541,11 +541,11 @@ impl Database for Mysql { .map_err(|_| database::Error::TorrentNotFound) } - async fn get_torrent_info_from_infohash(&self, infohash: &InfoHash) -> Result { + async fn get_torrent_info_from_info_hash(&self, info_hash: &InfoHash) -> Result { query_as::<_, DbTorrentInfo>( "SELECT torrent_id, info_hash, name, pieces, piece_length, private, root_hash FROM torrust_torrents WHERE info_hash = ?", ) - .bind(infohash.to_hex_string().to_uppercase()) // `info_hash` is stored as uppercase hex string + .bind(info_hash.to_hex_string().to_uppercase()) // `info_hash` is stored as uppercase hex string .fetch_one(&self.pool) .await .map_err(|_| database::Error::TorrentNotFound) @@ -603,7 +603,7 @@ impl Database for Mysql { .map_err(|_| database::Error::TorrentNotFound) } - async fn get_torrent_listing_from_infohash(&self, infohash: &InfoHash) -> Result { + async fn get_torrent_listing_from_info_hash(&self, info_hash: &InfoHash) -> Result { query_as::<_, TorrentListing>( "SELECT tt.torrent_id, tp.username AS uploader, tt.info_hash, ti.title, ti.description, tt.category_id, DATE_FORMAT(tt.date_uploaded, '%Y-%m-%d %H:%i:%s') AS date_uploaded, tt.size AS file_size, CAST(COALESCE(sum(ts.seeders),0) as signed) as seeders, @@ -615,7 +615,7 @@ impl Database for Mysql { WHERE tt.info_hash = ? GROUP BY torrent_id" ) - .bind(infohash.to_hex_string().to_uppercase()) // `info_hash` is stored as uppercase hex string + .bind(info_hash.to_hex_string().to_uppercase()) // `info_hash` is stored as uppercase hex string .fetch_one(&self.pool) .await .map_err(|_| database::Error::TorrentNotFound) diff --git a/src/databases/sqlite.rs b/src/databases/sqlite.rs index 70c6ac0a..c2678e1c 100644 --- a/src/databases/sqlite.rs +++ b/src/databases/sqlite.rs @@ -531,11 +531,11 @@ impl Database for Sqlite { .map_err(|_| database::Error::TorrentNotFound) } - async fn get_torrent_info_from_infohash(&self, infohash: &InfoHash) -> Result { + async fn get_torrent_info_from_info_hash(&self, info_hash: &InfoHash) -> Result { query_as::<_, DbTorrentInfo>( "SELECT torrent_id, info_hash, name, pieces, piece_length, private, root_hash FROM torrust_torrents WHERE info_hash = ?", ) - .bind(infohash.to_hex_string().to_uppercase()) // `info_hash` is stored as uppercase hex string + .bind(info_hash.to_hex_string().to_uppercase()) // `info_hash` is stored as uppercase hex string .fetch_one(&self.pool) .await .map_err(|_| database::Error::TorrentNotFound) @@ -593,7 +593,7 @@ impl Database for Sqlite { .map_err(|_| database::Error::TorrentNotFound) } - async fn get_torrent_listing_from_infohash(&self, infohash: &InfoHash) -> Result { + async fn get_torrent_listing_from_info_hash(&self, info_hash: &InfoHash) -> Result { query_as::<_, TorrentListing>( "SELECT tt.torrent_id, tp.username AS uploader, tt.info_hash, ti.title, ti.description, tt.category_id, tt.date_uploaded, tt.size AS file_size, CAST(COALESCE(sum(ts.seeders),0) as signed) as seeders, @@ -605,7 +605,7 @@ impl Database for Sqlite { WHERE tt.info_hash = ? GROUP BY ts.torrent_id" ) - .bind(infohash.to_string().to_uppercase()) // `info_hash` is stored as uppercase + .bind(info_hash.to_string().to_uppercase()) // `info_hash` is stored as uppercase .fetch_one(&self.pool) .await .map_err(|_| database::Error::TorrentNotFound) diff --git a/src/models/info_hash.rs b/src/models/info_hash.rs index 7392c791..3925d4a4 100644 --- a/src/models/info_hash.rs +++ b/src/models/info_hash.rs @@ -6,11 +6,11 @@ //! See [BEP 3. The `BitTorrent` Protocol Specification](https://www.bittorrent.org/beps/bep_0003.html) //! for the official specification. //! -//! This modules provides a type that can be used to represent infohashes. +//! This modules provides a type that can be used to represent info-hashes. //! //! > **NOTICE**: It only supports Info Hash v1. //! -//! Typically infohashes are represented as hex strings, but internally they are +//! Typically info-hashes are represented as hex strings, but internally they are //! a 20-byte array. //! //! # Calculating the info-hash of a torrent file @@ -109,7 +109,7 @@ //! } //! ``` //! -//! The infohash is the [SHA1](https://en.wikipedia.org/wiki/SHA-1) hash +//! The info-hash is the [SHA1](https://en.wikipedia.org/wiki/SHA-1) hash //! of the `info` attribute. That is, the SHA1 hash of: //! //! ```text @@ -217,14 +217,14 @@ impl std::convert::From<[u8; 20]> for InfoHash { /// Errors that can occur when converting from a `Vec` to an `InfoHash`. #[derive(Error, Debug)] pub enum ConversionError { - /// Not enough bytes for infohash. An infohash is 20 bytes. - #[error("not enough bytes for infohash: {message} {location}")] + /// Not enough bytes for info-hash. An info-hash is 20 bytes. + #[error("not enough bytes for info-hash: {message} {location}")] NotEnoughBytes { location: &'static Location<'static>, message: String, }, - /// Too many bytes for infohash. An infohash is 20 bytes. - #[error("too many bytes for infohash: {message} {location}")] + /// Too many bytes for info-hash. An info-hash is 20 bytes. + #[error("too many bytes for info-hash: {message} {location}")] TooManyBytes { location: &'static Location<'static>, message: String, diff --git a/src/routes/torrent.rs b/src/routes/torrent.rs index b9018bcd..e670c27d 100644 --- a/src/routes/torrent.rs +++ b/src/routes/torrent.rs @@ -144,14 +144,14 @@ pub async fn upload(req: HttpRequest, payload: Multipart, app_data: WebAppData) /// /// # Errors /// -/// Returns `ServiceError::BadRequest` if the torrent infohash is invalid. +/// Returns `ServiceError::BadRequest` if the torrent info-hash is invalid. pub async fn download_torrent_handler(req: HttpRequest, app_data: WebAppData) -> ServiceResult { - let info_hash = get_torrent_infohash_from_request(&req)?; + let info_hash = get_torrent_info_hash_from_request(&req)?; // optional let user = app_data.auth.get_user_compact_from_request(&req).await; - let mut torrent = app_data.database.get_torrent_from_infohash(&info_hash).await?; + let mut torrent = app_data.database.get_torrent_from_info_hash(&info_hash).await?; let settings = app_data.cfg.settings.read().await; @@ -199,9 +199,9 @@ pub async fn get(req: HttpRequest, app_data: WebAppData) -> ServiceResult ServiceResult ServiceResult, app_data: WebAppData) -> ServiceResult { let user = app_data.auth.get_user_compact_from_request(&req).await?; - let infohash = get_torrent_infohash_from_request(&req)?; + let info_hash = get_torrent_info_hash_from_request(&req)?; - let torrent_listing = app_data.database.get_torrent_listing_from_infohash(&infohash).await?; + let torrent_listing = app_data.database.get_torrent_listing_from_info_hash(&info_hash).await?; // check if user is owner or administrator if torrent_listing.uploader != user.username && !user.administrator { @@ -341,10 +341,10 @@ pub async fn delete(req: HttpRequest, app_data: WebAppData) -> ServiceResult, app_data: WebAppData) - Ok(HttpResponse::Ok().json(OkResponse { data: torrents_response })) } -fn get_torrent_infohash_from_request(req: &HttpRequest) -> Result { +fn get_torrent_info_hash_from_request(req: &HttpRequest) -> Result { match req.match_info().get("info_hash") { None => Err(ServiceError::BadRequest), Some(info_hash) => match InfoHash::from_str(info_hash) { diff --git a/tests/common/client.rs b/tests/common/client.rs index 113e2f5a..0135a517 100644 --- a/tests/common/client.rs +++ b/tests/common/client.rs @@ -93,17 +93,17 @@ impl Client { self.http_client.get("torrents", params).await } - pub async fn get_torrent(&self, infohash: &InfoHash) -> TextResponse { - self.http_client.get(&format!("torrent/{infohash}"), Query::empty()).await + pub async fn get_torrent(&self, info_hash: &InfoHash) -> TextResponse { + self.http_client.get(&format!("torrent/{info_hash}"), Query::empty()).await } - pub async fn delete_torrent(&self, infohash: &InfoHash) -> TextResponse { - self.http_client.delete(&format!("torrent/{infohash}")).await + pub async fn delete_torrent(&self, info_hash: &InfoHash) -> TextResponse { + self.http_client.delete(&format!("torrent/{info_hash}")).await } - pub async fn update_torrent(&self, infohash: &InfoHash, update_torrent_form: UpdateTorrentFrom) -> TextResponse { + pub async fn update_torrent(&self, info_hash: &InfoHash, update_torrent_form: UpdateTorrentFrom) -> TextResponse { self.http_client - .put(&format!("torrent/{infohash}"), &update_torrent_form) + .put(&format!("torrent/{info_hash}"), &update_torrent_form) .await } diff --git a/tests/common/contexts/torrent/fixtures.rs b/tests/common/contexts/torrent/fixtures.rs index 34146adf..e4ce70f1 100644 --- a/tests/common/contexts/torrent/fixtures.rs +++ b/tests/common/contexts/torrent/fixtures.rs @@ -92,7 +92,7 @@ impl TestTorrent { } } - pub fn infohash(&self) -> InfoHash { + pub fn info_hash(&self) -> InfoHash { self.file_info.info_hash.clone() } } diff --git a/tests/e2e/contexts/torrent/contract.rs b/tests/e2e/contexts/torrent/contract.rs index 54ee3646..929e0cea 100644 --- a/tests/e2e/contexts/torrent/contract.rs +++ b/tests/e2e/contexts/torrent/contract.rs @@ -150,7 +150,7 @@ mod for_guests { } #[tokio::test] - async fn it_should_allow_guests_to_get_torrent_details_searching_by_infohash() { + async fn it_should_allow_guests_to_get_torrent_details_searching_by_info_hash() { let mut env = TestEnv::new(); env.start().await; @@ -164,7 +164,7 @@ mod for_guests { let uploader = new_logged_in_user(&env).await; let (test_torrent, uploaded_torrent) = upload_random_torrent_to_index(&uploader, &env).await; - let response = client.get_torrent(&test_torrent.infohash()).await; + let response = client.get_torrent(&test_torrent.info_hash()).await; let torrent_details_response: TorrentDetailsResponse = serde_json::from_str(&response.body).unwrap(); @@ -213,7 +213,7 @@ mod for_guests { } #[tokio::test] - async fn it_should_allow_guests_to_download_a_torrent_file_searching_by_infohash() { + async fn it_should_allow_guests_to_download_a_torrent_file_searching_by_info_hash() { let mut env = TestEnv::new(); env.start().await; @@ -227,7 +227,7 @@ mod for_guests { let uploader = new_logged_in_user(&env).await; let (test_torrent, _torrent_listed_in_index) = upload_random_torrent_to_index(&uploader, &env).await; - let response = client.download_torrent(&test_torrent.infohash()).await; + let response = client.download_torrent(&test_torrent.info_hash()).await; let torrent = decode_torrent(&response.bytes).expect("could not decode downloaded torrent"); let uploaded_torrent = @@ -272,7 +272,7 @@ mod for_guests { let uploader = new_logged_in_user(&env).await; let (test_torrent, _uploaded_torrent) = upload_random_torrent_to_index(&uploader, &env).await; - let response = client.delete_torrent(&test_torrent.infohash()).await; + let response = client.delete_torrent(&test_torrent.info_hash()).await; assert_eq!(response.status, 401); } @@ -305,7 +305,7 @@ mod for_authenticated_users { let client = Client::authenticated(&env.server_socket_addr().unwrap(), &uploader.token); let test_torrent = random_torrent(); - let infohash = test_torrent.infohash().clone(); + let info_hash = test_torrent.info_hash().clone(); let form: UploadTorrentMultipartForm = test_torrent.index_info.into(); @@ -315,7 +315,7 @@ mod for_authenticated_users { assert_eq!( uploaded_torrent_response.data.info_hash.to_lowercase(), - infohash.to_lowercase() + info_hash.to_lowercase() ); assert!(response.is_json_and_ok()); } @@ -369,7 +369,7 @@ mod for_authenticated_users { } #[tokio::test] - async fn it_should_not_allow_uploading_a_torrent_with_a_infohash_that_already_exists() { + async fn it_should_not_allow_uploading_a_torrent_with_a_info_hash_that_already_exists() { let mut env = TestEnv::new(); env.start().await; @@ -388,7 +388,7 @@ mod for_authenticated_users { let form: UploadTorrentMultipartForm = first_torrent.index_info.into(); let _response = client.upload_torrent(form.into()).await; - // Upload the second torrent with the same infohash as the first one. + // Upload the second torrent with the same info-hash as the first one. // We need to change the title otherwise the torrent will be rejected // because of the duplicate title. first_torrent_clone.index_info.title = format!("{first_torrent_title}-clone"); @@ -417,7 +417,7 @@ mod for_authenticated_users { let client = Client::authenticated(&env.server_socket_addr().unwrap(), &downloader.token); // When the user downloads the torrent - let response = client.download_torrent(&test_torrent.infohash()).await; + let response = client.download_torrent(&test_torrent.info_hash()).await; let torrent = decode_torrent(&response.bytes).expect("could not decode downloaded torrent"); @@ -456,7 +456,7 @@ mod for_authenticated_users { let client = Client::authenticated(&env.server_socket_addr().unwrap(), &uploader.token); - let response = client.delete_torrent(&test_torrent.infohash()).await; + let response = client.delete_torrent(&test_torrent.info_hash()).await; assert_eq!(response.status, 403); } @@ -484,7 +484,7 @@ mod for_authenticated_users { let response = client .update_torrent( - &test_torrent.infohash(), + &test_torrent.info_hash(), UpdateTorrentFrom { title: Some(new_title.clone()), description: Some(new_description.clone()), @@ -524,7 +524,7 @@ mod for_authenticated_users { let response = client .update_torrent( - &test_torrent.infohash(), + &test_torrent.info_hash(), UpdateTorrentFrom { title: Some(new_title.clone()), description: Some(new_description.clone()), @@ -551,7 +551,7 @@ mod for_authenticated_users { use crate::e2e::environment::TestEnv; #[tokio::test] - async fn it_should_allow_admins_to_delete_torrents_searching_by_infohash() { + async fn it_should_allow_admins_to_delete_torrents_searching_by_info_hash() { let mut env = TestEnv::new(); env.start().await; @@ -566,7 +566,7 @@ mod for_authenticated_users { let admin = new_logged_in_admin(&env).await; let client = Client::authenticated(&env.server_socket_addr().unwrap(), &admin.token); - let response = client.delete_torrent(&test_torrent.infohash()).await; + let response = client.delete_torrent(&test_torrent.info_hash()).await; let deleted_torrent_response: DeletedTorrentResponse = serde_json::from_str(&response.body).unwrap(); @@ -595,7 +595,7 @@ mod for_authenticated_users { let response = client .update_torrent( - &test_torrent.infohash(), + &test_torrent.info_hash(), UpdateTorrentFrom { title: Some(new_title.clone()), description: Some(new_description.clone()),