github.com/david-imola/snapd@v0.0.0-20210611180407-2de8ddeece6d/usersession/xdgopenproxy/userd_launcher_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 xdgopenproxy_test 21 22 import ( 23 "fmt" 24 "io/ioutil" 25 "os" 26 "path/filepath" 27 "syscall" 28 29 "github.com/godbus/dbus" 30 . "gopkg.in/check.v1" 31 32 "github.com/snapcore/snapd/testutil" 33 "github.com/snapcore/snapd/usersession/xdgopenproxy" 34 ) 35 36 type userdSuite struct { 37 testutil.DBusTest 38 39 userd *fakeUserd 40 41 openError *dbus.Error 42 calls []string 43 } 44 45 var _ = Suite(&userdSuite{}) 46 47 func (s *userdSuite) SetUpSuite(c *C) { 48 s.DBusTest.SetUpSuite(c) 49 50 s.userd = &fakeUserd{s} 51 err := s.SessionBus.Export(s.userd, xdgopenproxy.UserdLauncherObjectPath, xdgopenproxy.UserdLauncherIface) 52 c.Assert(err, IsNil) 53 54 _, err = s.SessionBus.RequestName(xdgopenproxy.UserdLauncherBusName, dbus.NameFlagAllowReplacement|dbus.NameFlagReplaceExisting) 55 c.Assert(err, IsNil) 56 } 57 58 func (s *userdSuite) TearDownSuite(c *C) { 59 if s.SessionBus != nil { 60 _, err := s.SessionBus.ReleaseName(xdgopenproxy.UserdLauncherBusName) 61 c.Check(err, IsNil) 62 } 63 64 s.DBusTest.TearDownSuite(c) 65 } 66 67 func (s *userdSuite) SetUpTest(c *C) { 68 s.DBusTest.SetUpTest(c) 69 70 s.openError = nil 71 s.calls = nil 72 } 73 74 func (s *userdSuite) TestOpenFile(c *C) { 75 launcher := &xdgopenproxy.UserdLauncher{} 76 77 path := filepath.Join(c.MkDir(), "test.txt") 78 c.Assert(ioutil.WriteFile(path, []byte("hello world"), 0644), IsNil) 79 80 err := launcher.OpenFile(s.SessionBus, path) 81 c.Check(err, IsNil) 82 c.Check(s.calls, DeepEquals, []string{ 83 "OpenFile", 84 }) 85 } 86 87 func (s *userdSuite) TestOpenFileError(c *C) { 88 s.openError = dbus.MakeFailedError(fmt.Errorf("failure")) 89 90 launcher := &xdgopenproxy.UserdLauncher{} 91 92 path := filepath.Join(c.MkDir(), "test.txt") 93 c.Assert(ioutil.WriteFile(path, []byte("hello world"), 0644), IsNil) 94 95 err := launcher.OpenFile(s.SessionBus, path) 96 c.Check(err, FitsTypeOf, dbus.Error{}) 97 c.Check(err, ErrorMatches, "failure") 98 c.Check(s.calls, DeepEquals, []string{ 99 "OpenFile", 100 }) 101 } 102 103 func (s *userdSuite) TestOpenDir(c *C) { 104 launcher := &xdgopenproxy.UserdLauncher{} 105 106 dir := c.MkDir() 107 err := launcher.OpenFile(s.SessionBus, dir) 108 c.Check(err, IsNil) 109 c.Check(s.calls, DeepEquals, []string{ 110 "OpenFile", 111 }) 112 } 113 114 func (s *userdSuite) TestOpenMissingFile(c *C) { 115 launcher := &xdgopenproxy.UserdLauncher{} 116 117 path := filepath.Join(c.MkDir(), "no-such-file.txt") 118 err := launcher.OpenFile(s.SessionBus, path) 119 c.Check(err, ErrorMatches, "no such file or directory") 120 c.Check(s.calls, HasLen, 0) 121 } 122 123 func (s *userdSuite) TestOpenUnreadableFile(c *C) { 124 launcher := &xdgopenproxy.UserdLauncher{} 125 126 path := filepath.Join(c.MkDir(), "test.txt") 127 c.Assert(ioutil.WriteFile(path, []byte("hello world"), 0644), IsNil) 128 c.Assert(os.Chmod(path, 0), IsNil) 129 130 err := launcher.OpenFile(s.SessionBus, path) 131 c.Check(err, ErrorMatches, "permission denied") 132 c.Check(s.calls, HasLen, 0) 133 } 134 135 func (s *userdSuite) TestOpenURI(c *C) { 136 launcher := &xdgopenproxy.UserdLauncher{} 137 138 err := launcher.OpenURI(s.SessionBus, "http://example.com") 139 c.Check(err, IsNil) 140 c.Check(s.calls, DeepEquals, []string{ 141 "OpenURI http://example.com", 142 }) 143 } 144 145 func (s *userdSuite) TestOpenURIError(c *C) { 146 s.openError = dbus.MakeFailedError(fmt.Errorf("failure")) 147 148 launcher := &xdgopenproxy.UserdLauncher{} 149 err := launcher.OpenURI(s.SessionBus, "http://example.com") 150 c.Check(err, FitsTypeOf, dbus.Error{}) 151 c.Check(err, ErrorMatches, "failure") 152 c.Check(s.calls, DeepEquals, []string{ 153 "OpenURI http://example.com", 154 }) 155 } 156 157 type fakeUserd struct { 158 *userdSuite 159 } 160 161 func (p *fakeUserd) OpenFile(parentWindow string, clientFD dbus.UnixFD) *dbus.Error { 162 p.calls = append(p.calls, "OpenFile") 163 164 fd := int(clientFD) 165 defer syscall.Close(fd) 166 167 return p.openError 168 } 169 170 func (p *fakeUserd) OpenURL(uri string) *dbus.Error { 171 p.calls = append(p.calls, "OpenURI "+uri) 172 return p.openError 173 }