File: confdb_test.go

package info (click to toggle)
snapd 2.71-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 79,536 kB
  • sloc: ansic: 16,114; sh: 16,105; python: 9,941; makefile: 1,890; exp: 190; awk: 40; xml: 22
file content (184 lines) | stat: -rw-r--r-- 4,713 bytes parent folder | download | duplicates (2)
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
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
 * Copyright (C) 2024 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"

	. "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/interfaces/udev"
	"github.com/snapcore/snapd/snap"
	"github.com/snapcore/snapd/snap/snaptest"
)

type confdbSuite struct {
	iface    interfaces.Interface
	slot     *interfaces.ConnectedSlot
	slotInfo *snap.SlotInfo
	plug     *interfaces.ConnectedPlug
	plugInfo *snap.PlugInfo
}

var _ = Suite(&confdbSuite{
	iface: builtin.MustInterface("confdb"),
})

const plugYaml = `name: plugger
version: 1.0
type: app
plugs:
 read-ssid:
  interface: confdb
  account: foo
  view: bar/baz
apps:
 app:
  command: foo
  plugs: [read-ssid]
`

const slotYaml = `name: core
version: 1.0
type: os
slots:
 confdb:
  interface: confdb
`

func (s *confdbSuite) SetUpTest(c *C) {
	s.plug, s.plugInfo = MockConnectedPlug(c, plugYaml, nil, "read-ssid")
	s.slot, s.slotInfo = MockConnectedSlot(c, slotYaml, nil, "confdb")
}

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

func (s *confdbSuite) TestAutoConnect(c *C) {
	c.Assert(s.iface.AutoConnect(s.plugInfo, s.slotInfo), Equals, true)
}

func (s *confdbSuite) TestConfdbSanitizePlug(c *C) {
	type testcase struct {
		account string
		view    string
		role    string
		err     string
	}

	tcs := []testcase{
		{
			account: "my-acc",
			view:    "network/wifi",
			role:    "custodian",
		},
		{
			account: "my-acc",
			view:    "network/wifi",
		},
		{
			err: `confdb plug must have an "account" attribute`,
		},
		{
			account: "my-acc",
			err:     `confdb plug must have a "view" attribute`,
		},
		{
			account: "my-acc",
			view:    "reg/view",
			role:    "observer",
			err:     `optional confdb plug "role" attribute must be "custodian"`,
		},
		{
			account: "my-acc",
			view:    "foobar",
			err:     `confdb plug must have a valid "view" attribute: expected confdb and view names separated by a single '/': foobar`,
		},
		{
			account: "my-acc",
			view:    "0-foo/bar",
			err:     `confdb plug must have a valid "view" attribute: invalid confdb name: 0-foo does not match '^[a-z](?:-?[a-z0-9])*$'`,
		},
		{
			account: "my-acc",
			view:    "foo/0-bar",
			err:     `confdb plug must have a valid "view" attribute: invalid view name: 0-bar does not match '^[a-z](?:-?[a-z0-9])*$'`,
		},
		{
			account: "_my-acc",
			view:    "foo/bar",
			err:     `confdb plug must have a valid "account" attribute: format mismatch`,
		},
	}

	for _, tc := range tcs {
		var accLine, viewLine, roleLine string
		if tc.account != "" {
			accLine = fmt.Sprintf("  account: %s\n", tc.account)
		}
		if tc.view != "" {
			viewLine = fmt.Sprintf("  view: %s\n", tc.view)
		}
		if tc.role != "" {
			roleLine = fmt.Sprintf("  role: %s\n", tc.role)
		}

		mockSnapYaml := `name: ssid-reader
version: 1.0
plugs:
 read-ssid:
  interface: confdb
` + accLine + viewLine + roleLine

		info := snaptest.MockInfo(c, mockSnapYaml, nil)
		plug := info.Plugs["read-ssid"]
		err := interfaces.BeforePreparePlug(s.iface, plug)
		if tc.err == "" {
			c.Assert(err, IsNil)
		} else {
			c.Assert(err, NotNil)
			c.Assert(err.Error(), Equals, tc.err)
		}
	}
}

func (s *confdbSuite) TestConfdbDoesntAddRules(c *C) {
	apparmorSpec := apparmor.NewSpecification(s.plug.AppSet())
	err := apparmorSpec.AddConnectedPlug(s.iface, s.plug, s.slot)
	c.Assert(err, IsNil)
	c.Assert(apparmorSpec.SecurityTags(), HasLen, 0)
	c.Assert(apparmorSpec.SnippetForTag("snap.plugger.app"), Equals, "")

	seccompSpec := seccomp.NewSpecification(s.plug.AppSet())
	err = seccompSpec.AddConnectedPlug(s.iface, s.plug, s.slot)
	c.Assert(err, IsNil)
	c.Assert(seccompSpec.SecurityTags(), HasLen, 0)
	c.Check(seccompSpec.SnippetForTag("snap.plugger.app"), Equals, "")

	udevSpec := udev.NewSpecification(s.plug.AppSet())
	err = udevSpec.AddConnectedPlug(s.iface, s.plug, s.slot)
	c.Assert(err, IsNil)
	c.Assert(udevSpec.Snippets(), HasLen, 0)
}