github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/interfaces/builtin/network_control_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2016-2017 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/mount"
    32  	"github.com/snapcore/snapd/interfaces/seccomp"
    33  	"github.com/snapcore/snapd/interfaces/udev"
    34  	"github.com/snapcore/snapd/osutil"
    35  	"github.com/snapcore/snapd/snap"
    36  	"github.com/snapcore/snapd/testutil"
    37  )
    38  
    39  type NetworkControlInterfaceSuite struct {
    40  	iface    interfaces.Interface
    41  	slotInfo *snap.SlotInfo
    42  	slot     *interfaces.ConnectedSlot
    43  	plugInfo *snap.PlugInfo
    44  	plug     *interfaces.ConnectedPlug
    45  }
    46  
    47  var _ = Suite(&NetworkControlInterfaceSuite{
    48  	iface: builtin.MustInterface("network-control"),
    49  })
    50  
    51  const networkControlConsumerYaml = `name: consumer
    52  version: 0
    53  apps:
    54   app:
    55    plugs: [network-control]
    56  `
    57  
    58  const networkControlCoreYaml = `name: core
    59  version: 0
    60  type: os
    61  slots:
    62    network-control:
    63  `
    64  
    65  func (s *NetworkControlInterfaceSuite) SetUpTest(c *C) {
    66  	s.plug, s.plugInfo = MockConnectedPlug(c, networkControlConsumerYaml, nil, "network-control")
    67  	s.slot, s.slotInfo = MockConnectedSlot(c, networkControlCoreYaml, nil, "network-control")
    68  }
    69  
    70  func (s *NetworkControlInterfaceSuite) TestName(c *C) {
    71  	c.Assert(s.iface.Name(), Equals, "network-control")
    72  }
    73  
    74  func (s *NetworkControlInterfaceSuite) TestSanitizeSlot(c *C) {
    75  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.slotInfo), IsNil)
    76  }
    77  
    78  func (s *NetworkControlInterfaceSuite) TestSanitizePlug(c *C) {
    79  	c.Assert(interfaces.BeforePreparePlug(s.iface, s.plugInfo), IsNil)
    80  }
    81  
    82  func (s *NetworkControlInterfaceSuite) TestAppArmorSpec(c *C) {
    83  	spec := &apparmor.Specification{}
    84  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
    85  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"})
    86  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, "/run/netns/* rw,\n")
    87  	c.Assert(spec.UpdateNS(), DeepEquals, []string{`
    88  /var/ r,
    89  /var/lib/ r,
    90  /var/lib/snapd/ r,
    91  /var/lib/snapd/hostfs/ r,
    92  /var/lib/snapd/hostfs/var/ r,
    93  /var/lib/snapd/hostfs/var/lib/ r,
    94  /var/lib/snapd/hostfs/var/lib/dhcp/ r,
    95  /var/lib/dhcp/ r,
    96  mount options=(rw bind) /var/lib/snapd/hostfs/var/lib/dhcp/ -> /var/lib/dhcp/,
    97  umount /var/lib/dhcp/,
    98  `})
    99  }
   100  
   101  func (s *NetworkControlInterfaceSuite) TestSecCompSpec(c *C) {
   102  	spec := &seccomp.Specification{}
   103  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
   104  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"})
   105  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, "setns - CLONE_NEWNET\n")
   106  }
   107  
   108  func (s *NetworkControlInterfaceSuite) TestUDevSpec(c *C) {
   109  	spec := &udev.Specification{}
   110  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
   111  	c.Assert(spec.Snippets(), HasLen, 3)
   112  	c.Assert(spec.Snippets(), testutil.Contains, `# network-control
   113  KERNEL=="tun", TAG+="snap_consumer_app"`)
   114  	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))
   115  }
   116  
   117  func (s *NetworkControlInterfaceSuite) TestMountSpec(c *C) {
   118  	spec := &mount.Specification{}
   119  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
   120  	c.Assert(spec.MountEntries(), HasLen, 1)
   121  	c.Assert(spec.MountEntries(), DeepEquals, []osutil.MountEntry{{
   122  		Name:    "/var/lib/snapd/hostfs/var/lib/dhcp",
   123  		Dir:     "/var/lib/dhcp",
   124  		Options: []string{"bind", "rw", "x-snapd.ignore-missing"},
   125  	}})
   126  }
   127  
   128  func (s *NetworkControlInterfaceSuite) TestStaticInfo(c *C) {
   129  	si := interfaces.StaticInfoOf(s.iface)
   130  	c.Assert(si.ImplicitOnCore, Equals, true)
   131  	c.Assert(si.ImplicitOnClassic, Equals, true)
   132  	c.Assert(si.Summary, Equals, `allows configuring networking and network namespaces`)
   133  	c.Assert(si.BaseDeclarationSlots, testutil.Contains, "network-control")
   134  }
   135  
   136  func (s *NetworkControlInterfaceSuite) TestAutoConnect(c *C) {
   137  	c.Assert(s.iface.AutoConnect(s.plugInfo, s.slotInfo), Equals, true)
   138  }
   139  func (s *NetworkControlInterfaceSuite) TestInterfaces(c *C) {
   140  	c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
   141  }