@@ -988,9 +988,7 @@ impl Tracker {
988988 /// # Errors
989989 ///
990990 /// Will return a `key::Error` if unable to get any `auth_key`.
991- pub async fn verify_auth_key ( & self , key : & Key ) -> Result < ( ) , auth:: Error > {
992- // code-review: this function is public only because it's used in a test.
993- // We should change the test and make it private.
991+ async fn verify_auth_key ( & self , key : & Key ) -> Result < ( ) , auth:: Error > {
994992 match self . keys . read ( ) . await . get ( key) {
995993 None => Err ( auth:: Error :: UnableToReadKey {
996994 location : Location :: caller ( ) ,
@@ -1787,35 +1785,8 @@ mod tests {
17871785 use std:: str:: FromStr ;
17881786 use std:: time:: Duration ;
17891787
1790- use torrust_tracker_clock:: clock:: Time ;
1791- use torrust_tracker_configuration:: v2:: core:: PrivateMode ;
1792-
1793- use crate :: core:: auth:: { self , Key } ;
1788+ use crate :: core:: auth:: { self } ;
17941789 use crate :: core:: tests:: the_tracker:: private_tracker;
1795- use crate :: CurrentClock ;
1796-
1797- #[ tokio:: test]
1798- async fn it_should_generate_the_expiring_authentication_keys ( ) {
1799- let tracker = private_tracker ( ) ;
1800-
1801- let key = tracker. generate_auth_key ( Some ( Duration :: from_secs ( 100 ) ) ) . await . unwrap ( ) ;
1802-
1803- assert_eq ! (
1804- key. valid_until,
1805- Some ( CurrentClock :: now_add( & Duration :: from_secs( 100 ) ) . unwrap( ) )
1806- ) ;
1807- }
1808-
1809- #[ tokio:: test]
1810- async fn it_should_authenticate_a_peer_by_using_a_key ( ) {
1811- let tracker = private_tracker ( ) ;
1812-
1813- let expiring_key = tracker. generate_auth_key ( Some ( Duration :: from_secs ( 100 ) ) ) . await . unwrap ( ) ;
1814-
1815- let result = tracker. authenticate ( & expiring_key. key ( ) ) . await ;
1816-
1817- assert ! ( result. is_ok( ) ) ;
1818- }
18191790
18201791 #[ tokio:: test]
18211792 async fn it_should_fail_authenticating_a_peer_when_it_uses_an_unregistered_key ( ) {
@@ -1828,35 +1799,6 @@ mod tests {
18281799 assert ! ( result. is_err( ) ) ;
18291800 }
18301801
1831- #[ tokio:: test]
1832- async fn it_should_verify_a_valid_authentication_key ( ) {
1833- // todo: this should not be tested directly because
1834- // `verify_auth_key` should be a private method.
1835- let tracker = private_tracker ( ) ;
1836-
1837- let expiring_key = tracker. generate_auth_key ( Some ( Duration :: from_secs ( 100 ) ) ) . await . unwrap ( ) ;
1838-
1839- assert ! ( tracker. verify_auth_key( & expiring_key. key( ) ) . await . is_ok( ) ) ;
1840- }
1841-
1842- #[ tokio:: test]
1843- async fn it_should_accept_an_expired_key_when_checking_expiration_is_disabled_in_configuration ( ) {
1844- let mut tracker = private_tracker ( ) ;
1845-
1846- tracker. config . private_mode = Some ( PrivateMode {
1847- check_keys_expiration : false ,
1848- } ) ;
1849-
1850- let past_time = Some ( Duration :: ZERO ) ;
1851-
1852- let expiring_key = tracker
1853- . add_auth_key ( Key :: new ( "YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ" ) . unwrap ( ) , past_time)
1854- . await
1855- . unwrap ( ) ;
1856-
1857- assert ! ( tracker. authenticate( & expiring_key. key( ) ) . await . is_ok( ) ) ;
1858- }
1859-
18601802 #[ tokio:: test]
18611803 async fn it_should_fail_verifying_an_unregistered_authentication_key ( ) {
18621804 let tracker = private_tracker ( ) ;
@@ -1892,6 +1834,192 @@ mod tests {
18921834 assert ! ( result. is_ok( ) ) ;
18931835 assert ! ( tracker. verify_auth_key( & expiring_key. key( ) ) . await . is_ok( ) ) ;
18941836 }
1837+
1838+ mod with_expiring_and {
1839+
1840+ mod randomly_generated_keys {
1841+ use std:: time:: Duration ;
1842+
1843+ use torrust_tracker_clock:: clock:: Time ;
1844+ use torrust_tracker_configuration:: v2:: core:: PrivateMode ;
1845+
1846+ use crate :: core:: auth:: Key ;
1847+ use crate :: core:: tests:: the_tracker:: private_tracker;
1848+ use crate :: CurrentClock ;
1849+
1850+ #[ tokio:: test]
1851+ async fn it_should_generate_the_key ( ) {
1852+ let tracker = private_tracker ( ) ;
1853+
1854+ let peer_key = tracker. generate_auth_key ( Some ( Duration :: from_secs ( 100 ) ) ) . await . unwrap ( ) ;
1855+
1856+ assert_eq ! (
1857+ peer_key. valid_until,
1858+ Some ( CurrentClock :: now_add( & Duration :: from_secs( 100 ) ) . unwrap( ) )
1859+ ) ;
1860+ }
1861+
1862+ #[ tokio:: test]
1863+ async fn it_should_authenticate_a_peer_with_the_key ( ) {
1864+ let tracker = private_tracker ( ) ;
1865+
1866+ let peer_key = tracker. generate_auth_key ( Some ( Duration :: from_secs ( 100 ) ) ) . await . unwrap ( ) ;
1867+
1868+ let result = tracker. authenticate ( & peer_key. key ( ) ) . await ;
1869+
1870+ assert ! ( result. is_ok( ) ) ;
1871+ }
1872+
1873+ #[ tokio:: test]
1874+ async fn it_should_accept_an_expired_key_when_checking_expiration_is_disabled_in_configuration ( ) {
1875+ let mut tracker = private_tracker ( ) ;
1876+
1877+ tracker. config . private_mode = Some ( PrivateMode {
1878+ check_keys_expiration : false ,
1879+ } ) ;
1880+
1881+ let past_timestamp = Duration :: ZERO ;
1882+
1883+ let peer_key = tracker
1884+ . add_auth_key ( Key :: new ( "YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ" ) . unwrap ( ) , Some ( past_timestamp) )
1885+ . await
1886+ . unwrap ( ) ;
1887+
1888+ assert ! ( tracker. authenticate( & peer_key. key( ) ) . await . is_ok( ) ) ;
1889+ }
1890+ }
1891+
1892+ mod pre_generated_keys {
1893+ use std:: time:: Duration ;
1894+
1895+ use torrust_tracker_clock:: clock:: Time ;
1896+ use torrust_tracker_configuration:: v2:: core:: PrivateMode ;
1897+
1898+ use crate :: core:: auth:: Key ;
1899+ use crate :: core:: tests:: the_tracker:: private_tracker;
1900+ use crate :: core:: AddKeyRequest ;
1901+ use crate :: CurrentClock ;
1902+
1903+ #[ tokio:: test]
1904+ async fn it_should_add_a_pre_generated_key ( ) {
1905+ let tracker = private_tracker ( ) ;
1906+
1907+ let peer_key = tracker
1908+ . add_peer_key ( AddKeyRequest {
1909+ opt_key : Some ( Key :: new ( "YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ" ) . unwrap ( ) . to_string ( ) ) ,
1910+ opt_seconds_valid : Some ( 100 ) ,
1911+ } )
1912+ . await
1913+ . unwrap ( ) ;
1914+
1915+ assert_eq ! (
1916+ peer_key. valid_until,
1917+ Some ( CurrentClock :: now_add( & Duration :: from_secs( 100 ) ) . unwrap( ) )
1918+ ) ;
1919+ }
1920+
1921+ #[ tokio:: test]
1922+ async fn it_should_authenticate_a_peer_with_the_key ( ) {
1923+ let tracker = private_tracker ( ) ;
1924+
1925+ let peer_key = tracker
1926+ . add_peer_key ( AddKeyRequest {
1927+ opt_key : Some ( Key :: new ( "YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ" ) . unwrap ( ) . to_string ( ) ) ,
1928+ opt_seconds_valid : Some ( 100 ) ,
1929+ } )
1930+ . await
1931+ . unwrap ( ) ;
1932+
1933+ let result = tracker. authenticate ( & peer_key. key ( ) ) . await ;
1934+
1935+ assert ! ( result. is_ok( ) ) ;
1936+ }
1937+
1938+ #[ tokio:: test]
1939+ async fn it_should_accept_an_expired_key_when_checking_expiration_is_disabled_in_configuration ( ) {
1940+ let mut tracker = private_tracker ( ) ;
1941+
1942+ tracker. config . private_mode = Some ( PrivateMode {
1943+ check_keys_expiration : false ,
1944+ } ) ;
1945+
1946+ let peer_key = tracker
1947+ . add_peer_key ( AddKeyRequest {
1948+ opt_key : Some ( Key :: new ( "YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ" ) . unwrap ( ) . to_string ( ) ) ,
1949+ opt_seconds_valid : Some ( 0 ) ,
1950+ } )
1951+ . await
1952+ . unwrap ( ) ;
1953+
1954+ assert ! ( tracker. authenticate( & peer_key. key( ) ) . await . is_ok( ) ) ;
1955+ }
1956+ }
1957+ }
1958+
1959+ mod with_permanent_and {
1960+
1961+ mod randomly_generated_keys {
1962+ use crate :: core:: tests:: the_tracker:: private_tracker;
1963+
1964+ #[ tokio:: test]
1965+ async fn it_should_generate_the_key ( ) {
1966+ let tracker = private_tracker ( ) ;
1967+
1968+ let peer_key = tracker. generate_permanent_auth_key ( ) . await . unwrap ( ) ;
1969+
1970+ assert_eq ! ( peer_key. valid_until, None ) ;
1971+ }
1972+
1973+ #[ tokio:: test]
1974+ async fn it_should_authenticate_a_peer_with_the_key ( ) {
1975+ let tracker = private_tracker ( ) ;
1976+
1977+ let peer_key = tracker. generate_permanent_auth_key ( ) . await . unwrap ( ) ;
1978+
1979+ let result = tracker. authenticate ( & peer_key. key ( ) ) . await ;
1980+
1981+ assert ! ( result. is_ok( ) ) ;
1982+ }
1983+ }
1984+
1985+ mod pre_generated_keys {
1986+ use crate :: core:: auth:: Key ;
1987+ use crate :: core:: tests:: the_tracker:: private_tracker;
1988+ use crate :: core:: AddKeyRequest ;
1989+
1990+ #[ tokio:: test]
1991+ async fn it_should_add_a_pre_generated_key ( ) {
1992+ let tracker = private_tracker ( ) ;
1993+
1994+ let peer_key = tracker
1995+ . add_peer_key ( AddKeyRequest {
1996+ opt_key : Some ( Key :: new ( "YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ" ) . unwrap ( ) . to_string ( ) ) ,
1997+ opt_seconds_valid : None ,
1998+ } )
1999+ . await
2000+ . unwrap ( ) ;
2001+
2002+ assert_eq ! ( peer_key. valid_until, None ) ;
2003+ }
2004+
2005+ #[ tokio:: test]
2006+ async fn it_should_authenticate_a_peer_with_the_key ( ) {
2007+ let tracker = private_tracker ( ) ;
2008+
2009+ let peer_key = tracker
2010+ . add_peer_key ( AddKeyRequest {
2011+ opt_key : Some ( Key :: new ( "YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ" ) . unwrap ( ) . to_string ( ) ) ,
2012+ opt_seconds_valid : None ,
2013+ } )
2014+ . await
2015+ . unwrap ( ) ;
2016+
2017+ let result = tracker. authenticate ( & peer_key. key ( ) ) . await ;
2018+
2019+ assert ! ( result. is_ok( ) ) ;
2020+ }
2021+ }
2022+ }
18952023 }
18962024
18972025 mod handling_an_announce_request { }
0 commit comments