github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/systemd/escape_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2014-2015 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package systemd_test 21 22 import ( 23 . "gopkg.in/check.v1" 24 25 "github.com/snapcore/snapd/systemd" 26 ) 27 28 func (ts *SystemdTestSuite) TestEscapePath(c *C) { 29 tt := []struct { 30 in string 31 out string 32 comment string 33 }{ 34 { 35 `Hallöchen, Meister`, 36 `Hall\xc3\xb6chen\x2c\x20Meister`, 37 `utf-8 char and spaces`, 38 }, 39 { 40 `/tmp//waldi/foobar/`, 41 `tmp-waldi-foobar`, 42 `unclean path`, 43 }, 44 { 45 `/.foo/.bar`, 46 `\x2efoo-.bar`, 47 `leading dot escaped differently`, 48 }, 49 { 50 `.foo/.bar`, 51 `\x2efoo-.bar`, 52 `leading dot escaped differently (without leading slash)`, 53 }, 54 { 55 // TODO: this case shouldn't work it should cause an error to be 56 // fully compatible with systemd-escape(1), but it's not documented 57 // how or why systemd-escape considers this kind of path to be 58 // invalid, so for now we just process it in an expected and 59 // deterministic way 60 `/top_level_1/../top_level_2/`, 61 `top_level_2`, 62 `invalid path`, 63 }, 64 { 65 ``, 66 `-`, 67 `empty string`, 68 }, 69 { 70 `/`, 71 `-`, 72 `root /`, 73 }, 74 { 75 `////////`, 76 `-`, 77 `root / unclean`, 78 }, 79 { 80 `-`, 81 `\x2d`, 82 `just dash`, 83 }, 84 { 85 `/run/ubuntu------data`, 86 `run-ubuntu\x2d\x2d\x2d\x2d\x2d\x2ddata`, 87 `consecutive dashes`, 88 }, 89 { 90 `/path-with-forward-slash\`, 91 `path\x2dwith\x2dforward\x2dslash\x5c`, 92 `forward slash in path element`, 93 }, 94 { 95 `/run/1000/numbers`, 96 `run-1000-numbers`, 97 `numbers`, 98 }, 99 { 100 `/silly/path/,!@#$%^&*()`, 101 `silly-path-\x2c\x21\x40\x23\x24\x25\x5e\x26\x2a\x28\x29`, 102 `ascii punctuation`, 103 }, 104 { 105 `/run/mnt/data`, 106 `run-mnt-data`, 107 `typical data initramfs mount`, 108 }, 109 { 110 `run/mnt/data`, 111 `run-mnt-data`, 112 `typical data initramfs mount w/o leading slash`, 113 }, 114 { 115 `/run/mnt/ubuntu-seed`, 116 `run-mnt-ubuntu\x2dseed`, 117 `typical ubuntu-seed initramfs mount`, 118 }, 119 { 120 `/run/mnt/ubuntu data`, 121 `run-mnt-ubuntu\x20data`, 122 `path with space in it`, 123 }, 124 { 125 `/run/mnt/ubuntu_data`, 126 `run-mnt-ubuntu_data`, 127 `path with underscore in it`, 128 }, 129 { 130 ` `, 131 `\x20`, 132 "space character", 133 }, 134 { 135 ` `, 136 `\x09`, 137 `tab character`, 138 }, 139 { 140 `/home/日本語`, 141 `home-\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e`, 142 `utf-8 characters`, 143 }, 144 { 145 `/path⌘place-of-interest/hello`, 146 `path\xe2\x8c\x98place\x2dof\x2dinterest-hello`, 147 `utf-8 character embedded in ascii path element`, 148 }, 149 } 150 151 for _, t := range tt { 152 c.Assert(systemd.EscapeUnitNamePath(t.in), Equals, t.out, Commentf(t.comment+" (with input %q)", t.in)) 153 } 154 }