~c-lobrano/snappy/hw-assign-symlink

« back to all changes in this revision

Viewing changes to snappy/hwaccess_test.go

  • Committer: c-lobrano
  • Date: 2015-09-30 13:44:52 UTC
  • Revision ID: c.lobrano@gmail.com-20150930134452-ppoyd4v36vjisnih
Rework of hw-unassign for symlinks and other fixes

cmd_hwassign.go: 
 - Inverted hw assignement and symlink creation. The device is supposed to be already assigned to the snap, before creating the symlink.

errors.go:
 - added custom errors for symlinks.go

hwaccess.go: 
 - reworked RemoveHWAccess to manage symlinks
 - add some wrapper functions to make things simpler

hwaccess_test.go:
 - add tests for new functions

Show diffs side-by-side

added added

removed removed

Lines of Context:
338
338
        })
339
339
}
340
340
 
341
 
func (s *SnapTestSuite) TesthasSnapApparmorJSON(c *C) {
 
341
func (s *SnapTestSuite) TestHasSnapApparmorJSON(c *C) {
342
342
        err := hasSnapApparmorJSON("non-existent-app")
343
343
        c.Assert(err, Equals, ErrPackageNotFound)
344
344
 
351
351
}
352
352
 
353
353
func (s *SnapTestSuite) TestAddNewWritePathForSnap(c *C) {
354
 
        // try add path to non existent app
355
 
        err := addNewWritePathForSnap("non-existent-app", "/dev/ttyUSB0")
356
 
        c.Assert(err != nil, Equals, true)
357
 
 
358
354
        // try add same path twice
359
355
        makeInstalledMockSnap(s.tempdir, "")
360
 
        err = addNewWritePathForSnap("hello-app", "/dev/ttyUSB0")
 
356
        err := addNewWritePathForSnap("hello-app", "/dev/ttyUSB0")
361
357
        c.Assert(err, IsNil)
362
358
        err = addNewWritePathForSnap("hello-app", "/dev/ttyUSB0")
363
359
        c.Assert(err, Equals, ErrHWAccessAlreadyAdded)
364
360
 
365
 
        // check file path has been added right
366
 
        content, err := ioutil.ReadFile(filepath.Join(snapAppArmorDir, "hello-app.json.additional"))
367
 
        c.Assert(err, IsNil)
368
 
        c.Assert(string(content), Equals, `{
369
 
  "write_path": [
370
 
    "/dev/ttyUSB0"
371
 
  ],
372
 
  "read_path": [
373
 
    "/run/udev/data/*"
374
 
  ]
375
 
}
 
361
        // check .additional file is written right
 
362
        content, err := ioutil.ReadFile(filepath.Join(snapAppArmorDir, "hello-app.json.additional"))
 
363
        c.Assert(err, IsNil)
 
364
        c.Assert(string(content), Equals, `{
 
365
  "write_path": [
 
366
    "/dev/ttyUSB0"
 
367
  ],
 
368
  "read_path": [
 
369
    "/run/udev/data/*"
 
370
  ]
 
371
}
 
372
`)
 
373
}
 
374
 
 
375
func (s *SnapTestSuite) TestAddNewSymlinkPathForSnap(c *C) {
 
376
        makeInstalledMockSnap(s.tempdir, "")
 
377
 
 
378
        // try add as symlink to an hw device not in write path, yet
 
379
        err := addNewSymlinkPathForSnap("hello-app", "/dev/symtest0", "/dev/ttyUSB0")
 
380
        c.Assert(err, Equals, ErrHWAccessRemoveNotFound)
 
381
 
 
382
        // try add the same symlink twice
 
383
        err = addNewWritePathForSnap("hello-app", "/dev/ttyUSB0")
 
384
        c.Assert(err, IsNil)
 
385
        err = addNewSymlinkPathForSnap("hello-app", "/dev/symtest0", "/dev/ttyUSB0")
 
386
        c.Assert(err, IsNil)
 
387
        err = addNewSymlinkPathForSnap("hello-app", "/dev/symtest0", "/dev/ttyUSB0")
 
388
        c.Assert(err, Equals, ErrSymlinkToHWAlreadyAdded)
 
389
 
 
390
        // check .additional file is written right
 
391
        content, err := ioutil.ReadFile(filepath.Join(snapAppArmorDir, "hello-app.json.additional"))
 
392
        c.Assert(err, IsNil)
 
393
        c.Assert(string(content), Equals, `{
 
394
  "write_path": [
 
395
    "/dev/ttyUSB0"
 
396
  ],
 
397
  "read_path": [
 
398
    "/run/udev/data/*"
 
399
  ],
 
400
  "symlink_path": {
 
401
    "/dev/symtest0": "/dev/ttyUSB0"
 
402
  }
 
403
}
 
404
`)
 
405
 
 
406
        // try add a second symlink
 
407
        err = AddHWAccess("hello-app", "/dev/ttyUSB1")
 
408
        c.Assert(err, IsNil)
 
409
 
 
410
        err = AddSymlinkToHWDevice("hello-app", "/dev/ttyUSB1", "/dev/symtest1")
 
411
        c.Assert(err, IsNil)
 
412
 
 
413
        // check .additional file is written right
 
414
        content, err = ioutil.ReadFile(filepath.Join(snapAppArmorDir, "hello-app.json.additional"))
 
415
        c.Assert(err, IsNil)
 
416
        c.Assert(string(content), Equals, `{
 
417
  "write_path": [
 
418
    "/dev/ttyUSB0",
 
419
    "/dev/ttyUSB1"
 
420
  ],
 
421
  "read_path": [
 
422
    "/run/udev/data/*"
 
423
  ],
 
424
  "symlink_path": {
 
425
    "/dev/symtest0": "/dev/ttyUSB0",
 
426
    "/dev/symtest1": "/dev/ttyUSB1"
 
427
  }
 
428
}
 
429
`)
 
430
        // try add symlink with the same name of one of snap's write paths
 
431
        err = addNewSymlinkPathForSnap("hello-app", "/dev/ttyUSB1", "/dev/ttyUSB1")
 
432
        c.Assert(err, Equals, ErrSymlinkToHWNameCollision)
 
433
}
 
434
 
 
435
func (s *SnapTestSuite) TestRemoveSymlinkToHWDevice(c *C) {
 
436
        aaClickHookCmd = "true"
 
437
        makeInstalledMockSnap(s.tempdir, "")
 
438
 
 
439
        // Add access to 2 devices
 
440
        err := AddHWAccess("hello-app", "/dev/ttyUSB0")
 
441
        c.Assert(err, IsNil)
 
442
        err = AddHWAccess("hello-app", "/dev/ttyUSB1")
 
443
        c.Assert(err, IsNil)
 
444
 
 
445
        // Adding a symlinks
 
446
        err = AddSymlinkToHWDevice("hello-app", "/dev/ttyUSB0", "/dev/symtest0")
 
447
        c.Assert(err, IsNil)
 
448
 
 
449
        err = AddSymlinkToHWDevice("hello-app", "/dev/ttyUSB1", "/dev/symtest1")
 
450
        c.Assert(err, IsNil)
 
451
 
 
452
        // Remove hw device with symlink: the symlink is expected to be removed too
 
453
        err = RemoveHWAccess("hello-app", "/dev/ttyUSB1")
 
454
        c.Assert(err, IsNil)
 
455
 
 
456
        content, err := ioutil.ReadFile(filepath.Join(snapAppArmorDir, "hello-app.json.additional"))
 
457
        c.Assert(err, IsNil)
 
458
        c.Assert(string(content), Equals, `{
 
459
  "write_path": [
 
460
    "/dev/ttyUSB0"
 
461
  ],
 
462
  "read_path": [
 
463
    "/run/udev/data/*"
 
464
  ],
 
465
  "symlink_path": {
 
466
    "/dev/symtest0": "/dev/ttyUSB0"
 
467
  }
 
468
}
 
469
`)
 
470
 
 
471
        // Remove only the symlink
 
472
        err = RemoveHWAccess("hello-app", "/dev/symtest0")
 
473
        c.Assert(err, IsNil)
 
474
 
 
475
        // having removed the last symlink, SymlinkPath is expected to be nil
 
476
        appArmorAdditional, err := readHWAccessJSONFile("hello-app")
 
477
        c.Assert(err, IsNil)
 
478
        c.Assert(appArmorAdditional.SymlinkPath, IsNil)
 
479
 
 
480
        // expecting write path unchanged
 
481
        content, err = ioutil.ReadFile(filepath.Join(snapAppArmorDir, "hello-app.json.additional"))
 
482
        c.Assert(err, IsNil)
 
483
        c.Assert(string(content), Equals, `{
 
484
  "write_path": [
 
485
    "/dev/ttyUSB0"
 
486
  ],
 
487
  "read_path": [
 
488
    "/run/udev/data/*"
 
489
  ]
 
490
}
 
491
`)
 
492
}
 
493
 
 
494
func (s *SnapTestSuite) TestRemoveUdevRuleForSnap(c *C) {
 
495
        aaClickHookCmd = "true"
 
496
        makeInstalledMockSnap(s.tempdir, "")
 
497
 
 
498
        // Add access to 3 devices and 2 symlinks
 
499
        err := AddHWAccess("hello-app", "/dev/ttyUSB0")
 
500
        c.Assert(err, IsNil)
 
501
        err = AddHWAccess("hello-app", "/dev/ttyUSB1")
 
502
        c.Assert(err, IsNil)
 
503
        err = AddHWAccess("hello-app", "/dev/ttyUSB2")
 
504
        c.Assert(err, IsNil)
 
505
        err = AddSymlinkToHWDevice("hello-app", "/dev/ttyUSB0", "/dev/symlink0")
 
506
        c.Assert(err, IsNil)
 
507
        err = AddSymlinkToHWDevice("hello-app", "/dev/ttyUSB1", "/dev/symlink1")
 
508
        c.Assert(err, IsNil)
 
509
 
 
510
        // Remove the device without symlink
 
511
        err = RemoveHWAccess("hello-app", "/dev/ttyUSB2")
 
512
        c.Assert(err, IsNil)
 
513
        content, err := ioutil.ReadFile(filepath.Join(snapUdevRulesDir, "70-snappy_hwassign_hello-app.rules"))
 
514
        c.Assert(err, IsNil)
 
515
        c.Assert(string(content), Equals, `KERNEL=="ttyUSB0", TAG:="snappy-assign", ENV{SNAPPY_APP}:="hello-app"
 
516
KERNEL=="ttyUSB1", TAG:="snappy-assign", ENV{SNAPPY_APP}:="hello-app"
 
517
ACTION=="add", KERNEL=="ttyUSB0", TAG:="snappy-assign", ENV{SNAPPY_APP}:="hello-app", SYMLINK+="symlink0"
 
518
ACTION=="add", KERNEL=="ttyUSB1", TAG:="snappy-assign", ENV{SNAPPY_APP}:="hello-app", SYMLINK+="symlink1"
 
519
`)
 
520
        // Remove a device with a symlink
 
521
        err = RemoveHWAccess("hello-app", "/dev/ttyUSB1")
 
522
        c.Assert(err, IsNil)
 
523
        content, err = ioutil.ReadFile(filepath.Join(snapUdevRulesDir, "70-snappy_hwassign_hello-app.rules"))
 
524
        c.Assert(err, IsNil)
 
525
        c.Assert(string(content), Equals, `KERNEL=="ttyUSB0", TAG:="snappy-assign", ENV{SNAPPY_APP}:="hello-app"
 
526
ACTION=="add", KERNEL=="ttyUSB0", TAG:="snappy-assign", ENV{SNAPPY_APP}:="hello-app", SYMLINK+="symlink0"
 
527
`)
 
528
        // Remove the symlink
 
529
        err = RemoveHWAccess("hello-app", "/dev/symlink0")
 
530
        c.Assert(err, IsNil)
 
531
        content, err = ioutil.ReadFile(filepath.Join(snapUdevRulesDir, "70-snappy_hwassign_hello-app.rules"))
 
532
        c.Assert(err, IsNil)
 
533
        c.Assert(string(content), Equals, `KERNEL=="ttyUSB0", TAG:="snappy-assign", ENV{SNAPPY_APP}:="hello-app"
376
534
`)
377
535
}