| 1 |
/* |
| 2 |
Copyright 2013-2015 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 connectivity |
| 18 |
|
| 19 |
import ( |
| 20 |
"net/http/httptest" |
| 21 |
"sync" |
| 22 |
"testing" |
| 23 |
"time" |
| 24 |
|
| 25 |
"github.com/godbus/dbus/v5" |
| 26 |
. "gopkg.in/check.v1" |
| 27 |
|
| 28 |
"gitlab.com/ubports/development/core/lomiri-push-service/bus" |
| 29 |
"gitlab.com/ubports/development/core/lomiri-push-service/bus/networkmanager" |
| 30 |
testingbus "gitlab.com/ubports/development/core/lomiri-push-service/bus/testing" |
| 31 |
"gitlab.com/ubports/development/core/lomiri-push-service/config" |
| 32 |
"gitlab.com/ubports/development/core/lomiri-push-service/logger" |
| 33 |
helpers "gitlab.com/ubports/development/core/lomiri-push-service/testing" |
| 34 |
"gitlab.com/ubports/development/core/lomiri-push-service/testing/condition" |
| 35 |
"gitlab.com/ubports/development/core/lomiri-push-service/util" |
| 36 |
) |
| 37 |
|
| 38 |
// hook up gocheck |
| 39 |
func Test(t *testing.T) { TestingT(t) } |
| 40 |
|
| 41 |
type ConnSuite struct { |
| 42 |
timeouts []time.Duration |
| 43 |
log logger.Logger |
| 44 |
} |
| 45 |
|
| 46 |
var _ = Suite(&ConnSuite{}) |
| 47 |
|
| 48 |
func (s *ConnSuite) SetUpSuite(c *C) { |
| 49 |
s.timeouts = util.SwapTimeouts([]time.Duration{0, 0, 0, 0}) |
| 50 |
} |
| 51 |
|
| 52 |
func (s *ConnSuite) TearDownSuite(c *C) { |
| 53 |
util.SwapTimeouts(s.timeouts) |
| 54 |
s.timeouts = nil |
| 55 |
} |
| 56 |
|
| 57 |
func (s *ConnSuite) SetUpTest(c *C) { |
| 58 |
s.log = helpers.NewTestLogger(c, "debug") |
| 59 |
} |
| 60 |
|
| 61 |
var ( |
| 62 |
helloCon = dbus.ObjectPath("hello") |
| 63 |
helloConProps = map[string]dbus.Variant{"PrimaryConnection": dbus.MakeVariant(helloCon)} |
| 64 |
) |
| 65 |
|
| 66 |
/* |
| 67 |
tests for ConnectedState's Start() method |
| 68 |
*/ |
| 69 |
|
| 70 |
// when given a working config and bus, Start() will work |
| 71 |
func (s *ConnSuite) TestStartWorks(c *C) { |
| 72 |
endp := testingbus.NewTestingEndpoint(condition.Work(true), condition.Work(true), uint32(networkmanager.Connecting), helloCon) |
| 73 |
cs := ConnectedState{config: ConnectivityConfig{}, log: s.log, endp: endp} |
| 74 |
|
| 75 |
nopTicker := make(chan []interface{}) |
| 76 |
testingbus.SetWatchSource(endp, "StateChanged", nopTicker) |
| 77 |
testingbus.SetWatchSource(endp, "PropertiesChanged", nopTicker) |
| 78 |
defer close(nopTicker) |
| 79 |
|
| 80 |
c.Check(cs.start(), Equals, networkmanager.Connecting) |
| 81 |
} |
| 82 |
|
| 83 |
// if the bus fails a couple of times, we're still OK |
| 84 |
func (s *ConnSuite) TestStartRetriesConnect(c *C) { |
| 85 |
endp := testingbus.NewTestingEndpoint(condition.Fail2Work(2), condition.Work(true), uint32(networkmanager.Connecting), helloCon) |
| 86 |
cs := ConnectedState{config: ConnectivityConfig{}, log: s.log, endp: endp} |
| 87 |
|
| 88 |
nopTicker := make(chan []interface{}) |
| 89 |
testingbus.SetWatchSource(endp, "StateChanged", nopTicker) |
| 90 |
testingbus.SetWatchSource(endp, "PropertiesChanged", nopTicker) |
| 91 |
defer close(nopTicker) |
| 92 |
|
| 93 |
c.Check(cs.start(), Equals, networkmanager.Connecting) |
| 94 |
c.Check(cs.connAttempts, Equals, uint32(3)) // 1 more than the Fail2Work |
| 95 |
} |
| 96 |
|
| 97 |
// when the calls to NetworkManager fails for a bit, we're still OK |
| 98 |
func (s *ConnSuite) TestStartRetriesCall(c *C) { |
| 99 |
endp := testingbus.NewTestingEndpoint(condition.Work(true), condition.Fail2Work(5), uint32(networkmanager.Connecting), helloCon) |
| 100 |
cs := ConnectedState{config: ConnectivityConfig{}, log: s.log, endp: endp} |
| 101 |
|
| 102 |
nopTicker := make(chan []interface{}) |
| 103 |
testingbus.SetWatchSource(endp, "StateChanged", nopTicker) |
| 104 |
testingbus.SetWatchSource(endp, "PropertiesChanged", nopTicker) |
| 105 |
defer close(nopTicker) |
| 106 |
|
| 107 |
c.Check(cs.start(), Equals, networkmanager.Connecting) |
| 108 |
|
| 109 |
c.Check(cs.connAttempts, Equals, uint32(6)) |
| 110 |
} |
| 111 |
|
| 112 |
// when some of the calls to NetworkManager fails for a bit, we're still OK |
| 113 |
func (s *ConnSuite) TestStartRetriesCall2(c *C) { |
| 114 |
cond := condition.Chain(1, condition.Work(true), 1, condition.Work(false), |
| 115 |
1, condition.Work(true)) |
| 116 |
|
| 117 |
endp := testingbus.NewTestingEndpoint(condition.Work(true), cond, |
| 118 |
uint32(networkmanager.Connecting), helloCon, |
| 119 |
uint32(networkmanager.Connecting), helloCon, |
| 120 |
) |
| 121 |
cs := ConnectedState{config: ConnectivityConfig{}, log: s.log, endp: endp} |
| 122 |
|
| 123 |
nopTicker := make(chan []interface{}) |
| 124 |
testingbus.SetWatchSource(endp, "StateChanged", nopTicker) |
| 125 |
testingbus.SetWatchSource(endp, "PropertiesChanged", nopTicker) |
| 126 |
defer close(nopTicker) |
| 127 |
|
| 128 |
c.Check(cs.start(), Equals, networkmanager.Connecting) |
| 129 |
} |
| 130 |
|
| 131 |
// when... and bear with me... the bus works, and the first call to |
| 132 |
// get network manager's state works, but then you can't establish the |
| 133 |
// watch, we recover and try again. |
| 134 |
func (s *ConnSuite) TestStartRetriesWatch(c *C) { |
| 135 |
nmcond := condition.Chain( |
| 136 |
2, condition.Work(true), // 2 call to nm works |
| 137 |
1, condition.Work(false), // 1 call to nm fails |
| 138 |
0, condition.Work(true)) // and everything works from there on |
| 139 |
endp := testingbus.NewTestingEndpoint(condition.Work(true), nmcond, |
| 140 |
uint32(networkmanager.Connecting), |
| 141 |
uint32(networkmanager.Connecting), |
| 142 |
helloCon, |
| 143 |
) |
| 144 |
cs := ConnectedState{config: ConnectivityConfig{}, log: s.log, endp: endp} |
| 145 |
watchTicker := make(chan []interface{}, 1) |
| 146 |
nopTicker := make(chan []interface{}) |
| 147 |
testingbus.SetWatchSource(endp, "StateChanged", watchTicker) |
| 148 |
testingbus.SetWatchSource(endp, "PropertiesChanged", nopTicker) |
| 149 |
defer close(nopTicker) |
| 150 |
defer close(watchTicker) |
| 151 |
|
| 152 |
c.Check(cs.start(), Equals, networkmanager.Connecting) |
| 153 |
c.Check(cs.connAttempts, Equals, uint32(2)) |
| 154 |
watchTicker <- []interface{}{uint32(networkmanager.ConnectedGlobal)} |
| 155 |
c.Check(<-cs.networkStateCh, Equals, networkmanager.ConnectedGlobal) |
| 156 |
} |
| 157 |
|
| 158 |
// a racyEndpoint is an endpoint that behaves differently depending on |
| 159 |
// how much time passes between getting the state and setting up the |
| 160 |
// watch |
| 161 |
type racyEndpoint struct { |
| 162 |
stateGot bool |
| 163 |
maxTime time.Time |
| 164 |
delta time.Duration |
| 165 |
lock sync.RWMutex |
| 166 |
} |
| 167 |
|
| 168 |
func (rep *racyEndpoint) GetProperty(prop string) (interface{}, error) { |
| 169 |
switch prop { |
| 170 |
case "State": |
| 171 |
rep.lock.Lock() |
| 172 |
defer rep.lock.Unlock() |
| 173 |
rep.stateGot = true |
| 174 |
rep.maxTime = time.Now().Add(rep.delta) |
| 175 |
return uint32(networkmanager.Connecting), nil |
| 176 |
case "PrimaryConnection": |
| 177 |
return dbus.ObjectPath("/something"), nil |
| 178 |
default: |
| 179 |
return nil, nil |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
func (rep *racyEndpoint) WatchSignal(member string, f func(...interface{}), d func()) (bus.Cancellable, error) { |
| 184 |
if member == "StateChanged" { |
| 185 |
// we count never having gotten the state as happening "after" now. |
| 186 |
rep.lock.RLock() |
| 187 |
defer rep.lock.RUnlock() |
| 188 |
ok := !rep.stateGot || time.Now().Before(rep.maxTime) |
| 189 |
go func() { |
| 190 |
if ok { |
| 191 |
f(uint32(networkmanager.ConnectedGlobal)) |
| 192 |
} |
| 193 |
d() |
| 194 |
}() |
| 195 |
} |
| 196 |
return nil, nil |
| 197 |
} |
| 198 |
|
| 199 |
func (rep *racyEndpoint) WatchProperties(f func(map[string]dbus.Variant, []string), d func()) (bus.Cancellable, error) { |
| 200 |
return nil, nil |
| 201 |
} |
| 202 |
|
| 203 |
func (*racyEndpoint) Close() {} |
| 204 |
func (*racyEndpoint) Dial() error { return nil } |
| 205 |
func (*racyEndpoint) String() string { return "racyEndpoint" } |
| 206 |
func (*racyEndpoint) Call(string, []interface{}, ...interface{}) error { return nil } |
| 207 |
func (*racyEndpoint) GrabName(bool) <-chan error { return nil } |
| 208 |
func (*racyEndpoint) WatchMethod(bus.DispatchMap, string) error { return nil } |
| 209 |
func (*racyEndpoint) Signal(member string, suffix string, args []interface{}) error { return nil } |
| 210 |
func (*racyEndpoint) SetProperty(string, string, interface{}) error { return nil } |
| 211 |
|
| 212 |
var _ bus.Endpoint = (*racyEndpoint)(nil) |
| 213 |
|
| 214 |
// takeNext takes a value from given channel with a 1s timeout |
| 215 |
func takeNext(ch <-chan networkmanager.State) networkmanager.State { |
| 216 |
select { |
| 217 |
case <-time.After(time.Second): |
| 218 |
panic("channel stuck: too long waiting") |
| 219 |
case v := <-ch: |
| 220 |
return v |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
// test that if the nm state goes from connecting to connected very |
| 225 |
// shortly after calling GetState, we don't lose the event. |
| 226 |
func (s *ConnSuite) TestStartAvoidsRace(c *C) { |
| 227 |
for delta := time.Second; delta > 1; delta /= 2 { |
| 228 |
rep := &racyEndpoint{delta: delta} |
| 229 |
cs := ConnectedState{config: ConnectivityConfig{}, log: s.log, endp: rep} |
| 230 |
f := Commentf("when delta=%s", delta) |
| 231 |
c.Assert(cs.start(), Equals, networkmanager.Connecting, f) |
| 232 |
c.Assert(takeNext(cs.networkStateCh), Equals, networkmanager.ConnectedGlobal, f) |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
/* |
| 237 |
tests for step() |
| 238 |
*/ |
| 239 |
|
| 240 |
type testWebchk func(ch chan<- bool) |
| 241 |
|
| 242 |
func (x testWebchk) Webcheck(ch chan<- bool) { |
| 243 |
x(ch) |
| 244 |
} |
| 245 |
|
| 246 |
func (x testWebchk) Close() { |
| 247 |
} |
| 248 |
|
| 249 |
func (s *ConnSuite) TestSteps(c *C) { |
| 250 |
var webget_p condition.Interface = condition.Work(true) |
| 251 |
recheck_timeout := 50 * time.Millisecond |
| 252 |
|
| 253 |
cfg := ConnectivityConfig{ |
| 254 |
RecheckTimeout: config.ConfigTimeDuration{recheck_timeout}, |
| 255 |
} |
| 256 |
ch := make(chan networkmanager.State, 10) |
| 257 |
cs := &ConnectedState{ |
| 258 |
config: cfg, |
| 259 |
networkStateCh: ch, |
| 260 |
timer: time.NewTimer(time.Second), |
| 261 |
log: s.log, |
| 262 |
webchk: testWebchk(func(ch chan<- bool) { ch <- webget_p.OK() }), |
| 263 |
lastSent: false, |
| 264 |
} |
| 265 |
ch <- networkmanager.ConnectedGlobal |
| 266 |
f, e := cs.step() |
| 267 |
c.Check(e, IsNil) |
| 268 |
c.Check(f, Equals, true) |
| 269 |
ch <- networkmanager.Disconnected |
| 270 |
ch <- networkmanager.ConnectedGlobal |
| 271 |
f, e = cs.step() |
| 272 |
c.Check(e, IsNil) |
| 273 |
c.Check(f, Equals, false) |
| 274 |
f, e = cs.step() |
| 275 |
c.Check(e, IsNil) |
| 276 |
c.Check(f, Equals, true) |
| 277 |
|
| 278 |
// same scenario, but with failing web check |
| 279 |
webget_p = condition.Fail2Work(1) |
| 280 |
ch <- networkmanager.Disconnected |
| 281 |
ch <- networkmanager.ConnectedGlobal |
| 282 |
f, e = cs.step() |
| 283 |
c.Check(e, IsNil) |
| 284 |
c.Check(f, Equals, false) // first false is from the Disconnected |
| 285 |
|
| 286 |
// the next call to Step will time out |
| 287 |
_ch := make(chan bool, 1) |
| 288 |
_t := time.NewTimer(recheck_timeout / 2) |
| 289 |
|
| 290 |
go func() { |
| 291 |
f, e := cs.step() |
| 292 |
c.Check(e, IsNil) |
| 293 |
_ch <- f |
| 294 |
}() |
| 295 |
|
| 296 |
select { |
| 297 |
case <-_ch: |
| 298 |
c.Fatal("test failed to timeout") |
| 299 |
case <-_t.C: |
| 300 |
} |
| 301 |
|
| 302 |
// now an recheckTimeout later, we'll get true |
| 303 |
c.Check(<-_ch, Equals, true) |
| 304 |
|
| 305 |
ch <- networkmanager.Disconnected // this should trigger a 'false' |
| 306 |
ch <- networkmanager.Disconnected // this should not |
| 307 |
ch <- networkmanager.ConnectedGlobal // this should trigger a 'true' |
| 308 |
|
| 309 |
f, e = cs.step() |
| 310 |
c.Check(e, IsNil) |
| 311 |
c.Check(f, Equals, false) |
| 312 |
f, e = cs.step() |
| 313 |
c.Check(e, IsNil) |
| 314 |
c.Check(f, Equals, true) |
| 315 |
|
| 316 |
close(ch) // this should make it error out |
| 317 |
_, e = cs.step() |
| 318 |
c.Check(e, NotNil) |
| 319 |
} |
| 320 |
|
| 321 |
/* |
| 322 |
tests for ConnectedState() |
| 323 |
*/ |
| 324 |
|
| 325 |
// yes, this is an integration test |
| 326 |
func (s *ConnSuite) TestRun(c *C) { |
| 327 |
ts := httptest.NewServer(mkHandler(staticText)) |
| 328 |
defer ts.Close() |
| 329 |
|
| 330 |
cfg := ConnectivityConfig{ |
| 331 |
ConnectivityCheckURL: ts.URL, |
| 332 |
ConnectivityCheckMD5: staticHash, |
| 333 |
RecheckTimeout: config.ConfigTimeDuration{time.Second}, |
| 334 |
} |
| 335 |
|
| 336 |
endp := testingbus.NewTestingEndpoint(condition.Work(true), condition.Work(true), |
| 337 |
uint32(networkmanager.Disconnected), |
| 338 |
helloCon, |
| 339 |
uint32(networkmanager.Disconnected), |
| 340 |
helloCon, |
| 341 |
) |
| 342 |
|
| 343 |
watchTicker := make(chan []interface{}) |
| 344 |
testingbus.SetWatchSource(endp, "StateChanged", watchTicker) |
| 345 |
nopTicker := make(chan []interface{}) |
| 346 |
testingbus.SetWatchSource(endp, "PropertiesChanged", nopTicker) |
| 347 |
|
| 348 |
out := make(chan bool) |
| 349 |
dt := time.Second / 10 |
| 350 |
timer := time.NewTimer(dt) |
| 351 |
cs := New(endp, cfg, s.log) |
| 352 |
defer cs.Cancel() |
| 353 |
go cs.Track(out) |
| 354 |
var v bool |
| 355 |
expecteds := []struct { |
| 356 |
p bool |
| 357 |
s string |
| 358 |
todo string |
| 359 |
}{ |
| 360 |
{false, "first state is always false", ""}, |
| 361 |
{true, "then it should be true as per ConnectedGlobal above", "ConnectedGlobal"}, |
| 362 |
{false, "then it should be false (Disconnected)", "Disconnected"}, |
| 363 |
{false, "then it should be false again because it's restarted", "close"}, |
| 364 |
} |
| 365 |
|
| 366 |
defer func() { |
| 367 |
if watchTicker != nil { |
| 368 |
close(watchTicker) |
| 369 |
} |
| 370 |
}() |
| 371 |
defer close(nopTicker) |
| 372 |
for i, expected := range expecteds { |
| 373 |
switch expected.todo { |
| 374 |
case "ConnectedGlobal": |
| 375 |
watchTicker <- []interface{}{uint32(networkmanager.ConnectedGlobal)} |
| 376 |
case "Disconnected": |
| 377 |
watchTicker <- []interface{}{uint32(networkmanager.Disconnected)} |
| 378 |
case "close": |
| 379 |
close(watchTicker) |
| 380 |
watchTicker = nil |
| 381 |
} |
| 382 |
timer.Reset(dt) |
| 383 |
select { |
| 384 |
case v = <-out: |
| 385 |
break |
| 386 |
case <-timer.C: |
| 387 |
c.Fatalf("Timed out before getting value (#%d: %s)", i+1, expected.s) |
| 388 |
} |
| 389 |
c.Assert(v, Equals, expected.p, Commentf(expected.s)) |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
func (s *ConnSuite) TestRun4Active(c *C) { |
| 394 |
ts := httptest.NewServer(mkHandler(staticText)) |
| 395 |
defer ts.Close() |
| 396 |
|
| 397 |
cfg := ConnectivityConfig{ |
| 398 |
ConnectivityCheckURL: ts.URL, |
| 399 |
ConnectivityCheckMD5: staticHash, |
| 400 |
RecheckTimeout: config.ConfigTimeDuration{time.Second}, |
| 401 |
} |
| 402 |
|
| 403 |
endp := testingbus.NewTestingEndpoint(condition.Work(true), condition.Work(true), |
| 404 |
uint32(networkmanager.ConnectedGlobal), |
| 405 |
helloCon, |
| 406 |
) |
| 407 |
|
| 408 |
watchTicker := make(chan []interface{}) |
| 409 |
testingbus.SetWatchSource(endp, "PropertiesChanged", watchTicker) |
| 410 |
nopTicker := make(chan []interface{}) |
| 411 |
testingbus.SetWatchSource(endp, "StateChanged", nopTicker) |
| 412 |
|
| 413 |
out := make(chan bool) |
| 414 |
dt := time.Second / 10 |
| 415 |
timer := time.NewTimer(dt) |
| 416 |
cs := New(endp, cfg, s.log) |
| 417 |
defer cs.Cancel() |
| 418 |
go cs.Track(out) |
| 419 |
var v bool |
| 420 |
expecteds := []struct { |
| 421 |
p bool |
| 422 |
s string |
| 423 |
changedConn bool |
| 424 |
}{ |
| 425 |
{false, "first state is always false", false}, |
| 426 |
{true, "then it should be true as per ConnectedGlobal above", false}, |
| 427 |
{false, "then, false (PrimaryConnection changed)", true}, |
| 428 |
{true, "then it should be true (webcheck passed)", false}, |
| 429 |
} |
| 430 |
|
| 431 |
defer func() { |
| 432 |
if watchTicker != nil { |
| 433 |
close(watchTicker) |
| 434 |
} |
| 435 |
}() |
| 436 |
defer close(nopTicker) |
| 437 |
for i, expected := range expecteds { |
| 438 |
if expected.changedConn { |
| 439 |
watchTicker <- []interface{}{helloConProps, []string{}} |
| 440 |
} |
| 441 |
timer.Reset(dt) |
| 442 |
select { |
| 443 |
case v = <-out: |
| 444 |
break |
| 445 |
case <-timer.C: |
| 446 |
c.Fatalf("Timed out before getting value (#%d: %s)", i+1, expected.s) |
| 447 |
} |
| 448 |
c.Assert(v, Equals, expected.p, Commentf(expected.s)) |
| 449 |
} |
| 450 |
} |