forked from canonical/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxdgopenproxy_test.go
181 lines (147 loc) · 5.15 KB
/
xdgopenproxy_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
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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2018 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 xdgopenproxy_test
import (
"context"
"fmt"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"syscall"
"testing"
"github.com/godbus/dbus"
. "gopkg.in/check.v1"
"github.com/snapcore/snapd/xdgopenproxy"
)
func Test(t *testing.T) { TestingT(t) }
type xdgOpenSuite struct{}
var _ = Suite(&xdgOpenSuite{})
func (s *xdgOpenSuite) TestOpenURL(c *C) {
launcher := fakeBusObject(func(method string, args ...interface{}) error {
c.Check(method, Equals, "io.snapcraft.Launcher.OpenURL")
c.Check(args, DeepEquals, []interface{}{"http://example.org"})
return nil
})
c.Check(xdgopenproxy.Launch(launcher, "http://example.org"), IsNil)
}
func (s *xdgOpenSuite) TestOpenFile(c *C) {
path := filepath.Join(c.MkDir(), "test.txt")
c.Assert(ioutil.WriteFile(path, []byte("Hello world"), 0644), IsNil)
launcher := fakeBusObject(func(method string, args ...interface{}) error {
c.Check(method, Equals, "io.snapcraft.Launcher.OpenFile")
c.Assert(args, HasLen, 2)
c.Check(args[0], Equals, "")
c.Check(fdMatchesFile(int(args[1].(dbus.UnixFD)), path), IsNil)
return nil
})
c.Check(xdgopenproxy.Launch(launcher, path), IsNil)
}
func (s *xdgOpenSuite) TestOpenFileURL(c *C) {
path := filepath.Join(c.MkDir(), "test.txt")
c.Assert(ioutil.WriteFile(path, []byte("Hello world"), 0644), IsNil)
launcher := fakeBusObject(func(method string, args ...interface{}) error {
c.Check(method, Equals, "io.snapcraft.Launcher.OpenFile")
c.Assert(args, HasLen, 2)
c.Check(args[0], Equals, "")
c.Check(fdMatchesFile(int(args[1].(dbus.UnixFD)), path), IsNil)
return nil
})
u := url.URL{Scheme: "file", Path: path}
c.Check(xdgopenproxy.Launch(launcher, u.String()), IsNil)
}
func (s *xdgOpenSuite) TestOpenDir(c *C) {
dir := c.MkDir()
launcher := fakeBusObject(func(method string, args ...interface{}) error {
c.Check(method, Equals, "io.snapcraft.Launcher.OpenFile")
c.Assert(args, HasLen, 2)
c.Check(args[0], Equals, "")
c.Check(fdMatchesFile(int(args[1].(dbus.UnixFD)), dir), IsNil)
return nil
})
c.Check(xdgopenproxy.Launch(launcher, dir), IsNil)
}
func (s *xdgOpenSuite) TestOpenMissingFile(c *C) {
path := filepath.Join(c.MkDir(), "no-such-file.txt")
launcher := fakeBusObject(func(method string, args ...interface{}) error {
c.Error("unexpected D-Bus call")
return nil
})
c.Check(xdgopenproxy.Launch(launcher, path), ErrorMatches, "no such file or directory")
}
func (s *xdgOpenSuite) TestOpenUnreadableFile(c *C) {
if os.Getuid() == 0 {
c.Skip("test will not work for root")
}
path := filepath.Join(c.MkDir(), "test.txt")
c.Assert(ioutil.WriteFile(path, []byte("Hello world"), 0644), IsNil)
c.Assert(os.Chmod(path, 0), IsNil)
launcher := fakeBusObject(func(method string, args ...interface{}) error {
c.Error("unexpected D-Bus call")
return nil
})
c.Check(xdgopenproxy.Launch(launcher, path), ErrorMatches, "permission denied")
}
func fdMatchesFile(fd int, filename string) error {
var fdStat, fileStat syscall.Stat_t
if err := syscall.Fstat(fd, &fdStat); err != nil {
return err
}
if err := syscall.Stat(filename, &fileStat); err != nil {
return err
}
if fdStat.Dev != fileStat.Dev || fdStat.Ino != fileStat.Ino {
return fmt.Errorf("File descriptor and fd do not match")
}
return nil
}
// fakeBusObject is a dbus.BusObject implementation that forwards
// Call invocations
type fakeBusObject func(method string, args ...interface{}) error
func (f fakeBusObject) Call(method string, flags dbus.Flags, args ...interface{}) *dbus.Call {
err := f(method, args...)
return &dbus.Call{Err: err}
}
func (f fakeBusObject) CallWithContext(ctx context.Context, method string, flags dbus.Flags, args ...interface{}) *dbus.Call {
err := f(method, args...)
return &dbus.Call{Err: err}
}
func (f fakeBusObject) Go(method string, flags dbus.Flags, ch chan *dbus.Call, args ...interface{}) *dbus.Call {
return nil
}
func (f fakeBusObject) GoWithContext(ctx context.Context, method string, flags dbus.Flags, ch chan *dbus.Call, args ...interface{}) *dbus.Call {
return nil
}
func (f fakeBusObject) AddMatchSignal(iface, member string, options ...dbus.MatchOption) *dbus.Call {
return nil
}
func (f fakeBusObject) RemoveMatchSignal(iface, member string, options ...dbus.MatchOption) *dbus.Call {
return nil
}
func (f fakeBusObject) GetProperty(prop string) (dbus.Variant, error) {
return dbus.Variant{}, nil
}
func (f fakeBusObject) SetProperty(p string, v interface{}) error {
return nil
}
func (f fakeBusObject) Destination() string {
return ""
}
func (f fakeBusObject) Path() dbus.ObjectPath {
return ""
}