blob: e633c907364a2a7850f4d266899d20e59a3620a3 [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2018 The ChromiumOS Authors
Ned Nguyenbf081d02018-12-18 16:41:31 -07002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
Robert Kolchmeyerd14321e2023-07-11 17:36:25 -07005"""Test gconv_strip.
Ned Nguyenbf081d02018-12-18 16:41:31 -07006
Robert Kolchmeyerd14321e2023-07-11 17:36:25 -07007To run these tests, do the following inside the chroot:
8$ pytest -c /dev/null scripts/gconv_strip_unittest.py
9
10Explicitly using an empty pytest config is necessary because chromite's
11pytest.ini assumes pytest-xdist is installed, which it is not inside the chroot.
12"""
13
14import glob
Ned Nguyenbf081d02018-12-18 16:41:31 -070015import os
16
17from chromite.lib import cros_test_lib
18from chromite.lib import osutils
19from chromite.scripts import gconv_strip
20
Mike Frysinger807d8282022-04-28 22:45:17 -040021
Greg Edelstona4c9b3b2020-01-07 17:51:13 -070022pytestmark = cros_test_lib.pytestmark_inside_only
23
Ned Nguyenbf081d02018-12-18 16:41:31 -070024
Robert Kolchmeyerd14321e2023-07-11 17:36:25 -070025class GconvStripTest(cros_test_lib.MockTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -060026 """Tests for gconv_strip script."""
Ned Nguyenbf081d02018-12-18 16:41:31 -070027
Alex Klein1699fab2022-09-08 08:46:06 -060028 def testMultipleStringMatch(self):
29 self.assertEqual(
30 gconv_strip.MultipleStringMatch(
31 [b"hell", b"a", b"z", b"k", b"spec"],
32 b"hello_from a very special place",
33 ),
34 [True, True, False, False, True],
35 )
Ned Nguyenbf081d02018-12-18 16:41:31 -070036
Alex Klein1699fab2022-09-08 08:46:06 -060037 def testModuleRewrite(self):
38 tmp_gconv_module = os.path.join(self.tempdir, "gconv-modules")
Ned Nguyenbf081d02018-12-18 16:41:31 -070039
Alex Klein1699fab2022-09-08 08:46:06 -060040 data = """
Ned Nguyenbf081d02018-12-18 16:41:31 -070041# from to module cost
42alias FOO charset_foo
43alias BAR charset_bar
44module charset_foo charset_bar UNUSED_MODULE
45
46# from to module cost
47alias CHAR_A charset_A
48alias EUROPE charset_B
49module charset_A charset_B USED_MODULE
50module charset_foo charset_A USED_MODULE
51"""
Alex Klein1699fab2022-09-08 08:46:06 -060052 osutils.WriteFile(tmp_gconv_module, data)
Ned Nguyenbf081d02018-12-18 16:41:31 -070053
Robert Kolchmeyerd14321e2023-07-11 17:36:25 -070054 gmods = gconv_strip.GconvModules(
55 tmp_gconv_module, os.path.dirname(tmp_gconv_module)
56 )
Alex Klein1699fab2022-09-08 08:46:06 -060057 self.assertEqual(
58 gmods.Load(),
59 [
60 "BAR",
61 "CHAR_A",
62 "EUROPE",
63 "FOO",
64 "charset_A",
65 "charset_B",
66 "charset_bar",
67 "charset_foo",
68 ],
69 )
70 self.PatchObject(gconv_strip.lddtree, "ParseELF", return_value={})
Ned Nguyenbf081d02018-12-18 16:41:31 -070071
Alex Klein074f94f2023-06-22 10:32:06 -060072 class _StubStat:
Alex Klein1699fab2022-09-08 08:46:06 -060073 """Fake for lstat."""
74
75 st_size = 0
76
77 self.PatchObject(gconv_strip.os, "lstat", return_value=_StubStat)
78 self.PatchObject(gconv_strip.os, "unlink")
Mike Frysinger61b792c2023-02-02 09:02:27 -050079 gmods.Rewrite(["charset_A", "charset_B"], dryrun=False)
Alex Klein1699fab2022-09-08 08:46:06 -060080
81 expected = """
Ned Nguyenbf081d02018-12-18 16:41:31 -070082# from to module cost
83alias FOO charset_foo
84
85# from to module cost
86alias CHAR_A charset_A
87alias EUROPE charset_B
88module charset_A charset_B USED_MODULE
89module charset_foo charset_A USED_MODULE
90"""
91
Alex Klein1699fab2022-09-08 08:46:06 -060092 content = osutils.ReadFile(tmp_gconv_module)
93 self.assertEqual(content, expected)
Robert Kolchmeyerd14321e2023-07-11 17:36:25 -070094
95 def testGconvStrip(self):
96 """Tests GconvStrip end-to-end.
97
98 Creates a fake root directory with fake gconv modules, and expects the
99 non-sticky modules to be deleted.
100 """
101 modules_dir = os.path.join(self.tempdir, "usr", "lib64", "gconv")
102 extras_dir = os.path.join(modules_dir, "gconv-modules.d")
103 os.makedirs(extras_dir)
104 tmp_gconv_modules = os.path.join(modules_dir, "gconv-modules")
105 tmp_gconv_extras = os.path.join(extras_dir, "gconv-modules-extras.conf")
106
107 gconv_data = """
108# from to module cost
109alias UTF32// UTF-32//
110module UTF-32// INTERNAL UTF-32 1
111module INTERNAL UTF-32// UTF-32 1
112
113# from to module cost
114alias UTF7// UTF-7//
115module UTF-7// INTERNAL UTF-7 1
116module INTERNAL UTF-7// UTF-7 1
117"""
118 gconv_extras_data = """
119# from to module cost
120alias UTF16// UTF-16//
121module UTF-16// INTERNAL UTF-16 1
122module INTERNAL UTF-16// UTF-16 1
123
124# from to module cost
125alias EUCTW// EUC-TW//
126alias OSF0005000a// EUC-TW//
127module EUC-TW// INTERNAL EUC-TW 1
128module INTERNAL EUC-TW// EUC-TW 1
129"""
130 osutils.WriteFile(tmp_gconv_modules, gconv_data)
131 osutils.WriteFile(tmp_gconv_extras, gconv_extras_data)
132 for module in ["UTF-32.so", "UTF-7.so", "UTF-16.so", "EUC-TW.so"]:
133 osutils.Touch(os.path.join(modules_dir, module))
134
135 self.PatchObject(gconv_strip.lddtree, "ParseELF", return_value={})
136
137 class _StubOpts:
138 """Stub for GconvStrip args."""
139
140 def __init__(self, root):
141 self.root = root
142 self.dryrun = False
143
144 gconv_strip.GconvStrip(_StubOpts(self.tempdir))
145
146 expected_gconv_data = """
147# from to module cost
148alias UTF32// UTF-32//
149module UTF-32// INTERNAL UTF-32 1
150module INTERNAL UTF-32// UTF-32 1
151
152# from to module cost
153"""
154 expected_gconv_extras_data = """
155# from to module cost
156alias UTF16// UTF-16//
157module UTF-16// INTERNAL UTF-16 1
158module INTERNAL UTF-16// UTF-16 1
159
160# from to module cost
161"""
162 expected_modules = ["UTF-16.so", "UTF-32.so"]
163 actual_gconv_data = osutils.ReadFile(tmp_gconv_modules)
164 actual_gconv_extras_data = osutils.ReadFile(tmp_gconv_extras)
165 actual_modules = glob.glob(os.path.join(modules_dir, "*.so"))
166 actual_modules_names = sorted(
167 os.path.basename(x) for x in actual_modules
168 )
169 self.assertEqual(actual_gconv_data, expected_gconv_data)
170 self.assertEqual(actual_gconv_extras_data, expected_gconv_extras_data)
171 self.assertEqual(actual_modules_names, expected_modules)