github.com/stolowski/snapd@v0.0.0-20210407085831-115137ce5a22/osutil/strace/strace_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 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 strace_test 21 22 import ( 23 "io/ioutil" 24 "os" 25 "os/user" 26 "path/filepath" 27 "testing" 28 29 . "gopkg.in/check.v1" 30 31 "github.com/snapcore/snapd/dirs" 32 "github.com/snapcore/snapd/osutil/strace" 33 "github.com/snapcore/snapd/testutil" 34 ) 35 36 // Hook up check.v1 into the "go test" runner 37 func Test(t *testing.T) { TestingT(t) } 38 39 type straceSuite struct { 40 rootdir string 41 mockSudo *testutil.MockCmd 42 mockStrace *testutil.MockCmd 43 } 44 45 var _ = Suite(&straceSuite{}) 46 47 func (s *straceSuite) SetUpTest(c *C) { 48 s.rootdir = c.MkDir() 49 dirs.SetRootDir(s.rootdir) 50 51 s.mockSudo = testutil.MockCommand(c, "sudo", "") 52 s.mockStrace = testutil.MockCommand(c, "strace", "") 53 } 54 55 func (s *straceSuite) TearDownTest(c *C) { 56 dirs.SetRootDir("/") 57 s.mockSudo.Restore() 58 s.mockStrace.Restore() 59 } 60 61 func (s *straceSuite) TestStraceCommandHappy(c *C) { 62 u, err := user.Current() 63 c.Assert(err, IsNil) 64 65 cmd, err := strace.Command(nil, "foo") 66 c.Assert(err, IsNil) 67 c.Assert(cmd.Path, Equals, s.mockSudo.Exe()) 68 c.Assert(cmd.Args, DeepEquals, []string{ 69 s.mockSudo.Exe(), "-E", 70 s.mockStrace.Exe(), "-u", u.Username, "-f", 71 "-e", strace.ExcludedSyscalls, 72 // the command 73 "foo", 74 }) 75 } 76 77 func (s *straceSuite) TestStraceCommandNoSudo(c *C) { 78 origPath := os.Getenv("PATH") 79 defer func() { os.Setenv("PATH", origPath) }() 80 81 os.Setenv("PATH", "/not-exists") 82 _, err := strace.Command(nil, "foo") 83 c.Assert(err, ErrorMatches, `cannot use strace without sudo: exec: "sudo": executable file not found in \$PATH`) 84 } 85 86 func (s *straceSuite) TestStraceCommandNoStrace(c *C) { 87 origPath := os.Getenv("PATH") 88 defer func() { os.Setenv("PATH", origPath) }() 89 90 tmp := c.MkDir() 91 os.Setenv("PATH", tmp) 92 err := ioutil.WriteFile(filepath.Join(tmp, "sudo"), nil, 0755) 93 c.Assert(err, IsNil) 94 95 _, err = strace.Command(nil, "foo") 96 c.Assert(err, ErrorMatches, `cannot find an installed strace, please try 'snap install strace-static'`) 97 } 98 99 func (s *straceSuite) TestTraceExecCommand(c *C) { 100 u, err := user.Current() 101 c.Assert(err, IsNil) 102 103 cmd, err := strace.TraceExecCommand("/run/snapd/strace.log", "cmd") 104 c.Assert(err, IsNil) 105 c.Assert(cmd.Path, Equals, s.mockSudo.Exe()) 106 c.Assert(cmd.Args, DeepEquals, []string{ 107 s.mockSudo.Exe(), "-E", 108 s.mockStrace.Exe(), "-u", u.Username, "-f", 109 "-e", strace.ExcludedSyscalls, 110 // timing specific trace 111 "-ttt", 112 "-e", "trace=execve,execveat", 113 "-o", "/run/snapd/strace.log", 114 // the command 115 "cmd", 116 }) 117 118 }