File: cups_test.go

package info (click to toggle)
snapd 2.72-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 80,412 kB
  • sloc: sh: 16,506; ansic: 16,211; python: 11,213; makefile: 1,919; exp: 190; awk: 58; xml: 22
file content (264 lines) | stat: -rw-r--r-- 9,119 bytes parent folder | download | duplicates (3)
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
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
 * Copyright (C) 2020 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/interfaces"
	"github.com/snapcore/snapd/interfaces/apparmor"
	"github.com/snapcore/snapd/interfaces/builtin"
	"github.com/snapcore/snapd/interfaces/mount"
	"github.com/snapcore/snapd/osutil"
	"github.com/snapcore/snapd/snap"
	"github.com/snapcore/snapd/testutil"
)

type cupsSuite struct {
	iface interfaces.Interface

	plugInfo *snap.PlugInfo
	plug     *interfaces.ConnectedPlug

	providerSlotInfo *snap.SlotInfo
	providerSlot     *interfaces.ConnectedSlot

	providerLegacySlotInfo *snap.SlotInfo
	providerLegacySlot     *interfaces.ConnectedSlot
}

var _ = Suite(&cupsSuite{iface: builtin.MustInterface("cups")})

const cupsConsumerYaml = `name: consumer
version: 0
apps:
 app:
  plugs: [cups]
`

const cupsProviderYaml = `name: provider
version: 0
slots:
  cups-socket:
    interface: cups
    cups-socket-directory: $SNAP_COMMON/foo-subdir
apps:
 app:
  slots: [cups-socket]
`

const cupsProviderLegacyYaml = `name: provider
version: 0
slots:
  # no attribute
  cups: {}
`

func (s *cupsSuite) SetUpTest(c *C) {
	s.plug, s.plugInfo = MockConnectedPlug(c, cupsConsumerYaml, nil, "cups")
	s.providerSlot, s.providerSlotInfo = MockConnectedSlot(c, cupsProviderYaml, nil, "cups-socket")
	s.providerLegacySlot, s.providerLegacySlotInfo = MockConnectedSlot(c, cupsProviderLegacyYaml, nil, "cups")
}

func (s *cupsSuite) TestName(c *C) {
	c.Assert(s.iface.Name(), Equals, "cups")
}

func (s *cupsSuite) TestSanitizeSlot(c *C) {
	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.providerSlotInfo), IsNil)
	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.providerLegacySlotInfo), IsNil)
}

const invalidCupsProviderYamlFmt = `name: provider
version: 0
slots:
  cups-socket:
    interface: cups
    # regex not allowed
    cups-socket-directory: %s
apps:
 app:
  slots: [cups-socket]
`

func (s *cupsSuite) TestSanitizeInvalidSlot(c *C) {
	tt := []struct {
		snippet string
		err     string
	}{
		{
			"$SNAP_COMMON/foo-subdir/*",
			"cups-socket-directory is not usable: .* contains a reserved apparmor char .*",
		},
		{
			"$SNAP_COMMON/foo-subdir/../../../",
			`cups-socket-directory is not clean: \"\$SNAP_COMMON/foo-subdir/../../../\"`,
		},
		{
			"$SNAP/foo",
			`cups-socket-directory must be a directory of \$SNAP_COMMON or \$SNAP_DATA`,
		},
	}

	for _, t := range tt {
		yaml := fmt.Sprintf(invalidCupsProviderYamlFmt, t.snippet)

		_, invalidSlotInfo := MockConnectedSlot(c, yaml, nil, "cups-socket")

		err := interfaces.BeforePrepareSlot(s.iface, invalidSlotInfo)
		c.Assert(err, ErrorMatches, t.err)
	}
}

func (s *cupsSuite) TestSanitizePlug(c *C) {
	c.Assert(interfaces.BeforePreparePlug(s.iface, s.plugInfo), IsNil)
}

const expSnapUpdateNsSnippet = `  # Mount cupsd socket from cups snap to client snap
  mount options=(rw bind) "/var/snap/provider/common/foo-subdir/" -> /var/cups/,
  umount /var/cups/,
  # Writable directory /var/snap/provider/common/foo-subdir
  "/var/snap/provider/common/foo-subdir/" rw,
  "/var/snap/provider/common/" rw,
  "/var/snap/provider/" rw,
  # Writable mimic /var
  # .. permissions for traversing the prefix that is assumed to exist
  # .. variant with mimic at /
  # Allow reading the mimic directory, it must exist in the first place.
  "/" r,
  # Allow setting the read-only directory aside via a bind mount.
  "/tmp/.snap/" rw,
  mount options=(rbind, rw) "/" -> "/tmp/.snap/",
  # Allow mounting tmpfs over the read-only directory.
  mount fstype=tmpfs options=(rw) tmpfs -> "/",
  # Allow creating empty files and directories for bind mounting things
  # to reconstruct the now-writable parent directory.
  "/tmp/.snap/*/" rw,
  "/*/" rw,
  mount options=(rbind, rw) "/tmp/.snap/*/" -> "/*/",
  "/tmp/.snap/*" rw,
  "/*" rw,
  mount options=(bind, rw) "/tmp/.snap/*" -> "/*",
  # Allow unmounting the auxiliary directory.
  # TODO: use fstype=tmpfs here for more strictness (LP: #1613403)
  mount options=(rprivate) -> "/tmp/.snap/",
  umount "/tmp/.snap/",
  # Allow unmounting the destination directory as well as anything
  # inside.  This lets us perform the undo plan in case the writable
  # mimic fails.
  mount options=(rprivate) -> "/",
  mount options=(rprivate) -> "/*",
  mount options=(rprivate) -> "/*/",
  umount "/",
  umount "/*",
  umount "/*/",
  # .. variant with mimic at /var/
  "/var/" r,
  "/tmp/.snap/var/" rw,
  mount options=(rbind, rw) "/var/" -> "/tmp/.snap/var/",
  mount fstype=tmpfs options=(rw) tmpfs -> "/var/",
  "/tmp/.snap/var/*/" rw,
  "/var/*/" rw,
  mount options=(rbind, rw) "/tmp/.snap/var/*/" -> "/var/*/",
  "/tmp/.snap/var/*" rw,
  "/var/*" rw,
  mount options=(bind, rw) "/tmp/.snap/var/*" -> "/var/*",
  mount options=(rprivate) -> "/tmp/.snap/var/",
  umount "/tmp/.snap/var/",
  mount options=(rprivate) -> "/var/",
  mount options=(rprivate) -> "/var/*",
  mount options=(rprivate) -> "/var/*/",
  umount "/var/",
  umount "/var/*",
  umount "/var/*/",
`

func (s *cupsSuite) TestAppArmorSpec(c *C) {
	// consumer to provider on core for ConnectedPlug
	appSet, err := interfaces.NewSnapAppSet(s.plug.Snap(), nil)
	c.Assert(err, IsNil)
	spec := apparmor.NewSpecification(appSet)
	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.providerSlot), IsNil)
	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"})
	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, "# Allow communicating with the cups server")
	// no cups abstractions
	c.Assert(spec.SnippetForTag("snap.consumer.app"), Not(testutil.Contains), "#include <abstractions/cups-client>")
	// but has the lpoptions config file though
	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, `owner @{HOME}/.cups/lpoptions r,`)
	// the special mount rules are present
	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, `"/var/snap/provider/common/foo-subdir/**" mrwklix,`)
	// the writable mimic profile for snap-update-ns is generated as well
	c.Assert(strings.Join(spec.UpdateNS(), ""), Equals, expSnapUpdateNsSnippet)

	// consumer to legacy provider
	appSet, err = interfaces.NewSnapAppSet(s.plug.Snap(), nil)
	c.Assert(err, IsNil)
	specLegacy := apparmor.NewSpecification(appSet)
	c.Assert(specLegacy.AddConnectedPlug(s.iface, s.plug, s.providerLegacySlot), IsNil)
	c.Assert(specLegacy.SecurityTags(), DeepEquals, []string{"snap.consumer.app"})
	c.Assert(specLegacy.SnippetForTag("snap.consumer.app"), testutil.Contains, "# Allow communicating with the cups server")
	// no cups abstractions
	c.Assert(specLegacy.SnippetForTag("snap.consumer.app"), Not(testutil.Contains), "#include <abstractions/cups-client>")
	// but has the lpoptions config file though
	c.Assert(specLegacy.SnippetForTag("snap.consumer.app"), testutil.Contains, `owner @{HOME}/.cups/lpoptions r,`)
	// no special mounting rules
	c.Assert(specLegacy.SnippetForTag("snap.consumer.app"), Not(testutil.Contains), "/var/snap/provider/common/foo-subdir/** mrwklix,")
	// no writable mimic profile for snap-update-ns
	c.Assert(specLegacy.UpdateNS(), HasLen, 0)
}

func (s *cupsSuite) TestMountSpec(c *C) {
	// consumer to provider on core for ConnectedPlug
	spec := &mount.Specification{}
	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.providerSlot), IsNil)
	// mount entry for /var/cups/ for all namespaces
	c.Assert(spec.MountEntries(), DeepEquals, []osutil.MountEntry{
		{
			Name:    "/var/snap/provider/common/foo-subdir",
			Dir:     "/var/cups/",
			Options: []string{"bind", "rw"},
		},
	})
	// no user specific mounts
	c.Assert(spec.UserMountEntries(), HasLen, 0)

	// consumer to legacy provider has no mounts at all
	specLegacy := &mount.Specification{}
	c.Assert(specLegacy.AddConnectedPlug(s.iface, s.plug, s.providerLegacySlot), IsNil)
	c.Assert(specLegacy.MountEntries(), HasLen, 0)
	c.Assert(specLegacy.UserMountEntries(), HasLen, 0)
}

func (s *cupsSuite) TestStaticInfo(c *C) {
	si := interfaces.StaticInfoOf(s.iface)
	c.Assert(si.ImplicitOnCore, Equals, false)
	c.Assert(si.ImplicitOnClassic, Equals, false)
	c.Assert(si.Summary, Equals, `allows access to the CUPS socket for printing`)
	c.Assert(si.BaseDeclarationSlots, testutil.Contains, "cups")
	c.Assert(si.BaseDeclarationSlots, testutil.Contains, "deny-connection: true")
	c.Assert(si.BaseDeclarationSlots, testutil.Contains, "deny-auto-connection: true")
}

func (s *cupsSuite) TestInterfaces(c *C) {
	c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
}