| 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 testing |
| 18 |
|
| 19 |
import ( |
| 20 |
"testing" |
| 21 |
"time" |
| 22 |
|
| 23 |
"github.com/godbus/dbus/v5" |
| 24 |
. "gopkg.in/check.v1" |
| 25 |
|
| 26 |
"gitlab.com/ubports/development/core/lomiri-push-service/bus" |
| 27 |
"gitlab.com/ubports/development/core/lomiri-push-service/testing/condition" |
| 28 |
) |
| 29 |
|
| 30 |
// hook up gocheck |
| 31 |
func Test(t *testing.T) { TestingT(t) } |
| 32 |
|
| 33 |
type TestingEndpointSuite struct{} |
| 34 |
|
| 35 |
var _ = Suite(&TestingEndpointSuite{}) |
| 36 |
|
| 37 |
// Test that Call() with a positive condition returns the first return value |
| 38 |
// provided, as advertised. |
| 39 |
func (s *TestingEndpointSuite) TestCallReturnsFirstRetval(c *C) { |
| 40 |
var m, n uint32 = 42, 17 |
| 41 |
endp := NewTestingEndpoint(nil, condition.Work(true), m, n) |
| 42 |
var r uint32 |
| 43 |
e := endp.Call("what", bus.Args(), &r) |
| 44 |
c.Check(e, IsNil) |
| 45 |
c.Check(r, Equals, m) |
| 46 |
} |
| 47 |
|
| 48 |
// Test the same Call() but with multi-valued endpoint |
| 49 |
func (s *TestingEndpointSuite) TestMultiValuedCall(c *C) { |
| 50 |
var m, n uint32 = 42, 17 |
| 51 |
endp := NewMultiValuedTestingEndpoint(nil, condition.Work(true), []interface{}{m}, []interface{}{n}) |
| 52 |
var r uint32 |
| 53 |
e := endp.Call("what", bus.Args(), &r) |
| 54 |
c.Check(e, IsNil) |
| 55 |
c.Check(r, Equals, m) |
| 56 |
} |
| 57 |
|
| 58 |
// Test that Call() with a negative condition returns an error. |
| 59 |
func (s *TestingEndpointSuite) TestCallFails(c *C) { |
| 60 |
endp := NewTestingEndpoint(nil, condition.Work(false)) |
| 61 |
e := endp.Call("what", bus.Args()) |
| 62 |
c.Check(e, NotNil) |
| 63 |
} |
| 64 |
|
| 65 |
// Test that Call() with a positive condition and no return values panics with |
| 66 |
// a helpful message. |
| 67 |
func (s *TestingEndpointSuite) TestCallPanicsWithNiceMessage(c *C) { |
| 68 |
endp := NewTestingEndpoint(nil, condition.Work(true)) |
| 69 |
var x int32 |
| 70 |
c.Check(func() { endp.Call("", bus.Args(), &x) }, PanicMatches, "No return values provided.*") |
| 71 |
} |
| 72 |
|
| 73 |
// Test that Call() updates callArgs |
| 74 |
func (s *TestingEndpointSuite) TestCallArgs(c *C) { |
| 75 |
endp := NewTestingEndpoint(nil, condition.Work(true)) |
| 76 |
err := endp.Call("what", bus.Args("is", "this", "thing")) |
| 77 |
c.Assert(err, IsNil) |
| 78 |
c.Check(GetCallArgs(endp), DeepEquals, |
| 79 |
[]callArgs{{"what", []interface{}{"is", "this", "thing"}}}) |
| 80 |
} |
| 81 |
|
| 82 |
// Test that Call() fails but does not explode when asked to return values |
| 83 |
// that can't be packed into a dbus message. |
| 84 |
func (s *TestingEndpointSuite) TestCallFailsOnBadRetval(c *C) { |
| 85 |
endp := NewTestingEndpoint(nil, condition.Work(true), Equals) |
| 86 |
var r uint32 |
| 87 |
e := endp.Call("what", bus.Args(), &r) |
| 88 |
c.Check(e, NotNil) |
| 89 |
} |
| 90 |
|
| 91 |
// Test that Call() fails but does not explode when given an improper result |
| 92 |
// destination (one into which the dbus response can't be stuffed). |
| 93 |
func (s *TestingEndpointSuite) TestCallFailsOnBadArg(c *C) { |
| 94 |
endp := NewTestingEndpoint(nil, condition.Work(true), 1) |
| 95 |
r := func() {} |
| 96 |
e := endp.Call("what", bus.Args(), &r) |
| 97 |
c.Check(e, NotNil) |
| 98 |
} |
| 99 |
|
| 100 |
// Test that WatchSignal() with a positive condition sends the provided return |
| 101 |
// values over the channel. |
| 102 |
func (s *TestingEndpointSuite) TestWatch(c *C) { |
| 103 |
var m, n uint32 = 42, 17 |
| 104 |
endp := NewTestingEndpoint(nil, condition.Work(true), m, n) |
| 105 |
ch := make(chan uint32) |
| 106 |
w, e := endp.WatchSignal("which", func(us ...interface{}) { ch <- us[0].(uint32) }, func() { close(ch) }) |
| 107 |
c.Assert(e, IsNil) |
| 108 |
defer w.Cancel() |
| 109 |
c.Check(<-ch, Equals, m) |
| 110 |
c.Check(<-ch, Equals, n) |
| 111 |
} |
| 112 |
|
| 113 |
// Test that WatchSignal() calls the destructor callback when it runs out values |
| 114 |
func (s *TestingEndpointSuite) TestWatchDestructor(c *C) { |
| 115 |
endp := NewTestingEndpoint(nil, condition.Work(true)) |
| 116 |
ch := make(chan uint32) |
| 117 |
w, e := endp.WatchSignal("what", func(us ...interface{}) {}, func() { close(ch) }) |
| 118 |
c.Assert(e, IsNil) |
| 119 |
defer w.Cancel() |
| 120 |
_, ok := <-ch |
| 121 |
c.Check(ok, Equals, false) |
| 122 |
} |
| 123 |
|
| 124 |
// Test the endpoint can be closed |
| 125 |
func (s *TestingEndpointSuite) TestCloser(c *C) { |
| 126 |
endp := NewTestingEndpoint(nil, condition.Work(true)) |
| 127 |
endp.Close() |
| 128 |
c.Check(GetCallArgs(endp), DeepEquals, []callArgs{ |
| 129 |
{ |
| 130 |
Member: "::Close", |
| 131 |
Args: nil, |
| 132 |
}}) |
| 133 |
} |
| 134 |
|
| 135 |
// Test that WatchSignal() with a negative condition returns an error. |
| 136 |
func (s *TestingEndpointSuite) TestWatchFails(c *C) { |
| 137 |
endp := NewTestingEndpoint(nil, condition.Work(false)) |
| 138 |
w, e := endp.WatchSignal("what", func(us ...interface{}) {}, func() {}) |
| 139 |
c.Check(e, NotNil) |
| 140 |
c.Check(w, IsNil) |
| 141 |
} |
| 142 |
|
| 143 |
// Test WatchSignal can use a watchSource instead of a timeout and retvals (if |
| 144 |
// the former is not nil) |
| 145 |
func (s *TestingEndpointSuite) TestWatchSources(c *C) { |
| 146 |
watchTicker := make(chan []interface{}, 3) |
| 147 |
watchTicker <- []interface{}{true} |
| 148 |
watchTicker <- []interface{}{true} |
| 149 |
watchTicker <- []interface{}{true} |
| 150 |
c.Assert(len(watchTicker), Equals, 3) |
| 151 |
|
| 152 |
endp := NewTestingEndpoint(nil, condition.Work(true), 0, 0) |
| 153 |
SetWatchSource(endp, "what", watchTicker) |
| 154 |
ch := make(chan int) |
| 155 |
w, e := endp.WatchSignal("what", func(us ...interface{}) {}, func() { close(ch) }) |
| 156 |
c.Assert(e, IsNil) |
| 157 |
defer w.Cancel() |
| 158 |
|
| 159 |
close(watchTicker) |
| 160 |
// wait for the destructor to be called |
| 161 |
select { |
| 162 |
case <-time.Tick(10 * time.Millisecond): |
| 163 |
c.Fatal("timed out waiting for close on channel") |
| 164 |
case <-ch: |
| 165 |
} |
| 166 |
|
| 167 |
// now if all went well, the ticker will have been exhausted. |
| 168 |
c.Assert(len(watchTicker), Equals, 0) |
| 169 |
} |
| 170 |
|
| 171 |
// Test that WatchSignal() calls the destructor callback when canceled. |
| 172 |
func (s *TestingEndpointSuite) TestWatchCancel(c *C) { |
| 173 |
endp := NewTestingEndpoint(nil, condition.Work(true)) |
| 174 |
ch := make(chan uint32) |
| 175 |
w, e := endp.WatchSignal("what", func(us ...interface{}) {}, func() { close(ch) }) |
| 176 |
c.Assert(e, IsNil) |
| 177 |
defer w.Cancel() |
| 178 |
SetWatchSource(endp, "what", make(chan []interface{})) |
| 179 |
w.Cancel() |
| 180 |
_, ok := <-ch |
| 181 |
c.Check(ok, Equals, false) |
| 182 |
} |
| 183 |
|
| 184 |
// Tests that GetProperty() works |
| 185 |
func (s *TestingEndpointSuite) TestGetProperty(c *C) { |
| 186 |
var m uint32 = 42 |
| 187 |
endp := NewTestingEndpoint(nil, condition.Work(true), m) |
| 188 |
v, e := endp.GetProperty("what") |
| 189 |
c.Check(e, IsNil) |
| 190 |
c.Check(v, Equals, m) |
| 191 |
} |
| 192 |
|
| 193 |
// Tests that GetProperty() fails, too |
| 194 |
func (s *TestingEndpointSuite) TestGetPropertyFails(c *C) { |
| 195 |
endp := NewTestingEndpoint(nil, condition.Work(false)) |
| 196 |
_, e := endp.GetProperty("what") |
| 197 |
c.Check(e, NotNil) |
| 198 |
} |
| 199 |
|
| 200 |
// Tests that GetProperty() also fails if it's fed garbage |
| 201 |
func (s *TestingEndpointSuite) TestGetPropertyFailsGargling(c *C) { |
| 202 |
endp := NewMultiValuedTestingEndpoint(nil, condition.Work(true), []interface{}{}) |
| 203 |
_, e := endp.GetProperty("what") |
| 204 |
c.Check(e, NotNil) |
| 205 |
} |
| 206 |
|
| 207 |
// Test that WatchProperties() with a positive condition sends the |
| 208 |
// provided return values over the channel. |
| 209 |
func (s *TestingEndpointSuite) TestWatchProperties(c *C) { |
| 210 |
var m, n int32 = 42, 17 |
| 211 |
endp := NewMultiValuedTestingEndpoint(nil, condition.Work(true), |
| 212 |
[]interface{}{map[string]dbus.Variant{"s": dbus.MakeVariant(m)}, []string{}}, |
| 213 |
[]interface{}{map[string]dbus.Variant{"s": dbus.MakeVariant(n)}, []string{}}, |
| 214 |
) |
| 215 |
ch := make(chan int32) |
| 216 |
w, e := endp.WatchProperties(func(changed map[string]dbus.Variant, nvalited []string) { |
| 217 |
var s int32 |
| 218 |
changed["s"].Store(&s) |
| 219 |
ch <- s |
| 220 |
}, func() { close(ch) }) |
| 221 |
c.Assert(e, IsNil) |
| 222 |
defer w.Cancel() |
| 223 |
c.Check(<-ch, Equals, m) |
| 224 |
c.Check(<-ch, Equals, n) |
| 225 |
} |
| 226 |
|
| 227 |
// Test Dial() with a non-working bus fails |
| 228 |
func (s *TestingBusSuite) TestDialNoWork(c *C) { |
| 229 |
endp := NewTestingEndpoint(condition.Work(false), nil) |
| 230 |
err := endp.Dial() |
| 231 |
c.Check(err, NotNil) |
| 232 |
} |
| 233 |
|
| 234 |
// Test testingEndpoints serialize, more or less |
| 235 |
func (s *TestingBusSuite) TestEndpointString(c *C) { |
| 236 |
endp := NewTestingEndpoint(condition.Fail2Work(2), nil, "hello there") |
| 237 |
c.Check(endp.String(), Matches, ".*Still Broken.*hello there.*") |
| 238 |
} |
| 239 |
|
| 240 |
// Test that GrabName updates callArgs |
| 241 |
func (s *TestingEndpointSuite) TestGrabNameUpdatesCallArgs(c *C) { |
| 242 |
endp := NewTestingEndpoint(nil, condition.Work(true)) |
| 243 |
endp.GrabName(false) |
| 244 |
endp.GrabName(true) |
| 245 |
c.Check(GetCallArgs(endp), DeepEquals, []callArgs{ |
| 246 |
{ |
| 247 |
Member: "::GrabName", |
| 248 |
Args: []interface{}{false}, |
| 249 |
}, { |
| 250 |
Member: "::GrabName", |
| 251 |
Args: []interface{}{true}, |
| 252 |
}}) |
| 253 |
} |
| 254 |
|
| 255 |
// Test that Signal updates callArgs |
| 256 |
func (s *TestingEndpointSuite) TestSignalUpdatesCallArgs(c *C) { |
| 257 |
endp := NewTestingEndpoint(nil, condition.Work(true)) |
| 258 |
endp.Signal("hello", "", []interface{}{"world"}) |
| 259 |
endp.Signal("hello", "/potato", []interface{}{"there"}) |
| 260 |
c.Check(GetCallArgs(endp), DeepEquals, []callArgs{ |
| 261 |
{ |
| 262 |
Member: "::Signal", |
| 263 |
Args: []interface{}{"hello", "", []interface{}{"world"}}, |
| 264 |
}, { |
| 265 |
Member: "::Signal", |
| 266 |
Args: []interface{}{"hello", "/potato", []interface{}{"there"}}, |
| 267 |
}}) |
| 268 |
} |
| 269 |
|
| 270 |
// Test that WatchMethod updates callArgs |
| 271 |
func (s *TestingEndpointSuite) TestWatchMethodUpdatesCallArgs(c *C) { |
| 272 |
endp := NewTestingEndpoint(nil, condition.Work(true)) |
| 273 |
foo := func(string, []interface{}, []interface{}) ([]interface{}, error) { return nil, nil } |
| 274 |
foomp := bus.DispatchMap{"foo": foo} |
| 275 |
endp.WatchMethod(foomp, "/*") |
| 276 |
c.Check(GetCallArgs(endp), DeepEquals, []callArgs{ |
| 277 |
{ |
| 278 |
Member: "::WatchMethod", |
| 279 |
Args: []interface{}{foomp}, |
| 280 |
}}) |
| 281 |
} |
| 282 |
|
| 283 |
// Test that SetProperty updates callArgs |
| 284 |
func (s *TestingEndpointSuite) TestSetPropertyUpdatesCallArgs(c *C) { |
| 285 |
endp := NewTestingEndpoint(nil, condition.Work(true)) |
| 286 |
endp.SetProperty("prop", "suffix", "value") |
| 287 |
c.Check(GetCallArgs(endp), DeepEquals, []callArgs{ |
| 288 |
{ |
| 289 |
Member: "::SetProperty", |
| 290 |
Args: []interface{}{"prop", "suffix", "value"}, |
| 291 |
}}) |
| 292 |
} |