github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/dbusutil/dbusutil_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2020 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package dbusutil_test
    21  
    22  import (
    23  	"fmt"
    24  	"io/ioutil"
    25  	"net"
    26  	"os"
    27  	"path/filepath"
    28  	"testing"
    29  
    30  	. "gopkg.in/check.v1"
    31  
    32  	"github.com/snapcore/snapd/dbusutil"
    33  	"github.com/snapcore/snapd/dbusutil/dbustest"
    34  	"github.com/snapcore/snapd/dirs"
    35  	"github.com/snapcore/snapd/testutil"
    36  )
    37  
    38  func Test(t *testing.T) { TestingT(t) }
    39  
    40  type dbusutilSuite struct {
    41  	testutil.BaseTest
    42  }
    43  
    44  var _ = Suite(&dbusutilSuite{})
    45  
    46  const envVar = "DBUS_SESSION_BUS_ADDRESS"
    47  
    48  func (s *dbusutilSuite) SetUpTest(c *C) {
    49  	s.BaseTest.SetUpTest(c)
    50  
    51  	// Pretend we have an empty file system. This specifically makes
    52  	// /run/user/*/ empty as well.
    53  	dirs.SetRootDir(c.MkDir())
    54  	s.AddCleanup(func() { dirs.SetRootDir("") })
    55  
    56  	// Pretend that we don't have the environment variable with session bus
    57  	// address.
    58  	if value := os.Getenv(envVar); value != "" {
    59  		os.Unsetenv(envVar)
    60  		s.AddCleanup(func() { os.Setenv(envVar, value) })
    61  	}
    62  }
    63  
    64  func (*dbusutilSuite) TestIsSessionBusLikelyPresentNothing(c *C) {
    65  	c.Assert(dbusutil.IsSessionBusLikelyPresent(), Equals, false)
    66  }
    67  
    68  func (*dbusutilSuite) TestIsSessionBusLikelyPresentEnvVar(c *C) {
    69  	os.Setenv(envVar, "address")
    70  
    71  	c.Assert(dbusutil.IsSessionBusLikelyPresent(), Equals, true)
    72  }
    73  
    74  func (*dbusutilSuite) TestIsSessionBusLikelyPresentAddrFile(c *C) {
    75  	f := fmt.Sprintf("%s/%d/dbus-session", dirs.XdgRuntimeDirBase, os.Getuid())
    76  	c.Assert(os.MkdirAll(filepath.Dir(f), 0755), IsNil)
    77  	c.Assert(ioutil.WriteFile(f, []byte("address"), 0644), IsNil)
    78  
    79  	c.Assert(dbusutil.IsSessionBusLikelyPresent(), Equals, true)
    80  }
    81  
    82  func (*dbusutilSuite) TestIsSessionBusLikelyPresentSocket(c *C) {
    83  	f := fmt.Sprintf("%s/%d/bus", dirs.XdgRuntimeDirBase, os.Getuid())
    84  	c.Assert(os.MkdirAll(filepath.Dir(f), 0755), IsNil)
    85  	l, err := net.Listen("unix", f)
    86  	c.Assert(err, IsNil)
    87  	defer l.Close()
    88  
    89  	c.Assert(dbusutil.IsSessionBusLikelyPresent(), Equals, true)
    90  }
    91  
    92  func (*dbusutilSuite) TestSessionBusWithoutBus(c *C) {
    93  	_, err := dbusutil.SessionBus()
    94  	c.Assert(err, ErrorMatches, "cannot find session bus")
    95  }
    96  
    97  func (*dbusutilSuite) TestMockOnlySessionBusAvailable(c *C) {
    98  	stub, err := dbustest.StubConnection()
    99  	c.Assert(err, IsNil)
   100  	defer stub.Close()
   101  	restore := dbusutil.MockOnlySessionBusAvailable(stub)
   102  	defer restore()
   103  
   104  	conn, err := dbusutil.SessionBus()
   105  	c.Assert(err, IsNil)
   106  	c.Check(conn, Equals, stub)
   107  
   108  	c.Check(func() { dbusutil.SystemBus() }, PanicMatches, "DBus system bus should not have been used")
   109  }
   110  
   111  func (*dbusutilSuite) TestMockOnlySystemBusAvailable(c *C) {
   112  	stub, err := dbustest.StubConnection()
   113  	c.Assert(err, IsNil)
   114  	defer stub.Close()
   115  	restore := dbusutil.MockOnlySystemBusAvailable(stub)
   116  	defer restore()
   117  
   118  	c.Check(func() { dbusutil.SessionBus() }, PanicMatches, "DBus session bus should not have been used")
   119  
   120  	conn, err := dbusutil.SystemBus()
   121  	c.Assert(err, IsNil)
   122  	c.Check(conn, Equals, stub)
   123  }