aboutsummaryrefslogtreecommitdiff
path: root/pkg/apparmor/apparmor_linux_test.go
blob: ac3260723b06e0e0428b54d5d119b5eddc80be40 (plain)
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
// +build linux,apparmor

package apparmor

import (
	"os"
	"testing"
)

type versionExpected struct {
	output  string
	version int
}

func TestParseAAParserVersion(t *testing.T) {
	if !IsEnabled() {
		t.Skip("AppArmor disabled: skipping tests")
	}
	versions := []versionExpected{
		{
			output: `AppArmor parser version 2.10
Copyright (C) 1999-2008 Novell Inc.
Copyright 2009-2012 Canonical Ltd.

`,
			version: 210000,
		},
		{
			output: `AppArmor parser version 2.8
Copyright (C) 1999-2008 Novell Inc.
Copyright 2009-2012 Canonical Ltd.

`,
			version: 208000,
		},
		{
			output: `AppArmor parser version 2.20
Copyright (C) 1999-2008 Novell Inc.
Copyright 2009-2012 Canonical Ltd.

`,
			version: 220000,
		},
		{
			output: `AppArmor parser version 2.05
Copyright (C) 1999-2008 Novell Inc.
Copyright 2009-2012 Canonical Ltd.

`,
			version: 205000,
		},
		{
			output: `AppArmor parser version 2.9.95
Copyright (C) 1999-2008 Novell Inc.
Copyright 2009-2012 Canonical Ltd.

`,
			version: 209095,
		},
		{
			output: `AppArmor parser version 3.14.159
Copyright (C) 1999-2008 Novell Inc.
Copyright 2009-2012 Canonical Ltd.

`,
			version: 314159,
		},
	}

	for _, v := range versions {
		version, err := parseAAParserVersion(v.output)
		if err != nil {
			t.Fatalf("expected error to be nil for %#v, got: %v", v, err)
		}
		if version != v.version {
			t.Fatalf("expected version to be %d, was %d, for: %#v\n", v.version, version, v)
		}
	}
}

func TestInstallDefault(t *testing.T) {
	profile := "libpod-default-testing"
	aapath := "/sys/kernel/security/apparmor/"

	if _, err := os.Stat(aapath); err != nil {
		t.Skip("AppArmor isn't available in this environment")
	}

	// removes `profile`
	removeProfile := func() error {
		path := aapath + ".remove"

		f, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
		if err != nil {
			return err
		}
		defer f.Close()

		_, err = f.WriteString(profile)
		return err
	}

	// makes sure `profile` is loaded according to `state`
	checkLoaded := func(state bool) {
		loaded, err := IsLoaded(profile)
		if err != nil {
			t.Fatalf("Error searching AppArmor profile '%s': %v", profile, err)
		}
		if state != loaded {
			if state {
				t.Fatalf("AppArmor profile '%s' isn't loaded but should", profile)
			} else {
				t.Fatalf("AppArmor profile '%s' is loaded but shouldn't", profile)
			}
		}
	}

	// test installing the profile
	if err := InstallDefault(profile); err != nil {
		t.Fatalf("Couldn't install AppArmor profile '%s': %v", profile, err)
	}
	checkLoaded(true)

	// remove the profile and check again
	if err := removeProfile(); err != nil {
		t.Fatalf("Couldn't remove AppArmor profile '%s': %v", profile, err)
	}
	checkLoaded(false)
}