|
| 1 | +use bstr::{BStr, BString}; |
| 2 | +use gix_path::relative_path::Error; |
| 3 | +use gix_path::RelativePath; |
| 4 | + |
| 5 | +#[cfg(not(windows))] |
| 6 | +#[test] |
| 7 | +fn absolute_paths_return_err() { |
| 8 | + let path_str: &str = "/refs/heads"; |
| 9 | + let path_bstr: &BStr = path_str.into(); |
| 10 | + let path_u8a: &[u8; 11] = b"/refs/heads"; |
| 11 | + let path_u8: &[u8] = &b"/refs/heads"[..]; |
| 12 | + let path_bstring: BString = "/refs/heads".into(); |
| 13 | + |
| 14 | + assert!(matches!( |
| 15 | + TryInto::<&RelativePath>::try_into(path_str), |
| 16 | + Err(Error::IsAbsolute) |
| 17 | + )); |
| 18 | + assert!(matches!( |
| 19 | + TryInto::<&RelativePath>::try_into(path_bstr), |
| 20 | + Err(Error::IsAbsolute) |
| 21 | + )); |
| 22 | + assert!(matches!( |
| 23 | + TryInto::<&RelativePath>::try_into(path_u8), |
| 24 | + Err(Error::IsAbsolute) |
| 25 | + )); |
| 26 | + assert!(matches!( |
| 27 | + TryInto::<&RelativePath>::try_into(path_u8a), |
| 28 | + Err(Error::IsAbsolute) |
| 29 | + )); |
| 30 | + assert!(matches!( |
| 31 | + TryInto::<&RelativePath>::try_into(&path_bstring), |
| 32 | + Err(Error::IsAbsolute) |
| 33 | + )); |
| 34 | +} |
| 35 | + |
| 36 | +#[cfg(windows)] |
| 37 | +#[test] |
| 38 | +fn absolute_paths_with_backslashes_return_err() { |
| 39 | + let path_str: &str = r"c:\refs\heads"; |
| 40 | + let path_bstr: &BStr = path_str.into(); |
| 41 | + let path_u8: &[u8] = &b"c:\\refs\\heads"[..]; |
| 42 | + let path_bstring: BString = r"c:\refs\heads".into(); |
| 43 | + |
| 44 | + assert!(matches!( |
| 45 | + TryInto::<&RelativePath>::try_into(path_str), |
| 46 | + Err(Error::IsAbsolute) |
| 47 | + )); |
| 48 | + assert!(matches!( |
| 49 | + TryInto::<&RelativePath>::try_into(path_bstr), |
| 50 | + Err(Error::IsAbsolute) |
| 51 | + )); |
| 52 | + assert!(matches!( |
| 53 | + TryInto::<&RelativePath>::try_into(path_u8), |
| 54 | + Err(Error::IsAbsolute) |
| 55 | + )); |
| 56 | + assert!(matches!( |
| 57 | + TryInto::<&RelativePath>::try_into(&path_bstring), |
| 58 | + Err(Error::IsAbsolute) |
| 59 | + )); |
| 60 | +} |
| 61 | + |
| 62 | +#[test] |
| 63 | +fn dots_in_paths_return_err() { |
| 64 | + let path_str: &str = "./heads"; |
| 65 | + let path_bstr: &BStr = path_str.into(); |
| 66 | + let path_u8: &[u8] = &b"./heads"[..]; |
| 67 | + let path_bstring: BString = "./heads".into(); |
| 68 | + |
| 69 | + assert!(matches!( |
| 70 | + TryInto::<&RelativePath>::try_into(path_str), |
| 71 | + Err(Error::ContainsInvalidComponent(_)) |
| 72 | + )); |
| 73 | + assert!(matches!( |
| 74 | + TryInto::<&RelativePath>::try_into(path_bstr), |
| 75 | + Err(Error::ContainsInvalidComponent(_)) |
| 76 | + )); |
| 77 | + assert!(matches!( |
| 78 | + TryInto::<&RelativePath>::try_into(path_u8), |
| 79 | + Err(Error::ContainsInvalidComponent(_)) |
| 80 | + )); |
| 81 | + assert!(matches!( |
| 82 | + TryInto::<&RelativePath>::try_into(&path_bstring), |
| 83 | + Err(Error::ContainsInvalidComponent(_)) |
| 84 | + )); |
| 85 | +} |
| 86 | + |
| 87 | +#[test] |
| 88 | +fn dots_in_paths_with_backslashes_return_err() { |
| 89 | + let path_str: &str = r".\heads"; |
| 90 | + let path_bstr: &BStr = path_str.into(); |
| 91 | + let path_u8: &[u8] = &b".\\heads"[..]; |
| 92 | + let path_bstring: BString = r".\heads".into(); |
| 93 | + |
| 94 | + assert!(matches!( |
| 95 | + TryInto::<&RelativePath>::try_into(path_str), |
| 96 | + Err(Error::ContainsInvalidComponent(_)) |
| 97 | + )); |
| 98 | + assert!(matches!( |
| 99 | + TryInto::<&RelativePath>::try_into(path_bstr), |
| 100 | + Err(Error::ContainsInvalidComponent(_)) |
| 101 | + )); |
| 102 | + assert!(matches!( |
| 103 | + TryInto::<&RelativePath>::try_into(path_u8), |
| 104 | + Err(Error::ContainsInvalidComponent(_)) |
| 105 | + )); |
| 106 | + assert!(matches!( |
| 107 | + TryInto::<&RelativePath>::try_into(&path_bstring), |
| 108 | + Err(Error::ContainsInvalidComponent(_)) |
| 109 | + )); |
| 110 | +} |
| 111 | + |
| 112 | +#[test] |
| 113 | +fn double_dots_in_paths_return_err() { |
| 114 | + let path_str: &str = "../heads"; |
| 115 | + let path_bstr: &BStr = path_str.into(); |
| 116 | + let path_u8: &[u8] = &b"../heads"[..]; |
| 117 | + let path_bstring: BString = "../heads".into(); |
| 118 | + |
| 119 | + assert!(matches!( |
| 120 | + TryInto::<&RelativePath>::try_into(path_str), |
| 121 | + Err(Error::ContainsInvalidComponent(_)) |
| 122 | + )); |
| 123 | + assert!(matches!( |
| 124 | + TryInto::<&RelativePath>::try_into(path_bstr), |
| 125 | + Err(Error::ContainsInvalidComponent(_)) |
| 126 | + )); |
| 127 | + assert!(matches!( |
| 128 | + TryInto::<&RelativePath>::try_into(path_u8), |
| 129 | + Err(Error::ContainsInvalidComponent(_)) |
| 130 | + )); |
| 131 | + assert!(matches!( |
| 132 | + TryInto::<&RelativePath>::try_into(&path_bstring), |
| 133 | + Err(Error::ContainsInvalidComponent(_)) |
| 134 | + )); |
| 135 | +} |
| 136 | + |
| 137 | +#[test] |
| 138 | +fn double_dots_in_paths_with_backslashes_return_err() { |
| 139 | + let path_str: &str = r"..\heads"; |
| 140 | + let path_bstr: &BStr = path_str.into(); |
| 141 | + let path_u8: &[u8] = &b"..\\heads"[..]; |
| 142 | + let path_bstring: BString = r"..\heads".into(); |
| 143 | + |
| 144 | + assert!(matches!( |
| 145 | + TryInto::<&RelativePath>::try_into(path_str), |
| 146 | + Err(Error::ContainsInvalidComponent(_)) |
| 147 | + )); |
| 148 | + assert!(matches!( |
| 149 | + TryInto::<&RelativePath>::try_into(path_bstr), |
| 150 | + Err(Error::ContainsInvalidComponent(_)) |
| 151 | + )); |
| 152 | + assert!(matches!( |
| 153 | + TryInto::<&RelativePath>::try_into(path_u8), |
| 154 | + Err(Error::ContainsInvalidComponent(_)) |
| 155 | + )); |
| 156 | + assert!(matches!( |
| 157 | + TryInto::<&RelativePath>::try_into(&path_bstring), |
| 158 | + Err(Error::ContainsInvalidComponent(_)) |
| 159 | + )); |
| 160 | +} |
0 commit comments