github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/interfaces/builtin/common_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2016-2018 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 21 22 import ( 23 "fmt" 24 "os" 25 26 . "gopkg.in/check.v1" 27 28 "github.com/snapcore/snapd/dirs" 29 "github.com/snapcore/snapd/interfaces/apparmor" 30 "github.com/snapcore/snapd/interfaces/udev" 31 "github.com/snapcore/snapd/testutil" 32 ) 33 34 type commonIfaceSuite struct{} 35 36 var _ = Suite(&commonIfaceSuite{}) 37 38 func (s *commonIfaceSuite) TestUDevSpec(c *C) { 39 plug, _ := MockConnectedPlug(c, ` 40 name: consumer 41 version: 0 42 apps: 43 app-a: 44 plugs: [common] 45 app-b: 46 app-c: 47 plugs: [common] 48 `, nil, "common") 49 slot, _ := MockConnectedSlot(c, ` 50 name: producer 51 version: 0 52 slots: 53 common: 54 `, nil, "common") 55 56 // common interface can define connected plug udev rules 57 iface := &commonInterface{ 58 name: "common", 59 connectedPlugUDev: []string{`KERNEL=="foo"`}, 60 } 61 spec := &udev.Specification{} 62 c.Assert(spec.AddConnectedPlug(iface, plug, slot), IsNil) 63 c.Assert(spec.Snippets(), DeepEquals, []string{ 64 `# common 65 KERNEL=="foo", TAG+="snap_consumer_app-a"`, 66 fmt.Sprintf(`TAG=="snap_consumer_app-a", RUN+="%v/snap-device-helper $env{ACTION} snap_consumer_app-a $devpath $major:$minor"`, dirs.DistroLibExecDir), 67 // NOTE: app-b is unaffected as it doesn't have a plug reference. 68 `# common 69 KERNEL=="foo", TAG+="snap_consumer_app-c"`, 70 fmt.Sprintf(`TAG=="snap_consumer_app-c", RUN+="%v/snap-device-helper $env{ACTION} snap_consumer_app-c $devpath $major:$minor"`, dirs.DistroLibExecDir), 71 }) 72 73 // connected plug udev rules are optional 74 iface = &commonInterface{ 75 name: "common", 76 } 77 spec = &udev.Specification{} 78 c.Assert(spec.AddConnectedPlug(iface, plug, slot), IsNil) 79 c.Assert(spec.Snippets(), HasLen, 0) 80 } 81 82 // MockEvalSymlinks replaces the path/filepath.EvalSymlinks function used inside the caps package. 83 func MockEvalSymlinks(test *testutil.BaseTest, fn func(string) (string, error)) { 84 orig := evalSymlinks 85 evalSymlinks = fn 86 test.AddCleanup(func() { 87 evalSymlinks = orig 88 }) 89 } 90 91 // MockReadDir replaces the io/ioutil.ReadDir function used inside the caps package. 92 func MockReadDir(test *testutil.BaseTest, fn func(string) ([]os.FileInfo, error)) { 93 orig := readDir 94 readDir = fn 95 test.AddCleanup(func() { 96 readDir = orig 97 }) 98 } 99 100 func (s *commonIfaceSuite) TestSuppressPtraceTrace(c *C) { 101 plug, _ := MockConnectedPlug(c, ` 102 name: consumer 103 version: 0 104 apps: 105 app: 106 plugs: [common] 107 `, nil, "common") 108 slot, _ := MockConnectedSlot(c, ` 109 name: producer 110 version: 0 111 slots: 112 common: 113 `, nil, "common") 114 115 // setting nothing 116 iface := &commonInterface{ 117 name: "common", 118 suppressPtraceTrace: false, 119 usesPtraceTrace: false, 120 } 121 spec := &apparmor.Specification{} 122 c.Assert(spec.UsesPtraceTrace(), Equals, false) 123 c.Assert(spec.SuppressPtraceTrace(), Equals, false) 124 c.Assert(spec.AddConnectedPlug(iface, plug, slot), IsNil) 125 c.Assert(spec.UsesPtraceTrace(), Equals, false) 126 c.Assert(spec.SuppressPtraceTrace(), Equals, false) 127 128 // setting only uses 129 iface = &commonInterface{ 130 name: "common", 131 suppressPtraceTrace: false, 132 usesPtraceTrace: true, 133 } 134 spec = &apparmor.Specification{} 135 c.Assert(spec.UsesPtraceTrace(), Equals, false) 136 c.Assert(spec.SuppressPtraceTrace(), Equals, false) 137 c.Assert(spec.AddConnectedPlug(iface, plug, slot), IsNil) 138 c.Assert(spec.UsesPtraceTrace(), Equals, true) 139 c.Assert(spec.SuppressPtraceTrace(), Equals, false) 140 141 // setting only suppress 142 iface = &commonInterface{ 143 name: "common", 144 suppressPtraceTrace: true, 145 usesPtraceTrace: false, 146 } 147 spec = &apparmor.Specification{} 148 c.Assert(spec.UsesPtraceTrace(), Equals, false) 149 c.Assert(spec.SuppressPtraceTrace(), Equals, false) 150 c.Assert(spec.AddConnectedPlug(iface, plug, slot), IsNil) 151 c.Assert(spec.UsesPtraceTrace(), Equals, false) 152 c.Assert(spec.SuppressPtraceTrace(), Equals, true) 153 154 // setting both, only uses is set 155 iface = &commonInterface{ 156 name: "common", 157 suppressPtraceTrace: true, 158 usesPtraceTrace: true, 159 } 160 spec = &apparmor.Specification{} 161 c.Assert(spec.UsesPtraceTrace(), Equals, false) 162 c.Assert(spec.SuppressPtraceTrace(), Equals, false) 163 c.Assert(spec.AddConnectedPlug(iface, plug, slot), IsNil) 164 c.Assert(spec.UsesPtraceTrace(), Equals, true) 165 c.Assert(spec.SuppressPtraceTrace(), Equals, false) 166 } 167 168 func (s *commonIfaceSuite) TestSuppressHomeIx(c *C) { 169 plug, _ := MockConnectedPlug(c, ` 170 name: consumer 171 version: 0 172 apps: 173 app: 174 plugs: [common] 175 `, nil, "common") 176 slot, _ := MockConnectedSlot(c, ` 177 name: producer 178 version: 0 179 slots: 180 common: 181 `, nil, "common") 182 183 // setting nothing 184 iface := &commonInterface{ 185 name: "common", 186 suppressHomeIx: false, 187 } 188 spec := &apparmor.Specification{} 189 c.Assert(spec.SuppressHomeIx(), Equals, false) 190 c.Assert(spec.AddConnectedPlug(iface, plug, slot), IsNil) 191 c.Assert(spec.SuppressHomeIx(), Equals, false) 192 193 iface = &commonInterface{ 194 name: "common", 195 suppressHomeIx: true, 196 } 197 spec = &apparmor.Specification{} 198 c.Assert(spec.SuppressHomeIx(), Equals, false) 199 c.Assert(spec.AddConnectedPlug(iface, plug, slot), IsNil) 200 c.Assert(spec.SuppressHomeIx(), Equals, true) 201 } 202 203 func (s *commonIfaceSuite) TestControlsDeviceCgroup(c *C) { 204 plug, _ := MockConnectedPlug(c, ` 205 name: consumer 206 version: 0 207 apps: 208 app: 209 plugs: [common] 210 `, nil, "common") 211 slot, _ := MockConnectedSlot(c, ` 212 name: producer 213 version: 0 214 slots: 215 common: 216 `, nil, "common") 217 218 // setting nothing 219 iface := &commonInterface{ 220 name: "common", 221 controlsDeviceCgroup: false, 222 } 223 spec := &udev.Specification{} 224 c.Assert(spec.ControlsDeviceCgroup(), Equals, false) 225 c.Assert(spec.AddConnectedPlug(iface, plug, slot), IsNil) 226 c.Assert(spec.ControlsDeviceCgroup(), Equals, false) 227 228 iface = &commonInterface{ 229 name: "common", 230 controlsDeviceCgroup: true, 231 } 232 spec = &udev.Specification{} 233 c.Assert(spec.ControlsDeviceCgroup(), Equals, false) 234 c.Assert(spec.AddConnectedPlug(iface, plug, slot), IsNil) 235 c.Assert(spec.ControlsDeviceCgroup(), Equals, true) 236 }