| 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 client |
| 18 |
|
| 19 |
import ( |
| 20 |
"encoding/json" |
| 21 |
"errors" |
| 22 |
"flag" |
| 23 |
"fmt" |
| 24 |
"io/ioutil" |
| 25 |
"net/http" |
| 26 |
"net/http/httptest" |
| 27 |
"os" |
| 28 |
"path/filepath" |
| 29 |
"reflect" |
| 30 |
"testing" |
| 31 |
"time" |
| 32 |
|
| 33 |
"github.com/godbus/dbus/v5" |
| 34 |
. "gopkg.in/check.v1" |
| 35 |
|
| 36 |
"gitlab.com/ubports/development/core/lomiri-push-service/bus" |
| 37 |
"gitlab.com/ubports/development/core/lomiri-push-service/bus/networkmanager" |
| 38 |
"gitlab.com/ubports/development/core/lomiri-push-service/bus/polld" |
| 39 |
"gitlab.com/ubports/development/core/lomiri-push-service/bus/systemimage" |
| 40 |
testibus "gitlab.com/ubports/development/core/lomiri-push-service/bus/testing" |
| 41 |
"gitlab.com/ubports/development/core/lomiri-push-service/click" |
| 42 |
clickhelp "gitlab.com/ubports/development/core/lomiri-push-service/click/testing" |
| 43 |
"gitlab.com/ubports/development/core/lomiri-push-service/client/service" |
| 44 |
"gitlab.com/ubports/development/core/lomiri-push-service/client/session" |
| 45 |
"gitlab.com/ubports/development/core/lomiri-push-service/config" |
| 46 |
"gitlab.com/ubports/development/core/lomiri-push-service/identifier" |
| 47 |
idtesting "gitlab.com/ubports/development/core/lomiri-push-service/identifier/testing" |
| 48 |
"gitlab.com/ubports/development/core/lomiri-push-service/launch_helper" |
| 49 |
"gitlab.com/ubports/development/core/lomiri-push-service/poller" |
| 50 |
"gitlab.com/ubports/development/core/lomiri-push-service/protocol" |
| 51 |
helpers "gitlab.com/ubports/development/core/lomiri-push-service/testing" |
| 52 |
"gitlab.com/ubports/development/core/lomiri-push-service/testing/condition" |
| 53 |
"gitlab.com/ubports/development/core/lomiri-push-service/util" |
| 54 |
) |
| 55 |
|
| 56 |
func TestClient(t *testing.T) { TestingT(t) } |
| 57 |
|
| 58 |
// takeNext takes a value from given channel with a 5s timeout |
| 59 |
func takeNextBool(ch <-chan bool) bool { |
| 60 |
select { |
| 61 |
case <-time.After(5 * time.Second): |
| 62 |
panic("channel stuck: too long waiting") |
| 63 |
case v := <-ch: |
| 64 |
return v |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
type dumbCommon struct { |
| 69 |
startCount int |
| 70 |
stopCount int |
| 71 |
runningCount int |
| 72 |
running bool |
| 73 |
err error |
| 74 |
} |
| 75 |
|
| 76 |
func (d *dumbCommon) Start() error { |
| 77 |
d.startCount++ |
| 78 |
return d.err |
| 79 |
} |
| 80 |
func (d *dumbCommon) Stop() { |
| 81 |
d.stopCount++ |
| 82 |
} |
| 83 |
func (d *dumbCommon) IsRunning() bool { |
| 84 |
d.runningCount++ |
| 85 |
return d.running |
| 86 |
} |
| 87 |
|
| 88 |
type dumbPush struct { |
| 89 |
dumbCommon |
| 90 |
unregCount int |
| 91 |
unregArgs []string |
| 92 |
} |
| 93 |
|
| 94 |
func (d *dumbPush) Unregister(appId string) error { |
| 95 |
d.unregCount++ |
| 96 |
d.unregArgs = append(d.unregArgs, appId) |
| 97 |
return d.err |
| 98 |
} |
| 99 |
|
| 100 |
type postArgs struct { |
| 101 |
app *click.AppId |
| 102 |
nid string |
| 103 |
payload json.RawMessage |
| 104 |
} |
| 105 |
|
| 106 |
type dumbPostal struct { |
| 107 |
dumbCommon |
| 108 |
bcastCount int |
| 109 |
postCount int |
| 110 |
postArgs []postArgs |
| 111 |
} |
| 112 |
|
| 113 |
func (d *dumbPostal) Post(app *click.AppId, nid string, payload json.RawMessage) { |
| 114 |
d.postCount++ |
| 115 |
if app.Application == "lomiri-system-settings" { |
| 116 |
d.bcastCount++ |
| 117 |
} |
| 118 |
d.postArgs = append(d.postArgs, postArgs{app, nid, payload}) |
| 119 |
} |
| 120 |
|
| 121 |
var _ PostalService = (*dumbPostal)(nil) |
| 122 |
var _ PushService = (*dumbPush)(nil) |
| 123 |
|
| 124 |
type clientSuite struct { |
| 125 |
timeouts []time.Duration |
| 126 |
configPath string |
| 127 |
leveldbPath string |
| 128 |
log *helpers.TestLogger |
| 129 |
} |
| 130 |
|
| 131 |
var _ = Suite(&clientSuite{}) |
| 132 |
|
| 133 |
const ( |
| 134 |
staticText = "something ipsum dolor something" |
| 135 |
staticHash = "6155f83b471583f47c99998a472a178f" |
| 136 |
) |
| 137 |
|
| 138 |
func mkHandler(text string) http.HandlerFunc { |
| 139 |
return func(w http.ResponseWriter, r *http.Request) { |
| 140 |
w.(http.Flusher).Flush() |
| 141 |
w.Write([]byte(text)) |
| 142 |
w.(http.Flusher).Flush() |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
func (cs *clientSuite) SetUpSuite(c *C) { |
| 147 |
config.IgnoreParsedFlags = true // because configure() uses <flags> |
| 148 |
newIdentifier = func() (identifier.Id, error) { |
| 149 |
id := idtesting.Settable() |
| 150 |
id.Set("42") // must be hex of len 32 |
| 151 |
return id, nil |
| 152 |
} |
| 153 |
cs.timeouts = util.SwapTimeouts([]time.Duration{0}) |
| 154 |
cs.leveldbPath = "" |
| 155 |
} |
| 156 |
|
| 157 |
func (cs *clientSuite) TearDownSuite(c *C) { |
| 158 |
util.SwapTimeouts(cs.timeouts) |
| 159 |
cs.timeouts = nil |
| 160 |
newIdentifier = identifier.New |
| 161 |
} |
| 162 |
|
| 163 |
func (cs *clientSuite) writeTestConfig(overrides map[string]interface{}) { |
| 164 |
pem_file := helpers.SourceRelative("../server/acceptance/ssl/testing.cert") |
| 165 |
cfgMap := map[string]interface{}{ |
| 166 |
"fallback_vibration": &launch_helper.Vibration{Pattern: []uint32{1}}, |
| 167 |
"fallback_sound": "sounds/ubuntu/notifications/Blip.ogg", |
| 168 |
"connect_timeout": "7ms", |
| 169 |
"exchange_timeout": "10ms", |
| 170 |
"hosts_cache_expiry": "1h", |
| 171 |
"expect_all_repaired": "30m", |
| 172 |
"stabilizing_timeout": "0ms", |
| 173 |
"connectivity_check_url": "", |
| 174 |
"connectivity_check_md5": "", |
| 175 |
"connectivity_check_string": "", |
| 176 |
"addr": ":0", |
| 177 |
"cert_pem_file": pem_file, |
| 178 |
"recheck_timeout": "3h", |
| 179 |
"session_url": "xyzzy://", |
| 180 |
"registration_url": "reg://", |
| 181 |
"log_level": "debug", |
| 182 |
"poll_interval": "5m", |
| 183 |
"poll_settle": "20ms", |
| 184 |
"poll_net_wait": "1m", |
| 185 |
"poll_polld_wait": "3m", |
| 186 |
"poll_done_wait": "5s", |
| 187 |
"poll_busy_wait": "0s", |
| 188 |
} |
| 189 |
for k, v := range overrides { |
| 190 |
cfgMap[k] = v |
| 191 |
} |
| 192 |
cfgBlob, err := json.Marshal(cfgMap) |
| 193 |
if err != nil { |
| 194 |
panic(err) |
| 195 |
} |
| 196 |
ioutil.WriteFile(cs.configPath, cfgBlob, 0600) |
| 197 |
} |
| 198 |
|
| 199 |
func (cs *clientSuite) SetUpTest(c *C) { |
| 200 |
cs.log = helpers.NewTestLogger(c, "debug") |
| 201 |
dir := c.MkDir() |
| 202 |
cs.configPath = filepath.Join(dir, "config") |
| 203 |
|
| 204 |
cs.writeTestConfig(nil) |
| 205 |
} |
| 206 |
|
| 207 |
func (cs *clientSuite) TearDownTest(c *C) { |
| 208 |
//helpers.DumpGoroutines() |
| 209 |
} |
| 210 |
|
| 211 |
type sqlientSuite struct{ clientSuite } |
| 212 |
|
| 213 |
func (s *sqlientSuite) SetUpSuite(c *C) { |
| 214 |
s.clientSuite.SetUpSuite(c) |
| 215 |
s.leveldbPath = ":memory:" |
| 216 |
} |
| 217 |
|
| 218 |
var _ = Suite(&sqlientSuite{}) |
| 219 |
|
| 220 |
/***************************************************************** |
| 221 |
configure tests |
| 222 |
******************************************************************/ |
| 223 |
|
| 224 |
func (cs *clientSuite) TestConfigureWorks(c *C) { |
| 225 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 226 |
err := cli.configure() |
| 227 |
c.Assert(err, IsNil) |
| 228 |
c.Assert(cli.config, NotNil) |
| 229 |
c.Check(cli.config.ExchangeTimeout.TimeDuration(), Equals, time.Duration(10*time.Millisecond)) |
| 230 |
} |
| 231 |
|
| 232 |
func (cs *clientSuite) TestConfigureWorksWithFlags(c *C) { |
| 233 |
flag.CommandLine = flag.NewFlagSet("client", flag.ContinueOnError) |
| 234 |
os.Args = []string{"client", "-addr", "foo:7777"} |
| 235 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 236 |
err := cli.configure() |
| 237 |
c.Assert(err, IsNil) |
| 238 |
c.Assert(cli.config, NotNil) |
| 239 |
c.Check(cli.config.Addr, Equals, "foo:7777") |
| 240 |
} |
| 241 |
|
| 242 |
func (cs *clientSuite) TestConfigureSetsUpLog(c *C) { |
| 243 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 244 |
c.Check(cli.log, IsNil) |
| 245 |
err := cli.configure() |
| 246 |
c.Assert(err, IsNil) |
| 247 |
c.Assert(cli.log, NotNil) |
| 248 |
} |
| 249 |
|
| 250 |
func (cs *clientSuite) TestConfigureSetsUpPEM(c *C) { |
| 251 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 252 |
c.Check(cli.pem, IsNil) |
| 253 |
err := cli.configure() |
| 254 |
c.Assert(err, IsNil) |
| 255 |
c.Assert(cli.pem, NotNil) |
| 256 |
} |
| 257 |
|
| 258 |
func (cs *clientSuite) TestConfigureSetsUpIdder(c *C) { |
| 259 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 260 |
c.Check(cli.idder, IsNil) |
| 261 |
err := cli.configure() |
| 262 |
c.Assert(err, IsNil) |
| 263 |
c.Assert(cli.idder, NotNil) |
| 264 |
} |
| 265 |
|
| 266 |
func (cs *clientSuite) TestConfigureSetsUpEndpoints(c *C) { |
| 267 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 268 |
|
| 269 |
c.Check(cli.connectivityEndp, IsNil) |
| 270 |
c.Check(cli.systemImageEndp, IsNil) |
| 271 |
err := cli.configure() |
| 272 |
c.Assert(err, IsNil) |
| 273 |
c.Check(cli.connectivityEndp, NotNil) |
| 274 |
c.Check(cli.systemImageEndp, NotNil) |
| 275 |
} |
| 276 |
|
| 277 |
func (cs *clientSuite) TestConfigureSetsUpConnCh(c *C) { |
| 278 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 279 |
c.Check(cli.connCh, IsNil) |
| 280 |
err := cli.configure() |
| 281 |
c.Assert(err, IsNil) |
| 282 |
c.Assert(cli.connCh, NotNil) |
| 283 |
} |
| 284 |
|
| 285 |
func (cs *clientSuite) TestConfigureSetsUpAddresseeChecks(c *C) { |
| 286 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 287 |
c.Check(cli.unregisterCh, IsNil) |
| 288 |
err := cli.configure() |
| 289 |
c.Assert(err, IsNil) |
| 290 |
c.Assert(cli.unregisterCh, NotNil) |
| 291 |
app, err := click.ParseAppId("com.bar.baz_foo") |
| 292 |
c.Assert(err, IsNil) |
| 293 |
c.Assert(cli.installedChecker.Installed(app, false), Equals, false) |
| 294 |
} |
| 295 |
|
| 296 |
func (cs *clientSuite) TestConfigureBailsOnBadFilename(c *C) { |
| 297 |
cli := NewPushClient("/does/not/exist", cs.leveldbPath) |
| 298 |
err := cli.configure() |
| 299 |
c.Assert(err, NotNil) |
| 300 |
} |
| 301 |
|
| 302 |
func (cs *clientSuite) TestConfigureBailsOnBadConfig(c *C) { |
| 303 |
cli := NewPushClient("/etc/passwd", cs.leveldbPath) |
| 304 |
err := cli.configure() |
| 305 |
c.Assert(err, NotNil) |
| 306 |
} |
| 307 |
|
| 308 |
func (cs *clientSuite) TestConfigureBailsOnBadPEMFilename(c *C) { |
| 309 |
cs.writeTestConfig(map[string]interface{}{ |
| 310 |
"cert_pem_file": "/a/b/c", |
| 311 |
}) |
| 312 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 313 |
err := cli.configure() |
| 314 |
c.Assert(err, ErrorMatches, "reading PEM file: .*") |
| 315 |
} |
| 316 |
|
| 317 |
func (cs *clientSuite) TestConfigureBailsOnBadPEM(c *C) { |
| 318 |
cs.writeTestConfig(map[string]interface{}{ |
| 319 |
"cert_pem_file": "/etc/passwd", |
| 320 |
}) |
| 321 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 322 |
err := cli.configure() |
| 323 |
c.Assert(err, ErrorMatches, "no PEM found.*") |
| 324 |
} |
| 325 |
|
| 326 |
func (cs *clientSuite) TestConfigureBailsOnNoHosts(c *C) { |
| 327 |
cs.writeTestConfig(map[string]interface{}{ |
| 328 |
"addr": " ", |
| 329 |
}) |
| 330 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 331 |
err := cli.configure() |
| 332 |
c.Assert(err, ErrorMatches, "no hosts specified") |
| 333 |
} |
| 334 |
|
| 335 |
func (cs *clientSuite) TestConfigureRemovesBlanksInAddr(c *C) { |
| 336 |
cs.writeTestConfig(map[string]interface{}{ |
| 337 |
"addr": " foo: 443", |
| 338 |
}) |
| 339 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 340 |
err := cli.configure() |
| 341 |
c.Assert(err, IsNil) |
| 342 |
c.Check(cli.config.Addr, Equals, "foo:443") |
| 343 |
} |
| 344 |
|
| 345 |
/***************************************************************** |
| 346 |
addresses checking tests |
| 347 |
******************************************************************/ |
| 348 |
|
| 349 |
type testInstalledChecker func(*click.AppId, bool) bool |
| 350 |
|
| 351 |
func (tic testInstalledChecker) Installed(app *click.AppId, setVersion bool) bool { |
| 352 |
return tic(app, setVersion) |
| 353 |
} |
| 354 |
|
| 355 |
var ( |
| 356 |
appId1 = "com.example.app1_app1" |
| 357 |
appId2 = "com.example.app2_app2" |
| 358 |
appIdHello = "com.example.test_hello" |
| 359 |
app1 = clickhelp.MustParseAppId(appId1) |
| 360 |
app2 = clickhelp.MustParseAppId(appId2) |
| 361 |
appHello = clickhelp.MustParseAppId(appIdHello) |
| 362 |
) |
| 363 |
|
| 364 |
func (cs *clientSuite) TestCheckForAddressee(c *C) { |
| 365 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 366 |
cli.log = cs.log |
| 367 |
cli.unregisterCh = make(chan *click.AppId, 5) |
| 368 |
cli.StartAddresseeBatch() |
| 369 |
calls := 0 |
| 370 |
cli.installedChecker = testInstalledChecker(func(app *click.AppId, setVersion bool) bool { |
| 371 |
calls++ |
| 372 |
c.Assert(setVersion, Equals, true) |
| 373 |
if app.Original() == appId1 { |
| 374 |
return false |
| 375 |
} |
| 376 |
return true |
| 377 |
}) |
| 378 |
|
| 379 |
c.Check(cli.CheckForAddressee(&protocol.Notification{AppId: "bad-id"}), IsNil) |
| 380 |
c.Check(calls, Equals, 0) |
| 381 |
c.Assert(cli.unregisterCh, HasLen, 0) |
| 382 |
c.Check(cs.log.Captured(), Matches, `DEBUG notification "" for invalid app id "bad-id".\n`) |
| 383 |
cs.log.ResetCapture() |
| 384 |
|
| 385 |
c.Check(cli.CheckForAddressee(&protocol.Notification{AppId: appId1}), IsNil) |
| 386 |
c.Check(calls, Equals, 1) |
| 387 |
c.Assert(cli.unregisterCh, HasLen, 1) |
| 388 |
c.Check(<-cli.unregisterCh, DeepEquals, app1) |
| 389 |
c.Check(cs.log.Captured(), Matches, `DEBUG notification "" for missing app id "com.example.app1_app1".\n`) |
| 390 |
cs.log.ResetCapture() |
| 391 |
|
| 392 |
c.Check(cli.CheckForAddressee(&protocol.Notification{AppId: appId2}), DeepEquals, app2) |
| 393 |
c.Check(calls, Equals, 2) |
| 394 |
c.Check(cs.log.Captured(), Matches, "") |
| 395 |
cs.log.ResetCapture() |
| 396 |
|
| 397 |
c.Check(cli.CheckForAddressee(&protocol.Notification{AppId: appId1}), IsNil) |
| 398 |
c.Check(calls, Equals, 2) |
| 399 |
c.Check(cli.CheckForAddressee(&protocol.Notification{AppId: appId2}), DeepEquals, app2) |
| 400 |
c.Check(calls, Equals, 2) |
| 401 |
c.Check(cli.unregisterCh, HasLen, 0) |
| 402 |
c.Check(cs.log.Captured(), Matches, "") |
| 403 |
} |
| 404 |
|
| 405 |
/***************************************************************** |
| 406 |
deriveSessionConfig tests |
| 407 |
******************************************************************/ |
| 408 |
|
| 409 |
func (cs *clientSuite) TestDeriveSessionConfig(c *C) { |
| 410 |
info := map[string]interface{}{ |
| 411 |
"foo": 1, |
| 412 |
} |
| 413 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 414 |
err := cli.configure() |
| 415 |
c.Assert(err, IsNil) |
| 416 |
expected := session.ClientSessionConfig{ |
| 417 |
ConnectTimeout: 7 * time.Millisecond, |
| 418 |
ExchangeTimeout: 10 * time.Millisecond, |
| 419 |
HostsCachingExpiryTime: 1 * time.Hour, |
| 420 |
ExpectAllRepairedTime: 30 * time.Minute, |
| 421 |
PEM: cli.pem, |
| 422 |
Info: info, |
| 423 |
AddresseeChecker: cli, |
| 424 |
BroadcastCh: make(chan *session.BroadcastNotification), |
| 425 |
NotificationsCh: make(chan session.AddressedNotification), |
| 426 |
} |
| 427 |
// sanity check that we are looking at all fields |
| 428 |
vExpected := reflect.ValueOf(expected) |
| 429 |
nf := vExpected.NumField() |
| 430 |
for i := 0; i < nf; i++ { |
| 431 |
fv := vExpected.Field(i) |
| 432 |
// field isn't empty/zero |
| 433 |
c.Assert(fv, Not(DeepEquals), reflect.Zero(fv.Type()), Commentf("forgot about: %s", vExpected.Type().Field(i).Name)) |
| 434 |
} |
| 435 |
// finally compare |
| 436 |
conf := cli.deriveSessionConfig(info) |
| 437 |
// channels are ok as long as non-nil |
| 438 |
conf.BroadcastCh = nil |
| 439 |
conf.NotificationsCh = nil |
| 440 |
expected.BroadcastCh = nil |
| 441 |
expected.NotificationsCh = nil |
| 442 |
// and set it to nil |
| 443 |
c.Check(conf, DeepEquals, expected) |
| 444 |
} |
| 445 |
|
| 446 |
/***************************************************************** |
| 447 |
derivePushServiceSetup tests |
| 448 |
******************************************************************/ |
| 449 |
|
| 450 |
func (cs *clientSuite) TestDerivePushServiceSetup(c *C) { |
| 451 |
cs.writeTestConfig(map[string]interface{}{}) |
| 452 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 453 |
err := cli.configure() |
| 454 |
c.Assert(err, IsNil) |
| 455 |
cli.deviceId = "zoo" |
| 456 |
expected := &service.PushServiceSetup{ |
| 457 |
DeviceId: "zoo", |
| 458 |
RegURL: helpers.ParseURL("reg://"), |
| 459 |
InstalledChecker: cli.installedChecker, |
| 460 |
} |
| 461 |
// sanity check that we are looking at all fields |
| 462 |
vExpected := reflect.ValueOf(expected).Elem() |
| 463 |
nf := vExpected.NumField() |
| 464 |
for i := 0; i < nf; i++ { |
| 465 |
fv := vExpected.Field(i) |
| 466 |
// field isn't empty/zero |
| 467 |
c.Assert(fv, Not(DeepEquals), reflect.Zero(fv.Type()), Commentf("forgot about: %s", vExpected.Type().Field(i).Name)) |
| 468 |
} |
| 469 |
// finally compare |
| 470 |
setup, err := cli.derivePushServiceSetup() |
| 471 |
c.Assert(err, IsNil) |
| 472 |
c.Check(setup, DeepEquals, expected) |
| 473 |
} |
| 474 |
|
| 475 |
func (cs *clientSuite) TestDerivePushServiceSetupError(c *C) { |
| 476 |
cs.writeTestConfig(map[string]interface{}{ |
| 477 |
"registration_url": "%gh", |
| 478 |
}) |
| 479 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 480 |
err := cli.configure() |
| 481 |
c.Assert(err, IsNil) |
| 482 |
_, err = cli.derivePushServiceSetup() |
| 483 |
c.Check(err, ErrorMatches, "cannot parse registration url:.*") |
| 484 |
} |
| 485 |
|
| 486 |
/***************************************************************** |
| 487 |
derivePostalConfig tests |
| 488 |
******************************************************************/ |
| 489 |
func (cs *clientSuite) TestDerivePostalServiceSetup(c *C) { |
| 490 |
cs.writeTestConfig(map[string]interface{}{}) |
| 491 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 492 |
err := cli.configure() |
| 493 |
c.Assert(err, IsNil) |
| 494 |
expected := &service.PostalServiceSetup{ |
| 495 |
InstalledChecker: cli.installedChecker, |
| 496 |
FallbackVibration: cli.config.FallbackVibration, |
| 497 |
FallbackSound: cli.config.FallbackSound, |
| 498 |
} |
| 499 |
// sanity check that we are looking at all fields |
| 500 |
vExpected := reflect.ValueOf(expected).Elem() |
| 501 |
nf := vExpected.NumField() |
| 502 |
for i := 0; i < nf; i++ { |
| 503 |
fv := vExpected.Field(i) |
| 504 |
// field isn't empty/zero |
| 505 |
c.Assert(fv, Not(DeepEquals), reflect.Zero(fv.Type()), Commentf("forgot about: %s", vExpected.Type().Field(i).Name)) |
| 506 |
} |
| 507 |
// finally compare |
| 508 |
setup := cli.derivePostalServiceSetup() |
| 509 |
c.Check(setup, DeepEquals, expected) |
| 510 |
} |
| 511 |
|
| 512 |
/***************************************************************** |
| 513 |
derivePollerSetup tests |
| 514 |
******************************************************************/ |
| 515 |
type derivePollerSession struct{} |
| 516 |
|
| 517 |
func (s *derivePollerSession) ResetCookie() {} |
| 518 |
func (s *derivePollerSession) State() session.ClientSessionState { return session.Unknown } |
| 519 |
func (s *derivePollerSession) HasConnectivity(bool) {} |
| 520 |
func (s *derivePollerSession) KeepConnection() error { return nil } |
| 521 |
func (s *derivePollerSession) StopKeepConnection() {} |
| 522 |
|
| 523 |
func (cs *clientSuite) TestDerivePollerSetup(c *C) { |
| 524 |
cs.writeTestConfig(map[string]interface{}{}) |
| 525 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 526 |
cli.session = new(derivePollerSession) |
| 527 |
err := cli.configure() |
| 528 |
c.Assert(err, IsNil) |
| 529 |
expected := &poller.PollerSetup{ |
| 530 |
Times: poller.Times{ |
| 531 |
AlarmInterval: 5 * time.Minute, |
| 532 |
SessionStateSettle: 20 * time.Millisecond, |
| 533 |
NetworkWait: time.Minute, |
| 534 |
PolldWait: 3 * time.Minute, |
| 535 |
DoneWait: 5 * time.Second, |
| 536 |
}, |
| 537 |
Log: cli.log, |
| 538 |
SessionStateGetter: cli.session, |
| 539 |
} |
| 540 |
// sanity check that we are looking at all fields |
| 541 |
vExpected := reflect.ValueOf(expected).Elem() |
| 542 |
nf := vExpected.NumField() |
| 543 |
for i := 0; i < nf; i++ { |
| 544 |
fv := vExpected.Field(i) |
| 545 |
// field isn't empty/zero |
| 546 |
c.Assert(fv, Not(DeepEquals), reflect.Zero(fv.Type()), Commentf("forgot about: %s", vExpected.Type().Field(i).Name)) |
| 547 |
} |
| 548 |
// finally compare |
| 549 |
setup := cli.derivePollerSetup() |
| 550 |
c.Check(setup, DeepEquals, expected) |
| 551 |
} |
| 552 |
|
| 553 |
/***************************************************************** |
| 554 |
startService tests |
| 555 |
******************************************************************/ |
| 556 |
|
| 557 |
func (cs *clientSuite) TestStartPushServiceCallsStart(c *C) { |
| 558 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 559 |
d := new(dumbPush) |
| 560 |
cli.pushService = d |
| 561 |
c.Check(cli.startPushService(), IsNil) |
| 562 |
c.Check(d.startCount, Equals, 1) |
| 563 |
} |
| 564 |
|
| 565 |
func (cs *clientSuite) TestStartPostServiceCallsStart(c *C) { |
| 566 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 567 |
d := new(dumbPostal) |
| 568 |
cli.postalService = d |
| 569 |
c.Check(cli.startPostalService(), IsNil) |
| 570 |
c.Check(d.startCount, Equals, 1) |
| 571 |
} |
| 572 |
|
| 573 |
func (cs *clientSuite) TestSetupPushServiceSetupError(c *C) { |
| 574 |
cs.writeTestConfig(map[string]interface{}{ |
| 575 |
"registration_url": "%gh", |
| 576 |
}) |
| 577 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 578 |
err := cli.configure() |
| 579 |
c.Assert(err, IsNil) |
| 580 |
err = cli.setupPushService() |
| 581 |
c.Check(err, ErrorMatches, "cannot parse registration url:.*") |
| 582 |
} |
| 583 |
|
| 584 |
func (cs *clientSuite) TestSetupPushService(c *C) { |
| 585 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 586 |
c.Assert(cli.configure(), IsNil) |
| 587 |
c.Check(cli.pushService, IsNil) |
| 588 |
c.Check(cli.setupPushService(), IsNil) |
| 589 |
c.Check(cli.pushService, NotNil) |
| 590 |
} |
| 591 |
|
| 592 |
func (cs *clientSuite) TestStartPushErrorsOnPushStartError(c *C) { |
| 593 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 594 |
d := new(dumbPush) |
| 595 |
err := errors.New("potato") |
| 596 |
d.err = err |
| 597 |
cli.pushService = d |
| 598 |
c.Check(cli.startPushService(), Equals, err) |
| 599 |
c.Check(d.startCount, Equals, 1) |
| 600 |
} |
| 601 |
|
| 602 |
func (cs *clientSuite) TestStartPostalErrorsOnPostalStartError(c *C) { |
| 603 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 604 |
d := new(dumbPostal) |
| 605 |
err := errors.New("potato") |
| 606 |
d.err = err |
| 607 |
cli.postalService = d |
| 608 |
c.Check(cli.startPostalService(), Equals, err) |
| 609 |
c.Check(d.startCount, Equals, 1) |
| 610 |
} |
| 611 |
|
| 612 |
/***************************************************************** |
| 613 |
getDeviceId tests |
| 614 |
******************************************************************/ |
| 615 |
|
| 616 |
func (cs *clientSuite) TestGetDeviceIdWorks(c *C) { |
| 617 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 618 |
cli.log = cs.log |
| 619 |
cli.idder, _ = identifier.New() |
| 620 |
c.Check(cli.deviceId, Equals, "") |
| 621 |
c.Check(cli.getDeviceId(), IsNil) |
| 622 |
c.Check(cli.deviceId, HasLen, 40) |
| 623 |
} |
| 624 |
|
| 625 |
func (cs *clientSuite) TestGetDeviceIdCanFail(c *C) { |
| 626 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 627 |
cli.log = cs.log |
| 628 |
cli.idder = idtesting.Failing() |
| 629 |
c.Check(cli.deviceId, Equals, "") |
| 630 |
c.Check(cli.getDeviceId(), NotNil) |
| 631 |
} |
| 632 |
|
| 633 |
func (cs *clientSuite) TestGetDeviceIdIdentifierDoesTheUnexpected(c *C) { |
| 634 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 635 |
cli.log = cs.log |
| 636 |
settable := idtesting.Settable() |
| 637 |
cli.idder = settable |
| 638 |
settable.Set("not-hex") |
| 639 |
c.Check(cli.deviceId, Equals, "") |
| 640 |
c.Check(cli.getDeviceId(), ErrorMatches, "machine-id should be hex: .*") |
| 641 |
} |
| 642 |
|
| 643 |
/***************************************************************** |
| 644 |
takeTheBus tests |
| 645 |
******************************************************************/ |
| 646 |
|
| 647 |
func (cs *clientSuite) TestTakeTheBusWorks(c *C) { |
| 648 |
// http server used for connectivity test |
| 649 |
ts := httptest.NewServer(mkHandler(staticText)) |
| 650 |
defer ts.Close() |
| 651 |
|
| 652 |
// testing endpoints |
| 653 |
cCond := condition.Fail2Work(7) |
| 654 |
cEndp := testibus.NewTestingEndpoint(cCond, condition.Work(true), |
| 655 |
uint32(networkmanager.Connecting), |
| 656 |
dbus.ObjectPath("hello"), |
| 657 |
uint32(networkmanager.Connecting), |
| 658 |
dbus.ObjectPath("hello"), |
| 659 |
) |
| 660 |
siCond := condition.Fail2Work(2) |
| 661 |
siEndp := testibus.NewMultiValuedTestingEndpoint(siCond, condition.Work(true), []interface{}{map[string]string{ |
| 662 |
"version_detail": "ubuntu=20160304.2,device=20160304.2,custom=20160304.2,version=381", |
| 663 |
"last_update_date": "2016-03-04 15:25:31", |
| 664 |
"last_check_date": "2016-03-08 04:30:34", |
| 665 |
"target_version_detail": "-1", |
| 666 |
"device_name": "mako", |
| 667 |
"target_build_number": "-1", |
| 668 |
"channel_name": "ubuntu-touch/rc-proposed/ubuntu", |
| 669 |
"current_build_number": "381", |
| 670 |
}}) |
| 671 |
tickerCh := make(chan []interface{}) |
| 672 |
nopTickerCh := make(chan []interface{}) |
| 673 |
testibus.SetWatchSource(cEndp, "StateChanged", tickerCh) |
| 674 |
testibus.SetWatchSource(cEndp, "PropertiesChanged", nopTickerCh) |
| 675 |
defer close(tickerCh) |
| 676 |
defer close(nopTickerCh) |
| 677 |
// ok, create the thing |
| 678 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 679 |
cli.log = cs.log |
| 680 |
err := cli.configure() |
| 681 |
c.Assert(err, IsNil) |
| 682 |
|
| 683 |
// and stomp on things for testing |
| 684 |
cli.config.ConnectivityConfig.ConnectivityCheckURL = ts.URL |
| 685 |
cli.config.ConnectivityConfig.ConnectivityCheckMD5 = staticHash |
| 686 |
cli.connectivityEndp = cEndp |
| 687 |
cli.systemImageEndp = siEndp |
| 688 |
|
| 689 |
c.Assert(cli.takeTheBus(), IsNil) |
| 690 |
|
| 691 |
c.Check(takeNextBool(cli.connCh), Equals, false) |
| 692 |
tickerCh <- []interface{}{uint32(networkmanager.ConnectedGlobal)} |
| 693 |
c.Check(takeNextBool(cli.connCh), Equals, true) |
| 694 |
// the connectivity endpoint retried until connected |
| 695 |
c.Check(cCond.OK(), Equals, true) |
| 696 |
// the systemimage endpoint retried until connected |
| 697 |
c.Check(siCond.OK(), Equals, true) |
| 698 |
} |
| 699 |
|
| 700 |
// takeTheBus can, in fact, fail |
| 701 |
func (cs *clientSuite) TestTakeTheBusCanFail(c *C) { |
| 702 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 703 |
err := cli.configure() |
| 704 |
cli.log = cs.log |
| 705 |
c.Assert(err, IsNil) |
| 706 |
|
| 707 |
// and stomp on things for testing |
| 708 |
cli.connectivityEndp = testibus.NewTestingEndpoint(condition.Work(true), condition.Work(false)) |
| 709 |
cli.systemImageEndp = testibus.NewTestingEndpoint(condition.Work(true), condition.Work(false)) |
| 710 |
|
| 711 |
c.Check(cli.takeTheBus(), NotNil) |
| 712 |
c.Assert(cli.setupPostalService(), IsNil) |
| 713 |
} |
| 714 |
|
| 715 |
/***************************************************************** |
| 716 |
seenStateFactory tests |
| 717 |
******************************************************************/ |
| 718 |
|
| 719 |
func (cs *clientSuite) TestSeenStateFactoryNoDbPath(c *C) { |
| 720 |
cli := NewPushClient(cs.configPath, "") |
| 721 |
ln, err := cli.seenStateFactory() |
| 722 |
c.Assert(err, IsNil) |
| 723 |
defer ln.Close() |
| 724 |
c.Check(fmt.Sprintf("%T", ln), Equals, "*seenstate.memSeenState") |
| 725 |
} |
| 726 |
|
| 727 |
func (cs *clientSuite) TestSeenStateFactoryWithDbPath(c *C) { |
| 728 |
cli := NewPushClient(cs.configPath, ":memory:") |
| 729 |
ln, err := cli.seenStateFactory() |
| 730 |
c.Assert(err, IsNil) |
| 731 |
defer ln.Close() |
| 732 |
c.Check(fmt.Sprintf("%T", ln), Equals, "*seenstate.sqliteSeenState") |
| 733 |
} |
| 734 |
|
| 735 |
/***************************************************************** |
| 736 |
filterBroadcastNotification tests |
| 737 |
******************************************************************/ |
| 738 |
|
| 739 |
var siInfoRes = &systemimage.InfoResult{ |
| 740 |
Device: "mako", |
| 741 |
Channel: "daily", |
| 742 |
BuildNumber: 102, |
| 743 |
LastUpdate: "Unknown", |
| 744 |
} |
| 745 |
|
| 746 |
func (cs *clientSuite) TestFilterBroadcastNotification(c *C) { |
| 747 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 748 |
cli.systemImageInfo = siInfoRes |
| 749 |
// empty |
| 750 |
msg := &session.BroadcastNotification{} |
| 751 |
c.Check(cli.filterBroadcastNotification(msg), Equals, false) |
| 752 |
// same build number, we let the helper deal |
| 753 |
msg = &session.BroadcastNotification{ |
| 754 |
Decoded: []map[string]interface{}{ |
| 755 |
map[string]interface{}{ |
| 756 |
"daily/mako": []interface{}{float64(102), "tubular"}, |
| 757 |
}, |
| 758 |
}, |
| 759 |
} |
| 760 |
c.Check(cli.filterBroadcastNotification(msg), Equals, true) |
| 761 |
} |
| 762 |
|
| 763 |
func (cs *clientSuite) TestFilterBroadcastNotificationRobust(c *C) { |
| 764 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 765 |
cli.systemImageInfo = siInfoRes |
| 766 |
msg := &session.BroadcastNotification{ |
| 767 |
Decoded: []map[string]interface{}{ |
| 768 |
map[string]interface{}{}, |
| 769 |
}, |
| 770 |
} |
| 771 |
c.Check(cli.filterBroadcastNotification(msg), Equals, false) |
| 772 |
for _, broken := range []interface{}{ |
| 773 |
5, |
| 774 |
[]interface{}{}, |
| 775 |
[]interface{}{55}, |
| 776 |
} { |
| 777 |
msg := &session.BroadcastNotification{ |
| 778 |
Decoded: []map[string]interface{}{ |
| 779 |
map[string]interface{}{ |
| 780 |
"daily/mako": broken, |
| 781 |
}, |
| 782 |
}, |
| 783 |
} |
| 784 |
c.Check(cli.filterBroadcastNotification(msg), Equals, false) |
| 785 |
} |
| 786 |
} |
| 787 |
|
| 788 |
/***************************************************************** |
| 789 |
handleBroadcastNotification tests |
| 790 |
******************************************************************/ |
| 791 |
|
| 792 |
var ( |
| 793 |
positiveBroadcastNotification = &session.BroadcastNotification{ |
| 794 |
Decoded: []map[string]interface{}{ |
| 795 |
map[string]interface{}{ |
| 796 |
"daily/mako": []interface{}{float64(102), "tubular"}, |
| 797 |
}, |
| 798 |
map[string]interface{}{ |
| 799 |
"daily/mako": []interface{}{float64(103), "tubular"}, |
| 800 |
}, |
| 801 |
}, |
| 802 |
} |
| 803 |
negativeBroadcastNotification = &session.BroadcastNotification{ |
| 804 |
Decoded: []map[string]interface{}{ |
| 805 |
map[string]interface{}{}, |
| 806 |
}, |
| 807 |
} |
| 808 |
) |
| 809 |
|
| 810 |
func (cs *clientSuite) TestHandleBroadcastNotification(c *C) { |
| 811 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 812 |
cli.systemImageInfo = siInfoRes |
| 813 |
cli.log = cs.log |
| 814 |
d := new(dumbPostal) |
| 815 |
cli.postalService = d |
| 816 |
c.Check(cli.handleBroadcastNotification(positiveBroadcastNotification), IsNil) |
| 817 |
// we dun posted |
| 818 |
c.Check(d.bcastCount, Equals, 1) |
| 819 |
c.Assert(d.postArgs, HasLen, 1) |
| 820 |
expectedApp, _ := click.ParseAppId("_lomiri-system-settings") |
| 821 |
c.Check(d.postArgs[0].app, DeepEquals, expectedApp) |
| 822 |
c.Check(d.postArgs[0].nid, Equals, "") |
| 823 |
expectedData, _ := json.Marshal(positiveBroadcastNotification.Decoded[1]) |
| 824 |
c.Check([]byte(d.postArgs[0].payload), DeepEquals, expectedData) |
| 825 |
} |
| 826 |
|
| 827 |
func (cs *clientSuite) TestHandleBroadcastNotificationNothingToDo(c *C) { |
| 828 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 829 |
cli.systemImageInfo = siInfoRes |
| 830 |
cli.log = cs.log |
| 831 |
d := new(dumbPostal) |
| 832 |
cli.postalService = d |
| 833 |
c.Check(cli.handleBroadcastNotification(negativeBroadcastNotification), IsNil) |
| 834 |
// we not dun no posted |
| 835 |
c.Check(d.bcastCount, Equals, 0) |
| 836 |
} |
| 837 |
|
| 838 |
/***************************************************************** |
| 839 |
handleUnicastNotification tests |
| 840 |
******************************************************************/ |
| 841 |
|
| 842 |
var payload = `{"message": "aGVsbG8=", "notification": {"card": {"icon": "icon-value", "summary": "summary-value", "body": "body-value", "actions": []}}}` |
| 843 |
var notif = &protocol.Notification{AppId: appIdHello, Payload: []byte(payload), MsgId: "42"} |
| 844 |
|
| 845 |
func (cs *clientSuite) TestHandleUcastNotification(c *C) { |
| 846 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 847 |
cli.log = cs.log |
| 848 |
d := new(dumbPostal) |
| 849 |
cli.postalService = d |
| 850 |
|
| 851 |
c.Check(cli.handleUnicastNotification(session.AddressedNotification{appHello, notif}), IsNil) |
| 852 |
// check we sent the notification |
| 853 |
c.Check(d.postCount, Equals, 1) |
| 854 |
c.Assert(d.postArgs, HasLen, 1) |
| 855 |
c.Check(d.postArgs[0].app, Equals, appHello) |
| 856 |
c.Check(d.postArgs[0].nid, Equals, notif.MsgId) |
| 857 |
c.Check(d.postArgs[0].payload, DeepEquals, notif.Payload) |
| 858 |
} |
| 859 |
|
| 860 |
/***************************************************************** |
| 861 |
handleUnregister tests |
| 862 |
******************************************************************/ |
| 863 |
|
| 864 |
type testPushService struct { |
| 865 |
err error |
| 866 |
unregistered string |
| 867 |
} |
| 868 |
|
| 869 |
func (ps *testPushService) Start() error { |
| 870 |
return nil |
| 871 |
} |
| 872 |
|
| 873 |
func (ps *testPushService) Unregister(appId string) error { |
| 874 |
ps.unregistered = appId |
| 875 |
return ps.err |
| 876 |
} |
| 877 |
|
| 878 |
func (cs *clientSuite) TestHandleUnregister(c *C) { |
| 879 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 880 |
cli.log = cs.log |
| 881 |
cli.installedChecker = testInstalledChecker(func(app *click.AppId, setVersion bool) bool { |
| 882 |
c.Check(setVersion, Equals, false) |
| 883 |
c.Check(app.Original(), Equals, appId1) |
| 884 |
return false |
| 885 |
}) |
| 886 |
ps := &testPushService{} |
| 887 |
cli.pushService = ps |
| 888 |
cli.handleUnregister(app1) |
| 889 |
c.Assert(ps.unregistered, Equals, appId1) |
| 890 |
c.Check(cs.log.Captured(), Equals, "DEBUG unregistered token for com.example.app1_app1\n") |
| 891 |
} |
| 892 |
|
| 893 |
func (cs *clientSuite) TestHandleUnregisterNop(c *C) { |
| 894 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 895 |
cli.log = cs.log |
| 896 |
cli.installedChecker = testInstalledChecker(func(app *click.AppId, setVersion bool) bool { |
| 897 |
c.Check(setVersion, Equals, false) |
| 898 |
c.Check(app.Original(), Equals, appId1) |
| 899 |
return true |
| 900 |
}) |
| 901 |
ps := &testPushService{} |
| 902 |
cli.pushService = ps |
| 903 |
cli.handleUnregister(app1) |
| 904 |
c.Assert(ps.unregistered, Equals, "") |
| 905 |
} |
| 906 |
|
| 907 |
func (cs *clientSuite) TestHandleUnregisterError(c *C) { |
| 908 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 909 |
cli.log = cs.log |
| 910 |
cli.installedChecker = testInstalledChecker(func(app *click.AppId, setVersion bool) bool { |
| 911 |
return false |
| 912 |
}) |
| 913 |
fail := errors.New("BAD") |
| 914 |
ps := &testPushService{err: fail} |
| 915 |
cli.pushService = ps |
| 916 |
cli.handleUnregister(app1) |
| 917 |
c.Check(cs.log.Captured(), Matches, "ERROR unregistering com.example.app1_app1: BAD\n") |
| 918 |
} |
| 919 |
|
| 920 |
/***************************************************************** |
| 921 |
doLoop tests |
| 922 |
******************************************************************/ |
| 923 |
|
| 924 |
var nopConn = func(bool) {} |
| 925 |
var nopBcast = func(*session.BroadcastNotification) error { return nil } |
| 926 |
var nopUcast = func(session.AddressedNotification) error { return nil } |
| 927 |
var nopUnregister = func(*click.AppId) {} |
| 928 |
|
| 929 |
func (cs *clientSuite) TestDoLoopConn(c *C) { |
| 930 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 931 |
cli.log = cs.log |
| 932 |
cli.systemImageInfo = siInfoRes |
| 933 |
cli.connCh = make(chan bool, 1) |
| 934 |
cli.connCh <- true |
| 935 |
c.Assert(cli.initSessionAndPoller(), IsNil) |
| 936 |
|
| 937 |
ch := make(chan bool, 1) |
| 938 |
go cli.doLoop(func(bool) { ch <- true }, nopBcast, nopUcast, nopUnregister) |
| 939 |
c.Check(takeNextBool(ch), Equals, true) |
| 940 |
} |
| 941 |
|
| 942 |
func (cs *clientSuite) TestDoLoopBroadcast(c *C) { |
| 943 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 944 |
cli.log = cs.log |
| 945 |
cli.systemImageInfo = siInfoRes |
| 946 |
c.Assert(cli.initSessionAndPoller(), IsNil) |
| 947 |
cli.broadcastCh = make(chan *session.BroadcastNotification, 1) |
| 948 |
cli.broadcastCh <- &session.BroadcastNotification{} |
| 949 |
|
| 950 |
ch := make(chan bool, 1) |
| 951 |
go cli.doLoop(nopConn, func(_ *session.BroadcastNotification) error { ch <- true; return nil }, nopUcast, nopUnregister) |
| 952 |
c.Check(takeNextBool(ch), Equals, true) |
| 953 |
} |
| 954 |
|
| 955 |
func (cs *clientSuite) TestDoLoopNotif(c *C) { |
| 956 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 957 |
cli.log = cs.log |
| 958 |
cli.systemImageInfo = siInfoRes |
| 959 |
c.Assert(cli.initSessionAndPoller(), IsNil) |
| 960 |
cli.notificationsCh = make(chan session.AddressedNotification, 1) |
| 961 |
cli.notificationsCh <- session.AddressedNotification{} |
| 962 |
|
| 963 |
ch := make(chan bool, 1) |
| 964 |
go cli.doLoop(nopConn, nopBcast, func(session.AddressedNotification) error { ch <- true; return nil }, nopUnregister) |
| 965 |
c.Check(takeNextBool(ch), Equals, true) |
| 966 |
} |
| 967 |
|
| 968 |
func (cs *clientSuite) TestDoLoopUnregister(c *C) { |
| 969 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 970 |
cli.log = cs.log |
| 971 |
cli.systemImageInfo = siInfoRes |
| 972 |
c.Assert(cli.initSessionAndPoller(), IsNil) |
| 973 |
cli.unregisterCh = make(chan *click.AppId, 1) |
| 974 |
cli.unregisterCh <- app1 |
| 975 |
|
| 976 |
ch := make(chan bool, 1) |
| 977 |
go cli.doLoop(nopConn, nopBcast, nopUcast, func(app *click.AppId) { c.Check(app.Original(), Equals, appId1); ch <- true }) |
| 978 |
c.Check(takeNextBool(ch), Equals, true) |
| 979 |
} |
| 980 |
|
| 981 |
/***************************************************************** |
| 982 |
doStart tests |
| 983 |
******************************************************************/ |
| 984 |
|
| 985 |
func (cs *clientSuite) TestDoStartWorks(c *C) { |
| 986 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 987 |
one_called := false |
| 988 |
two_called := false |
| 989 |
one := func() error { one_called = true; return nil } |
| 990 |
two := func() error { two_called = true; return nil } |
| 991 |
c.Check(cli.doStart(one, two), IsNil) |
| 992 |
c.Check(one_called, Equals, true) |
| 993 |
c.Check(two_called, Equals, true) |
| 994 |
} |
| 995 |
|
| 996 |
func (cs *clientSuite) TestDoStartFailsAsExpected(c *C) { |
| 997 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 998 |
one_called := false |
| 999 |
two_called := false |
| 1000 |
failure := errors.New("Failure") |
| 1001 |
one := func() error { one_called = true; return failure } |
| 1002 |
two := func() error { two_called = true; return nil } |
| 1003 |
c.Check(cli.doStart(one, two), Equals, failure) |
| 1004 |
c.Check(one_called, Equals, true) |
| 1005 |
c.Check(two_called, Equals, false) |
| 1006 |
} |
| 1007 |
|
| 1008 |
/***************************************************************** |
| 1009 |
Loop() tests |
| 1010 |
******************************************************************/ |
| 1011 |
|
| 1012 |
type loopSession struct{ hasConn bool } |
| 1013 |
type loopPoller struct{} |
| 1014 |
|
| 1015 |
func (s *loopSession) ResetCookie() {} |
| 1016 |
func (s *loopSession) State() session.ClientSessionState { |
| 1017 |
if s.hasConn { |
| 1018 |
return session.Connected |
| 1019 |
} else { |
| 1020 |
return session.Disconnected |
| 1021 |
} |
| 1022 |
} |
| 1023 |
func (s *loopSession) HasConnectivity(hasConn bool) { s.hasConn = hasConn } |
| 1024 |
func (s *loopSession) KeepConnection() error { return nil } |
| 1025 |
func (s *loopSession) StopKeepConnection() {} |
| 1026 |
|
| 1027 |
func (p *loopPoller) HasConnectivity(hasConn bool) {} |
| 1028 |
func (p *loopPoller) IsConnected() bool { return false } |
| 1029 |
func (p *loopPoller) Start() error { return nil } |
| 1030 |
func (p *loopPoller) Run() error { return nil } |
| 1031 |
|
| 1032 |
func (cs *clientSuite) TestLoop(c *C) { |
| 1033 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 1034 |
cli.connCh = make(chan bool) |
| 1035 |
cli.sessionConnectedCh = make(chan uint32) |
| 1036 |
cli.log = cs.log |
| 1037 |
cli.connectivityEndp = testibus.NewTestingEndpoint(condition.Work(true), condition.Work(true), |
| 1038 |
uint32(networkmanager.ConnectedGlobal)) |
| 1039 |
cli.systemImageInfo = siInfoRes |
| 1040 |
d := new(dumbPostal) |
| 1041 |
cli.postalService = d |
| 1042 |
c.Assert(cli.startPostalService(), IsNil) |
| 1043 |
|
| 1044 |
c.Assert(cli.initSessionAndPoller(), IsNil) |
| 1045 |
|
| 1046 |
cli.broadcastCh = make(chan *session.BroadcastNotification) |
| 1047 |
|
| 1048 |
// we use tick() to make sure things have been through the |
| 1049 |
// event loop at least once before looking at things; |
| 1050 |
// otherwise there's a race between what we're trying to look |
| 1051 |
// at and the loop itself. |
| 1052 |
tick := func() { cli.sessionConnectedCh <- 42 } |
| 1053 |
|
| 1054 |
c.Assert(cli.session, NotNil) |
| 1055 |
cli.session.StopKeepConnection() |
| 1056 |
cli.session = &loopSession{} |
| 1057 |
cli.poller = &loopPoller{} |
| 1058 |
|
| 1059 |
go cli.Loop() |
| 1060 |
|
| 1061 |
// sessionConnectedCh to nothing in particular, but it'll help sync this test |
| 1062 |
cli.sessionConnectedCh <- 42 |
| 1063 |
tick() |
| 1064 |
c.Check(cs.log.Captured(), Matches, "(?msi).*Session connected after 42 attempts$") |
| 1065 |
|
| 1066 |
// loop() should have connected: |
| 1067 |
// * connCh to the connectivity checker |
| 1068 |
c.Check(cli.session.State(), Equals, session.Disconnected) |
| 1069 |
cli.connCh <- true |
| 1070 |
tick() |
| 1071 |
c.Check(cli.session.State(), Equals, session.Connected) |
| 1072 |
cli.connCh <- false |
| 1073 |
tick() |
| 1074 |
c.Check(cli.session.State(), Equals, session.Disconnected) |
| 1075 |
|
| 1076 |
// * session.BroadcastCh to the notifications handler |
| 1077 |
c.Check(d.bcastCount, Equals, 0) |
| 1078 |
cli.broadcastCh <- positiveBroadcastNotification |
| 1079 |
tick() |
| 1080 |
c.Check(d.bcastCount, Equals, 1) |
| 1081 |
} |
| 1082 |
|
| 1083 |
/***************************************************************** |
| 1084 |
Start() tests |
| 1085 |
******************************************************************/ |
| 1086 |
|
| 1087 |
// Test for some key mocked DBus services required for integration testing. |
| 1088 |
// XXX this is a hack. |
| 1089 |
func (cs *clientSuite) hasDbus() bool { |
| 1090 |
if bus.SystemBus.Endpoint(systemimage.BusAddress, cs.log).Dial() != nil { |
| 1091 |
return false |
| 1092 |
} |
| 1093 |
|
| 1094 |
if bus.SessionBus.Endpoint(polld.BusAddress, cs.log).Dial() != nil { |
| 1095 |
return false |
| 1096 |
} |
| 1097 |
|
| 1098 |
return true |
| 1099 |
} |
| 1100 |
|
| 1101 |
func (cs *clientSuite) TestStart(c *C) { |
| 1102 |
if !cs.hasDbus() { |
| 1103 |
c.Skip("no dbus") |
| 1104 |
} |
| 1105 |
|
| 1106 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 1107 |
// before start, everything sucks: |
| 1108 |
// no service, |
| 1109 |
c.Check(cli.pushService, IsNil) |
| 1110 |
// no config, |
| 1111 |
c.Check(string(cli.config.Addr), Equals, "") |
| 1112 |
// no device id, |
| 1113 |
c.Check(cli.deviceId, HasLen, 0) |
| 1114 |
// no session, |
| 1115 |
c.Check(cli.session, IsNil) |
| 1116 |
// no bus, |
| 1117 |
c.Check(cli.systemImageEndp, IsNil) |
| 1118 |
// no nuthin'. |
| 1119 |
|
| 1120 |
// so we start, |
| 1121 |
err := cli.Start() |
| 1122 |
// and it works |
| 1123 |
c.Assert(err, IsNil) |
| 1124 |
|
| 1125 |
// and now everthing is better! We have a config, |
| 1126 |
c.Check(string(cli.config.Addr), Equals, ":0") |
| 1127 |
// and a device id, |
| 1128 |
c.Check(cli.deviceId, HasLen, 40) |
| 1129 |
// and a session, |
| 1130 |
c.Check(cli.session, NotNil) |
| 1131 |
// and a bus, |
| 1132 |
c.Check(cli.systemImageEndp, NotNil) |
| 1133 |
// and a service, |
| 1134 |
c.Check(cli.pushService, NotNil) |
| 1135 |
// and everthying us just peachy! |
| 1136 |
cli.pushService.(*service.PushService).Stop() // cleanup |
| 1137 |
cli.postalService.Stop() // cleanup |
| 1138 |
} |
| 1139 |
|
| 1140 |
func (cs *clientSuite) TestStartCanFail(c *C) { |
| 1141 |
cli := NewPushClient("/does/not/exist", cs.leveldbPath) |
| 1142 |
// easiest way for it to fail is to feed it a bad config |
| 1143 |
err := cli.Start() |
| 1144 |
// and it works. Err. Doesn't. |
| 1145 |
c.Check(err, NotNil) |
| 1146 |
} |
| 1147 |
|
| 1148 |
func (cs *clientSuite) TestinitSessionAndPollerErr(c *C) { |
| 1149 |
cli := NewPushClient(cs.configPath, cs.leveldbPath) |
| 1150 |
cli.log = cs.log |
| 1151 |
cli.systemImageInfo = siInfoRes |
| 1152 |
// change the cli.pem value so initSessionAndPoller fails |
| 1153 |
cli.pem = []byte("foo") |
| 1154 |
c.Assert(cli.initSessionAndPoller(), NotNil) |
| 1155 |
} |