forked from canonical/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainer_test.go
389 lines (315 loc) · 10.6 KB
/
container_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2016 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package snap_test
import (
"io/ioutil"
"os"
"path/filepath"
"syscall"
. "gopkg.in/check.v1"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/snap/snapdir"
"github.com/snapcore/snapd/testutil"
)
type FileSuite struct{}
var _ = Suite(&FileSuite{})
type validateSuite struct {
testutil.BaseTest
}
var _ = Suite(&validateSuite{})
func discard(string, ...interface{}) {}
func (s *validateSuite) SetUpTest(c *C) {
s.BaseTest.SetUpTest(c)
s.BaseTest.AddCleanup(snap.MockSanitizePlugsSlots(func(snapInfo *snap.Info) {}))
}
func (s *validateSuite) TearDownTest(c *C) {
s.BaseTest.TearDownTest(c)
}
func (s *validateSuite) TestValidateContainerReallyEmptyFails(c *C) {
const yaml = `name: empty-snap
version: 1
`
d := c.MkDir()
// the snap dir is a 0700 directory with nothing in it
info, err := snap.InfoFromSnapYaml([]byte(yaml))
c.Assert(err, IsNil)
err = snap.ValidateContainer(snapdir.New(d), info, discard)
c.Check(err, Equals, snap.ErrMissingPaths)
}
func (s *validateSuite) TestValidateContainerEmptyButBadPermFails(c *C) {
const yaml = `name: empty-snap
version: 1
`
d := c.MkDir()
stat, err := os.Stat(d)
c.Assert(err, IsNil)
c.Check(stat.Mode().Perm(), Equals, os.FileMode(0700)) // just to be sure
c.Assert(os.Mkdir(filepath.Join(d, "meta"), 0755), IsNil)
c.Assert(ioutil.WriteFile(filepath.Join(d, "meta", "snap.yaml"), nil, 0444), IsNil)
// snapdir has /meta/snap.yaml, but / is 0700
info, err := snap.InfoFromSnapYaml([]byte(yaml))
c.Assert(err, IsNil)
err = snap.ValidateContainer(snapdir.New(d), info, discard)
c.Check(err, Equals, snap.ErrBadModes)
}
func (s *validateSuite) TestValidateContainerMissingSnapYamlFails(c *C) {
const yaml = `name: empty-snap
version: 1
`
d := c.MkDir()
c.Assert(os.Chmod(d, 0755), IsNil)
c.Assert(os.Mkdir(filepath.Join(d, "meta"), 0755), IsNil)
// snapdir's / and /meta are 0755 (i.e. OK), but no /meta/snap.yaml
info, err := snap.InfoFromSnapYaml([]byte(yaml))
c.Assert(err, IsNil)
err = snap.ValidateContainer(snapdir.New(d), info, discard)
c.Check(err, Equals, snap.ErrMissingPaths)
}
func (s *validateSuite) TestValidateContainerSnapYamlBadPermsFails(c *C) {
const yaml = `name: empty-snap
version: 1
`
d := c.MkDir()
c.Assert(os.Chmod(d, 0755), IsNil)
c.Assert(os.Mkdir(filepath.Join(d, "meta"), 0755), IsNil)
c.Assert(ioutil.WriteFile(filepath.Join(d, "meta", "snap.yaml"), nil, 0), IsNil)
// snapdir's / and /meta are 0755 (i.e. OK),
// /meta/snap.yaml exists, but isn't readable
info, err := snap.InfoFromSnapYaml([]byte(yaml))
c.Assert(err, IsNil)
err = snap.ValidateContainer(snapdir.New(d), info, discard)
c.Check(err, Equals, snap.ErrBadModes)
}
func (s *validateSuite) TestValidateContainerSnapYamlNonRegularFails(c *C) {
const yaml = `name: empty-snap
version: 1
`
d := c.MkDir()
c.Assert(os.Chmod(d, 0755), IsNil)
c.Assert(os.Mkdir(filepath.Join(d, "meta"), 0755), IsNil)
c.Assert(syscall.Mkfifo(filepath.Join(d, "meta", "snap.yaml"), 0444), IsNil)
// snapdir's / and /meta are 0755 (i.e. OK),
// /meta/snap.yaml exists, is readable, but isn't a file
info, err := snap.InfoFromSnapYaml([]byte(yaml))
c.Assert(err, IsNil)
err = snap.ValidateContainer(snapdir.New(d), info, discard)
c.Check(err, Equals, snap.ErrBadModes)
}
// emptyContainer returns a minimal container that passes
// ValidateContainer: / and /meta exist and are 0755, and
// /meta/snap.yaml is a regular world-readable file.
func emptyContainer(c *C) *snapdir.SnapDir {
d := c.MkDir()
c.Assert(os.Chmod(d, 0755), IsNil)
c.Assert(os.Mkdir(filepath.Join(d, "meta"), 0755), IsNil)
c.Assert(ioutil.WriteFile(filepath.Join(d, "meta", "snap.yaml"), nil, 0444), IsNil)
return snapdir.New(d)
}
func (s *validateSuite) TestValidateContainerMinimalOKPermWorks(c *C) {
const yaml = `name: empty-snap
version: 1
`
d := emptyContainer(c)
// snapdir's / and /meta are 0755 (i.e. OK),
// /meta/snap.yaml exists, is readable regular file
// (this could be considered a test of emptyContainer)
info, err := snap.InfoFromSnapYaml([]byte(yaml))
c.Assert(err, IsNil)
err = snap.ValidateContainer(d, info, discard)
c.Check(err, IsNil)
}
func (s *validateSuite) TestValidateContainerMissingAppsFails(c *C) {
const yaml = `name: empty-snap
version: 1
apps:
foo:
command: foo
`
d := emptyContainer(c)
// snapdir is empty: no apps
info, err := snap.InfoFromSnapYaml([]byte(yaml))
c.Assert(err, IsNil)
err = snap.ValidateContainer(d, info, discard)
c.Check(err, Equals, snap.ErrMissingPaths)
}
func (s *validateSuite) TestValidateContainerBadAppPermsFails(c *C) {
const yaml = `name: empty-snap
version: 1
apps:
foo:
command: foo
`
d := emptyContainer(c)
c.Assert(ioutil.WriteFile(filepath.Join(d.Path(), "foo"), nil, 0444), IsNil)
// snapdir contains the app, but the app is not executable
info, err := snap.InfoFromSnapYaml([]byte(yaml))
c.Assert(err, IsNil)
err = snap.ValidateContainer(d, info, discard)
c.Check(err, Equals, snap.ErrBadModes)
}
func (s *validateSuite) TestValidateContainerBadAppDirPermsFails(c *C) {
const yaml = `name: empty-snap
version: 1
apps:
foo:
command: apps/foo
`
d := emptyContainer(c)
c.Assert(os.Mkdir(filepath.Join(d.Path(), "apps"), 0700), IsNil)
c.Assert(ioutil.WriteFile(filepath.Join(d.Path(), "apps", "foo"), nil, 0555), IsNil)
// snapdir contains executable app, but path to executable isn't rx
info, err := snap.InfoFromSnapYaml([]byte(yaml))
c.Assert(err, IsNil)
err = snap.ValidateContainer(d, info, discard)
c.Check(err, Equals, snap.ErrBadModes)
}
func (s *validateSuite) TestValidateContainerBadSvcPermsFails(c *C) {
const yaml = `name: empty-snap
version: 1
apps:
bar:
command: svcs/bar
daemon: simple
`
d := emptyContainer(c)
c.Assert(os.Mkdir(filepath.Join(d.Path(), "svcs"), 0755), IsNil)
c.Assert(ioutil.WriteFile(filepath.Join(d.Path(), "svcs", "bar"), nil, 0), IsNil)
// snapdir contains service, but it isn't executable
info, err := snap.InfoFromSnapYaml([]byte(yaml))
c.Assert(err, IsNil)
err = snap.ValidateContainer(d, info, discard)
c.Check(err, Equals, snap.ErrBadModes)
}
func (s *validateSuite) TestValidateContainerCompleterFails(c *C) {
const yaml = `name: empty-snap
version: 1
apps:
foo:
command: cmds/foo
completer: comp/foo.sh
`
d := emptyContainer(c)
c.Assert(os.Mkdir(filepath.Join(d.Path(), "cmds"), 0755), IsNil)
c.Assert(ioutil.WriteFile(filepath.Join(d.Path(), "cmds", "foo"), nil, 0555), IsNil)
c.Assert(os.Mkdir(filepath.Join(d.Path(), "comp"), 0755), IsNil)
// snapdir contains executable app, in a rx path, but refers
// to a completer that doesn't exist
info, err := snap.InfoFromSnapYaml([]byte(yaml))
c.Assert(err, IsNil)
err = snap.ValidateContainer(d, info, discard)
c.Check(err, Equals, snap.ErrMissingPaths)
}
func (s *validateSuite) TestValidateContainerBadAppPathOK(c *C) {
// we actually support this, but don't validate it here
const yaml = `name: empty-snap
version: 1
apps:
foo:
command: ../../../bin/echo
`
d := emptyContainer(c)
// snapdir does not contain the app, but the command is
// "outside" so it might be OK
info, err := snap.InfoFromSnapYaml([]byte(yaml))
c.Assert(err, IsNil)
err = snap.ValidateContainer(d, info, discard)
c.Check(err, IsNil)
}
func (s *validateSuite) TestValidateContainerSymlinksFails(c *C) {
c.Skip("checking symlink targets not implemented yet")
const yaml = `name: empty-snap
version: 1
apps:
foo:
command: foo
`
d := emptyContainer(c)
fn := filepath.Join(d.Path(), "foo")
c.Assert(ioutil.WriteFile(fn+".real", nil, 0444), IsNil)
c.Assert(os.Symlink(fn+".real", fn), IsNil)
// snapdir contains a command that's a symlink to a file that's not world-rx
info, err := snap.InfoFromSnapYaml([]byte(yaml))
c.Assert(err, IsNil)
err = snap.ValidateContainer(d, info, discard)
c.Check(err, Equals, snap.ErrBadModes)
}
func (s *validateSuite) TestValidateContainerSymlinksOK(c *C) {
const yaml = `name: empty-snap
version: 1
apps:
foo:
command: foo
`
d := emptyContainer(c)
fn := filepath.Join(d.Path(), "foo")
c.Assert(ioutil.WriteFile(fn+".real", nil, 0555), IsNil)
c.Assert(os.Symlink(fn+".real", fn), IsNil)
// snapdir contains a command that's a symlink to a file that's world-rx
info, err := snap.InfoFromSnapYaml([]byte(yaml))
c.Assert(err, IsNil)
err = snap.ValidateContainer(d, info, discard)
c.Check(err, IsNil)
}
func (s *validateSuite) TestValidateContainerAppsOK(c *C) {
const yaml = `name: empty-snap
version: 1
apps:
foo:
command: cmds/foo
completer: comp/foo.sh
bar:
command: svcs/bar
daemon: simple
baz:
command: cmds/foo --with=baz
quux:
command: cmds/foo
daemon: simple
meep:
command: comp/foo.sh
daemon: simple
`
d := emptyContainer(c)
c.Assert(os.Mkdir(filepath.Join(d.Path(), "cmds"), 0755), IsNil)
c.Assert(ioutil.WriteFile(filepath.Join(d.Path(), "cmds", "foo"), nil, 0555), IsNil)
c.Assert(os.Mkdir(filepath.Join(d.Path(), "comp"), 0755), IsNil)
c.Assert(ioutil.WriteFile(filepath.Join(d.Path(), "comp", "foo.sh"), nil, 0555), IsNil)
c.Assert(os.Mkdir(filepath.Join(d.Path(), "svcs"), 0700), IsNil)
c.Assert(ioutil.WriteFile(filepath.Join(d.Path(), "svcs", "bar"), nil, 0500), IsNil)
c.Assert(os.Mkdir(filepath.Join(d.Path(), "garbage"), 0755), IsNil)
c.Assert(os.Mkdir(filepath.Join(d.Path(), "garbage", "zero"), 0), IsNil)
defer os.Chmod(filepath.Join(d.Path(), "garbage", "zero"), 0755)
// snapdir contains:
// * a command that's world-rx, and its directory is
// world-rx, and its completer is world-r in a world-rx
// directory
// * a service that's root-executable, and its directory is
// not readable nor searchable - and that's OK! (NOTE as
// this test should pass as non-rooot, the directory is 0700
// instead of 0000)
// * a command with arguments
// * a service that is also a command
// * a service that is also a completer (WAT)
// * an extra directory only root can look at (this would fail
// if not running the suite as root, and SkipDir didn't
// work)
info, err := snap.InfoFromSnapYaml([]byte(yaml))
c.Assert(err, IsNil)
err = snap.ValidateContainer(d, info, discard)
c.Check(err, IsNil)
}