1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package unit
16
17 import (
18 "testing"
19 )
20
21 func TestUnitNameEscape(t *testing.T) {
22 tests := []struct {
23 in string
24 out string
25 isPath bool
26 }{
27
28 {
29 in: "",
30 out: "-",
31 isPath: true,
32 },
33
34 {
35 in: "/////////",
36 out: "-",
37 isPath: true,
38 },
39
40 {
41 in: "///foo////bar/////tail//////",
42 out: "foo-bar-tail",
43 isPath: true,
44 },
45
46 {
47 in: "",
48 out: "",
49 isPath: false,
50 },
51
52 {
53 in: ".",
54 out: `\x2e`,
55 isPath: true,
56 },
57
58 {
59 in: "/.",
60 out: `\x2e`,
61 isPath: true,
62 },
63
64 {
65 in: "/////////.",
66 out: `\x2e`,
67 isPath: true,
68 },
69
70 {
71 in: "/////////.///////////////",
72 out: `\x2e`,
73 isPath: true,
74 },
75
76 {
77 in: ".....",
78 out: `\x2e....`,
79 isPath: true,
80 },
81
82 {
83 in: "/.foo/.bar",
84 out: `\x2efoo-.bar`,
85 isPath: true,
86 },
87
88 {
89 in: ".foo/.bar",
90 out: `\x2efoo-.bar`,
91 isPath: true,
92 },
93
94 {
95 in: ".foo/.bar",
96 out: `\x2efoo-.bar`,
97 isPath: false,
98 },
99
100 {
101 in: `///..\-!#??///`,
102 out: `---..\x5c\x2d\x21\x23\x3f\x3f---`,
103 isPath: false,
104 },
105
106 {
107 in: `///..\-!#??///`,
108 out: `\x2e.\x5c\x2d\x21\x23\x3f\x3f`,
109 isPath: true,
110 },
111
112 {
113 in: `user-cloudinit@/var/lib/coreos/vagrant/vagrantfile-user-data.service`,
114 out: `user\x2dcloudinit\x40-var-lib-coreos-vagrant-vagrantfile\x2duser\x2ddata.service`,
115 isPath: false,
116 },
117 }
118
119 for i, tt := range tests {
120 var s string
121 if tt.isPath {
122 s = UnitNamePathEscape(tt.in)
123 } else {
124 s = UnitNameEscape(tt.in)
125 }
126 if s != tt.out {
127 t.Errorf("case %d: failed escaping %v isPath: %v - expected %v, got %v", i, tt.in, tt.isPath, tt.out, s)
128 }
129 }
130 }
131
132 func TestUnitNameUnescape(t *testing.T) {
133 tests := []struct {
134 in string
135 out string
136 isPath bool
137 }{
138
139 {
140 in: "",
141 out: "/",
142 isPath: true,
143 },
144
145 {
146 in: "",
147 out: "",
148 isPath: false,
149 },
150
151 {
152 in: "---------",
153 out: "/////////",
154 isPath: true,
155 },
156
157 {
158 in: `---..\x5c\x2d\x21\x23\x3f\x3f---`,
159 out: `///..\-!#??///`,
160 isPath: false,
161 },
162
163 {
164 in: `\x2e.\x5c\x2d\x21\x23\x3f\x3f`,
165 out: `/..\-!#??`,
166 isPath: true,
167 },
168
169 {
170 in: `\x2e.\x5c\x2d\xaZ\x.o\x21\x23\x3f\x3f`,
171 out: `/..\-\xaZ\x.o!#??`,
172 isPath: true,
173 },
174
175 {
176 in: `\x2e.\x5c\x\x2d\xaZ\x.o\x21\x23\x3f\x3f\x3`,
177 out: `/..\\x-\xaZ\x.o!#??\x3`,
178 isPath: true,
179 },
180
181 {
182 in: `\x2e.\x5c\x\x2d\xaZ\x.o\x21\x23\x3f\x3f\x`,
183 out: `/..\\x-\xaZ\x.o!#??\x`,
184 isPath: true,
185 },
186
187 {
188 in: `\x2e.\x5c\x\x2d\xaZ\x.o\x21\x23\x3f\x3f\`,
189 out: `/..\\x-\xaZ\x.o!#??\`,
190 isPath: true,
191 },
192
193 {
194 in: `user\x2dcloudinit\x40-var-lib-coreos-vagrant-vagrantfile\x2duser\x2ddata.service`,
195 out: `user-cloudinit@/var/lib/coreos/vagrant/vagrantfile-user-data.service`,
196 isPath: false,
197 },
198 }
199
200 for i, tt := range tests {
201 var s string
202 if tt.isPath {
203 s = UnitNamePathUnescape(tt.in)
204 } else {
205 s = UnitNameUnescape(tt.in)
206 }
207 if s != tt.out {
208 t.Errorf("case %d: failed unescaping %v isPath: %v - expected %v, got %v", i, tt.in, tt.isPath, tt.out, s)
209 }
210 }
211 }
212
View as plain text