github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/interfaces/builtin/appstream_metadata_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  	"os"
    24  	"path/filepath"
    25  
    26  	. "gopkg.in/check.v1"
    27  
    28  	"github.com/snapcore/snapd/dirs"
    29  	"github.com/snapcore/snapd/interfaces"
    30  	"github.com/snapcore/snapd/interfaces/apparmor"
    31  	"github.com/snapcore/snapd/interfaces/builtin"
    32  	"github.com/snapcore/snapd/interfaces/mount"
    33  	"github.com/snapcore/snapd/snap"
    34  	"github.com/snapcore/snapd/testutil"
    35  )
    36  
    37  type AppstreamMetadataInterfaceSuite struct {
    38  	iface    interfaces.Interface
    39  	slot     *interfaces.ConnectedSlot
    40  	slotInfo *snap.SlotInfo
    41  	plug     *interfaces.ConnectedPlug
    42  	plugInfo *snap.PlugInfo
    43  }
    44  
    45  var _ = Suite(&AppstreamMetadataInterfaceSuite{
    46  	iface: builtin.MustInterface("appstream-metadata"),
    47  })
    48  
    49  func (s *AppstreamMetadataInterfaceSuite) SetUpTest(c *C) {
    50  	const coreYaml = `name: core
    51  version: 0
    52  type: os
    53  slots:
    54    appstream-metadata:
    55      interface: appstream-metadata
    56  `
    57  	s.slot, s.slotInfo = MockConnectedSlot(c, coreYaml, nil, "appstream-metadata")
    58  
    59  	const consumerYaml = `name: consumer
    60  version: 0
    61  apps:
    62    app:
    63      plugs: [appstream-metadata]
    64  `
    65  	s.plug, s.plugInfo = MockConnectedPlug(c, consumerYaml, nil, "appstream-metadata")
    66  }
    67  
    68  func (s *AppstreamMetadataInterfaceSuite) TearDownTest(c *C) {
    69  	dirs.SetRootDir("/")
    70  }
    71  
    72  func (s *AppstreamMetadataInterfaceSuite) TestName(c *C) {
    73  	c.Check(s.iface.Name(), Equals, "appstream-metadata")
    74  }
    75  
    76  func (s *AppstreamMetadataInterfaceSuite) TestSanitize(c *C) {
    77  	c.Check(interfaces.BeforePreparePlug(s.iface, s.plugInfo), IsNil)
    78  	c.Check(interfaces.BeforePrepareSlot(s.iface, s.slotInfo), IsNil)
    79  }
    80  
    81  func (s *AppstreamMetadataInterfaceSuite) TestAppArmorConnectedPlug(c *C) {
    82  	spec := &apparmor.Specification{}
    83  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
    84  	c.Assert(spec.SecurityTags(), HasLen, 1)
    85  	c.Check(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, `/var/cache/app-info/** r,`)
    86  	c.Check(spec.UpdateNS(), testutil.Contains, "  # Read-only access to /usr/share/metainfo\n")
    87  }
    88  
    89  func (s *AppstreamMetadataInterfaceSuite) TestMountConnectedPlug(c *C) {
    90  	tmpdir := c.MkDir()
    91  	dirs.SetRootDir(tmpdir)
    92  
    93  	c.Assert(os.MkdirAll(filepath.Join(tmpdir, "/usr/share/metainfo"), 0777), IsNil)
    94  	c.Assert(os.MkdirAll(filepath.Join(tmpdir, "/usr/share/appdata"), 0777), IsNil)
    95  	c.Assert(os.MkdirAll(filepath.Join(tmpdir, "/usr/share/app-info"), 0777), IsNil)
    96  	c.Assert(os.MkdirAll(filepath.Join(tmpdir, "/var/cache/app-info"), 0777), IsNil)
    97  	c.Assert(os.MkdirAll(filepath.Join(tmpdir, "/var/lib/app-info"), 0777), IsNil)
    98  	c.Assert(os.MkdirAll(filepath.Join(tmpdir, "/var/lib/apt/lists"), 0777), IsNil)
    99  
   100  	spec := &mount.Specification{}
   101  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
   102  	entries := spec.MountEntries()
   103  	c.Assert(entries, HasLen, 6)
   104  
   105  	const hostfs = "/var/lib/snapd/hostfs"
   106  	c.Check(entries[0].Name, Equals, filepath.Join(hostfs, dirs.GlobalRootDir, "/usr/share/metainfo"))
   107  	c.Check(entries[0].Dir, Equals, "/usr/share/metainfo")
   108  	c.Check(entries[0].Options, DeepEquals, []string{"bind", "ro"})
   109  	c.Check(entries[1].Name, Equals, filepath.Join(hostfs, dirs.GlobalRootDir, "/usr/share/appdata"))
   110  	c.Check(entries[1].Dir, Equals, "/usr/share/appdata")
   111  	c.Check(entries[1].Options, DeepEquals, []string{"bind", "ro"})
   112  	c.Check(entries[2].Name, Equals, filepath.Join(hostfs, dirs.GlobalRootDir, "/usr/share/app-info"))
   113  	c.Check(entries[2].Dir, Equals, "/usr/share/app-info")
   114  	c.Check(entries[2].Options, DeepEquals, []string{"bind", "ro"})
   115  	c.Check(entries[3].Name, Equals, filepath.Join(hostfs, dirs.GlobalRootDir, "/var/cache/app-info"))
   116  	c.Check(entries[3].Dir, Equals, "/var/cache/app-info")
   117  	c.Check(entries[3].Options, DeepEquals, []string{"bind", "ro"})
   118  	c.Check(entries[4].Name, Equals, filepath.Join(hostfs, dirs.GlobalRootDir, "/var/lib/app-info"))
   119  	c.Check(entries[4].Dir, Equals, "/var/lib/app-info")
   120  	c.Check(entries[4].Options, DeepEquals, []string{"bind", "ro"})
   121  	c.Check(entries[5].Name, Equals, filepath.Join(hostfs, dirs.GlobalRootDir, "/var/lib/apt/lists"))
   122  	c.Check(entries[5].Dir, Equals, "/var/lib/apt/lists")
   123  	c.Check(entries[5].Options, DeepEquals, []string{"bind", "ro"})
   124  }
   125  
   126  func (s *AppstreamMetadataInterfaceSuite) TestStaticInfo(c *C) {
   127  	si := interfaces.StaticInfoOf(s.iface)
   128  	c.Check(si.ImplicitOnCore, Equals, false)
   129  	c.Check(si.ImplicitOnClassic, Equals, true)
   130  	c.Check(si.Summary, Equals, "allows access to AppStream metadata")
   131  	c.Check(si.BaseDeclarationSlots, testutil.Contains, "appstream-metadata")
   132  }
   133  
   134  func (s *AppstreamMetadataInterfaceSuite) TestInterfaces(c *C) {
   135  	c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
   136  }