~chipaca/snappy/snappy-tar-pack-mknod

« back to all changes in this revision

Viewing changes to policy/policy_test.go

  • Committer: John R. Lenton
  • Date: 2015-04-02 20:30:49 UTC
  • mto: (289.9.12 framework-policy)
  • mto: This revision was merged to the branch mainline in revision 318.
  • Revision ID: jlenton@gmail.com-20150402203049-d1m1cdiwoc1a8dko
moved policy to its own package; incorporated fixes via jdstrand

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2014-2015 Canonical Ltd
 
3
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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/>.
 
15
 *
 
16
 */
 
17
 
 
18
package policy
 
19
 
 
20
import (
 
21
        "fmt"
 
22
        "io/ioutil"
 
23
        "os"
 
24
        "path/filepath"
 
25
        "testing"
 
26
 
 
27
        . "launchpad.net/gocheck"
 
28
        "sort"
 
29
)
 
30
 
 
31
// Hook up gocheck into the "go test" runner.
 
32
func Test(t *testing.T) { TestingT(t) }
 
33
 
 
34
type policySuite struct {
 
35
        orig string
 
36
        dest string
 
37
        appg string
 
38
}
 
39
 
 
40
var _ = Suite(&policySuite{})
 
41
 
 
42
func (s *policySuite) SetUpTest(c *C) {
 
43
        s.orig = c.MkDir()
 
44
        s.dest = c.MkDir()
 
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)
 
54
                        }
 
55
                }
 
56
        }
 
57
}
 
58
 
 
59
func (s *policySuite) TestHelperInstallRemove(c *C) {
 
60
        err := helper(Install, filepath.Join(s.appg, "*"), s.dest, "foo_")
 
61
        c.Check(err, IsNil)
 
62
        g, err := filepath.Glob(filepath.Join(s.dest, "*"))
 
63
        c.Check(err, IsNil)
 
64
        c.Assert(g, HasLen, 3)
 
65
        // Glob already sorts the returned list, but that's not documented
 
66
        sort.Strings(g)
 
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])
 
72
        c.Check(err, IsNil)
 
73
        c.Check(string(bs), Equals, "apparmor/policygroups0")
 
74
        // now, remove it
 
75
        err = helper(Remove, filepath.Join(s.appg, "*"), s.dest, "foo_")
 
76
        c.Check(err, IsNil)
 
77
        g, err = filepath.Glob(filepath.Join(s.dest, "*"))
 
78
        c.Check(err, IsNil)
 
79
        c.Check(g, HasLen, 0)
 
80
}
 
81
 
 
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_")
 
87
        c.Check(err, IsNil)
 
88
        bs, err := ioutil.ReadFile(filepath.Join(dest, "foo_policygroups0"))
 
89
        c.Check(err, IsNil)
 
90
        c.Check(string(bs), Equals, "apparmor/policygroups0")
 
91
}
 
92
 
 
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.*`)
 
96
}
 
97
 
 
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.*")
 
103
}
 
104
 
 
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.*")
 
108
}
 
109
 
 
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.*")
 
115
}
 
116
 
 
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.*")
 
122
}
 
123
 
 
124
func (s *policySuite) TestHelperRemoveBadDirmode(c *C) {
 
125
        err := helper(Install, filepath.Join(s.appg, "*"), s.dest, "foo_")
 
126
        c.Assert(err, IsNil)
 
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.*")
 
131
}
 
132
 
 
133
func (s *policySuite) TestFrameworkRoundtrip(c *C) {
 
134
        origSecbase := secbase
 
135
        secbase = s.dest
 
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_*"))
 
140
        c.Check(err, IsNil)
 
141
        c.Check(g, HasLen, 4*3)
 
142
        c.Check(FrameworkOp(Remove, "foo", s.orig), IsNil)
 
143
        g, err = filepath.Glob(filepath.Join(secbase, "*", "*", "*"))
 
144
        c.Check(err, IsNil)
 
145
        c.Check(g, HasLen, 0)
 
146
}
 
147
 
 
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.*")
 
151
}
 
152
 
 
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")
 
156
}