gitee.com/mysnapcore/mysnapd@v0.1.0/interfaces/polkit/backend_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2021 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 polkit_test 21 22 import ( 23 "io/ioutil" 24 "os" 25 "path/filepath" 26 "testing" 27 28 . "gopkg.in/check.v1" 29 30 "gitee.com/mysnapcore/mysnapd/dirs" 31 "gitee.com/mysnapcore/mysnapd/interfaces" 32 "gitee.com/mysnapcore/mysnapd/interfaces/ifacetest" 33 "gitee.com/mysnapcore/mysnapd/interfaces/polkit" 34 "gitee.com/mysnapcore/mysnapd/snap" 35 "gitee.com/mysnapcore/mysnapd/testutil" 36 ) 37 38 func Test(t *testing.T) { 39 TestingT(t) 40 } 41 42 type backendSuite struct { 43 ifacetest.BackendSuite 44 } 45 46 var _ = Suite(&backendSuite{}) 47 48 var testedConfinementOpts = []interfaces.ConfinementOptions{ 49 {}, 50 {DevMode: true}, 51 {JailMode: true}, 52 {Classic: true}, 53 } 54 55 func (s *backendSuite) SetUpTest(c *C) { 56 s.Backend = &polkit.Backend{} 57 s.BackendSuite.SetUpTest(c) 58 c.Assert(s.Repo.AddBackend(s.Backend), IsNil) 59 } 60 61 func (s *backendSuite) TearDownTest(c *C) { 62 s.BackendSuite.TearDownTest(c) 63 } 64 65 // Tests for Setup() and Remove() 66 func (s *backendSuite) TestName(c *C) { 67 c.Check(s.Backend.Name(), Equals, interfaces.SecurityPolkit) 68 } 69 70 func (s *backendSuite) TestInstallingSnapWritesPolicyFiles(c *C) { 71 // NOTE: Hand out a permanent policy so that .policy file is generated. 72 s.Iface.PolkitPermanentSlotCallback = func(spec *polkit.Specification, slot *snap.SlotInfo) error { 73 return spec.AddPolicy("foo", polkit.Policy("<policyconfig/>")) 74 } 75 for _, opts := range testedConfinementOpts { 76 snapInfo := s.InstallSnap(c, opts, "", ifacetest.SambaYamlV1, 0) 77 policy := filepath.Join(dirs.SnapPolkitPolicyDir, "snap.samba.interface.foo.policy") 78 // file called "snap.sambda.interface.foo.policy" was created 79 c.Check(policy, testutil.FileContains, "<policyconfig/>") 80 s.RemoveSnap(c, snapInfo) 81 } 82 } 83 84 func (s *backendSuite) TestRemovingSnapRemovesPolicyFiles(c *C) { 85 // NOTE: Hand out a permanent snippet so that .policy file is generated. 86 s.Iface.PolkitPermanentSlotCallback = func(spec *polkit.Specification, slot *snap.SlotInfo) error { 87 return spec.AddPolicy("foo", polkit.Policy("<policyconfig/>")) 88 } 89 for _, opts := range testedConfinementOpts { 90 snapInfo := s.InstallSnap(c, opts, "", ifacetest.SambaYamlV1, 0) 91 s.RemoveSnap(c, snapInfo) 92 policy := filepath.Join(dirs.SnapPolkitPolicyDir, "snap.samba.interface.foo.policy") 93 // file called "snap.sambda.interface.foo.policy" was removed 94 c.Check(policy, testutil.FileAbsent) 95 } 96 } 97 98 func (s *backendSuite) TestNoPolicyFiles(c *C) { 99 for _, opts := range testedConfinementOpts { 100 snapInfo := s.InstallSnap(c, opts, "", ifacetest.SambaYamlV1, 0) 101 policy := filepath.Join(dirs.SnapPolkitPolicyDir, "snap.samba.interface.foo.policy") 102 // Without any snippets, there the .conf file is not created. 103 c.Check(policy, testutil.FileAbsent) 104 s.RemoveSnap(c, snapInfo) 105 } 106 c.Check(dirs.SnapPolkitPolicyDir, testutil.FileAbsent) 107 } 108 109 func (s *backendSuite) TestUnexpectedPolicyFilesremoved(c *C) { 110 err := os.MkdirAll(dirs.SnapPolkitPolicyDir, 0700) 111 c.Assert(err, IsNil) 112 policyFile := filepath.Join(dirs.SnapPolkitPolicyDir, "snap.samba.interface.something.policy") 113 114 for _, opts := range testedConfinementOpts { 115 c.Assert(ioutil.WriteFile(policyFile, []byte("<policyconfig/>"), 0644), IsNil) 116 // Installing snap removes unexpected policy files 117 snapInfo := s.InstallSnap(c, opts, "", ifacetest.SambaYamlV1, 0) 118 c.Check(policyFile, testutil.FileAbsent) 119 120 c.Assert(ioutil.WriteFile(policyFile, []byte("<policyconfig/>"), 0644), IsNil) 121 // Removing snap also removes unexpected policy files 122 s.RemoveSnap(c, snapInfo) 123 c.Check(policyFile, testutil.FileAbsent) 124 } 125 } 126 127 func (s *backendSuite) TestSandboxFeatures(c *C) { 128 c.Assert(s.Backend.SandboxFeatures(), HasLen, 0) 129 }