From 8f4ada196559f6db17628842bd90e0023b7a4e77 Mon Sep 17 00:00:00 2001 From: Quentin Date: Sat, 7 Nov 2020 13:53:32 +0100 Subject: [PATCH 1/4] Add a test for parse_bucket_key --- src/api/api_server.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/api/api_server.rs b/src/api/api_server.rs index 6221d784c..dfdd46873 100644 --- a/src/api/api_server.rs +++ b/src/api/api_server.rs @@ -250,6 +250,10 @@ async fn handler_inner(garage: Arc, req: Request) -> Result Result<(&str, Option<&str>), Error> { let path = path.trim_start_matches('/'); @@ -265,3 +269,16 @@ fn parse_bucket_key(path: &str) -> Result<(&str, Option<&str>), Error> { None => Ok((path, None)), } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn parse_bucket_with_key() -> Result<(), Error> { + let (bucket,key) = parse_bucket_key("/my_bucket/a/super/file.jpg")?; + assert_eq!(bucket, "my_bucket"); + assert_eq!(key.expect("key must be set"), "a/super/file.jpg"); + Ok(()) + } +} From 9a50ce12a8f334a306022921a4941a1ff37c8a8b Mon Sep 17 00:00:00 2001 From: Quentin Date: Sat, 7 Nov 2020 13:59:30 +0100 Subject: [PATCH 2/4] Fix formatting --- src/api/api_server.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/api/api_server.rs b/src/api/api_server.rs index dfdd46873..507e6a9da 100644 --- a/src/api/api_server.rs +++ b/src/api/api_server.rs @@ -276,9 +276,9 @@ mod tests { #[test] fn parse_bucket_with_key() -> Result<(), Error> { - let (bucket,key) = parse_bucket_key("/my_bucket/a/super/file.jpg")?; + let (bucket, key) = parse_bucket_key("/my_bucket/a/super/file.jpg")?; assert_eq!(bucket, "my_bucket"); - assert_eq!(key.expect("key must be set"), "a/super/file.jpg"); + assert_eq!(key.expect("key must be set"), "a/super/file.jpg"); Ok(()) } } From e1415f597631518f002080fe2016b6207b223f4f Mon Sep 17 00:00:00 2001 From: Quentin Date: Sat, 7 Nov 2020 15:34:53 +0100 Subject: [PATCH 3/4] Add a second test --- src/api/api_server.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/api/api_server.rs b/src/api/api_server.rs index 507e6a9da..3f1c2f214 100644 --- a/src/api/api_server.rs +++ b/src/api/api_server.rs @@ -275,10 +275,21 @@ mod tests { use super::*; #[test] - fn parse_bucket_with_key() -> Result<(), Error> { + fn parse_bucket_containing_a_key() -> Result<(), Error> { let (bucket, key) = parse_bucket_key("/my_bucket/a/super/file.jpg")?; assert_eq!(bucket, "my_bucket"); assert_eq!(key.expect("key must be set"), "a/super/file.jpg"); Ok(()) + } + + #[test] + fn parse_bucket_containing_no_key() -> Result<(), Error> { + let (bucket, key) = parse_bucket_key("/my_bucket/")?; + assert_eq!(bucket, "my_bucket"); + assert!(key.is_none()); + let (bucket, key) = parse_bucket_key("/my_bucket")?; + assert_eq!(bucket, "my_bucket"); + assert!(key.is_none()); + Ok(()) } } From 6a8b972f3a9c0c329915af6f301f849ea2b8cbe3 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Sun, 8 Nov 2020 13:39:44 +0100 Subject: [PATCH 4/4] Modif parse_bucket_key to fail when bucket is "" --- src/api/api_server.rs | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/api/api_server.rs b/src/api/api_server.rs index 3f1c2f214..9dc74dac8 100644 --- a/src/api/api_server.rs +++ b/src/api/api_server.rs @@ -72,11 +72,6 @@ async fn handler_inner(garage: Arc, req: Request) -> Result, req: Request) -> Result Result<(&str, Option<&str>), Error> { let path = path.trim_start_matches('/'); - match path.find('/') { + let (bucket, key) = match path.find('/') { Some(i) => { let key = &path[i + 1..]; if key.len() > 0 { - Ok((&path[..i], Some(key))) + (&path[..i], Some(key)) } else { - Ok((&path[..i], None)) + (&path[..i], None) } } - None => Ok((path, None)), + None => (path, None), + }; + if bucket.len() == 0 { + return Err(Error::BadRequest(format!("No bucket specified"))); } + Ok((bucket, key)) } #[cfg(test)] @@ -280,7 +279,7 @@ mod tests { assert_eq!(bucket, "my_bucket"); assert_eq!(key.expect("key must be set"), "a/super/file.jpg"); Ok(()) - } + } #[test] fn parse_bucket_containing_no_key() -> Result<(), Error> { @@ -292,4 +291,14 @@ mod tests { assert!(key.is_none()); Ok(()) } + + #[test] + fn parse_bucket_containing_no_bucket() { + let parsed = parse_bucket_key(""); + assert!(parsed.is_err()); + let parsed = parse_bucket_key("/"); + assert!(parsed.is_err()); + let parsed = parse_bucket_key("////"); + assert!(parsed.is_err()); + } }