File: user_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 (251 lines) | stat: -rw-r--r-- 9,322 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
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
 * Copyright (C) 2019 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 main_test

import (
	"bytes"
	"fmt"
	"os"
	"path/filepath"

	. "gopkg.in/check.v1"

	update "github.com/snapcore/snapd/cmd/snap-update-ns"
	"github.com/snapcore/snapd/dirs"
	"github.com/snapcore/snapd/osutil"
	"github.com/snapcore/snapd/testutil"
)

type userSuite struct{}

var _ = Suite(&userSuite{})

func (s *userSuite) TestIsPlausibleHomeHappy(c *C) {
	tmpHomeDir := c.MkDir()
	err := update.IsPlausibleHome(tmpHomeDir)
	c.Assert(err, IsNil)
}

func (s *userSuite) TestIsPlausibleHomeErrorPathEmpty(c *C) {
	err := update.IsPlausibleHome("")
	c.Assert(err, ErrorMatches, "cannot allow empty path")
}

func (s *userSuite) TestIsPlausibleHomeErrorPathNotClean(c *C) {
	err := update.IsPlausibleHome("/PathNotClean/")
	c.Assert(err, ErrorMatches, "cannot allow unclean path")
}

func (s *userSuite) TestIsPlausibleHomeErrorPathRelative(c *C) {
	err := update.IsPlausibleHome("PathRelative")
	c.Assert(err, ErrorMatches, "cannot allow relative path")
}

func (s *userSuite) TestIsPlausibleHomeErrorPathNotExist(c *C) {
	tmpHomeDir := c.MkDir() + "/user-does-not-exist"
	err := update.IsPlausibleHome(tmpHomeDir)
	c.Assert(err, ErrorMatches, "no such file or directory")
}

func (s *userSuite) TestLock(c *C) {
	dirs.SetRootDir(c.MkDir())
	defer dirs.SetRootDir("/")
	c.Assert(os.MkdirAll(dirs.FeaturesDir, 0755), IsNil)
	tmpHomeDir := c.MkDir()
	restore := update.MockSnapConfineUserEnv("/run/user/1234/snap.snapname", tmpHomeDir)
	defer restore()
	upCtx, err := update.NewUserProfileUpdateContext("foo", false, 1234)
	c.Assert(err, IsNil)

	// Locking is a no-op.
	unlock, err := upCtx.Lock()
	c.Assert(err, IsNil)
	c.Check(unlock, NotNil)
	unlock()
}

func (s *userSuite) TestAssumptionsHomeValid(c *C) {
	tmpHomeDir := c.MkDir()
	restore := update.MockSnapConfineUserEnv("/run/user/1234/snap.snapname", tmpHomeDir)
	defer restore()
	upCtx, err := update.NewUserProfileUpdateContext("foo", false, 1234)
	c.Assert(err, IsNil)
	as := upCtx.Assumptions()
	c.Check(as.UnrestrictedPaths(), DeepEquals, []string{tmpHomeDir})
	c.Check(as.ModeForPath(tmpHomeDir+"/dir"), Equals, os.FileMode(0755))
}

func (s *userSuite) TestAssumptionsHomeInvalid(c *C) {
	restore := update.MockSnapConfineUserEnv("/run/user/1234/snap.snapname", "")
	defer restore()
	upCtx, err := update.NewUserProfileUpdateContext("foo", false, 1234)
	c.Assert(err, IsNil)
	as := upCtx.Assumptions()
	c.Check(as.UnrestrictedPaths(), IsNil)
}

func (s *userSuite) TestLoadDesiredProfileHomeRequiredAndValid(c *C) {
	// Mock directories but to simplify testing use the real value for XDG.
	dirs.SetRootDir(c.MkDir())
	defer dirs.SetRootDir("/")
	dirs.XdgRuntimeDirBase = "/run/user"
	tmpHomeDir := c.MkDir()
	restore := update.MockSnapConfineUserEnv("/run/user/1234/snap.snapname", tmpHomeDir)
	defer restore()
	upCtx, err := update.NewUserProfileUpdateContext("foo", false, 1234)
	c.Assert(err, IsNil)

	input := "$XDG_RUNTIME_DIR/doc/by-app/snap.foo $XDG_RUNTIME_DIR/doc none bind,rw 0 0\n" +
		"none $HOME/.local/share none x-snapd.kind=ensure-dir,x-snapd.must-exist-dir=$HOME 0 0\n"
	output := "/run/user/1234/doc/by-app/snap.foo /run/user/1234/doc none bind,rw 0 0\n" +
		fmt.Sprintf("none %s/.local/share none x-snapd.kind=ensure-dir,x-snapd.must-exist-dir=%s 0 0\n", tmpHomeDir, tmpHomeDir)

	// Write a desired user mount profile for snap "foo".
	path := update.DesiredUserProfilePath("foo")
	c.Assert(os.MkdirAll(filepath.Dir(path), 0755), IsNil)
	c.Assert(os.WriteFile(path, []byte(input), 0644), IsNil)

	// Ask the user profile update helper to read the desired profile.
	profile, err := upCtx.LoadDesiredProfile()
	c.Assert(err, IsNil)
	builder := &bytes.Buffer{}
	profile.WriteTo(builder)

	// Note that the profile read back contains expanded $XDG_RUNTIME_DIR and $HOME.
	c.Check(builder.String(), Equals, output)
}

func (s *userSuite) TestLoadDesiredProfileHomeNotRequiredAndMissing(c *C) {
	// Mock directories but to simplify testing use the real value for XDG.
	dirs.SetRootDir(c.MkDir())
	defer dirs.SetRootDir("/")
	dirs.XdgRuntimeDirBase = "/run/user"
	//tmpHomeDir := c.MkDir()
	restore := update.MockSnapConfineUserEnv("/run/user/1234/snap.snapname", "")
	defer restore()
	upCtx, err := update.NewUserProfileUpdateContext("foo", false, 1234)
	c.Assert(err, IsNil)

	input := "$XDG_RUNTIME_DIR/doc/by-app/snap.foo $XDG_RUNTIME_DIR/doc none bind,rw 0 0\n"
	output := "/run/user/1234/doc/by-app/snap.foo /run/user/1234/doc none bind,rw 0 0\n"

	// Write a desired user mount profile for snap "foo".
	path := update.DesiredUserProfilePath("foo")
	c.Assert(os.MkdirAll(filepath.Dir(path), 0755), IsNil)
	c.Assert(os.WriteFile(path, []byte(input), 0644), IsNil)

	// Ask the user profile update helper to read the desired profile.
	profile, err := upCtx.LoadDesiredProfile()
	c.Assert(err, IsNil)
	builder := &bytes.Buffer{}
	profile.WriteTo(builder)

	// Note that the profile read back contains expanded $XDG_RUNTIME_DIR
	c.Check(builder.String(), Equals, output)
}

func (s *userSuite) TestLoadDesiredProfileErrorHomeRequiredAndMissing(c *C) {
	// Mock directories but to simplify testing use the real value for XDG.
	dirs.SetRootDir(c.MkDir())
	defer dirs.SetRootDir("/")
	dirs.XdgRuntimeDirBase = "/run/user"
	//tmpHomeDir := c.MkDir()
	restore := update.MockSnapConfineUserEnv("/run/user/1234/snap.snapname", "")
	defer restore()
	upCtx, err := update.NewUserProfileUpdateContext("foo", false, 1234)
	c.Assert(err, IsNil)

	input := "$XDG_RUNTIME_DIR/doc/by-app/snap.foo $XDG_RUNTIME_DIR/doc none bind,rw 0 0\n" +
		"none $HOME/.local/share none x-snapd.kind=ensure-dir,x-snapd.must-exist-dir=$HOME 0 0\n"

	// Write a desired user mount profile for snap "foo".
	path := update.DesiredUserProfilePath("foo")
	c.Assert(os.MkdirAll(filepath.Dir(path), 0755), IsNil)
	c.Assert(os.WriteFile(path, []byte(input), 0644), IsNil)

	// Ask the user profile update helper to read the desired profile.
	profile, err := upCtx.LoadDesiredProfile()
	c.Assert(err, ErrorMatches, `cannot expand mount entry \(none \$HOME/.local/share none x-snapd.kind=ensure-dir,x-snapd.must-exist-dir=\$HOME 0 0\): cannot use invalid home directory \"\": cannot allow empty path`)
	c.Assert(profile, IsNil)
}

func (s *userSuite) TestLoadCurrentProfile(c *C) {
	// Mock directories.
	dirs.SetRootDir(c.MkDir())
	tmpHomeDir := c.MkDir()
	restore := update.MockSnapConfineUserEnv("/run/user/1234/snap.snapname", tmpHomeDir)
	defer restore()
	upCtx, err := update.NewUserProfileUpdateContext("foo", false, 1234)
	c.Assert(err, IsNil)

	// Write a current user mount profile for snap "foo".
	text := "/run/user/1234/doc/by-app/snap.foo /run/user/1234/doc none bind,rw 0 0\n"
	path := update.CurrentUserProfilePath(upCtx.InstanceName(), 1234)
	c.Assert(os.MkdirAll(filepath.Dir(path), 0755), IsNil)
	c.Assert(os.WriteFile(path, []byte(text), 0644), IsNil)

	// Ask the user profile update helper to read the current profile.
	profile, err := upCtx.LoadCurrentProfile()
	c.Assert(err, IsNil)
	builder := &bytes.Buffer{}
	profile.WriteTo(builder)

	// Note that the profile is empty.
	// Currently user profiles are not persisted so the presence of a profile on-disk is ignored.
	c.Check(builder.String(), Equals, "")
}

func (s *userSuite) TestSaveCurrentProfile(c *C) {
	// Mock directories and create directory runtime mount profiles.
	dirs.SetRootDir(c.MkDir())
	defer dirs.SetRootDir("/")
	c.Assert(os.MkdirAll(dirs.SnapRunNsDir, 0755), IsNil)
	tmpHomeDir := c.MkDir()
	restore := update.MockSnapConfineUserEnv("/run/user/1234/snap.snapname", tmpHomeDir)
	defer restore()
	upCtx, err := update.NewUserProfileUpdateContext("foo", false, 1234)
	c.Assert(err, IsNil)

	// Prepare a mount profile to be saved.
	text := "/run/user/1234/doc/by-app/snap.foo /run/user/1234/doc none bind,rw 0 0\n"
	profile, err := osutil.LoadMountProfileText(text)
	c.Assert(err, IsNil)

	// Write a fake current user mount profile for snap "foo".
	path := update.CurrentUserProfilePath("foo", 1234)
	c.Assert(os.MkdirAll(filepath.Dir(path), 0755), IsNil)
	c.Assert(os.WriteFile(path, []byte("banana"), 0644), IsNil)

	// Ask the user profile update helper to write the current profile.
	err = upCtx.SaveCurrentProfile(profile)
	c.Assert(err, IsNil)

	// Note that the profile was not modified.
	// Currently user profiles are not persisted.
	c.Check(path, testutil.FileEquals, "banana")
}

func (s *userSuite) TestDesiredUserProfilePath(c *C) {
	c.Check(update.DesiredUserProfilePath("foo"), Equals, "/var/lib/snapd/mount/snap.foo.user-fstab")
}

func (s *userSuite) TestCurrentUserProfilePath(c *C) {
	c.Check(update.CurrentUserProfilePath("foo", 12345), Equals, "/run/snapd/ns/snap.foo.12345.user-fstab")
}