]> git.proxmox.com Git - proxmox-backup.git/commitdiff
add test for escape_unit
authorDietmar Maurer <dietmar@proxmox.com>
Wed, 21 Oct 2020 09:30:49 +0000 (11:30 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Wed, 21 Oct 2020 09:31:24 +0000 (11:31 +0200)
src/tools/systemd.rs

index fdfd9d47b256d3231bff6ea35d7d09114b542257..f8ae789cf45b0b36966086a3ea6a106d711fa7f1 100644 (file)
@@ -130,3 +130,32 @@ pub fn stop_unit(unit: &str) -> Result<(), Error> {
 
     Ok(())
 }
+
+#[test]
+fn test_escape_unit() -> Result<(), Error> {
+
+    fn test_escape(i: &str, expected: &str, is_path: bool) {
+        let escaped = escape_unit(i, is_path);
+        assert_eq!(escaped, expected);
+        let unescaped = unescape_unit(&escaped).unwrap();
+        if is_path {
+            let mut p = i.trim_matches('/');
+            if p.is_empty() { p = "/"; }
+            assert_eq!(p, unescaped);
+        } else {
+            assert_eq!(i, unescaped);
+        }
+    }
+
+    test_escape(".test", "\\x2etest", false);
+    test_escape("t.est", "t.est", false);
+    test_escape("_test_", "_test_", false);
+
+    test_escape("/", "-", false);
+    test_escape("//", "--", false);
+
+    test_escape("/", "-", true);
+    test_escape("//", "-", true);
+
+    Ok(())
+}