2
* Copyright (C) 2014-2015 Canonical Ltd
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License version 3 as
6
* published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
27
. "launchpad.net/gocheck"
31
// Hook up gocheck into the "go test" runner.
32
func Test(t *testing.T) { TestingT(t) }
34
type policySuite struct {
40
var _ = Suite(&policySuite{})
42
func (s *policySuite) SetUpTest(c *C) {
45
s.appg = filepath.Join(s.orig, "meta", "framework-policy", "apparmor", "policygroups")
46
for _, i := range []string{"apparmor", "seccomp"} {
47
for _, j := range []string{"policygroups", "templates"} {
48
base := filepath.Join(s.orig, "meta", "framework-policy", i, j)
49
c.Assert(os.MkdirAll(base, 0755), IsNil)
50
for k := 0; k < 3; k++ {
51
name := fmt.Sprintf(filepath.Join(base, "%s%d"), j, k)
52
content := fmt.Sprintf("%s/%s%d", i, j, k)
53
c.Assert(ioutil.WriteFile(name, []byte(content), 0644), IsNil)
59
func (s *policySuite) TestHelperInstallRemove(c *C) {
60
err := helper(Install, filepath.Join(s.appg, "*"), s.dest, "foo_")
62
g, err := filepath.Glob(filepath.Join(s.dest, "*"))
64
c.Assert(g, HasLen, 3)
65
// Glob already sorts the returned list, but that's not documented
67
c.Check(filepath.Base(g[0]), Equals, "foo_policygroups0")
68
c.Check(filepath.Base(g[1]), Equals, "foo_policygroups1")
69
c.Check(filepath.Base(g[2]), Equals, "foo_policygroups2")
70
// check the contents of one of them
71
bs, err := ioutil.ReadFile(g[0])
73
c.Check(string(bs), Equals, "apparmor/policygroups0")
75
err = helper(Remove, filepath.Join(s.appg, "*"), s.dest, "foo_")
77
g, err = filepath.Glob(filepath.Join(s.dest, "*"))
82
func (s *policySuite) TestHelperInstallMkdir(c *C) {
83
dest := filepath.Join(s.dest, "bar")
84
_, err := os.Stat(dest)
85
c.Assert(os.IsNotExist(err), Equals, true)
86
err = helper(Install, filepath.Join(s.appg, "*"), dest, "foo_")
88
bs, err := ioutil.ReadFile(filepath.Join(dest, "foo_policygroups0"))
90
c.Check(string(bs), Equals, "apparmor/policygroups0")
93
func (s *policySuite) TestHelperBadTargetdir(c *C) {
94
err := helper(42, "/*", "/root/if-you-see-this-directory-something-is-horribly-wrong", "__")
95
c.Check(err, ErrorMatches, `.*unable.*make.*directory.*`)
98
func (s *policySuite) TestHelperBadFile(c *C) {
99
fn := filepath.Join(s.appg, "badbad")
100
c.Assert(os.Symlink(fn, fn), IsNil)
101
err := helper(42, filepath.Join(s.appg, "*"), s.dest, "foo_")
102
c.Check(err, ErrorMatches, ".*not a regular file.*")
105
func (s *policySuite) TestHelperBadOp(c *C) {
106
err := helper(42, filepath.Join(s.appg, "*"), s.dest, "foo_")
107
c.Check(err, ErrorMatches, ".*unknown operation.*")
110
func (s *policySuite) TestHelperInstallBadFilemode(c *C) {
111
fn := filepath.Join(s.appg, "policygroups0")
112
c.Assert(os.Chmod(fn, 0), IsNil)
113
err := helper(Install, filepath.Join(s.appg, "*"), s.dest, "foo_")
114
c.Check(err, ErrorMatches, ".*unable to read.*")
117
func (s *policySuite) TestHelperInstallBadTarget(c *C) {
118
c.Assert(os.Chmod(s.dest, 0), IsNil)
119
defer os.Chmod(s.dest, 0755)
120
err := helper(Install, filepath.Join(s.appg, "*"), s.dest, "foo_")
121
c.Check(err, ErrorMatches, ".*unable to create.*")
124
func (s *policySuite) TestHelperRemoveBadDirmode(c *C) {
125
err := helper(Install, filepath.Join(s.appg, "*"), s.dest, "foo_")
127
c.Assert(os.Chmod(s.dest, 0), IsNil)
128
defer os.Chmod(s.dest, 0755)
129
err = helper(Remove, filepath.Join(s.appg, "*"), s.dest, "foo_")
130
c.Assert(err, ErrorMatches, ".*unable to remove.*")
133
func (s *policySuite) TestFrameworkRoundtrip(c *C) {
134
origSecbase := secbase
136
defer func() { secbase = origSecbase }()
137
c.Check(FrameworkOp(Install, "foo", s.orig), IsNil)
138
// check the files were copied, with the packagename prepended properly
139
g, err := filepath.Glob(filepath.Join(secbase, "*", "*", "foo_*"))
141
c.Check(g, HasLen, 4*3)
142
c.Check(FrameworkOp(Remove, "foo", s.orig), IsNil)
143
g, err = filepath.Glob(filepath.Join(secbase, "*", "*", "*"))
145
c.Check(g, HasLen, 0)
148
func (s *policySuite) TestFrameworkError(c *C) {
149
// check we get errors from the helper, is all
150
c.Check(FrameworkOp(42, "foo", s.orig), ErrorMatches, ".*unknown operation.*")
153
func (s *policySuite) TestOpString(c *C) {
154
c.Check(fmt.Sprintf("%s", Install), Equals, "Install")
155
c.Check(fmt.Sprintf("%s", Remove), Equals, "Remove")