• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# -*- coding: utf-8 -*-
2#                     The LLVM Compiler Infrastructure
3#
4# This file is distributed under the University of Illinois Open Source
5# License. See LICENSE.TXT for details.
6
7import libear
8import libscanbuild.intercept as sut
9import unittest
10import os.path
11
12
13class InterceptUtilTest(unittest.TestCase):
14
15    def test_format_entry_filters_action(self):
16        def test(command):
17            trace = {'command': command, 'directory': '/opt/src/project'}
18            return list(sut.format_entry(trace))
19
20        self.assertTrue(test(['cc', '-c', 'file.c', '-o', 'file.o']))
21        self.assertFalse(test(['cc', '-E', 'file.c']))
22        self.assertFalse(test(['cc', '-MM', 'file.c']))
23        self.assertFalse(test(['cc', 'this.o', 'that.o', '-o', 'a.out']))
24
25    def test_format_entry_normalize_filename(self):
26        parent = os.path.join(os.sep, 'home', 'me')
27        current = os.path.join(parent, 'project')
28
29        def test(filename):
30            trace = {'directory': current, 'command': ['cc', '-c', filename]}
31            return list(sut.format_entry(trace))[0]['file']
32
33        self.assertEqual(os.path.join(current, 'file.c'), test('file.c'))
34        self.assertEqual(os.path.join(current, 'file.c'), test('./file.c'))
35        self.assertEqual(os.path.join(parent, 'file.c'), test('../file.c'))
36        self.assertEqual(os.path.join(current, 'file.c'),
37                         test(os.path.join(current, 'file.c')))
38
39    def test_sip(self):
40        def create_status_report(filename, message):
41            content = """#!/usr/bin/env sh
42                         echo 'sa-la-la-la'
43                         echo 'la-la-la'
44                         echo '{0}'
45                         echo 'sa-la-la-la'
46                         echo 'la-la-la'
47                      """.format(message)
48            lines = [line.strip() for line in content.split('\n')]
49            with open(filename, 'w') as handle:
50                handle.write('\n'.join(lines))
51                handle.close()
52            os.chmod(filename, 0x1ff)
53
54        def create_csrutil(dest_dir, status):
55            filename = os.path.join(dest_dir, 'csrutil')
56            message = 'System Integrity Protection status: {0}'.format(status)
57            return create_status_report(filename, message)
58
59        def create_sestatus(dest_dir, status):
60            filename = os.path.join(dest_dir, 'sestatus')
61            message = 'SELinux status:\t{0}'.format(status)
62            return create_status_report(filename, message)
63
64        ENABLED = 'enabled'
65        DISABLED = 'disabled'
66
67        OSX = 'darwin'
68        LINUX = 'linux'
69
70        with libear.TemporaryDirectory() as tmpdir:
71            try:
72                saved = os.environ['PATH']
73                os.environ['PATH'] = tmpdir + ':' + saved
74
75                create_csrutil(tmpdir, ENABLED)
76                self.assertTrue(sut.is_preload_disabled(OSX))
77
78                create_csrutil(tmpdir, DISABLED)
79                self.assertFalse(sut.is_preload_disabled(OSX))
80
81                create_sestatus(tmpdir, ENABLED)
82                self.assertTrue(sut.is_preload_disabled(LINUX))
83
84                create_sestatus(tmpdir, DISABLED)
85                self.assertFalse(sut.is_preload_disabled(LINUX))
86            finally:
87                os.environ['PATH'] = saved
88
89        try:
90            saved = os.environ['PATH']
91            os.environ['PATH'] = ''
92            # shall be false when it's not in the path
93            self.assertFalse(sut.is_preload_disabled(OSX))
94            self.assertFalse(sut.is_preload_disabled(LINUX))
95
96            self.assertFalse(sut.is_preload_disabled('unix'))
97        finally:
98            os.environ['PATH'] = saved
99