[apparmor] [patch] Add tests for aa.py get_output() and get_reqs()

Christian Boltz apparmor at cboltz.de
Mon Feb 1 18:35:07 UTC 2016


Hello,

$subject.

To make these tests independent from the underlaying system, add a
fake_ldd script that provides hardcoded ldd output for the "known"
executables and libraries.

To avoid interferences with the real system (especially symlinks), all
paths in fake_ldd have '/AATest' prepended.


[ 66-add-tests-for-get_output-and-get_reqs.diff ]

--- utils/test/test-aa.py       2016-01-26 22:22:14.660008000 +0100
+++ utils/test/test-aa.py       2016-02-01 18:53:10.085684909 +0100
@@ -14,8 +14,9 @@
 from common_test import read_file, write_file
 
 import os
 
-from apparmor.aa import (check_for_apparmor, get_interpreter_and_abstraction, create_new_profile,
+import apparmor.aa as aa  # needed to set global vars in some tests
+from apparmor.aa import (check_for_apparmor, get_output, get_reqs, get_interpreter_and_abstraction, create_new_profile,
      get_profile_flags, set_profile_flags, is_skippable_file, is_skippable_dir,
      parse_profile_start, parse_profile_data, separate_vars, store_list_var, write_header,
      var_transform, serialize_parse_profile_start)
@@ -72,6 +73,26 @@
         mounts = write_file(self.tmpdir, 'mounts', self.MOUNTS_WITH_SECURITYFS % self.tmpdir)
         self.assertEqual('%s/security/apparmor' % self.tmpdir, check_for_apparmor(filesystems, mounts))
 
+class AATest_get_output(AATest):
+    tests = [
+        (['./fake_ldd', '/AATest/lib64/libc-2.22.so'],  (0, ['        /AATest/lib64/ld-linux-x86-64.so.2 (0x0000556858473000)', '        linux-vdso.so.1 (0x00007ffe98912000)']     )),
+        (['./fake_ldd', '/tmp/aa-test-foo'],            (0, ['        not a dynamic executable']                                                                                    )),
+        (['./fake_ldd', 'invalid'],                     (1, ['']                                                                                                                    )),  # stderr is not part of output
+    ]
+    def _run_test(self, params, expected):
+        self.assertEqual(get_output(params), expected)
+
+class AATest_get_reqs(AATest):
+    tests = [
+        ('/AATest/bin/bash',    ['/AATest/lib64/libreadline.so.6', '/AATest/lib64/libtinfo.so.6', '/AATest/lib64/libdl.so.2', '/AATest/lib64/libc.so.6', '/AATest/lib64/ld-linux-x86-64.so.2']),
+        ('/tmp/aa-test-foo',    []),
+    ]
+
+    def _run_test(self, params, expected):
+        aa.cfg['settings']['ldd'] = './fake_ldd'
+
+        self.assertEqual(get_reqs(params), expected)
+
 class AaTest_create_new_profile(AATest):
     tests = [
         # file content              expected interpreter    expected abstraction (besides 'base')
--- utils/test/fake_ldd 2016-02-01 19:22:59.738008345 +0100
+++ utils/test/fake_ldd 2016-02-01 19:23:58.781645883 +0100
@@ -0,0 +1,56 @@
+#!/usr/bin/python3
+
+import sys
+
+if len(sys.argv) != 2:
+    raise Exception('wrong number of arguments in fake_ldd')
+
+if sys.argv[1] == '/AATest/bin/bash':
+    print('        linux-vdso.so.1 (0x00007ffcf97f4000)')
+    print('        libreadline.so.6 => /AATest/lib64/libreadline.so.6 (0x00007f2c41324000)')
+    print('        libtinfo.so.6 => /AATest/lib64/libtinfo.so.6 (0x00007f2c410f9000)')
+    print('        libdl.so.2 => /AATest/lib64/libdl.so.2 (0x00007f2c40ef5000)')
+    print('        libc.so.6 => /AATest/lib64/libc.so.6 (0x00007f2c40b50000)')
+    print('        /AATest/lib64/ld-linux-x86-64.so.2 (0x000055782c473000)')
+
+elif sys.argv[1] == '/AATest/lib64/ld-2.22.so':
+    print('        linux-vdso.so.1 (0x00007ffcf97f4000)')
+
+elif sys.argv[1] == '/AATest/lib64/libc-2.22.so':
+    print('        /AATest/lib64/ld-linux-x86-64.so.2 (0x0000556858473000)')
+    print('        linux-vdso.so.1 (0x00007ffe98912000)')
+
+elif sys.argv[1] == '/AATest/lib64/libdl.so.2':
+    print('        linux-vdso.so.1 (0x00007ffec2538000)')
+    print('        libc.so.6 => /AATest/lib64/libc.so.6 (0x00007f8865346000)')
+    print('        /AATest/lib64/ld-linux-x86-64.so.2 (0x0000560c3bcee000)')
+
+elif sys.argv[1] == '/AATest/lib64/libtinfo.so.6':
+    print('        linux-vdso.so.1 (0x00007fff30518000)')
+    print('        libc.so.6 => /AATest/lib64/libc.so.6 (0x00007fb6f2ea3000)')
+    print('        /AATest/lib64/ld-linux-x86-64.so.2 (0x00005631fe8d3000)')
+
+elif sys.argv[1] == '/AATest/lib64/libreadline.so.6':
+    print('        linux-vdso.so.1 (0x00007ffcb5b62000)')
+    print('        libtinfo.so.6 => /AATest/lib64/libtinfo.so.6 (0x00007f2a4ed07000)')
+    print('        libc.so.6 => /AATest/lib64/libc.so.6 (0x00007f2a4e961000)')
+    print('        /AATest/lib64/ld-linux-x86-64.so.2 (0x000055f749c89000)')
+
+elif sys.argv[1] == '/AATest/lib64/ld-linux-x86-64.so.2':
+    print('        statically linked')
+
+elif sys.argv[1] == '/AATest/lib64/libc.so.6':
+    print('        /AATest/lib64/ld-linux-x86-64.so.2 (0x000055b65f7a9000)')
+    print('        linux-vdso.so.1 (0x00007ffde132b000)')
+
+
+elif sys.argv[1].startswith('/tmp/aa-test-'):  # test file generated by test-aa.py
+    print('        not a dynamic executable')
+
+elif sys.argv[1] == 'TEMPLATE':
+    print('')
+    print('')
+    print('')
+
+else:
+    raise Exception('unknown parameter in fake_ldd: %s' % sys.argv[1])



Regards,

Christian Boltz
-- 
"Der wahrscheinlich ärgerlichste Aspekt eines Computerprogrammes
ist die Art und Weise, in der es auf Ihre Fehler reagiert"
[L. Lamport, LaTeX-Handbuch]
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: This is a digitally signed message part.
URL: <https://lists.ubuntu.com/archives/apparmor/attachments/20160201/f8b52d88/attachment.pgp>


More information about the AppArmor mailing list