github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/interfaces/builtin/joystick_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2017-2018 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package builtin_test
    21  
    22  import (
    23  	. "gopkg.in/check.v1"
    24  
    25  	"github.com/snapcore/snapd/interfaces"
    26  	"github.com/snapcore/snapd/interfaces/apparmor"
    27  	"github.com/snapcore/snapd/interfaces/builtin"
    28  	"github.com/snapcore/snapd/interfaces/udev"
    29  	"github.com/snapcore/snapd/snap"
    30  	"github.com/snapcore/snapd/testutil"
    31  )
    32  
    33  type JoystickInterfaceSuite struct {
    34  	iface    interfaces.Interface
    35  	slotInfo *snap.SlotInfo
    36  	slot     *interfaces.ConnectedSlot
    37  	plugInfo *snap.PlugInfo
    38  	plug     *interfaces.ConnectedPlug
    39  }
    40  
    41  var _ = Suite(&JoystickInterfaceSuite{
    42  	iface: builtin.MustInterface("joystick"),
    43  })
    44  
    45  const joystickConsumerYaml = `name: consumer
    46  version: 0
    47  apps:
    48   app:
    49    plugs: [joystick]
    50  `
    51  
    52  const joystickCoreYaml = `name: core
    53  version: 0
    54  type: os
    55  slots:
    56    joystick:
    57  `
    58  
    59  func (s *JoystickInterfaceSuite) SetUpTest(c *C) {
    60  	s.plug, s.plugInfo = MockConnectedPlug(c, joystickConsumerYaml, nil, "joystick")
    61  	s.slot, s.slotInfo = MockConnectedSlot(c, joystickCoreYaml, nil, "joystick")
    62  }
    63  
    64  func (s *JoystickInterfaceSuite) TestName(c *C) {
    65  	c.Assert(s.iface.Name(), Equals, "joystick")
    66  }
    67  
    68  func (s *JoystickInterfaceSuite) TestSanitizeSlot(c *C) {
    69  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.slotInfo), IsNil)
    70  }
    71  
    72  func (s *JoystickInterfaceSuite) TestSanitizePlug(c *C) {
    73  	c.Assert(interfaces.BeforePreparePlug(s.iface, s.plugInfo), IsNil)
    74  }
    75  
    76  func (s *JoystickInterfaceSuite) TestAppArmorSpec(c *C) {
    77  	spec := &apparmor.Specification{}
    78  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
    79  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"})
    80  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, `/dev/input/js{[0-9],[12][0-9],3[01]} rw,`)
    81  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, `/run/udev/data/c13:{6[5-9],[7-9][0-9],[1-9][0-9][0-9]*} r,`)
    82  }
    83  
    84  func (s *JoystickInterfaceSuite) TestUDevSpec(c *C) {
    85  	spec := &udev.Specification{}
    86  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
    87  	c.Assert(spec.Snippets(), HasLen, 4)
    88  	c.Assert(spec.Snippets(), testutil.Contains, `# joystick
    89  KERNEL=="js[0-9]*", TAG+="snap_consumer_app"`)
    90  	c.Assert(spec.Snippets(), testutil.Contains, `# joystick
    91  KERNEL=="event[0-9]*", SUBSYSTEM=="input", ENV{ID_INPUT_JOYSTICK}=="1", TAG+="snap_consumer_app"`)
    92  	c.Assert(spec.Snippets(), testutil.Contains, `# joystick
    93  KERNEL=="full", SUBSYSTEM=="mem", TAG+="snap_consumer_app"`)
    94  	c.Assert(spec.Snippets(), testutil.Contains, `TAG=="snap_consumer_app", RUN+="/usr/lib/snapd/snap-device-helper $env{ACTION} snap_consumer_app $devpath $major:$minor"`)
    95  	c.Assert(spec.TriggeredSubsystems(), DeepEquals, []string{"input/joystick"})
    96  }
    97  
    98  func (s *JoystickInterfaceSuite) TestStaticInfo(c *C) {
    99  	si := interfaces.StaticInfoOf(s.iface)
   100  	c.Assert(si.ImplicitOnCore, Equals, true)
   101  	c.Assert(si.ImplicitOnClassic, Equals, true)
   102  	c.Assert(si.Summary, Equals, `allows access to joystick devices`)
   103  	c.Assert(si.BaseDeclarationSlots, testutil.Contains, "joystick")
   104  }
   105  
   106  func (s *JoystickInterfaceSuite) TestAutoConnect(c *C) {
   107  	c.Assert(s.iface.AutoConnect(s.plugInfo, s.slotInfo), Equals, true)
   108  }
   109  
   110  func (s *JoystickInterfaceSuite) TestInterfaces(c *C) {
   111  	c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
   112  }