@@ -81,7 +81,7 @@ use aquatic_udp_protocol::ConnectionId as Cookie;
8181use cookie_builder:: { assemble, decode, disassemble, encode} ;
8282use zerocopy:: AsBytes ;
8383
84- use super :: error:: { self , Error } ;
84+ use super :: error:: Error ;
8585use crate :: shared:: crypto:: keys:: CipherArrayBlowfish ;
8686
8787/// Generates a new connection cookie.
@@ -106,6 +106,8 @@ pub fn make(fingerprint: u64, issue_at: f64) -> Result<Cookie, Error> {
106106 Ok ( zerocopy:: FromBytes :: read_from ( cookie. as_slice ( ) ) . expect ( "it should be the same size" ) )
107107}
108108
109+ use std:: ops:: Range ;
110+
109111/// Checks if the supplied `connection_cookie` is valid.
110112///
111113/// # Errors
@@ -114,32 +116,32 @@ pub fn make(fingerprint: u64, issue_at: f64) -> Result<Cookie, Error> {
114116///
115117/// # Panics
116118///
117- /// It would panic if cookie min value is larger than the max value .
118- pub fn check ( cookie : & Cookie , fingerprint : u64 , min : f64 , max : f64 ) -> Result < f64 , Error > {
119- assert ! ( min < max , "min is larger than max " ) ;
119+ /// It would panic if the range is empty .
120+ pub fn check ( cookie : & Cookie , fingerprint : u64 , valid_range : Range < f64 > ) -> Result < f64 , Error > {
121+ assert ! ( valid_range . is_empty ( ) , "a range should be defined " ) ;
120122
121123 let cookie_bytes = CipherArrayBlowfish :: from_slice ( cookie. 0 . as_bytes ( ) ) ;
122124 let cookie_bytes = decode ( * cookie_bytes) ;
123125
124126 let issue_time = disassemble ( fingerprint, cookie_bytes) ;
125127
126128 if !issue_time. is_normal ( ) {
127- return Err ( Error :: InvalidConnectionId {
128- bad_id : error :: ConnectionCookie ( * cookie ) ,
129+ return Err ( Error :: ConnectionIdNotNormal {
130+ not_normal_value : issue_time ,
129131 } ) ;
130132 }
131133
132- if issue_time < min {
134+ if issue_time < valid_range . start {
133135 return Err ( Error :: ConnectionIdExpired {
134- bad_age : issue_time,
135- min_age : min ,
136+ expired_value : issue_time,
137+ min_value : valid_range . start ,
136138 } ) ;
137139 }
138140
139- if issue_time > max {
141+ if issue_time > valid_range . end {
140142 return Err ( Error :: ConnectionIdFromFuture {
141- future_age : issue_time,
142- max_age : max ,
143+ future_value : issue_time,
144+ max_value : valid_range . end ,
143145 } ) ;
144146 }
145147
@@ -262,7 +264,7 @@ mod tests {
262264 let min = issue_at - 10.0 ;
263265 let max = issue_at + 10.0 ;
264266
265- let result = check ( & cookie, fingerprint, min, max) . unwrap ( ) ;
267+ let result = check ( & cookie, fingerprint, min.. max) . unwrap ( ) ;
266268
267269 // we should have exactly the same bytes returned
268270 assert_eq ! ( result. to_ne_bytes( ) , issue_at. to_ne_bytes( ) ) ;
@@ -277,7 +279,7 @@ mod tests {
277279 let min = issue_at + 10.0 ;
278280 let max = issue_at + 20.0 ;
279281
280- let result = check ( & cookie, fingerprint, min, max) . unwrap_err ( ) ;
282+ let result = check ( & cookie, fingerprint, min.. max) . unwrap_err ( ) ;
281283
282284 match result {
283285 Error :: ConnectionIdExpired { .. } => { } // Expected error
@@ -295,7 +297,7 @@ mod tests {
295297 let min = issue_at - 20.0 ;
296298 let max = issue_at - 10.0 ;
297299
298- let result = check ( & cookie, fingerprint, min, max) . unwrap_err ( ) ;
300+ let result = check ( & cookie, fingerprint, min.. max) . unwrap_err ( ) ;
299301
300302 match result {
301303 Error :: ConnectionIdFromFuture { .. } => { } // Expected error
0 commit comments