github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/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  	"fmt"
    24  
    25  	. "gopkg.in/check.v1"
    26  
    27  	"github.com/snapcore/snapd/dirs"
    28  	"github.com/snapcore/snapd/interfaces"
    29  	"github.com/snapcore/snapd/interfaces/apparmor"
    30  	"github.com/snapcore/snapd/interfaces/builtin"
    31  	"github.com/snapcore/snapd/interfaces/udev"
    32  	"github.com/snapcore/snapd/snap"
    33  	"github.com/snapcore/snapd/testutil"
    34  )
    35  
    36  type JoystickInterfaceSuite struct {
    37  	iface    interfaces.Interface
    38  	slotInfo *snap.SlotInfo
    39  	slot     *interfaces.ConnectedSlot
    40  	plugInfo *snap.PlugInfo
    41  	plug     *interfaces.ConnectedPlug
    42  }
    43  
    44  var _ = Suite(&JoystickInterfaceSuite{
    45  	iface: builtin.MustInterface("joystick"),
    46  })
    47  
    48  const joystickConsumerYaml = `name: consumer
    49  version: 0
    50  apps:
    51   app:
    52    plugs: [joystick]
    53  `
    54  
    55  const joystickCoreYaml = `name: core
    56  version: 0
    57  type: os
    58  slots:
    59    joystick:
    60  `
    61  
    62  func (s *JoystickInterfaceSuite) SetUpTest(c *C) {
    63  	s.plug, s.plugInfo = MockConnectedPlug(c, joystickConsumerYaml, nil, "joystick")
    64  	s.slot, s.slotInfo = MockConnectedSlot(c, joystickCoreYaml, nil, "joystick")
    65  }
    66  
    67  func (s *JoystickInterfaceSuite) TestName(c *C) {
    68  	c.Assert(s.iface.Name(), Equals, "joystick")
    69  }
    70  
    71  func (s *JoystickInterfaceSuite) TestSanitizeSlot(c *C) {
    72  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.slotInfo), IsNil)
    73  }
    74  
    75  func (s *JoystickInterfaceSuite) TestSanitizePlug(c *C) {
    76  	c.Assert(interfaces.BeforePreparePlug(s.iface, s.plugInfo), IsNil)
    77  }
    78  
    79  func (s *JoystickInterfaceSuite) TestAppArmorSpec(c *C) {
    80  	spec := &apparmor.Specification{}
    81  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
    82  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"})
    83  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, `/dev/input/js{[0-9],[12][0-9],3[01]} rw,`)
    84  	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,`)
    85  }
    86  
    87  func (s *JoystickInterfaceSuite) TestUDevSpec(c *C) {
    88  	spec := &udev.Specification{}
    89  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
    90  	c.Assert(spec.Snippets(), HasLen, 4)
    91  	c.Assert(spec.Snippets(), testutil.Contains, `# joystick
    92  KERNEL=="js[0-9]*", TAG+="snap_consumer_app"`)
    93  	c.Assert(spec.Snippets(), testutil.Contains, `# joystick
    94  KERNEL=="event[0-9]*", SUBSYSTEM=="input", ENV{ID_INPUT_JOYSTICK}=="1", TAG+="snap_consumer_app"`)
    95  	c.Assert(spec.Snippets(), testutil.Contains, `# joystick
    96  KERNEL=="full", SUBSYSTEM=="mem", TAG+="snap_consumer_app"`)
    97  	c.Assert(spec.Snippets(), testutil.Contains, fmt.Sprintf(`TAG=="snap_consumer_app", RUN+="%v/snap-device-helper $env{ACTION} snap_consumer_app $devpath $major:$minor"`, dirs.DistroLibExecDir))
    98  	c.Assert(spec.TriggeredSubsystems(), DeepEquals, []string{"input/joystick"})
    99  }
   100  
   101  func (s *JoystickInterfaceSuite) TestStaticInfo(c *C) {
   102  	si := interfaces.StaticInfoOf(s.iface)
   103  	c.Assert(si.ImplicitOnCore, Equals, true)
   104  	c.Assert(si.ImplicitOnClassic, Equals, true)
   105  	c.Assert(si.Summary, Equals, `allows access to joystick devices`)
   106  	c.Assert(si.BaseDeclarationSlots, testutil.Contains, "joystick")
   107  }
   108  
   109  func (s *JoystickInterfaceSuite) TestAutoConnect(c *C) {
   110  	c.Assert(s.iface.AutoConnect(s.plugInfo, s.slotInfo), Equals, true)
   111  }
   112  
   113  func (s *JoystickInterfaceSuite) TestInterfaces(c *C) {
   114  	c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
   115  }