1#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6
7from oeqa.selftest.case import OESelftestTestCase
8from oeqa.core.decorator import OETestTag
9from oeqa.utils.commands import runCmd, bitbake, get_bb_var, get_bb_vars, runqemu
10from oeqa.utils.sshcontrol import SSHControl
11import glob
12import os
13import json
14
15class ImageFeatures(OESelftestTestCase):
16
17    test_user = 'tester'
18    root_user = 'root'
19
20    @OETestTag("runqemu")
21    def test_non_root_user_can_connect_via_ssh_without_password(self):
22        """
23        Summary: Check if non root user can connect via ssh without password
24        Expected: 1. Connection to the image via ssh using root user without providing a password should be allowed.
25                  2. Connection to the image via ssh using tester user without providing a password should be allowed.
26        Product: oe-core
27        Author: Ionut Chisanovici <ionutx.chisanovici@intel.com>
28        AutomatedBy: Daniel Istrate <daniel.alexandrux.istrate@intel.com>
29        """
30
31        features = 'EXTRA_IMAGE_FEATURES = "ssh-server-openssh empty-root-password allow-empty-password allow-root-login"\n'
32        features += 'INHERIT += "extrausers"\n'
33        features += 'EXTRA_USERS_PARAMS = "useradd -p \'\' {}; usermod -s /bin/sh {};"'.format(self.test_user, self.test_user)
34        self.write_config(features)
35
36        # Build a core-image-minimal
37        bitbake('core-image-minimal')
38
39        with runqemu("core-image-minimal") as qemu:
40            # Attempt to ssh with each user into qemu with empty password
41            for user in [self.root_user, self.test_user]:
42                ssh = SSHControl(ip=qemu.ip, logfile=qemu.sshlog, user=user)
43                status, output = ssh.run("true")
44                self.assertEqual(status, 0, 'ssh to user %s failed with %s' % (user, output))
45
46    @OETestTag("runqemu")
47    def test_all_users_can_connect_via_ssh_without_password(self):
48        """
49        Summary:     Check if all users can connect via ssh without password
50        Expected: 1. Connection to the image via ssh using root user without providing a password should NOT be allowed.
51                  2. Connection to the image via ssh using tester user without providing a password should be allowed.
52        Product:     oe-core
53        Author:      Ionut Chisanovici <ionutx.chisanovici@intel.com>
54        AutomatedBy: Daniel Istrate <daniel.alexandrux.istrate@intel.com>
55        """
56
57        features = 'EXTRA_IMAGE_FEATURES = "ssh-server-openssh allow-empty-password allow-root-login"\n'
58        features += 'INHERIT += "extrausers"\n'
59        features += 'EXTRA_USERS_PARAMS = "useradd -p \'\' {}; usermod -s /bin/sh {};"'.format(self.test_user, self.test_user)
60        self.write_config(features)
61
62        # Build a core-image-minimal
63        bitbake('core-image-minimal')
64
65        with runqemu("core-image-minimal") as qemu:
66            # Attempt to ssh with each user into qemu with empty password
67            for user in [self.root_user, self.test_user]:
68                ssh = SSHControl(ip=qemu.ip, logfile=qemu.sshlog, user=user)
69                status, output = ssh.run("true")
70                if user == 'root':
71                    self.assertNotEqual(status, 0, 'ssh to user root was allowed when it should not have been')
72                else:
73                    self.assertEqual(status, 0, 'ssh to user tester failed with %s' % output)
74
75
76    def test_wayland_support_in_image(self):
77        """
78        Summary:     Check Wayland support in image
79        Expected:    1. Wayland image can be build
80                     2. Wayland feature can be installed
81        Product:     oe-core
82        Author:      Ionut Chisanovici <ionutx.chisanovici@intel.com>
83        AutomatedBy: Daniel Istrate <daniel.alexandrux.istrate@intel.com>
84        """
85
86        distro_features = get_bb_var('DISTRO_FEATURES')
87        if not ('opengl' in distro_features and 'wayland' in distro_features):
88            self.skipTest('neither opengl nor wayland present on DISTRO_FEATURES so core-image-weston cannot be built')
89
90        # Build a core-image-weston
91        bitbake('core-image-weston')
92
93    def test_bmap(self):
94        """
95        Summary:     Check bmap support
96        Expected:    1. core-image-minimal can be build with bmap support
97                     2. core-image-minimal is sparse
98        Product:     oe-core
99        Author:      Ed Bartosh <ed.bartosh@linux.intel.com>
100        """
101
102        features = 'IMAGE_FSTYPES += " ext4 ext4.bmap ext4.bmap.gz"'
103        self.write_config(features)
104
105        image = 'core-image-minimal'
106        bitbake(image)
107        bb_vars = get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_LINK_NAME'], image)
108
109        image_path = os.path.join(bb_vars['DEPLOY_DIR_IMAGE'], "%s.ext4" % bb_vars['IMAGE_LINK_NAME'])
110        bmap_path = "%s.bmap" % image_path
111        gzip_path = "%s.gz" % bmap_path
112
113        # check if result image, bmap and bmap.gz files are in deploy directory
114        self.assertTrue(os.path.exists(image_path))
115        self.assertTrue(os.path.exists(bmap_path))
116        self.assertTrue(os.path.exists(gzip_path))
117
118        # check if result image is sparse
119        image_stat = os.stat(image_path)
120        self.assertGreater(image_stat.st_size, image_stat.st_blocks * 512)
121
122        # check if the resulting gzip is valid, --force is needed in case gzip_path is a symlink
123        self.assertTrue(runCmd('gzip --test --force %s' % gzip_path))
124
125    def test_hypervisor_fmts(self):
126        """
127        Summary:     Check various hypervisor formats
128        Expected:    1. core-image-minimal can be built with vmdk, vdi and
129                        qcow2 support.
130                     2. qemu-img says each image has the expected format
131        Product:     oe-core
132        Author:      Tom Rini <trini@konsulko.com>
133        """
134
135        img_types = [ 'vmdk', 'vdi', 'qcow2' ]
136        features = ""
137        for itype in img_types:
138            features += 'IMAGE_FSTYPES += "ext4.%s"\n' % itype
139        self.write_config(features)
140
141        image = 'core-image-minimal'
142        bitbake(image)
143        bb_vars = get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_LINK_NAME'], image)
144
145        for itype in img_types:
146            image_path = os.path.join(bb_vars['DEPLOY_DIR_IMAGE'], "%s.ext4.%s" %
147                                      (bb_vars['IMAGE_LINK_NAME'], itype))
148
149            # check if result image file is in deploy directory
150            self.assertTrue(os.path.exists(image_path))
151
152            # check if result image is vmdk
153            sysroot = get_bb_var('STAGING_DIR_NATIVE', 'core-image-minimal')
154            result = runCmd('qemu-img info --output json %s' % image_path,
155                            native_sysroot=sysroot)
156            try:
157                data = json.loads(result.output)
158                self.assertEqual(data.get('format'), itype,
159                                 msg="Unexpected format in '%s'" % (result.output))
160            except json.decoder.JSONDecodeError:
161                self.fail("Could not parse '%ss'" % result.output)
162
163    def test_long_chain_conversion(self):
164        """
165        Summary:     Check for chaining many CONVERSION_CMDs together
166        Expected:    1. core-image-minimal can be built with
167                        ext4.bmap.gz.bz2.zst.xz.u-boot and also create a
168                        sha256sum
169                     2. The above image has a valid sha256sum
170        Product:     oe-core
171        Author:      Tom Rini <trini@konsulko.com>
172        """
173
174        conv = "ext4.bmap.gz.bz2.zst.xz.u-boot"
175        features = 'IMAGE_FSTYPES += "%s %s.sha256sum"' % (conv, conv)
176        self.write_config(features)
177
178        image = 'core-image-minimal'
179        bitbake(image)
180        bb_vars = get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_LINK_NAME'], image)
181        image_path = os.path.join(bb_vars['DEPLOY_DIR_IMAGE'], "%s.%s" %
182                                  (bb_vars['IMAGE_LINK_NAME'], conv))
183
184        # check if resulting image is in the deploy directory
185        self.assertTrue(os.path.exists(image_path))
186        self.assertTrue(os.path.exists(image_path + ".sha256sum"))
187
188        # check if the resulting sha256sum agrees
189        self.assertTrue(runCmd('cd %s;sha256sum -c %s.%s.sha256sum' %
190                               (bb_vars['DEPLOY_DIR_IMAGE'], bb_vars['IMAGE_LINK_NAME'], conv)))
191
192    def test_image_fstypes(self):
193        """
194        Summary:     Check if image of supported image fstypes can be built
195        Expected:    core-image-minimal can be built for various image types
196        Product:     oe-core
197        Author:      Ed Bartosh <ed.bartosh@linux.intel.com>
198        """
199        image = 'core-image-minimal'
200
201        all_image_types = set(get_bb_var("IMAGE_TYPES", image).split())
202        skip_image_types = set(('container', 'elf', 'f2fs', 'tar.zst', 'wic.zst', 'squashfs-lzo', 'vfat'))
203        img_types = all_image_types - skip_image_types
204
205        config = """
206IMAGE_FSTYPES += "%s"
207WKS_FILE = "wictestdisk.wks"
208MKUBIFS_ARGS ?= "-m 2048 -e 129024 -c 2047"
209UBINIZE_ARGS ?= "-m 2048 -p 128KiB -s 512"
210MULTIUBI_BUILD += "mtd_2_128"
211MKUBIFS_ARGS_mtd_2_128 ?= "-m 2048 -e 129024 -c 2047"
212UBINIZE_ARGS_mtd_2_128 ?= "-m 2048 -p 128KiB -s 512"
213MULTIUBI_BUILD += "mtd_4_256"
214MKUBIFS_ARGS_mtd_4_256 ?= "-m 4096 -e 253952 -c 4096"
215UBINIZE_ARGS_mtd_4_256 ?= "-m 4096 -p 256KiB"
216""" % ' '.join(img_types)
217        self.write_config(config)
218
219        bitbake(image)
220        bb_vars = get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_LINK_NAME', 'MULTIUBI_BUILD'], image)
221
222        for itype in img_types:
223            if itype == 'multiubi':
224                # For multiubi build we need to manage MULTIUBI_BUILD entry to append
225                # specific name to IMAGE_LINK_NAME
226                for vname in bb_vars['MULTIUBI_BUILD'].split():
227                    image_path = os.path.join(bb_vars['DEPLOY_DIR_IMAGE'], "%s_%s.ubifs" % (bb_vars['IMAGE_LINK_NAME'], vname))
228                    # check if result image is in deploy directory
229                    self.assertTrue(os.path.exists(image_path),
230                                    "%s image %s doesn't exist" % (itype, image_path))
231            else:
232                image_path = os.path.join(bb_vars['DEPLOY_DIR_IMAGE'], "%s.%s" % (bb_vars['IMAGE_LINK_NAME'], itype))
233                # check if result image is in deploy directory
234                self.assertTrue(os.path.exists(image_path),
235                                "%s image %s doesn't exist" % (itype, image_path))
236
237    def test_useradd_static(self):
238        config = """
239USERADDEXTENSION = "useradd-staticids"
240USERADD_ERROR_DYNAMIC = "skip"
241USERADD_UID_TABLES += "files/static-passwd"
242USERADD_GID_TABLES += "files/static-group"
243"""
244        self.write_config(config)
245        bitbake("core-image-base")
246
247    def test_no_busybox_base_utils(self):
248        config = """
249# Enable wayland
250DISTRO_FEATURES:append = " pam opengl wayland"
251
252# Switch to systemd
253DISTRO_FEATURES:append = " systemd usrmerge"
254VIRTUAL-RUNTIME_init_manager = "systemd"
255VIRTUAL-RUNTIME_initscripts = ""
256VIRTUAL-RUNTIME_syslog = ""
257VIRTUAL-RUNTIME_login_manager = "shadow-base"
258DISTRO_FEATURES_BACKFILL_CONSIDERED = "sysvinit"
259
260# Replace busybox
261PREFERRED_PROVIDER_virtual/base-utils = "packagegroup-core-base-utils"
262VIRTUAL-RUNTIME_base-utils = "packagegroup-core-base-utils"
263VIRTUAL-RUNTIME_base-utils-hwclock = "util-linux-hwclock"
264VIRTUAL-RUNTIME_base-utils-syslog = ""
265
266# Skip busybox
267SKIP_RECIPE[busybox] = "Don't build this"
268"""
269        self.write_config(config)
270
271        bitbake("--graphviz core-image-weston")
272
273    def test_image_gen_debugfs(self):
274        """
275        Summary:     Check debugfs generation
276        Expected:    1. core-image-minimal can be build with IMAGE_GEN_DEBUGFS variable set
277                     2. debug filesystem is created when variable set
278                     3. debug symbols available
279        Product:     oe-core
280        Author:      Humberto Ibarra <humberto.ibarra.lopez@intel.com>
281                     Yeoh Ee Peng <ee.peng.yeoh@intel.com>
282        """
283
284        image = 'core-image-minimal'
285        image_fstypes_debugfs = 'tar.bz2'
286        features = 'IMAGE_GEN_DEBUGFS = "1"\n'
287        features += 'IMAGE_FSTYPES_DEBUGFS = "%s"\n' % image_fstypes_debugfs
288        self.write_config(features)
289
290        bitbake(image)
291        bb_vars = get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_LINK_NAME'], image)
292
293        dbg_tar_file = os.path.join(bb_vars['DEPLOY_DIR_IMAGE'], "%s-dbg.%s" % (bb_vars['IMAGE_LINK_NAME'], image_fstypes_debugfs))
294        self.assertTrue(os.path.exists(dbg_tar_file), 'debug filesystem not generated at %s' % dbg_tar_file)
295        result = runCmd('cd %s; tar xvf %s' % (bb_vars['DEPLOY_DIR_IMAGE'], dbg_tar_file))
296        self.assertEqual(result.status, 0, msg='Failed to extract %s: %s' % (dbg_tar_file, result.output))
297        result = runCmd('find %s -name %s' % (bb_vars['DEPLOY_DIR_IMAGE'], "udevadm"))
298        self.assertTrue("udevadm" in result.output, msg='Failed to find udevadm: %s' % result.output)
299        dbg_symbols_targets = result.output.splitlines()
300        self.assertTrue(dbg_symbols_targets, msg='Failed to split udevadm: %s' % dbg_symbols_targets)
301        for t in dbg_symbols_targets:
302            result = runCmd('objdump --syms %s | grep debug' % t)
303            self.assertTrue("debug" in result.output, msg='Failed to find debug symbol: %s' % result.output)
304
305    def test_empty_image(self):
306        """Test creation of image with no packages"""
307        image = 'test-empty-image'
308        bitbake(image)
309        bb_vars = get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_LINK_NAME'], image)
310        manifest = os.path.join(bb_vars['DEPLOY_DIR_IMAGE'], "%s.manifest" % bb_vars['IMAGE_LINK_NAME'])
311        self.assertTrue(os.path.exists(manifest))
312
313        with open(manifest, "r") as f:
314                self.assertEqual(len(f.read().strip()),0)
315
316    def test_mandb(self):
317        """
318        Test that an image containing manpages has working man and apropos commands.
319        """
320        config = """
321DISTRO_FEATURES:append = " api-documentation"
322CORE_IMAGE_EXTRA_INSTALL = "man-pages kmod-doc"
323"""
324        self.write_config(config)
325        bitbake("core-image-minimal")
326
327        with runqemu('core-image-minimal', ssh=False, runqemuparams='nographic') as qemu:
328            # This manpage is provided by man-pages
329            status, output = qemu.run_serial("apropos 8859")
330            self.assertEqual(status, 1, 'Failed to run apropos: %s' % (output))
331            self.assertIn("iso_8859_15", output)
332
333            # This manpage is provided by kmod
334            status, output = qemu.run_serial("man --pager=cat modprobe")
335            self.assertEqual(status, 1, 'Failed to run man: %s' % (output))
336            self.assertIn("force-modversion", output)
337