/
basedeclaration_test.go
336 lines (299 loc) · 9.26 KB
/
basedeclaration_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
// -*- 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 builtin_test
import (
"fmt"
"strings"
. "gopkg.in/check.v1"
"github.com/snapcore/snapd/asserts"
"github.com/snapcore/snapd/interfaces"
"github.com/snapcore/snapd/interfaces/builtin"
"github.com/snapcore/snapd/interfaces/policy"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/snap/snaptest"
)
type baseDeclSuite struct {
baseDecl *asserts.BaseDeclaration
}
var _ = Suite(&baseDeclSuite{})
func (s *baseDeclSuite) SetUpSuite(c *C) {
s.baseDecl = asserts.BuiltinBaseDeclaration()
}
func (s *baseDeclSuite) connectCand(c *C, iface, slotYaml, plugYaml string) *policy.ConnectCandidate {
if slotYaml == "" {
slotYaml = fmt.Sprintf(`name: slot-snap
slots:
%s:
`, iface)
}
if plugYaml == "" {
plugYaml = fmt.Sprintf(`name: plug-snap
plugs:
%s:
`, iface)
}
slotSnap := snaptest.MockInfo(c, slotYaml, nil)
plugSnap := snaptest.MockInfo(c, plugYaml, nil)
return &policy.ConnectCandidate{
Plug: plugSnap.Plugs[iface],
Slot: slotSnap.Slots[iface],
BaseDeclaration: s.baseDecl,
}
}
func (s *baseDeclSuite) installSlotCand(c *C, iface string, snapType snap.Type, yaml string) *policy.InstallCandidate {
if yaml == "" {
yaml = fmt.Sprintf(`name: install-slot-snap
type: %s
slots:
%s:
`, snapType, iface)
}
snap := snaptest.MockInfo(c, yaml, nil)
return &policy.InstallCandidate{
Snap: snap,
BaseDeclaration: s.baseDecl,
}
}
const declTempl = `type: snap-declaration
authority-id: canonical
series: 16
snap-name: @name@
snap-id: @snapid@
publisher-id: @publisher@
@plugsSlots@
timestamp: 2016-09-30T12:00:00Z
sign-key-sha3-384: Jv8_JiHiIzJVcO9M55pPdqSDWUvuhfDIBJUS-3VW7F_idjix7Ffn5qMxB21ZQuij
AXNpZw==`
func (s *baseDeclSuite) mockSnapDecl(c *C, name, snapID, publisher string, plugsSlots string) *asserts.SnapDeclaration {
encoded := strings.Replace(declTempl, "@name@", name, 1)
encoded = strings.Replace(encoded, "@snapid@", snapID, 1)
encoded = strings.Replace(encoded, "@publisher@", publisher, 1)
if plugsSlots != "" {
encoded = strings.Replace(encoded, "@plugsSlots@", strings.TrimSpace(plugsSlots), 1)
} else {
encoded = strings.Replace(encoded, "@plugsSlots@\n", "", 1)
}
a, err := asserts.Decode([]byte(encoded))
c.Assert(err, IsNil)
return a.(*asserts.SnapDeclaration)
}
func (s *baseDeclSuite) TestAutoConnection(c *C) {
all := builtin.Interfaces()
// these have more complex or in flux policies and have their
// own separate tests
snowflakes := map[string]bool{
"content": true,
"home": true,
"lxd-support": true,
"snapd-control": true,
}
// these simply auto-connect, anything else doesn't
autoconnect := map[string]bool{
"browser-support": true,
"gsettings": true,
"mir": true,
"network": true,
"network-bind": true,
"opengl": true,
"optical-drive": true,
"pulseaudio": true,
"screen-inhibit-control": true,
"unity7": true,
"upower-observe": true,
"x11": true,
}
for _, iface := range all {
if snowflakes[iface.Name()] {
continue
}
expected := autoconnect[iface.Name()]
comm := Commentf(iface.Name())
// cross-check with past behavior
c.Check(expected, Equals, iface.LegacyAutoConnect(), comm)
// check base declaration
cand := s.connectCand(c, iface.Name(), "", "")
err := cand.CheckAutoConnect()
if expected {
c.Check(err, IsNil, comm)
} else {
c.Check(err, NotNil, comm)
}
}
}
func (s *baseDeclSuite) TestAutoConnectPlugSlot(c *C) {
all := builtin.Interfaces()
// these have more complex or in flux policies and have their
// own separate tests
snowflakes := map[string]bool{
"content": true,
"home": true,
"lxd-support": true,
}
for _, iface := range all {
if snowflakes[iface.Name()] {
continue
}
c.Check(iface.AutoConnect(nil, nil), Equals, true)
}
}
func (s *baseDeclSuite) TestInterimAutoConnectionHome(c *C) {
// home will be controlled by AutoConnect(plug, slot) until
// we have on-classic support in decls
// to stop it from working on non-classic
cand := s.connectCand(c, "home", "", "")
err := cand.CheckAutoConnect()
c.Check(err, IsNil)
}
func (s *baseDeclSuite) TestInterimAutoConnectionSnapdControl(c *C) {
// snapd-control is auto-connect until we have snap declaration editing
cand := s.connectCand(c, "snapd-control", "", "")
err := cand.CheckAutoConnect()
c.Check(err, IsNil)
}
func (s *baseDeclSuite) TestAutoConnectionContent(c *C) {
// content will also depend for now AutoConnect(plug, slot)
// random snaps cannot connect with content
cand := s.connectCand(c, "content", "", "")
err := cand.CheckAutoConnect()
c.Check(err, NotNil)
}
func (s *baseDeclSuite) TestAutoConnectionLxdSupport(c *C) {
// by default, don't auto-connect
cand := s.connectCand(c, "lxd-support", "", "")
err := cand.CheckAutoConnect()
c.Check(err, IsNil)
// allow auto-connect to particular snap id
plugsSlots := `
slots:
docker-support:
allow-auto-connection:
plug-snap-id:
- J60k4JY0HppjwOjW8dZdYc8obXKxujRu
`
lxdDecl := s.mockSnapDecl(c, "lxd", "J60k4JY0HppjwOjW8dZdYc8obXKxujRu", "canonical", plugsSlots)
cand.PlugSnapDeclaration = lxdDecl
err = cand.CheckAutoConnect()
c.Check(err, IsNil)
}
// FIXME: this should fail but doesn't
func (s *baseDeclSuite) TestAutoConnectionLxdSupportNoMatch(c *C) {
cand := s.connectCand(c, "lxd-support", "", "")
// don't allow auto-connect to non-matching snap id
plugsSlots := `
slots:
docker-support:
allow-auto-connection:
plug-snap-id:
- nonmatchingnonmatchingnonmatchin
`
lxdDecl := s.mockSnapDecl(c, "lxd", "J60k4JY0HppjwOjW8dZdYc8obXKxujRu", "canonical", plugsSlots)
cand.PlugSnapDeclaration = lxdDecl
err := cand.CheckAutoConnect()
c.Check(err, NotNil)
}
// describe installation rules for slots succinctly for cross-checking,
// if an interface is not mentioned here a slot of its type can only
// be installed by a core snap (and this was taken care by
// SanitizeSlot),
// otherwise the entry for the interface is the list of snap types it
// can be installed by (using the declaration naming);
// ATM a nil entry means even stricter rules that would need be tested
// separately and whose implementation is in flux for now
var (
unconstrained = []string{"core", "kernel", "gadget", "app"}
slotInstallation = map[string][]string{
// unconstrained
"bluez": unconstrained,
"fwupd": unconstrained,
"location-control": unconstrained,
"location-observe": unconstrained,
"modem-manager": unconstrained,
"network-manager": unconstrained,
"pulseaudio": unconstrained,
"udisks2": unconstrained,
// other
"bool-file": []string{"core", "gadget"},
"browser-support": []string{"core"},
"content": []string{"app", "gadget"},
"docker-support": []string{"core"},
"gpio": []string{"core", "gadget"},
"hidraw": []string{"core", "gadget"},
"lxd-support": []string{"core"},
"mir": []string{"app"},
"mpris": []string{"app"},
"ppp": []string{"core"},
"serial-port": []string{"core", "gadget"},
// snowflakes
"docker": nil,
}
)
func contains(l []string, s string) bool {
for _, s1 := range l {
if s == s1 {
return true
}
}
return false
}
func (s *baseDeclSuite) TestSlotInstallation(c *C) {
typMap := map[string]snap.Type{
"core": snap.TypeOS,
"app": snap.TypeApp,
"kernel": snap.TypeKernel,
"gadget": snap.TypeGadget,
}
all := builtin.Interfaces()
for _, iface := range all {
types, ok := slotInstallation[iface.Name()]
compareWithSanitize := false
if !ok { // common ones, only core can install them,
// their plain SanitizeSlot checked for that
types = []string{"core"}
compareWithSanitize = true
}
if types == nil {
// snowflake needs to be tested specially
continue
}
for name, snapType := range typMap {
ok := contains(types, name)
ic := s.installSlotCand(c, iface.Name(), snapType, ``)
slotInfo := ic.Snap.Slots[iface.Name()]
err := ic.Check()
comm := Commentf("%s by %s snap", iface.Name(), name)
if ok {
c.Check(err, IsNil, comm)
} else {
c.Check(err, NotNil, comm)
}
if compareWithSanitize {
sanitizeErr := iface.SanitizeSlot(&interfaces.Slot{SlotInfo: slotInfo})
if err == nil {
c.Check(sanitizeErr, IsNil, comm)
} else {
c.Check(sanitizeErr, NotNil, comm)
}
}
}
}
// test docker specially
ic := s.installSlotCand(c, "docker", snap.TypeApp, ``)
err := ic.Check()
c.Assert(err, Not(IsNil))
c.Assert(err, ErrorMatches, "installation not allowed by \"docker\" slot rule of interface \"docker\"")
}