github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/interfaces/builtin/ofono_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  	"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/seccomp"
    32  	"github.com/snapcore/snapd/interfaces/udev"
    33  	"github.com/snapcore/snapd/release"
    34  	"github.com/snapcore/snapd/snap"
    35  	"github.com/snapcore/snapd/snap/snaptest"
    36  	"github.com/snapcore/snapd/testutil"
    37  )
    38  
    39  type OfonoInterfaceSuite 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(&OfonoInterfaceSuite{
    48  	iface: builtin.MustInterface("ofono"),
    49  })
    50  
    51  func (s *OfonoInterfaceSuite) SetUpTest(c *C) {
    52  	var mockPlugSnapInfoYaml = `name: other
    53  version: 1.0
    54  apps:
    55   app:
    56    command: foo
    57    plugs: [ofono]
    58  `
    59  	const mockSlotSnapInfoYaml = `name: ofono
    60  version: 1.0
    61  slots:
    62   ofono:
    63    interface: ofono
    64  apps:
    65   app:
    66    command: foo
    67    slots: [ofono]
    68  `
    69  	snapInfo := snaptest.MockInfo(c, mockSlotSnapInfoYaml, nil)
    70  	s.slotInfo = snapInfo.Slots["ofono"]
    71  	s.slot = interfaces.NewConnectedSlot(s.slotInfo, nil, nil)
    72  	snapInfo = snaptest.MockInfo(c, mockPlugSnapInfoYaml, nil)
    73  	s.plugInfo = snapInfo.Plugs["ofono"]
    74  	s.plug = interfaces.NewConnectedPlug(s.plugInfo, nil, nil)
    75  }
    76  
    77  func (s *OfonoInterfaceSuite) TestName(c *C) {
    78  	c.Assert(s.iface.Name(), Equals, "ofono")
    79  }
    80  
    81  // The label glob when all apps are bound to the ofono slot
    82  func (s *OfonoInterfaceSuite) TestConnectedPlugSnippetUsesSlotLabelAll(c *C) {
    83  	app1 := &snap.AppInfo{Name: "app1"}
    84  	app2 := &snap.AppInfo{Name: "app2"}
    85  	slot := interfaces.NewConnectedSlot(&snap.SlotInfo{
    86  		Snap: &snap.Info{
    87  			SuggestedName: "ofono",
    88  			Apps:          map[string]*snap.AppInfo{"app1": app1, "app2": app2},
    89  		},
    90  		Name:      "ofono",
    91  		Interface: "ofono",
    92  		Apps:      map[string]*snap.AppInfo{"app1": app1, "app2": app2},
    93  	}, nil, nil)
    94  
    95  	release.OnClassic = false
    96  
    97  	apparmorSpec := &apparmor.Specification{}
    98  	err := apparmorSpec.AddConnectedPlug(s.iface, s.plug, slot)
    99  	c.Assert(err, IsNil)
   100  	c.Assert(apparmorSpec.SecurityTags(), DeepEquals, []string{"snap.other.app"})
   101  	c.Assert(apparmorSpec.SnippetForTag("snap.other.app"), testutil.Contains, `peer=(label="snap.ofono.*"),`)
   102  }
   103  
   104  // The label uses alternation when some, but not all, apps is bound to the ofono slot
   105  func (s *OfonoInterfaceSuite) TestConnectedPlugSnippetUsesSlotLabelSome(c *C) {
   106  	app1 := &snap.AppInfo{Name: "app1"}
   107  	app2 := &snap.AppInfo{Name: "app2"}
   108  	app3 := &snap.AppInfo{Name: "app3"}
   109  	slot := interfaces.NewConnectedSlot(&snap.SlotInfo{
   110  		Snap: &snap.Info{
   111  			SuggestedName: "ofono",
   112  			Apps:          map[string]*snap.AppInfo{"app1": app1, "app2": app2, "app3": app3},
   113  		},
   114  		Name:      "ofono",
   115  		Interface: "ofono",
   116  		Apps:      map[string]*snap.AppInfo{"app1": app1, "app2": app2},
   117  	}, nil, nil)
   118  
   119  	release.OnClassic = false
   120  
   121  	apparmorSpec := &apparmor.Specification{}
   122  	err := apparmorSpec.AddConnectedPlug(s.iface, s.plug, slot)
   123  	c.Assert(err, IsNil)
   124  	c.Assert(apparmorSpec.SecurityTags(), DeepEquals, []string{"snap.other.app"})
   125  	c.Assert(apparmorSpec.SnippetForTag("snap.other.app"), testutil.Contains, `peer=(label="snap.ofono.{app1,app2}"),`)
   126  }
   127  
   128  // The label uses short form when exactly one app is bound to the ofono slot
   129  func (s *OfonoInterfaceSuite) TestConnectedPlugSnippetUsesSlotLabelOne(c *C) {
   130  	app := &snap.AppInfo{Name: "app"}
   131  	slot := interfaces.NewConnectedSlot(&snap.SlotInfo{
   132  		Snap: &snap.Info{
   133  			SuggestedName: "ofono",
   134  			Apps:          map[string]*snap.AppInfo{"app": app},
   135  		},
   136  		Name:      "ofono",
   137  		Interface: "ofono",
   138  		Apps:      map[string]*snap.AppInfo{"app": app},
   139  	}, nil, nil)
   140  
   141  	release.OnClassic = false
   142  
   143  	apparmorSpec := &apparmor.Specification{}
   144  	err := apparmorSpec.AddConnectedPlug(s.iface, s.plug, slot)
   145  	c.Assert(err, IsNil)
   146  	c.Assert(apparmorSpec.SecurityTags(), DeepEquals, []string{"snap.other.app"})
   147  	c.Assert(apparmorSpec.SnippetForTag("snap.other.app"), testutil.Contains, `peer=(label="snap.ofono.app"),`)
   148  }
   149  
   150  func (s *OfonoInterfaceSuite) TestConnectedPlugSnippetUsesUnconfinedLabelOnClassic(c *C) {
   151  	release.OnClassic = true
   152  
   153  	apparmorSpec := &apparmor.Specification{}
   154  	err := apparmorSpec.AddConnectedPlug(s.iface, s.plug, s.slot)
   155  	c.Assert(err, IsNil)
   156  	c.Assert(apparmorSpec.SecurityTags(), DeepEquals, []string{"snap.other.app"})
   157  	snippet := apparmorSpec.SnippetForTag("snap.other.app")
   158  	// verify apparmor connected
   159  	c.Assert(snippet, testutil.Contains, "#include <abstractions/dbus-strict>")
   160  	// verify classic connected
   161  	c.Assert(snippet, testutil.Contains, "peer=(label=unconfined),")
   162  }
   163  
   164  func (s *OfonoInterfaceSuite) TestConnectedPlugSnippetAppArmor(c *C) {
   165  	release.OnClassic = false
   166  	apparmorSpec := &apparmor.Specification{}
   167  	err := apparmorSpec.AddConnectedPlug(s.iface, s.plug, s.slot)
   168  	c.Assert(err, IsNil)
   169  	c.Assert(apparmorSpec.SecurityTags(), DeepEquals, []string{"snap.other.app"})
   170  	snippet := apparmorSpec.SnippetForTag("snap.other.app")
   171  	// verify apparmor connected
   172  	c.Assert(snippet, testutil.Contains, "#include <abstractions/dbus-strict>")
   173  	// verify classic didn't connect
   174  	c.Assert(snippet, Not(testutil.Contains), "peer=(label=unconfined),")
   175  }
   176  
   177  func (s *OfonoInterfaceSuite) TestConnectedSlotSnippetAppArmor(c *C) {
   178  	apparmorSpec := &apparmor.Specification{}
   179  	err := apparmorSpec.AddConnectedSlot(s.iface, s.plug, s.slot)
   180  	c.Assert(err, IsNil)
   181  	aasnippets := apparmorSpec.Snippets()
   182  	c.Assert(aasnippets, HasLen, 1)
   183  	c.Assert(aasnippets["snap.ofono.app"], HasLen, 1)
   184  	snippet := string(aasnippets["snap.ofono.app"][0])
   185  	c.Check(string(snippet), testutil.Contains, "peer=(label=\"snap.other.app\")")
   186  }
   187  
   188  func (s *OfonoInterfaceSuite) TestPermanentSlotSnippetAppArmor(c *C) {
   189  	apparmorSpec := &apparmor.Specification{}
   190  	err := apparmorSpec.AddPermanentSlot(s.iface, s.slotInfo)
   191  	c.Assert(err, IsNil)
   192  	c.Assert(apparmorSpec.SecurityTags(), DeepEquals, []string{"snap.ofono.app"})
   193  	c.Assert(apparmorSpec.SnippetForTag("snap.ofono.app"), testutil.Contains, "/dev/net/tun rw,")
   194  }
   195  
   196  func (s *OfonoInterfaceSuite) TestPermanentSlotSnippetSecComp(c *C) {
   197  	seccompSpec := &seccomp.Specification{}
   198  	err := seccompSpec.AddPermanentSlot(s.iface, s.slotInfo)
   199  	c.Assert(err, IsNil)
   200  	c.Assert(seccompSpec.SecurityTags(), DeepEquals, []string{"snap.ofono.app"})
   201  	c.Assert(seccompSpec.SnippetForTag("snap.ofono.app"), testutil.Contains, "listen\n")
   202  }
   203  
   204  func (s *OfonoInterfaceSuite) TestPermanentSlotSnippetUDev(c *C) {
   205  	spec := &udev.Specification{}
   206  	c.Assert(spec.AddPermanentSlot(s.iface, s.slotInfo), IsNil)
   207  	c.Assert(spec.Snippets(), HasLen, 5)
   208  	c.Assert(spec.Snippets()[0], testutil.Contains, `LABEL="ofono_isi_end"`)
   209  	c.Assert(spec.Snippets(), testutil.Contains, `# ofono
   210  KERNEL=="tty[a-zA-Z]*[0-9]*|cdc-wdm[0-9]*", TAG+="snap_ofono_app"`)
   211  	c.Assert(spec.Snippets(), testutil.Contains, `# ofono
   212  KERNEL=="tun", TAG+="snap_ofono_app"`)
   213  	c.Assert(spec.Snippets(), testutil.Contains, `# ofono
   214  KERNEL=="dsp", TAG+="snap_ofono_app"`)
   215  	c.Assert(spec.Snippets(), testutil.Contains, fmt.Sprintf(`TAG=="snap_ofono_app", RUN+="%v/snap-device-helper $env{ACTION} snap_ofono_app $devpath $major:$minor"`, dirs.DistroLibExecDir))
   216  }
   217  
   218  func (s *OfonoInterfaceSuite) TestInterfaces(c *C) {
   219  	c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
   220  }