1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 #
4 # Copyright © 2022 Emmanuel Fleury <emmanuel.fleury@gmail.com>
5 # Copyright © 2022 Marco Trevisan <mail@3v1n0.net>
6 #
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License, or (at your option) any later version.
11 #
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 # MA 02110-1301 USA
21
22 """ Integration tests for g_message functions on low-memory. """
23
24 import collections
25 import os
26 import subprocess
27 import unittest
28
29 import taptestrunner
30
31 Result = collections.namedtuple("Result", ("info", "out", "err"))
32
33
34 class ESC[4;38;5;81mTestMessagesLowMemory(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
35 """Integration test for checking g_message()’s behavior on low memory.
36
37 This can be run when installed or uninstalled. When uninstalled,
38 it requires G_TEST_BUILDDIR and G_TEST_SRCDIR to be set.
39
40 The idea with this test harness is to test if g_message and friends
41 assert instead of crashing if memory is exhausted, printing the expected
42 error message.
43 """
44
45 test_binary = "messages-low-memory"
46
47 def setUp(self):
48 ext = ""
49 if os.name == "nt":
50 ext = ".exe"
51 if "G_TEST_BUILDDIR" in os.environ:
52 self._test_binary = os.path.join(
53 os.environ["G_TEST_BUILDDIR"], self.test_binary + ext
54 )
55 else:
56 self._test_binary = os.path.join(
57 os.path.dirname(__file__), self.test_binary + ext
58 )
59 print("messages-low-memory:", self._test_binary)
60
61 def runTestBinary(self, *args):
62 print("Running:", *args)
63
64 env = os.environ.copy()
65 env["LC_ALL"] = "C.UTF-8"
66 env["G_DEBUG"] = "fatal-warnings"
67 print("Environment:", env)
68
69 # We want to ensure consistent line endings...
70 info = subprocess.run(
71 *args,
72 stdout=subprocess.PIPE,
73 stderr=subprocess.PIPE,
74 env=env,
75 universal_newlines=True,
76 )
77 out = info.stdout.strip()
78 err = info.stderr.strip()
79
80 result = Result(info, out, err)
81
82 print("Return code:", result.info.returncode)
83 print("Output:", result.out)
84 print("Error:", result.err)
85 return result
86
87 def test_message_memory_allocation_failure(self):
88 """Test running g_message() when memory is exhausted."""
89 result = self.runTestBinary(self._test_binary)
90
91 if result.info.returncode == 77:
92 self.skipTest("Not supported")
93
94 if os.name == "nt":
95 self.assertEqual(result.info.returncode, 3)
96 else:
97 self.assertEqual(result.info.returncode, -6)
98 self.assertIn("failed to allocate memory", result.err)
99
100
101 if __name__ == "__main__":
102 unittest.main(testRunner=taptestrunner.TAPTestRunner())