gitee.com/mysnapcore/mysnapd@v0.1.0/interfaces/builtin/qualcomm_ipc_router_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2021 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  	"gitee.com/mysnapcore/mysnapd/interfaces"
    26  	"gitee.com/mysnapcore/mysnapd/interfaces/apparmor"
    27  	"gitee.com/mysnapcore/mysnapd/interfaces/builtin"
    28  	"gitee.com/mysnapcore/mysnapd/interfaces/seccomp"
    29  	apparmor_sandbox "gitee.com/mysnapcore/mysnapd/sandbox/apparmor"
    30  	"gitee.com/mysnapcore/mysnapd/snap"
    31  	"gitee.com/mysnapcore/mysnapd/testutil"
    32  )
    33  
    34  type QrtrInterfaceSuite struct {
    35  	iface    interfaces.Interface
    36  	slotInfo *snap.SlotInfo
    37  	slot     *interfaces.ConnectedSlot
    38  	plugInfo *snap.PlugInfo
    39  	plug     *interfaces.ConnectedPlug
    40  }
    41  
    42  var _ = Suite(&QrtrInterfaceSuite{
    43  	iface: builtin.MustInterface("qualcomm-ipc-router"),
    44  })
    45  
    46  const qipcrtrConsumerYaml = `name: consumer
    47  version: 0
    48  apps:
    49   app:
    50    plugs: [qualcomm-ipc-router]
    51  `
    52  
    53  const qipcrtrCoreYaml = `name: core
    54  version: 0
    55  type: os
    56  slots:
    57    qualcomm-ipc-router:
    58  `
    59  
    60  func (s *QrtrInterfaceSuite) SetUpTest(c *C) {
    61  	s.plug, s.plugInfo = MockConnectedPlug(c, qipcrtrConsumerYaml, nil, "qualcomm-ipc-router")
    62  	s.slot, s.slotInfo = MockConnectedSlot(c, qipcrtrCoreYaml, nil, "qualcomm-ipc-router")
    63  }
    64  
    65  func (s *QrtrInterfaceSuite) TestName(c *C) {
    66  	c.Assert(s.iface.Name(), Equals, "qualcomm-ipc-router")
    67  }
    68  
    69  func (s *QrtrInterfaceSuite) TestSanitizeSlot(c *C) {
    70  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.slotInfo), IsNil)
    71  }
    72  
    73  func (s *QrtrInterfaceSuite) TestSanitizePlug(c *C) {
    74  	c.Assert(interfaces.BeforePreparePlug(s.iface, s.plugInfo), IsNil)
    75  }
    76  
    77  func (s *QrtrInterfaceSuite) TestSanitizePlugConnectionFullAppArmorSandboxFeatures(c *C) {
    78  	r := apparmor_sandbox.MockFeatures(nil, nil, []string{"qipcrtr-socket"}, nil)
    79  	defer r()
    80  	c.Assert(interfaces.BeforeConnectPlug(s.iface, s.plug), IsNil)
    81  }
    82  
    83  func (s *QrtrInterfaceSuite) TestSanitizePlugConnectionMissingAppArmorSandboxFeatures(c *C) {
    84  	r := apparmor_sandbox.MockLevel(apparmor_sandbox.Full)
    85  	defer r()
    86  	r = apparmor_sandbox.MockFeatures(nil, nil, nil, nil)
    87  	defer r()
    88  	err := interfaces.BeforeConnectPlug(s.iface, s.plug)
    89  	c.Assert(err, ErrorMatches, "cannot connect plug on system without qipcrtr socket support")
    90  }
    91  
    92  func (s *QrtrInterfaceSuite) TestSanitizePlugConnectionMissingNoAppArmor(c *C) {
    93  	r := apparmor_sandbox.MockLevel(apparmor_sandbox.Unsupported)
    94  	defer r()
    95  	err := interfaces.BeforeConnectPlug(s.iface, s.plug)
    96  	c.Assert(err, IsNil)
    97  }
    98  
    99  func (s *QrtrInterfaceSuite) TestAppArmorSpec(c *C) {
   100  	spec := &apparmor.Specification{}
   101  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
   102  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"})
   103  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, "network qipcrtr,\n")
   104  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, "capability net_admin,\n")
   105  }
   106  
   107  func (s *QrtrInterfaceSuite) TestSecCompSpec(c *C) {
   108  	spec := &seccomp.Specification{}
   109  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
   110  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"})
   111  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, "bind\n")
   112  }
   113  
   114  func (s *QrtrInterfaceSuite) TestStaticInfo(c *C) {
   115  	si := interfaces.StaticInfoOf(s.iface)
   116  	c.Assert(si.ImplicitOnCore, Equals, true)
   117  	c.Assert(si.ImplicitOnClassic, Equals, true)
   118  	c.Assert(si.Summary, Equals, `allows access to the Qualcomm IPC Router sockets`)
   119  	c.Assert(si.BaseDeclarationSlots, testutil.Contains, "qualcomm-ipc-router")
   120  }
   121  
   122  func (s *QrtrInterfaceSuite) TestInterfaces(c *C) {
   123  	c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
   124  }