File: strace_test.go

package info (click to toggle)
snapd 2.73-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 81,460 kB
  • sloc: sh: 16,736; ansic: 16,652; python: 11,215; makefile: 1,966; exp: 190; awk: 58; xml: 22
file content (157 lines) | stat: -rw-r--r-- 4,146 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
 * Copyright (C) 2018 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package strace_test

import (
	"os"
	"os/exec"
	"path/filepath"
	"testing"

	. "gopkg.in/check.v1"

	"github.com/snapcore/snapd/dirs"
	"github.com/snapcore/snapd/osutil/strace"
	"github.com/snapcore/snapd/osutil/user"
	"github.com/snapcore/snapd/testutil"
)

// Hook up check.v1 into the "go test" runner
func Test(t *testing.T) { TestingT(t) }

type straceSuite struct {
	rootdir    string
	mockSudo   *testutil.MockCmd
	mockStrace *testutil.MockCmd
}

var _ = Suite(&straceSuite{})

func (s *straceSuite) SetUpTest(c *C) {
	s.rootdir = c.MkDir()
	dirs.SetRootDir(s.rootdir)

	s.mockSudo = testutil.MockCommand(c, "sudo", "")
	s.mockStrace = testutil.MockCommand(c, "strace", "")
}

func (s *straceSuite) TearDownTest(c *C) {
	dirs.SetRootDir("/")
	s.mockSudo.Restore()
	s.mockStrace.Restore()
}

func (s *straceSuite) TestStraceCommandHappy(c *C) {
	cmd, err := strace.Command(nil)
	c.Assert(err, IsNil)
	c.Assert(cmd.Path, Equals, s.mockSudo.Exe())
	c.Assert(cmd.Args, DeepEquals, []string{
		s.mockSudo.Exe(), "--",
		s.mockStrace.Exe(),
		"-f",
		"-e", strace.ExcludedSyscalls,
	})

	cmd, err = strace.CommandWithTraceePid(123, nil)
	c.Assert(err, IsNil)
	c.Assert(cmd.Path, Equals, s.mockSudo.Exe())
	c.Assert(cmd.Args, DeepEquals, []string{
		s.mockSudo.Exe(), "--",
		s.mockStrace.Exe(),
		"-f",
		"-e", strace.ExcludedSyscalls,
		"-p", "123",
	})
}

func (s *straceSuite) TestStraceCommandHappyFromSnap(c *C) {
	straceStaticPath := filepath.Join(dirs.SnapMountDir, "strace-static", "current", "bin", "strace")
	err := os.MkdirAll(filepath.Dir(straceStaticPath), 0755)
	c.Assert(err, IsNil)
	mockStraceStatic := testutil.MockCommand(c, straceStaticPath, "")
	defer mockStraceStatic.Restore()

	cmd, err := strace.Command(nil)
	c.Assert(err, IsNil)
	c.Check(cmd.Path, Equals, s.mockSudo.Exe())
	c.Check(cmd.Args, DeepEquals, []string{
		s.mockSudo.Exe(), "--",
		mockStraceStatic.Exe(),
		"-f",
		"-e", strace.ExcludedSyscalls,
	})
}

func (s *straceSuite) TestStraceCommandNoSudo(c *C) {
	tmp := c.MkDir()

	if user.GetentBased {
		getEntPath, err := exec.LookPath("getent")
		c.Assert(err, IsNil)
		err = os.Symlink(getEntPath, filepath.Join(tmp, "getent"))
		c.Assert(err, IsNil)
	}

	origPath := os.Getenv("PATH")
	defer func() { os.Setenv("PATH", origPath) }()
	os.Setenv("PATH", tmp)

	_, err := strace.Command(nil)
	c.Assert(err, ErrorMatches, `cannot use strace without sudo: exec: "sudo": executable file not found in \$PATH`)
}

func (s *straceSuite) TestStraceCommandNoStrace(c *C) {
	tmp := c.MkDir()

	if user.GetentBased {
		getEntPath, err := exec.LookPath("getent")
		c.Assert(err, IsNil)
		err = os.Symlink(getEntPath, filepath.Join(tmp, "getent"))
		c.Assert(err, IsNil)
	}

	origPath := os.Getenv("PATH")
	defer func() { os.Setenv("PATH", origPath) }()

	os.Setenv("PATH", tmp)
	err := os.WriteFile(filepath.Join(tmp, "sudo"), nil, 0755)
	c.Assert(err, IsNil)

	_, err = strace.Command(nil)
	c.Assert(err, ErrorMatches, `cannot find an installed strace, please try 'snap install strace-static'`)
}

func (s *straceSuite) TestTraceExecCommand(c *C) {
	cmd, err := strace.TraceExecCommandForPid(123, "/run/snapd/strace.log")
	c.Assert(err, IsNil)
	c.Assert(cmd.Path, Equals, s.mockSudo.Exe())
	c.Assert(cmd.Args, DeepEquals, []string{
		s.mockSudo.Exe(), "--",
		s.mockStrace.Exe(),
		"-f",
		"-e", strace.ExcludedSyscalls,
		// timing specific trace
		"-ttt",
		"-e", "trace=execve,execveat",
		"-o", "/run/snapd/strace.log",
		"-p", "123",
	})

}