~apparmor-dev/apparmor/apparmor-ubuntu-citrain-trusty

« back to all changes in this revision

Viewing changes to libraries/libapparmor/swig/python/test/test_python.py.in

  • Committer: Seth Arnold
  • Date: 2014-03-12 02:05:16 UTC
  • mto: This revision was merged to the branch mainline in revision 1496.
  • Revision ID: seth.arnold@canonical.com-20140312020516-zjike3pmw6hi861h
[ Jamie Strandboge ]
 * debian/debhelper/dh_apparmor: exit with error if aa-easyprof does not
   exist
 * debian/control: drop Depends on apparmor-easyprof to Suggests for
   dh-apparmor
[ Seth Arnold, Jamie Strandboge, Steve Beattie, John Johansen, Tyler Hicks ]
* New upstream snapshot (LP: #1278702, #1061693, #1285653) dropping very
  large Ubuntu delta and fixing the following bugs:
  - Adjust fonts abstraction for libthai (LP: #1278702)
  - Support translated XDG user directories (LP: #1061693)
  - Adjust abstractions/web-data to include /var/www/html (LP: #1285653)
    Refresh 0002-add-debian-integration-to-lighttpd.patch to include
    /etc/lighttpd/conf-available/*.conf
  - Adjust debian/libapparmor1.symbols to reflect new upstream versioning
    for the aa_query_label() function
  - Raise exceptions in Python bindings when something fails
* ship new Python replacements for previous Perl-based tools
  - debian/apparmor-utils.install: remove usr/share/perl5/Immunix/*.pm and add
    usr/sbin/aa-autodep, usr/sbin/aa-cleanprof and usr/sbin/aa-mergeprof
  - debian/control:
    + remove various Perl dependencies
    + add python-apparmor and python3-apparmor
    + python3-apparmor Breaks: apparmor-easyprof to move the file since it
      ships dist-packages/apparmor/__init__.py now
  - debian/apparmor-utils.manpages: ship new manpages for aa-cleanprof and
    aa-mergeprof
  - debian/rules: build and install Python tools
* debian/apparmor.install:
  - install apparmorfs, dovecot, kernelvars, securityfs, sys,
    and xdg-user-dirs tunables and xdg-user-dirs.d directory
* debian/apparmor.dirs:
  - install /etc/apparmor.d/tunables/xdg-user-dirs.d
* debian/apparmor.postinst: create xdg-user-dirs.d
* debian/apparmor.postrm: remove xdg-user-dirs.d
* Remaining patches:
  - 0001-add-chromium-browser.patch
  - 0002-add-debian-integration-to-lighttpd.patch
  - 0003-ubuntu-manpage-updates.patch
  - 0004-libapparmor-layout-deb.patch (renamed from 0008)
  - 0005-libapparmor-mention-dbus-method-in-getcon-man.patch (renamed from
    0068)
  - 0006-etc-writable.patch (renamed from 0070)
  - 0007-aa-utils_are_bilingual.patch (renamed from 0077)
  - 0008-remove-ptrace.patch
  - 0009-convert-to-rules.patch
  - 0010-list-fns.patch
  - 0011-parse-mode.patch
  - 0012-add-decimal-interp.patch
  - 0013-policy_mediates.patch
  - 0014-fix-failpath.patch
  - 0015-feature_file.patch
  - 0016-fix-network.patch
  - 0017-aare-to-class.patch
  - 0018-add-mediation-unix.patch
  - 0019-parser_version.patch
  - 0020-caching.patch
  - 0021-label-class.patch
  - 0022-signal.patch
  - 0023-fix-lexer-debug.patch
  - 0024-ptrace.patch
  - 0025-use-diff-encode.patch
  - 0026-fix-serialize.patch
  - 0027-fix-af.patch
  - 0028-opt_arg.patch
  - 0029-tests-cond-dbus.patch
  - 0030-tests.diff
* Move manpages from libapparmor1 to libapparmor-dev
  - debian/libapparmor-dev.manpages: install aa_change_hat.2,
    aa_change_profile.2, aa_find_mountpoint.2, aa_getcon.2
  - debian/control: libapparmor-dev Replaces: and Breaks: libapparmor1
* Move /usr/lib/python3/dist-packages/apparmor/__init__.py from
  apparmor-easyprof to python3-apparmor
  - debian/control: python3-apparmor Breaks: apparmor-easyprof
  - debian/apparmor-easyprof.install: remove
    usr/lib/python*.*/site-packages/apparmor*
* New profiles and abstractions:
  - debian/apparmor.install: tunables/dovecot, tunables/kernelvars,
    tunables/xdg-user-dirs, tunables/xdg-user-dirs.d
* Test merge from upstream new pyutils branch (rev 2385)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! @PYTHON@
 
2
# ------------------------------------------------------------------
 
3
#
 
4
#   Copyright (C) 2013 Canonical Ltd.
 
5
#   Author: Steve Beattie <steve@nxnw.org>
 
6
#
 
7
#   This program is free software; you can redistribute it and/or
 
8
#   modify it under the terms of version 2 of the GNU General Public
 
9
#   License published by the Free Software Foundation.
 
10
#
 
11
# ------------------------------------------------------------------
 
12
 
 
13
import ctypes
 
14
import os
 
15
import unittest
 
16
import LibAppArmor as libapparmor
 
17
 
 
18
TESTDIR = "../../../testsuite/test_multi"
 
19
 
 
20
# map of testsuite .out entries that aren't a simple to_lower() and
 
21
# s/ /_/ of the field name
 
22
 
 
23
OUTPUT_MAP = {
 
24
    'Event type':       'event',
 
25
    'Mask':             'requested_mask',
 
26
    'Command':          'comm',
 
27
    'Token':            'magic_token',
 
28
    'ErrorCode':        'error_code',
 
29
    'Network family':   'net_family',
 
30
    'Socket type':      'net_sock_type',
 
31
    'Protocol':         'net_protocol',
 
32
    'Local addr':       'net_local_addr',
 
33
    'Foreign addr':     'net_foreign_addr',
 
34
    'Local port':       'net_local_port',
 
35
    'Foreign port':     'net_foreign_port',
 
36
    'Audit subid':      'audit_sub_id',
 
37
}
 
38
 
 
39
# FIXME: pull this automatically out of LibAppArmor, but swig
 
40
# classes aren't real enough. (Perhaps we're not using swig correctly)
 
41
EVENT_MAP = {
 
42
    libapparmor.AA_RECORD_ALLOWED:  'AA_RECORD_ALLOWED',
 
43
    libapparmor.AA_RECORD_AUDIT:    'AA_RECORD_AUDIT',
 
44
    libapparmor.AA_RECORD_DENIED:   'AA_RECORD_DENIED',
 
45
    libapparmor.AA_RECORD_ERROR:    'AA_RECORD_ERROR',
 
46
    libapparmor.AA_RECORD_HINT:     'AA_RECORD_HINT',
 
47
    libapparmor.AA_RECORD_INVALID:  'AA_RECORD_INVALID',
 
48
    libapparmor.AA_RECORD_STATUS:   'AA_RECORD_STATUS',
 
49
}
 
50
 
 
51
# default is None if not in this table
 
52
NO_VALUE_MAP = {
 
53
    'fsuid': int(ctypes.c_ulong(-1).value),
 
54
    'ouid':  int(ctypes.c_ulong(-1).value),
 
55
}
 
56
 
 
57
 
 
58
class AAPythonBindingsTests(unittest.TestCase):
 
59
 
 
60
    def setUp(self):
 
61
        # REPORT ALL THE OUTPUT
 
62
        self.maxDiff = None
 
63
 
 
64
    def _runtest(self, testname):
 
65
        infile = "%s.in" % (testname)
 
66
        outfile = "%s.out" % (testname)
 
67
        # infile *should* only contain one line
 
68
        with open(os.path.join(TESTDIR, infile), 'r') as f:
 
69
            line = f.read()
 
70
            swig_record = libapparmor.parse_record(line)
 
71
 
 
72
        record = self.create_record_dict(swig_record)
 
73
        record['file'] = infile
 
74
        libapparmor.free_record(swig_record)
 
75
 
 
76
        expected = self.parse_output_file(outfile)
 
77
        self.assertEquals(expected, record,
 
78
                          "expected records did not match\n" +
 
79
                          "expected = %s\nactual = %s" % (expected, record))
 
80
 
 
81
    def parse_output_file(self, outfile):
 
82
        '''parse testcase .out file and return dict'''
 
83
 
 
84
        output = dict()
 
85
        with open(os.path.join(TESTDIR, outfile), 'r') as f:
 
86
            lines = f.readlines()
 
87
 
 
88
        count = 0
 
89
        for l in lines:
 
90
            line = l.rstrip('\n')
 
91
            count += 1
 
92
            if line == "START":
 
93
                self.assertEquals(count, 1,
 
94
                                  "Unexpected output format in %s" % (outfile))
 
95
                continue
 
96
            else:
 
97
                key, value = line.split(": ", 1)
 
98
                if key in OUTPUT_MAP:
 
99
                    newkey = OUTPUT_MAP[key]
 
100
                else:
 
101
                    newkey = key.lower().replace(' ', '_')
 
102
                # check if entry already exists?
 
103
                output[newkey] = value
 
104
 
 
105
        return output
 
106
 
 
107
    def create_record_dict(self, record):
 
108
        '''parse the swig created record and construct a dict from it'''
 
109
 
 
110
        new_record = dict()
 
111
        for key in [x for x in dir(record) if not (x.startswith('_') or x == 'this')]:
 
112
            value = record.__getattr__(key)
 
113
            if key == "event" and value in EVENT_MAP:
 
114
                new_record[key] = EVENT_MAP[value]
 
115
            elif key == "version":
 
116
                # FIXME: out files should report log version?
 
117
                # FIXME: or can we just deprecate v1 logs?
 
118
                continue
 
119
            elif key in NO_VALUE_MAP:
 
120
                if NO_VALUE_MAP[key] == value:
 
121
                    continue
 
122
                else:
 
123
                    new_record[key] = str(value)
 
124
            elif record.__getattr__(key):
 
125
                new_record[key] = str(value)
 
126
 
 
127
        return new_record
 
128
 
 
129
 
 
130
def find_testcases(testdir):
 
131
    '''dig testcases out of passed directory'''
 
132
 
 
133
    for f in os.listdir(testdir):
 
134
        if f.endswith(".in"):
 
135
            yield f[:-3]
 
136
 
 
137
 
 
138
def main():
 
139
    for f in find_testcases(TESTDIR):
 
140
        def stub_test(self, testname=f):
 
141
            self._runtest(testname)
 
142
        stub_test.__doc__ = "test %s" % (f)
 
143
        setattr(AAPythonBindingsTests, 'test_%s' % (f), stub_test)
 
144
    return unittest.main(verbosity=2)
 
145
 
 
146
if __name__ == "__main__":
 
147
    main()