github.com/stolowski/snapd@v0.0.0-20210407085831-115137ce5a22/osutil/mount/mount_linux_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 mount_test 21 22 import ( 23 "syscall" 24 "testing" 25 26 . "gopkg.in/check.v1" 27 28 "github.com/snapcore/snapd/osutil/mount" 29 ) 30 31 func Test(t *testing.T) { TestingT(t) } 32 33 type mountSuite struct{} 34 35 var _ = Suite(&mountSuite{}) 36 37 func (s *mountSuite) TestMountFlagsToOpts(c *C) { 38 // Known flags are converted to symbolic names. 39 opts, unknown := mount.MountFlagsToOpts(syscall.MS_REMOUNT | 40 syscall.MS_BIND | syscall.MS_REC | syscall.MS_RDONLY | syscall.MS_SHARED | 41 syscall.MS_SLAVE | syscall.MS_PRIVATE | syscall.MS_UNBINDABLE) 42 c.Check(opts, DeepEquals, []string{"MS_REMOUNT", "MS_BIND", "MS_REC", 43 "MS_RDONLY", "MS_SHARED", "MS_SLAVE", "MS_PRIVATE", "MS_UNBINDABLE"}) 44 c.Check(unknown, Equals, 0) 45 // Unknown flags are retained and returned. 46 opts, unknown = mount.MountFlagsToOpts(1 << 24) 47 c.Check(opts, DeepEquals, []string(nil)) 48 c.Check(unknown, Equals, 1<<24) 49 // Known and unknown flags work in tandem. 50 opts, unknown = mount.MountFlagsToOpts(syscall.MS_BIND | 1<<24) 51 c.Check(opts, DeepEquals, []string{"MS_BIND"}) 52 c.Check(unknown, Equals, 1<<24) 53 } 54 55 func (s *mountSuite) TestUnmountFlagsToOpts(c *C) { 56 // Known flags are converted to symbolic names. 57 const UMOUNT_NOFOLLOW = 8 58 opts, unknown := mount.UnmountFlagsToOpts(syscall.MNT_FORCE | 59 syscall.MNT_DETACH | syscall.MNT_EXPIRE | UMOUNT_NOFOLLOW) 60 c.Check(opts, DeepEquals, []string{"UMOUNT_NOFOLLOW", "MNT_FORCE", 61 "MNT_DETACH", "MNT_EXPIRE"}) 62 c.Check(unknown, Equals, 0) 63 // Unknown flags are retained and returned. 64 opts, unknown = mount.UnmountFlagsToOpts(1 << 24) 65 c.Check(opts, DeepEquals, []string(nil)) 66 c.Check(unknown, Equals, 1<<24) 67 // Known and unknown flags work in tandem. 68 opts, unknown = mount.UnmountFlagsToOpts(syscall.MNT_DETACH | 1<<24) 69 c.Check(opts, DeepEquals, []string{"MNT_DETACH"}) 70 c.Check(unknown, Equals, 1<<24) 71 } 72 73 var benchResultOpt []string 74 var benchResultUnknown int 75 76 func benchUnmount(flags int, b *testing.B) { 77 var opts []string 78 var unknown int 79 for n := 0; n < b.N; n++ { 80 opts, unknown = mount.UnmountFlagsToOpts(flags) 81 } 82 benchResultOpt = opts 83 benchResultUnknown = unknown 84 } 85 86 func benchMount(flags int, b *testing.B) { 87 var opts []string 88 var unknown int 89 for n := 0; n < b.N; n++ { 90 opts, unknown = mount.MountFlagsToOpts(flags) 91 } 92 benchResultOpt = opts 93 benchResultUnknown = unknown 94 } 95 96 func BenchmarkUnmountFlagsToOptsAllMissing(b *testing.B) { benchUnmount(1<<24, b) } 97 func BenchmarkUnmountFlagsToOptsMixed(b *testing.B) { benchUnmount(syscall.MNT_DETACH|1<<24, b) } 98 func BenchmarkUnmountFlagsToOptsAllPresent(b *testing.B) { 99 const UMOUNT_NOFOLLOW = 8 100 benchUnmount(syscall.MNT_FORCE| 101 syscall.MNT_DETACH|syscall.MNT_EXPIRE|UMOUNT_NOFOLLOW, b) 102 } 103 104 func BenchmarkMountFlagsToOptsAllMissing(b *testing.B) { benchMount(1<<24, b) } 105 106 func BenchmarkMountFlagsToOptsAllPresent(b *testing.B) { 107 benchMount(syscall.MS_REMOUNT| 108 syscall.MS_BIND|syscall.MS_REC|syscall.MS_RDONLY|syscall.MS_SHARED| 109 syscall.MS_SLAVE|syscall.MS_PRIVATE|syscall.MS_UNBINDABLE, b) 110 } 111 func BenchmarkMountFlagsToOptsMixed(b *testing.B) { benchMount(syscall.MS_BIND|1<<24, b) } 112 113 func BenchmarkMountFlagsToOptsTypical(b *testing.B) { 114 benchMount(syscall.MS_BIND, b) 115 }