File: dbus_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 (215 lines) | stat: -rw-r--r-- 7,614 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
// -*- 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 wrappers_test

import (
	"os"
	"path/filepath"

	. "gopkg.in/check.v1"

	"github.com/snapcore/snapd/dirs"
	_ "github.com/snapcore/snapd/interfaces/builtin"
	"github.com/snapcore/snapd/snap"
	"github.com/snapcore/snapd/snap/snaptest"
	"github.com/snapcore/snapd/testutil"
	"github.com/snapcore/snapd/wrappers"
)

type dbusTestSuite struct {
	tempdir string
}

var _ = Suite(&dbusTestSuite{})

func (s *dbusTestSuite) SetUpTest(c *C) {
	s.tempdir = c.MkDir()
	dirs.SetRootDir(s.tempdir)
}

func (s *dbusTestSuite) TearDownTest(c *C) {
	dirs.SetRootDir("")
}

const dbusSnapYaml = `
name: snapname
version: 1.0
slots:
  system1:
    interface: dbus
    bus: system
    name: org.example.Foo
  system2:
    interface: dbus
    bus: system
    name: org.example.Bar
  session1:
    interface: dbus
    bus: session
    name: org.example.Foo
  session2:
    interface: dbus
    bus: session
    name: org.example.Bar
apps:
  system-svc:
    command: bin/start-system
    daemon: simple
    activates-on:
      - system1
      - system2
  session-svc:
    command: bin/start-session
    daemon: simple
    daemon-scope: user
    activates-on:
      - session1
      - session2
`

func (s *dbusTestSuite) TestGenerateDBusActivationFile(c *C) {
	info := snaptest.MockSnap(c, dbusSnapYaml, &snap.SideInfo{Revision: snap.R(12)})

	app := info.Apps["system-svc"]
	svcWrapper, err := wrappers.GenerateDBusActivationFile(app, "org.example.Foo")
	c.Assert(err, IsNil)
	c.Check(string(svcWrapper), Equals, `[D-BUS Service]
Name=org.example.Foo
Comment=Bus name for snap application snapname.system-svc
SystemdService=snap.snapname.system-svc.service
Exec=/usr/bin/snap run snapname.system-svc
AssumedAppArmorLabel=snap.snapname.system-svc
User=root
X-Snap=snapname
`)

	app = info.Apps["session-svc"]
	svcWrapper, err = wrappers.GenerateDBusActivationFile(app, "org.example.Foo")
	c.Assert(err, IsNil)
	c.Check(string(svcWrapper), Equals, `[D-BUS Service]
Name=org.example.Foo
Comment=Bus name for snap application snapname.session-svc
SystemdService=snap.snapname.session-svc.service
Exec=/usr/bin/snap run snapname.session-svc
AssumedAppArmorLabel=snap.snapname.session-svc
X-Snap=snapname
`)
}

func (s *dbusTestSuite) TestAddSnapDBusActivationFiles(c *C) {
	info := snaptest.MockSnap(c, dbusSnapYaml, &snap.SideInfo{Revision: snap.R(12)})

	err := wrappers.AddSnapDBusActivationFiles(info)
	c.Assert(err, IsNil)

	c.Check(filepath.Join(dirs.SnapDBusSessionServicesDir, "org.example.Foo.service"), testutil.FileContains, "SystemdService=snap.snapname.session-svc.service\n")
	c.Check(filepath.Join(dirs.SnapDBusSessionServicesDir, "org.example.Bar.service"), testutil.FileContains, "SystemdService=snap.snapname.session-svc.service\n")

	c.Check(filepath.Join(dirs.SnapDBusSystemServicesDir, "org.example.Foo.service"), testutil.FileContains, "SystemdService=snap.snapname.system-svc.service\n")
	c.Check(filepath.Join(dirs.SnapDBusSystemServicesDir, "org.example.Bar.service"), testutil.FileContains, "SystemdService=snap.snapname.system-svc.service\n")
}

func (s *dbusTestSuite) TestAddSnapDBusActivationFilesRemovesLeftovers(c *C) {
	c.Assert(os.MkdirAll(dirs.SnapDBusSessionServicesDir, 0755), IsNil)
	c.Assert(os.MkdirAll(dirs.SnapDBusSystemServicesDir, 0755), IsNil)

	sessionSvc := filepath.Join(dirs.SnapDBusSessionServicesDir, "org.example.Baz.service")
	c.Assert(os.WriteFile(sessionSvc, []byte("[D-BUS Service]\nX-Snap=snapname\n"), 0644), IsNil)
	systemSvc := filepath.Join(dirs.SnapDBusSessionServicesDir, "org.example.Baz.service")
	c.Assert(os.WriteFile(systemSvc, []byte("[D-BUS Service]\nX-Snap=snapname\n"), 0644), IsNil)

	otherSessionSvc := filepath.Join(dirs.SnapDBusSessionServicesDir, "org.example.OtherSnap.service")
	c.Assert(os.WriteFile(otherSessionSvc, []byte("[D-BUS Service]\nX-Snap=other-snap\n"), 0644), IsNil)
	otherSystemSvc := filepath.Join(dirs.SnapDBusSystemServicesDir, "org.example.OtherSnap.service")
	c.Assert(os.WriteFile(otherSystemSvc, []byte("[D-BUS Service]\nX-Snap=other-snap\n"), 0644), IsNil)

	info := snaptest.MockSnap(c, dbusSnapYaml, &snap.SideInfo{Revision: snap.R(12)})
	err := wrappers.AddSnapDBusActivationFiles(info)
	c.Assert(err, IsNil)

	c.Check(sessionSvc, testutil.FileAbsent)
	c.Check(systemSvc, testutil.FileAbsent)

	// Files belonging to other snap are left as is
	c.Check(otherSessionSvc, testutil.FilePresent)
	c.Check(otherSystemSvc, testutil.FilePresent)
}

func (s *dbusTestSuite) TestRemoveSnapDBusActivationFiles(c *C) {
	c.Assert(os.MkdirAll(dirs.SnapDBusSessionServicesDir, 0755), IsNil)
	c.Assert(os.MkdirAll(dirs.SnapDBusSystemServicesDir, 0755), IsNil)

	sessionSvc := filepath.Join(dirs.SnapDBusSessionServicesDir, "org.example.Baz.service")
	c.Assert(os.WriteFile(sessionSvc, []byte("[D-BUS Service]\nX-Snap=snapname\n"), 0644), IsNil)
	systemSvc := filepath.Join(dirs.SnapDBusSessionServicesDir, "org.example.Baz.service")
	c.Assert(os.WriteFile(systemSvc, []byte("[D-BUS Service]\nX-Snap=snapname\n"), 0644), IsNil)

	otherSessionSvc := filepath.Join(dirs.SnapDBusSessionServicesDir, "org.example.OtherSnap.service")
	c.Assert(os.WriteFile(otherSessionSvc, []byte("[D-BUS Service]\nX-Snap=other-snap\n"), 0644), IsNil)
	otherSystemSvc := filepath.Join(dirs.SnapDBusSystemServicesDir, "org.example.OtherSnap.service")
	c.Assert(os.WriteFile(otherSystemSvc, []byte("[D-BUS Service]\nX-Snap=other-snap\n"), 0644), IsNil)

	info := snaptest.MockSnap(c, dbusSnapYaml, &snap.SideInfo{Revision: snap.R(12)})
	err := wrappers.RemoveSnapDBusActivationFiles(info)
	c.Assert(err, IsNil)

	c.Check(sessionSvc, testutil.FileAbsent)
	c.Check(systemSvc, testutil.FileAbsent)

	// Files belonging to other snap are left as is
	c.Check(otherSessionSvc, testutil.FilePresent)
	c.Check(otherSystemSvc, testutil.FilePresent)
}

func (s *dbusTestSuite) TestAddSnapDBusActivationFilesInvalidData(c *C) {
	info, err := snap.InfoFromSnapYaml([]byte(`
name: snapname
version: 1.0
slots:
  invalid-name:
    interface: dbus
    bus: system
    name: 'invalid bus name'
  invalid-bus:
    interface: dbus
    bus: accessibility
    name: org.example.Foo
apps:
  svc:
    command: bin/svc
    daemon: simple
    activates-on: [invalid-name, invalid-bus]
`))
	c.Assert(err, IsNil)
	// The slots with invalid data have been removed
	c.Check(info.Slots, HasLen, 0)
	c.Check(info.BadInterfaces["invalid-name"], Equals, `invalid DBus bus name: "invalid bus name"`)
	c.Check(info.BadInterfaces["invalid-bus"], Equals, `bus 'accessibility' must be one of 'session' or 'system'`)

	// No activation files are written out for the invalid slots
	err = wrappers.AddSnapDBusActivationFiles(info)
	c.Assert(err, IsNil)
	matches, err := filepath.Glob(filepath.Join(dirs.SnapDBusSessionServicesDir, "*.service"))
	c.Check(err, IsNil)
	c.Check(matches, HasLen, 0)
	matches, err = filepath.Glob(filepath.Join(dirs.SnapDBusSystemServicesDir, "*.service"))
	c.Check(err, IsNil)
	c.Check(matches, HasLen, 0)
}