File: netlink_driver_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 (279 lines) | stat: -rw-r--r-- 11,600 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
 * Copyright (C) 2021 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 (
	. "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/seccomp"
	"github.com/snapcore/snapd/snap"
	"github.com/snapcore/snapd/snap/snaptest"
	"github.com/snapcore/snapd/testutil"
)

type NetlinkDriverInterfaceSuite struct {
	testutil.BaseTest

	iface interfaces.Interface

	// slots from gadget
	gadgetNetlinkSlotInfo       *snap.SlotInfo
	gadgetNetlinkSlot           *interfaces.ConnectedSlot
	gadgetMissingNumberSlotInfo *snap.SlotInfo
	gadgetMissingNumberSlot     *interfaces.ConnectedSlot
	gadgetMissingNameSlotInfo   *snap.SlotInfo
	gadgetMissingNameSlot       *interfaces.ConnectedSlot
	gadgetBadNumberSlotInfo     *snap.SlotInfo
	gadgetBadNumberSlot         *interfaces.ConnectedSlot
	gadgetBadNameSlotInfo       *snap.SlotInfo
	gadgetBadNameSlot           *interfaces.ConnectedSlot
	gadgetBadNameStringSlotInfo *snap.SlotInfo
	gadgetBadNameStringSlot     *interfaces.ConnectedSlot

	// slot from core
	osNetlinkSlotInfo *snap.SlotInfo
	osNetlinkSlot     *interfaces.ConnectedSlot

	// plugs from app
	appToGadgetPlugDriverInfo            *snap.PlugInfo
	appToGadgetPlugDriver                *interfaces.ConnectedPlug
	appToCorePlugDriverInfo              *snap.PlugInfo
	appToCorePlugDriver                  *interfaces.ConnectedPlug
	appMissingFamilyNamePlugDriverInfo   *snap.PlugInfo
	appMissingFamilyNamePlugDriver       *interfaces.ConnectedPlug
	appBadFamilyNamePlugDriverInfo       *snap.PlugInfo
	appBadFamilyNamePlugDriver           *interfaces.ConnectedPlug
	appBadFamilyNameStringPlugDriverInfo *snap.PlugInfo
	appBadFamilyNameStringPlugDriver     *interfaces.ConnectedPlug
}

var _ = Suite(&NetlinkDriverInterfaceSuite{
	iface: builtin.MustInterface("netlink-driver"),
})

func (s *NetlinkDriverInterfaceSuite) SetUpTest(c *C) {
	gadgetInfo := snaptest.MockInfo(c, `
name: my-device
version: 0
type: gadget
slots:
    my-driver:
        interface: netlink-driver
        family: 100
        family-name: foo-driver
    missing-number:
        interface: netlink-driver
        family-name: missing-number
    missing-name:
        interface: netlink-driver
        family: 400
    bad-number:
        interface: netlink-driver
        family: one-hundred
        family-name: foo-driver
    bad-family-name:
        interface: netlink-driver
        family: 100
        family-name: foo---------
    bad-family-name-string:
        interface: netlink-driver
        family: 100
        family-name: 12123323443432
`, nil)
	appSet, err := interfaces.NewSnapAppSet(gadgetInfo, nil)
	c.Assert(err, IsNil)

	s.gadgetNetlinkSlotInfo = gadgetInfo.Slots["my-driver"]
	s.gadgetNetlinkSlot = interfaces.NewConnectedSlot(s.gadgetNetlinkSlotInfo, appSet, nil, nil)
	s.gadgetMissingNumberSlotInfo = gadgetInfo.Slots["missing-number"]
	s.gadgetMissingNumberSlot = interfaces.NewConnectedSlot(s.gadgetMissingNumberSlotInfo, appSet, nil, nil)
	s.gadgetMissingNameSlotInfo = gadgetInfo.Slots["missing-name"]
	s.gadgetMissingNameSlot = interfaces.NewConnectedSlot(s.gadgetMissingNameSlotInfo, appSet, nil, nil)
	s.gadgetBadNumberSlotInfo = gadgetInfo.Slots["bad-number"]
	s.gadgetBadNumberSlot = interfaces.NewConnectedSlot(s.gadgetBadNumberSlotInfo, appSet, nil, nil)
	s.gadgetBadNameSlotInfo = gadgetInfo.Slots["bad-family-name"]
	s.gadgetBadNameSlot = interfaces.NewConnectedSlot(s.gadgetBadNameSlotInfo, appSet, nil, nil)
	s.gadgetBadNameStringSlotInfo = gadgetInfo.Slots["bad-family-name-string"]
	s.gadgetBadNameStringSlot = interfaces.NewConnectedSlot(s.gadgetBadNameStringSlotInfo, appSet, nil, nil)

	osInfo := snaptest.MockInfo(c, `
name: my-core
version: 0
type: os
slots:
    my-driver:
        interface: netlink-driver
        family: 777
        family-name: seven-7-seven
`, nil)
	appSet, err = interfaces.NewSnapAppSet(osInfo, nil)
	c.Assert(err, IsNil)
	s.osNetlinkSlotInfo = osInfo.Slots["my-driver"]
	s.osNetlinkSlot = interfaces.NewConnectedSlot(s.osNetlinkSlotInfo, appSet, nil, nil)

	// Snap Consumers
	consumingSnapInfo := snaptest.MockInfo(c, `
name: client-snap
version: 0
plugs:
  plug-for-netlink-driver-777:
    interface: netlink-driver
    family-name: seven-7-seven
  plug-for-netlink-driver-foo:
    interface: netlink-driver
    family-name: foo-driver
  missing-family-name:
    interface: netlink-driver
  invalid-family-name:
    interface: netlink-driver
    family-name: ---foo-----
  invalid-family-name-string:
    interface: netlink-driver
    family-name: 1213123
apps:
  netlink-test:
    command: bin/foo.sh
    plugs: 
      - plug-for-netlink-driver-777
      - plug-for-netlink-driver-foo
`, nil)

	appSet, err = interfaces.NewSnapAppSet(consumingSnapInfo, nil)
	c.Assert(err, IsNil)

	s.appToCorePlugDriverInfo = consumingSnapInfo.Plugs["plug-for-netlink-driver-777"]
	s.appToCorePlugDriver = interfaces.NewConnectedPlug(s.appToCorePlugDriverInfo, appSet, nil, nil)

	s.appToGadgetPlugDriverInfo = consumingSnapInfo.Plugs["plug-for-netlink-driver-foo"]
	s.appToGadgetPlugDriver = interfaces.NewConnectedPlug(s.appToCorePlugDriverInfo, appSet, nil, nil)

	s.appBadFamilyNamePlugDriverInfo = consumingSnapInfo.Plugs["invalid-family-name"]
	s.appBadFamilyNamePlugDriver = interfaces.NewConnectedPlug(s.appBadFamilyNamePlugDriverInfo, appSet, nil, nil)

	s.appBadFamilyNameStringPlugDriverInfo = consumingSnapInfo.Plugs["invalid-family-name-string"]
	s.appBadFamilyNameStringPlugDriver = interfaces.NewConnectedPlug(s.appBadFamilyNameStringPlugDriverInfo, appSet, nil, nil)

	s.appMissingFamilyNamePlugDriverInfo = consumingSnapInfo.Plugs["missing-family-name"]
	s.appMissingFamilyNamePlugDriver = interfaces.NewConnectedPlug(s.appMissingFamilyNamePlugDriverInfo, appSet, nil, nil)
}

func (s *NetlinkDriverInterfaceSuite) 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 operating a kernel driver module exposing itself via a netlink protocol family")
	c.Assert(si.BaseDeclarationSlots, testutil.Contains, "netlink-driver")
}

func (s *NetlinkDriverInterfaceSuite) TestAutoConnect(c *C) {
	// ensure the plug definitions in the YAML didn't change
	c.Check(s.appToCorePlugDriverInfo.Attrs["family-name"], Equals, "seven-7-seven")
	c.Check(s.osNetlinkSlotInfo.Attrs["family-name"], Equals, "seven-7-seven")

	// ensure the plug definitions in the YAML didn't change
	c.Check(s.appToGadgetPlugDriverInfo.Attrs["family-name"], Equals, "foo-driver")
	c.Check(s.gadgetNetlinkSlotInfo.Attrs["family-name"], Equals, "foo-driver")

	// with matching family-name attributes, it works
	c.Check(s.iface.AutoConnect(s.appToCorePlugDriverInfo, s.osNetlinkSlotInfo), Equals, true)
	c.Check(s.iface.AutoConnect(s.appToGadgetPlugDriverInfo, s.gadgetNetlinkSlotInfo), Equals, true)

	// with different family-name attributes, it doesn't
	c.Check(s.iface.AutoConnect(s.appToCorePlugDriverInfo, s.gadgetNetlinkSlotInfo), Equals, false)
	c.Check(s.iface.AutoConnect(s.appToGadgetPlugDriverInfo, s.osNetlinkSlotInfo), Equals, false)
}

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

func (s *NetlinkDriverInterfaceSuite) TestSanitizeSlotGadgetSnap(c *C) {
	// netlink slot on gadget accepted
	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.gadgetNetlinkSlotInfo), IsNil)

	// slots without number attribute are rejected
	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.gadgetMissingNumberSlotInfo), ErrorMatches,
		"netlink-driver slot must have a family number attribute")

	// slots with number attribute that isnt a number
	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.gadgetBadNumberSlotInfo), ErrorMatches,
		"netlink-driver slot family number attribute must be an int")

	// slots without family-name
	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.gadgetMissingNameSlotInfo), ErrorMatches,
		"netlink-driver slot must have a family-name attribute")

	// slots with family-name attribute that isn't a string
	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.gadgetBadNameStringSlotInfo), ErrorMatches,
		`netlink-driver slot family-name attribute must be a string`)

	// slots with bad family-name
	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.gadgetBadNameSlotInfo), ErrorMatches,
		`netlink-driver slot family-name "foo---------" is invalid`)
}

func (s *NetlinkDriverInterfaceSuite) TestSanitizePlugAppSnap(c *C) {
	// netlink plug on app accepted
	c.Assert(interfaces.BeforePreparePlug(s.iface, s.appToCorePlugDriverInfo), IsNil)

	// plugs without family-name are rejected
	c.Assert(interfaces.BeforePreparePlug(s.iface, s.appMissingFamilyNamePlugDriverInfo), ErrorMatches,
		"netlink-driver plug must have a family-name attribute")

	// slots with family-name attribute that isn't a string
	c.Assert(interfaces.BeforePreparePlug(s.iface, s.appBadFamilyNameStringPlugDriverInfo), ErrorMatches,
		`netlink-driver plug family-name attribute must be a string`)

	// slots with bad family-name
	c.Assert(interfaces.BeforePreparePlug(s.iface, s.appBadFamilyNamePlugDriverInfo), ErrorMatches,
		`netlink-driver plug family-name "---foo-----" is invalid`)
}

func (s *NetlinkDriverInterfaceSuite) TestSanitizeSlotOsSnap(c *C) {
	// netlink slot on OS accepted
	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.osNetlinkSlotInfo), IsNil)
}

func (s *NetlinkDriverInterfaceSuite) TestSanitizePlug(c *C) {
	c.Assert(interfaces.BeforePreparePlug(s.iface, s.appToCorePlugDriverInfo), IsNil)
	c.Assert(interfaces.BeforePreparePlug(s.iface, s.appToGadgetPlugDriverInfo), IsNil)
}

func (s *NetlinkDriverInterfaceSuite) TestApparmorConnectedPlug(c *C) {
	spec := apparmor.NewSpecification(s.appToCorePlugDriver.AppSet())
	c.Assert(spec.AddConnectedPlug(s.iface, s.appToCorePlugDriver, s.gadgetNetlinkSlot), IsNil)
	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.client-snap.netlink-test"})
	c.Assert(spec.SnippetForTag("snap.client-snap.netlink-test"), testutil.Contains, `network netlink,`)
}

func (s *NetlinkDriverInterfaceSuite) TestSecCompConnectedPlug(c *C) {
	spec := seccomp.NewSpecification(s.appToCorePlugDriver.AppSet())
	c.Assert(spec.AddConnectedPlug(s.iface, s.appToCorePlugDriver, s.osNetlinkSlot), IsNil)
	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.client-snap.netlink-test"})
	c.Assert(spec.SnippetForTag("snap.client-snap.netlink-test"), testutil.Contains, `socket AF_NETLINK - 777`)

	spec2 := seccomp.NewSpecification(s.appToGadgetPlugDriver.AppSet())
	c.Assert(spec2.AddConnectedPlug(s.iface, s.appToGadgetPlugDriver, s.gadgetNetlinkSlot), IsNil)
	c.Assert(spec2.SecurityTags(), DeepEquals, []string{"snap.client-snap.netlink-test"})
	c.Assert(spec2.SnippetForTag("snap.client-snap.netlink-test"), testutil.Contains, `socket AF_NETLINK - 100`)
}