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

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2020 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/snap"
    29  	"github.com/snapcore/snapd/testutil"
    30  )
    31  
    32  type NetworkStatusSuite struct {
    33  	iface        interfaces.Interface
    34  	coreSlotInfo *snap.SlotInfo
    35  	coreSlot     *interfaces.ConnectedSlot
    36  	plugInfo     *snap.PlugInfo
    37  	plug         *interfaces.ConnectedPlug
    38  }
    39  
    40  var _ = Suite(&NetworkStatusSuite{
    41  	iface: builtin.MustInterface("network-status"),
    42  })
    43  
    44  func (s *NetworkStatusSuite) SetUpSuite(c *C) {
    45  	const coreProviderYaml = `name: core
    46  type: os
    47  version: 0
    48  slots:
    49    network-status:
    50  `
    51  	s.coreSlot, s.coreSlotInfo = MockConnectedSlot(c, coreProviderYaml, nil, "network-status")
    52  
    53  	const consumerYaml = `name: consumer
    54  version: 1.0
    55  apps:
    56    app:
    57      command: foo
    58      plugs: [network-status]
    59  `
    60  	s.plug, s.plugInfo = MockConnectedPlug(c, consumerYaml, nil, "network-status")
    61  }
    62  
    63  func (s *NetworkStatusSuite) TestName(c *C) {
    64  	c.Check(s.iface.Name(), Equals, "network-status")
    65  }
    66  
    67  func (s *NetworkStatusSuite) TestAppArmorConnectedPlug(c *C) {
    68  	// If the slot is provided by a snap, access is restricted to the snap's label
    69  	spec := &apparmor.Specification{}
    70  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.coreSlot), IsNil)
    71  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"})
    72  	c.Check(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, `peer=(label=unconfined)`)
    73  	c.Check(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, "interface=org.freedesktop.portal.NetworkMonitor")
    74  }
    75  
    76  func (s *NetworkStatusSuite) TestAppArmorConnectedSlot(c *C) {
    77  	spec := &apparmor.Specification{}
    78  	c.Assert(spec.AddConnectedSlot(s.iface, s.plug, s.coreSlot), IsNil)
    79  	c.Assert(spec.SecurityTags(), HasLen, 0)
    80  }
    81  
    82  func (s *NetworkStatusSuite) TestAppArmorPermanentSlot(c *C) {
    83  	spec := &apparmor.Specification{}
    84  	c.Assert(spec.AddPermanentSlot(s.iface, s.coreSlotInfo), IsNil)
    85  	c.Assert(spec.SecurityTags(), HasLen, 0)
    86  }
    87  
    88  func (s *NetworkStatusSuite) TestInterfaces(c *C) {
    89  	c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
    90  }