github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/interfaces/builtin/display_control_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2019 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 "io/ioutil" 26 "os" 27 "path/filepath" 28 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/snap" 33 "github.com/snapcore/snapd/testutil" 34 ) 35 36 type displayControlInterfaceSuite struct { 37 testutil.BaseTest 38 39 iface interfaces.Interface 40 slotInfo *snap.SlotInfo 41 slot *interfaces.ConnectedSlot 42 plugInfo *snap.PlugInfo 43 plug *interfaces.ConnectedPlug 44 45 tmpdir string 46 } 47 48 var _ = Suite(&displayControlInterfaceSuite{ 49 iface: builtin.MustInterface("display-control"), 50 }) 51 52 const displayControlConsumerYaml = `name: consumer 53 version: 0 54 apps: 55 app: 56 plugs: [display-control] 57 ` 58 59 const displayControlCoreYaml = `name: core 60 version: 0 61 type: os 62 slots: 63 display-control: 64 ` 65 66 func (s *displayControlInterfaceSuite) SetUpTest(c *C) { 67 s.plug, s.plugInfo = MockConnectedPlug(c, displayControlConsumerYaml, nil, "display-control") 68 s.slot, s.slotInfo = MockConnectedSlot(c, displayControlCoreYaml, nil, "display-control") 69 70 s.tmpdir = c.MkDir() 71 } 72 73 func (s *displayControlInterfaceSuite) TestName(c *C) { 74 c.Assert(s.iface.Name(), Equals, "display-control") 75 } 76 77 func (s *displayControlInterfaceSuite) TestSanitizeSlot(c *C) { 78 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.slotInfo), IsNil) 79 } 80 81 func (s *displayControlInterfaceSuite) TestSanitizePlug(c *C) { 82 c.Assert(interfaces.BeforePreparePlug(s.iface, s.plugInfo), IsNil) 83 } 84 85 func (s *displayControlInterfaceSuite) TestAppArmorSpec(c *C) { 86 c.Assert(os.MkdirAll(filepath.Join(s.tmpdir, "foo_backlight"), 0755), IsNil) 87 c.Assert(os.MkdirAll(filepath.Join(s.tmpdir, "bar_backlight"), 0755), IsNil) 88 builtin.MockReadDir(&s.BaseTest, func(path string) ([]os.FileInfo, error) { 89 return ioutil.ReadDir(s.tmpdir) 90 }) 91 builtin.MockEvalSymlinks(&s.BaseTest, func(path string) (string, error) { 92 return "(dereferenced)" + path, nil 93 }) 94 spec := &apparmor.Specification{} 95 c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil) 96 c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"}) 97 c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, "/sys/class/backlight/ r,\n") 98 c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, "autodetected backlight: bar_backlight\n") 99 c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, "(dereferenced)/sys/class/backlight/bar_backlight/{,**} r,\n") 100 c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, "autodetected backlight: foo_backlight\n") 101 c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, "(dereferenced)/sys/class/backlight/foo_backlight/{,**} r,\n") 102 } 103 104 func (s *displayControlInterfaceSuite) TestStaticInfo(c *C) { 105 si := interfaces.StaticInfoOf(s.iface) 106 c.Assert(si.ImplicitOnCore, Equals, true) 107 c.Assert(si.ImplicitOnClassic, Equals, true) 108 c.Assert(si.Summary, Equals, `allows configuring display parameters`) 109 c.Assert(si.BaseDeclarationSlots, testutil.Contains, "display-control") 110 } 111 112 func (s *displayControlInterfaceSuite) TestAutoConnect(c *C) { 113 c.Assert(s.iface.AutoConnect(s.plugInfo, s.slotInfo), Equals, true) 114 } 115 116 func (s *displayControlInterfaceSuite) TestInterfaces(c *C) { 117 c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface) 118 }