Debian Codegraph @

bus/endpoint_test.go

raw
1 /*
2 Copyright 2013-2014 Canonical Ltd.
3
4 This program is free software: you can redistribute it and/or modify it
5 under the terms of the GNU General Public License version 3, as published
6 by the Free Software Foundation.
7
8 This program is distributed in the hope that it will be useful, but
9 WITHOUT ANY WARRANTY; without even the implied warranties of
10 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 PURPOSE. See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along
14 with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 package bus
18
19 import (
20 . "gopkg.in/check.v1"
21 "gitlab.com/ubports/development/core/lomiri-push-service/logger"
22 helpers "gitlab.com/ubports/development/core/lomiri-push-service/testing"
23 "os"
24 )
25
26 type EndpointSuite struct {
27 log logger.Logger
28 }
29
30 var _ = Suite(&EndpointSuite{})
31
32 func (s *EndpointSuite) SetUpTest(c *C) {
33 s.log = helpers.NewTestLogger(c, "debug")
34 }
35
36 // TODO: this is going to remain empty until go-dbus grows some
37 // testing amenities (already talked about it with jamesh)
38
39 // Tests that we can connect to the *actual* system bus.
40 // XXX: maybe connect to a mock/fake/etc bus?
41 func (s *EndpointSuite) TestDial(c *C) {
42 // if somebody's set up the env var, assume it's "live"
43 if os.Getenv("DBUS_SYSTEM_BUS_ADDRESS") == "" {
44 // otherwise, check
45 if _, err := os.Stat("/var/run/dbus/system_bus_socket"); os.IsNotExist(err) {
46 c.Skip("system bus not present")
47 }
48 }
49 endp := newEndpoint(SystemBus, BusDaemonAddress, s.log)
50 c.Assert(endp.conn, IsNil)
51 err := endp.Dial()
52 c.Assert(err, IsNil)
53 defer endp.Close() // yes, a second close. On purpose.
54 c.Assert(endp.conn, NotNil)
55 endp.Close() // the first close. If you're counting right.
56 c.Assert(endp.conn, IsNil) // Close cleans up
57 }
58
59 // Test that if we try to connect to the session bus when no session
60 // bus is available, we get a reasonable result (i.e., an error).
61 func (s *EndpointSuite) TestDialCanFail(c *C) {
62 db := "DBUS_SESSION_BUS_ADDRESS"
63 odb := os.Getenv(db)
64 defer os.Setenv(db, odb)
65 os.Setenv(db, "")
66
67 endp := newEndpoint(SessionBus, Address{"", "", ""}, s.log)
68 err := endp.Dial()
69 c.Check(err, NotNil)
70 }