github.com/rigado/snapd@v2.42.5-go-mod+incompatible/interfaces/builtin/optical_drive_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 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  	. "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/interfaces/udev"
    29  	"github.com/snapcore/snapd/snap"
    30  	"github.com/snapcore/snapd/snap/snaptest"
    31  	"github.com/snapcore/snapd/testutil"
    32  )
    33  
    34  type OpticalDriveInterfaceSuite struct {
    35  	iface    interfaces.Interface
    36  	slotInfo *snap.SlotInfo
    37  	slot     *interfaces.ConnectedSlot
    38  
    39  	// Consuming Snap
    40  	testPlugReadonly     *interfaces.ConnectedPlug
    41  	testPlugReadonlyInfo *snap.PlugInfo
    42  	testPlugWritable     *interfaces.ConnectedPlug
    43  	testPlugWritableInfo *snap.PlugInfo
    44  	testPlugDefault      *interfaces.ConnectedPlug
    45  	testPlugDefaultInfo  *snap.PlugInfo
    46  }
    47  
    48  var _ = Suite(&OpticalDriveInterfaceSuite{
    49  	iface: builtin.MustInterface("optical-drive"),
    50  })
    51  
    52  const opticalDriveConsumerYaml = `name: consumer
    53  version: 0
    54  plugs:
    55   plug-for-readonly:
    56    interface: optical-drive
    57    write: false
    58   plug-for-writable:
    59    interface: optical-drive
    60    write: true
    61  apps:
    62   app:
    63    plugs: [optical-drive]
    64   app-readonly:
    65    plugs: [plug-for-readonly]
    66   app-writable:
    67    plugs: [plug-for-writable]
    68  `
    69  
    70  const opticalDriveCoreYaml = `name: core
    71  version: 0
    72  type: os
    73  slots:
    74    optical-drive:
    75  `
    76  
    77  func (s *OpticalDriveInterfaceSuite) SetUpTest(c *C) {
    78  	consumingSnapInfo := snaptest.MockInfo(c, opticalDriveConsumerYaml, nil)
    79  
    80  	s.testPlugDefaultInfo = consumingSnapInfo.Plugs["optical-drive"]
    81  	s.testPlugDefault = interfaces.NewConnectedPlug(s.testPlugDefaultInfo, nil, nil)
    82  	s.testPlugReadonlyInfo = consumingSnapInfo.Plugs["plug-for-readonly"]
    83  	s.testPlugReadonly = interfaces.NewConnectedPlug(s.testPlugReadonlyInfo, nil, nil)
    84  	s.testPlugWritableInfo = consumingSnapInfo.Plugs["plug-for-writable"]
    85  	s.testPlugWritable = interfaces.NewConnectedPlug(s.testPlugWritableInfo, nil, nil)
    86  
    87  	s.slot, s.slotInfo = MockConnectedSlot(c, opticalDriveCoreYaml, nil, "optical-drive")
    88  }
    89  
    90  func (s *OpticalDriveInterfaceSuite) TestName(c *C) {
    91  	c.Assert(s.iface.Name(), Equals, "optical-drive")
    92  }
    93  
    94  func (s *OpticalDriveInterfaceSuite) TestSanitizeSlot(c *C) {
    95  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.slotInfo), IsNil)
    96  }
    97  
    98  func (s *OpticalDriveInterfaceSuite) TestSanitizePlug(c *C) {
    99  	c.Assert(interfaces.BeforePreparePlug(s.iface, s.testPlugDefaultInfo), IsNil)
   100  	c.Assert(interfaces.BeforePreparePlug(s.iface, s.testPlugReadonlyInfo), IsNil)
   101  	c.Assert(interfaces.BeforePreparePlug(s.iface, s.testPlugWritableInfo), IsNil)
   102  }
   103  
   104  func (s *OpticalDriveInterfaceSuite) TestAppArmorSpec(c *C) {
   105  	type options struct {
   106  		appName         string
   107  		includeSnippets []string
   108  		excludeSnippets []string
   109  	}
   110  	checkConnectedPlugSnippet := func(plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot, opts *options) {
   111  		apparmorSpec := &apparmor.Specification{}
   112  		err := apparmorSpec.AddConnectedPlug(s.iface, plug, slot)
   113  		c.Assert(err, IsNil)
   114  		c.Assert(apparmorSpec.SecurityTags(), DeepEquals, []string{opts.appName})
   115  		for _, expectedSnippet := range opts.includeSnippets {
   116  			c.Assert(apparmorSpec.SnippetForTag(opts.appName), testutil.Contains, expectedSnippet)
   117  		}
   118  		for _, unexpectedSnippet := range opts.excludeSnippets {
   119  			c.Assert(apparmorSpec.SnippetForTag(opts.appName), Not(testutil.Contains), unexpectedSnippet)
   120  		}
   121  	}
   122  
   123  	expectedSnippet1 := `/dev/scd[0-9]* r,`
   124  	expectedSnippet2 := `/dev/scd[0-9]* w,`
   125  
   126  	checkConnectedPlugSnippet(s.testPlugDefault, s.slot, &options{
   127  		appName:         "snap.consumer.app",
   128  		includeSnippets: []string{expectedSnippet1},
   129  		excludeSnippets: []string{expectedSnippet2},
   130  	})
   131  	checkConnectedPlugSnippet(s.testPlugReadonly, s.slot, &options{
   132  		appName:         "snap.consumer.app-readonly",
   133  		includeSnippets: []string{expectedSnippet1},
   134  		excludeSnippets: []string{expectedSnippet2},
   135  	})
   136  	checkConnectedPlugSnippet(s.testPlugWritable, s.slot, &options{
   137  		appName:         "snap.consumer.app-writable",
   138  		includeSnippets: []string{expectedSnippet1, expectedSnippet2},
   139  		excludeSnippets: []string{},
   140  	})
   141  }
   142  
   143  func (s *OpticalDriveInterfaceSuite) TestUDevSpec(c *C) {
   144  	spec := &udev.Specification{}
   145  	c.Assert(spec.AddConnectedPlug(s.iface, s.testPlugDefault, s.slot), IsNil)
   146  	c.Assert(spec.AddConnectedPlug(s.iface, s.testPlugReadonly, s.slot), IsNil)
   147  	c.Assert(spec.AddConnectedPlug(s.iface, s.testPlugWritable, s.slot), IsNil)
   148  	c.Assert(spec.Snippets(), HasLen, 12) // four rules multiplied by three apps
   149  	c.Assert(spec.Snippets(), testutil.Contains, `# optical-drive
   150  KERNEL=="sr[0-9]*", TAG+="snap_consumer_app"`)
   151  	c.Assert(spec.Snippets(), testutil.Contains, `TAG=="snap_consumer_app", RUN+="/usr/lib/snapd/snap-device-helper $env{ACTION} snap_consumer_app $devpath $major:$minor"`)
   152  }
   153  
   154  func (s *OpticalDriveInterfaceSuite) TestStaticInfo(c *C) {
   155  	si := interfaces.StaticInfoOf(s.iface)
   156  	c.Assert(si.ImplicitOnCore, Equals, false)
   157  	c.Assert(si.ImplicitOnClassic, Equals, true)
   158  	c.Assert(si.Summary, Equals, `allows access to optical drives`)
   159  	c.Assert(si.BaseDeclarationSlots, testutil.Contains, "optical-drive")
   160  }
   161  
   162  func (s *OpticalDriveInterfaceSuite) TestInterfaces(c *C) {
   163  	c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
   164  }