mod components;
use core::fmt;
use core::hash::Hasher;
pub use components::*;
use crate::common::CheckedPathError;
use crate::no_std_compat::*;
use crate::typed::{Utf8TypedPath, Utf8TypedPathBuf};
use crate::{private, Encoding, UnixEncoding, Utf8Encoding, Utf8Path, Utf8PathBuf};
pub type Utf8UnixPath = Utf8Path<Utf8UnixEncoding>;
pub type Utf8UnixPathBuf = Utf8PathBuf<Utf8UnixEncoding>;
#[derive(Copy, Clone)]
pub struct Utf8UnixEncoding;
impl private::Sealed for Utf8UnixEncoding {}
impl<'a> Utf8Encoding<'a> for Utf8UnixEncoding {
type Components = Utf8UnixComponents<'a>;
fn label() -> &'static str {
"unix"
}
fn components(path: &'a str) -> Self::Components {
Utf8UnixComponents::new(path)
}
fn hash<H: Hasher>(path: &str, h: &mut H) {
UnixEncoding::hash(path.as_bytes(), h);
}
fn push(current_path: &mut String, path: &str) {
unsafe {
UnixEncoding::push(current_path.as_mut_vec(), path.as_bytes());
}
}
fn push_checked(current_path: &mut String, path: &str) -> Result<(), CheckedPathError> {
unsafe { UnixEncoding::push_checked(current_path.as_mut_vec(), path.as_bytes()) }
}
}
impl fmt::Debug for Utf8UnixEncoding {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Utf8UnixEncoding").finish()
}
}
impl fmt::Display for Utf8UnixEncoding {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Utf8UnixEncoding")
}
}
impl<T> Utf8Path<T>
where
T: for<'enc> Utf8Encoding<'enc>,
{
pub fn has_unix_encoding(&self) -> bool {
T::label() == Utf8UnixEncoding::label()
}
pub fn with_unix_encoding(&self) -> Utf8PathBuf<Utf8UnixEncoding> {
self.with_encoding()
}
pub fn with_unix_encoding_checked(
&self,
) -> Result<Utf8PathBuf<Utf8UnixEncoding>, CheckedPathError> {
self.with_encoding_checked()
}
}
impl Utf8UnixPath {
pub fn to_typed_path(&self) -> Utf8TypedPath {
Utf8TypedPath::unix(self)
}
pub fn to_typed_path_buf(&self) -> Utf8TypedPathBuf {
Utf8TypedPathBuf::from_unix(self)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn push_should_replace_current_path_with_provided_path_if_provided_path_is_absolute() {
let mut current_path = String::new();
Utf8UnixEncoding::push(&mut current_path, "/abc");
assert_eq!(current_path, "/abc");
let mut current_path = String::from("some/path");
Utf8UnixEncoding::push(&mut current_path, "/abc");
assert_eq!(current_path, "/abc");
let mut current_path = String::from("/some/path/");
Utf8UnixEncoding::push(&mut current_path, "/abc");
assert_eq!(current_path, "/abc");
}
#[test]
fn push_should_append_path_to_current_path_with_a_separator_if_provided_path_is_relative() {
let mut current_path = String::new();
Utf8UnixEncoding::push(&mut current_path, "abc");
assert_eq!(current_path, "abc");
let mut current_path = String::from("some/path");
Utf8UnixEncoding::push(&mut current_path, "abc");
assert_eq!(current_path, "some/path/abc");
let mut current_path = String::from("some/path/");
Utf8UnixEncoding::push(&mut current_path, "abc");
assert_eq!(current_path, "some/path/abc");
}
#[test]
fn push_checked_should_fail_if_providing_an_absolute_path() {
let mut current_path = String::new();
assert_eq!(
Utf8UnixEncoding::push_checked(&mut current_path, "/abc"),
Err(CheckedPathError::UnexpectedRoot)
);
assert_eq!(current_path, "");
let mut current_path = String::from("some/path");
assert_eq!(
Utf8UnixEncoding::push_checked(&mut current_path, "/abc"),
Err(CheckedPathError::UnexpectedRoot)
);
assert_eq!(current_path, "some/path");
let mut current_path = String::from("/some/path/");
assert_eq!(
Utf8UnixEncoding::push_checked(&mut current_path, "/abc"),
Err(CheckedPathError::UnexpectedRoot)
);
assert_eq!(current_path, "/some/path/");
}
#[test]
fn push_checked_should_fail_if_providing_a_path_with_disallowed_filename_characters() {
let mut current_path = String::new();
assert_eq!(
Utf8UnixEncoding::push_checked(&mut current_path, "some/inva\0lid/path"),
Err(CheckedPathError::InvalidFilename)
);
assert_eq!(current_path, "");
let mut current_path = String::from("some/path");
assert_eq!(
Utf8UnixEncoding::push_checked(&mut current_path, "some/inva\0lid/path"),
Err(CheckedPathError::InvalidFilename)
);
assert_eq!(current_path, "some/path");
let mut current_path = String::from("/some/path/");
assert_eq!(
Utf8UnixEncoding::push_checked(&mut current_path, "some/inva\0lid/path"),
Err(CheckedPathError::InvalidFilename)
);
assert_eq!(current_path, "/some/path/");
}
#[test]
fn push_checked_should_fail_if_providing_a_path_that_would_escape_the_current_path() {
let mut current_path = String::new();
assert_eq!(
Utf8UnixEncoding::push_checked(&mut current_path, ".."),
Err(CheckedPathError::PathTraversalAttack)
);
assert_eq!(current_path, "");
let mut current_path = String::from("some/path");
assert_eq!(
Utf8UnixEncoding::push_checked(&mut current_path, ".."),
Err(CheckedPathError::PathTraversalAttack)
);
assert_eq!(current_path, "some/path");
let mut current_path = String::from("/some/path/");
assert_eq!(
Utf8UnixEncoding::push_checked(&mut current_path, ".."),
Err(CheckedPathError::PathTraversalAttack)
);
assert_eq!(current_path, "/some/path/");
}
#[test]
fn push_checked_should_append_path_to_current_path_with_a_separator_if_does_not_violate_rules()
{
let mut current_path = String::new();
assert_eq!(
Utf8UnixEncoding::push_checked(&mut current_path, "abc/../def/."),
Ok(()),
);
assert_eq!(current_path, "abc/../def/.");
let mut current_path = String::from("some/path");
assert_eq!(
Utf8UnixEncoding::push_checked(&mut current_path, "abc/../def/."),
Ok(()),
);
assert_eq!(current_path, "some/path/abc/../def/.");
let mut current_path = String::from("/some/path/");
assert_eq!(
Utf8UnixEncoding::push_checked(&mut current_path, "abc/../def/."),
Ok(()),
);
assert_eq!(current_path, "/some/path/abc/../def/.");
}
}