-
Notifications
You must be signed in to change notification settings - Fork 594
/
Copy pathdbusutil_test.go
122 lines (96 loc) · 3.38 KB
/
dbusutil_test.go
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
// -*- 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 dbusutil_test
import (
"fmt"
"net"
"os"
"path/filepath"
"testing"
. "gopkg.in/check.v1"
"github.com/snapcore/snapd/dbusutil"
"github.com/snapcore/snapd/dbusutil/dbustest"
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/testutil"
)
func Test(t *testing.T) { TestingT(t) }
type dbusutilSuite struct {
testutil.BaseTest
}
var _ = Suite(&dbusutilSuite{})
const envVar = "DBUS_SESSION_BUS_ADDRESS"
func (s *dbusutilSuite) SetUpTest(c *C) {
s.BaseTest.SetUpTest(c)
// Pretend we have an empty file system. This specifically makes
// /run/user/*/ empty as well.
dirs.SetRootDir(c.MkDir())
s.AddCleanup(func() { dirs.SetRootDir("") })
// Pretend that we don't have the environment variable with session bus
// address.
if value := os.Getenv(envVar); value != "" {
os.Unsetenv(envVar)
s.AddCleanup(func() { os.Setenv(envVar, value) })
}
}
func (*dbusutilSuite) TestIsSessionBusLikelyPresentNothing(c *C) {
c.Assert(dbusutil.IsSessionBusLikelyPresent(), Equals, false)
}
func (*dbusutilSuite) TestIsSessionBusLikelyPresentEnvVar(c *C) {
os.Setenv(envVar, "address")
c.Assert(dbusutil.IsSessionBusLikelyPresent(), Equals, true)
}
func (*dbusutilSuite) TestIsSessionBusLikelyPresentAddrFile(c *C) {
f := fmt.Sprintf("%s/%d/dbus-session", dirs.XdgRuntimeDirBase, os.Getuid())
c.Assert(os.MkdirAll(filepath.Dir(f), 0755), IsNil)
c.Assert(os.WriteFile(f, []byte("address"), 0644), IsNil)
c.Assert(dbusutil.IsSessionBusLikelyPresent(), Equals, true)
}
func (*dbusutilSuite) TestIsSessionBusLikelyPresentSocket(c *C) {
f := fmt.Sprintf("%s/%d/bus", dirs.XdgRuntimeDirBase, os.Getuid())
c.Assert(os.MkdirAll(filepath.Dir(f), 0755), IsNil)
l, err := net.Listen("unix", f)
c.Assert(err, IsNil)
defer l.Close()
c.Assert(dbusutil.IsSessionBusLikelyPresent(), Equals, true)
}
func (*dbusutilSuite) TestSessionBusWithoutBus(c *C) {
_, err := dbusutil.SessionBus()
c.Assert(err, ErrorMatches, "cannot find session bus")
}
func (*dbusutilSuite) TestMockOnlySessionBusAvailable(c *C) {
stub, err := dbustest.StubConnection()
c.Assert(err, IsNil)
defer stub.Close()
restore := dbusutil.MockOnlySessionBusAvailable(stub)
defer restore()
conn, err := dbusutil.SessionBus()
c.Assert(err, IsNil)
c.Check(conn, Equals, stub)
c.Check(func() { dbusutil.SystemBus() }, PanicMatches, "DBus system bus should not have been used")
}
func (*dbusutilSuite) TestMockOnlySystemBusAvailable(c *C) {
stub, err := dbustest.StubConnection()
c.Assert(err, IsNil)
defer stub.Close()
restore := dbusutil.MockOnlySystemBusAvailable(stub)
defer restore()
c.Check(func() { dbusutil.SessionBus() }, PanicMatches, "DBus session bus should not have been used")
conn, err := dbusutil.SystemBus()
c.Assert(err, IsNil)
c.Check(conn, Equals, stub)
}