gitee.com/mysnapcore/mysnapd@v0.1.0/interfaces/builtin/netlink_audit_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  	"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/snap/snaptest"
    32  	"gitee.com/mysnapcore/mysnapd/testutil"
    33  )
    34  
    35  type NetlinkAuditInterfaceSuite struct {
    36  	iface    interfaces.Interface
    37  	slotInfo *snap.SlotInfo
    38  	slot     *interfaces.ConnectedSlot
    39  	plugInfo *snap.PlugInfo
    40  	plug     *interfaces.ConnectedPlug
    41  }
    42  
    43  const netlinkAuditMockPlugSnapInfoYaml = `name: other
    44  version: 1.0
    45  apps:
    46   app2:
    47    command: foo
    48    plugs: [netlink-audit]
    49  `
    50  
    51  var _ = Suite(&NetlinkAuditInterfaceSuite{
    52  	iface: builtin.MustInterface("netlink-audit"),
    53  })
    54  
    55  func (s *NetlinkAuditInterfaceSuite) SetUpTest(c *C) {
    56  	s.slotInfo = &snap.SlotInfo{
    57  		Snap:      &snap.Info{SuggestedName: "core", SnapType: snap.TypeOS},
    58  		Name:      "netlink-audit",
    59  		Interface: "netlink-audit",
    60  	}
    61  	s.slot = interfaces.NewConnectedSlot(s.slotInfo, nil, nil)
    62  	plugSnap := snaptest.MockInfo(c, netlinkAuditMockPlugSnapInfoYaml, nil)
    63  	s.plugInfo = plugSnap.Plugs["netlink-audit"]
    64  	s.plug = interfaces.NewConnectedPlug(s.plugInfo, nil, nil)
    65  }
    66  
    67  func (s *NetlinkAuditInterfaceSuite) TestName(c *C) {
    68  	c.Assert(s.iface.Name(), Equals, "netlink-audit")
    69  }
    70  
    71  func (s *NetlinkAuditInterfaceSuite) TestSanitizeSlot(c *C) {
    72  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.slotInfo), IsNil)
    73  }
    74  
    75  func (s *NetlinkAuditInterfaceSuite) TestSanitizePlug(c *C) {
    76  	c.Assert(interfaces.BeforePreparePlug(s.iface, s.plugInfo), IsNil)
    77  }
    78  
    79  func (s *NetlinkAuditInterfaceSuite) TestSanitizePlugConnectionMissingAppArmorSandboxFeatures(c *C) {
    80  	r := apparmor_sandbox.MockLevel(apparmor_sandbox.Full)
    81  	defer r()
    82  	r = apparmor_sandbox.MockFeatures(nil, nil, nil, nil)
    83  	defer r()
    84  	err := interfaces.BeforeConnectPlug(s.iface, s.plug)
    85  	c.Assert(err, ErrorMatches, "cannot connect plug on system without audit_read support")
    86  }
    87  
    88  func (s *NetlinkAuditInterfaceSuite) TestSanitizePlugConnectionMissingNoAppArmor(c *C) {
    89  	r := apparmor_sandbox.MockLevel(apparmor_sandbox.Unsupported)
    90  	defer r()
    91  	err := interfaces.BeforeConnectPlug(s.iface, s.plug)
    92  	c.Assert(err, IsNil)
    93  }
    94  
    95  func (s *NetlinkAuditInterfaceSuite) TestAppArmorSpec(c *C) {
    96  	spec := &apparmor.Specification{}
    97  	err := spec.AddConnectedPlug(s.iface, s.plug, s.slot)
    98  	c.Assert(err, IsNil)
    99  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.other.app2"})
   100  	c.Check(spec.SnippetForTag("snap.other.app2"), testutil.Contains, "capability audit_write,\n")
   101  }
   102  
   103  func (s *NetlinkAuditInterfaceSuite) TestSecCompSpec(c *C) {
   104  	spec := &seccomp.Specification{}
   105  	err := spec.AddConnectedPlug(s.iface, s.plug, s.slot)
   106  	c.Assert(err, IsNil)
   107  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.other.app2"})
   108  	c.Check(spec.SnippetForTag("snap.other.app2"), testutil.Contains, "socket AF_NETLINK - NETLINK_AUDIT\n")
   109  }
   110  
   111  func (s *NetlinkAuditInterfaceSuite) TestInterfaces(c *C) {
   112  	c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
   113  }