xref: /qemu/tests/qtest/cdrom-test.c (revision b2ce76a0730e48e60633a698cd876d55917ac9bc)
142f45505SThomas Huth /*
242f45505SThomas Huth  * Various tests for emulated CD-ROM drives.
342f45505SThomas Huth  *
442f45505SThomas Huth  * Copyright (c) 2018 Red Hat Inc.
542f45505SThomas Huth  *
642f45505SThomas Huth  * Author:
742f45505SThomas Huth  *    Thomas Huth <thuth@redhat.com>
842f45505SThomas Huth  *
942f45505SThomas Huth  * This work is licensed under the terms of the GNU GPL, version 2
1042f45505SThomas Huth  * or later. See the COPYING file in the top-level directory.
1142f45505SThomas Huth  */
1242f45505SThomas Huth 
1342f45505SThomas Huth #include "qemu/osdep.h"
1442f45505SThomas Huth #include "libqtest.h"
1542f45505SThomas Huth #include "boot-sector.h"
1649d43a59SThomas Huth #include "qapi/qmp/qdict.h"
1742f45505SThomas Huth 
1842f45505SThomas Huth static char isoimage[] = "cdrom-boot-iso-XXXXXX";
1942f45505SThomas Huth 
2042f45505SThomas Huth static int exec_genisoimg(const char **args)
2142f45505SThomas Huth {
2242f45505SThomas Huth     gchar *out_err = NULL;
2342f45505SThomas Huth     gint exit_status = -1;
2442f45505SThomas Huth     bool success;
2542f45505SThomas Huth 
2642f45505SThomas Huth     success = g_spawn_sync(NULL, (gchar **)args, NULL,
2742f45505SThomas Huth                            G_SPAWN_SEARCH_PATH | G_SPAWN_STDOUT_TO_DEV_NULL,
2842f45505SThomas Huth                            NULL, NULL, NULL, &out_err, &exit_status, NULL);
2942f45505SThomas Huth     if (!success) {
3042f45505SThomas Huth         return -ENOENT;
3142f45505SThomas Huth     }
3242f45505SThomas Huth     if (out_err) {
3342f45505SThomas Huth         fputs(out_err, stderr);
3442f45505SThomas Huth         g_free(out_err);
3542f45505SThomas Huth     }
3642f45505SThomas Huth 
3742f45505SThomas Huth     return exit_status;
3842f45505SThomas Huth }
3942f45505SThomas Huth 
4042f45505SThomas Huth static int prepare_image(const char *arch, char *isoimage)
4142f45505SThomas Huth {
4242f45505SThomas Huth     char srcdir[] = "cdrom-test-dir-XXXXXX";
4342f45505SThomas Huth     char *codefile = NULL;
4442f45505SThomas Huth     int ifh, ret = -1;
4542f45505SThomas Huth     const char *args[] = {
4642f45505SThomas Huth         "genisoimage", "-quiet", "-l", "-no-emul-boot",
4742f45505SThomas Huth         "-b", NULL, "-o", isoimage, srcdir, NULL
4842f45505SThomas Huth     };
4942f45505SThomas Huth 
5042f45505SThomas Huth     ifh = mkstemp(isoimage);
5142f45505SThomas Huth     if (ifh < 0) {
5242f45505SThomas Huth         perror("Error creating temporary iso image file");
5342f45505SThomas Huth         return -1;
5442f45505SThomas Huth     }
5542f45505SThomas Huth     if (!mkdtemp(srcdir)) {
5642f45505SThomas Huth         perror("Error creating temporary directory");
5742f45505SThomas Huth         goto cleanup;
5842f45505SThomas Huth     }
5942f45505SThomas Huth 
6042f45505SThomas Huth     if (g_str_equal(arch, "i386") || g_str_equal(arch, "x86_64") ||
6142f45505SThomas Huth         g_str_equal(arch, "s390x")) {
6242f45505SThomas Huth         codefile = g_strdup_printf("%s/bootcode-XXXXXX", srcdir);
6342f45505SThomas Huth         ret = boot_sector_init(codefile);
6442f45505SThomas Huth         if (ret) {
6542f45505SThomas Huth             goto cleanup;
6642f45505SThomas Huth         }
6742f45505SThomas Huth     } else {
6842f45505SThomas Huth         /* Just create a dummy file */
6942f45505SThomas Huth         char txt[] = "empty disc";
7042f45505SThomas Huth         codefile = g_strdup_printf("%s/readme.txt", srcdir);
7142f45505SThomas Huth         if (!g_file_set_contents(codefile, txt, sizeof(txt) - 1, NULL)) {
7242f45505SThomas Huth             fprintf(stderr, "Failed to create '%s'\n", codefile);
7342f45505SThomas Huth             goto cleanup;
7442f45505SThomas Huth         }
7542f45505SThomas Huth     }
7642f45505SThomas Huth 
7742f45505SThomas Huth     args[5] = strchr(codefile, '/') + 1;
7842f45505SThomas Huth     ret = exec_genisoimg(args);
7942f45505SThomas Huth     if (ret) {
8042f45505SThomas Huth         fprintf(stderr, "genisoimage failed: %i\n", ret);
8142f45505SThomas Huth     }
8242f45505SThomas Huth 
8342f45505SThomas Huth     unlink(codefile);
8442f45505SThomas Huth 
8542f45505SThomas Huth cleanup:
8642f45505SThomas Huth     g_free(codefile);
8742f45505SThomas Huth     rmdir(srcdir);
8842f45505SThomas Huth     close(ifh);
8942f45505SThomas Huth 
9042f45505SThomas Huth     return ret;
9142f45505SThomas Huth }
9242f45505SThomas Huth 
9349d43a59SThomas Huth /**
9449d43a59SThomas Huth  * Check that at least the -cdrom parameter is basically working, i.e. we can
9549d43a59SThomas Huth  * see the filename of the ISO image in the output of "info block" afterwards
9649d43a59SThomas Huth  */
9749d43a59SThomas Huth static void test_cdrom_param(gconstpointer data)
9849d43a59SThomas Huth {
9949d43a59SThomas Huth     QTestState *qts;
10049d43a59SThomas Huth     char *resp;
10149d43a59SThomas Huth 
10288b988c8SMarkus Armbruster     qts = qtest_initf("-M %s -cdrom %s", (const char *)data, isoimage);
10349d43a59SThomas Huth     resp = qtest_hmp(qts, "info block");
10449d43a59SThomas Huth     g_assert(strstr(resp, isoimage) != 0);
10549d43a59SThomas Huth     g_free(resp);
10649d43a59SThomas Huth     qtest_quit(qts);
10749d43a59SThomas Huth }
10849d43a59SThomas Huth 
10949d43a59SThomas Huth static void add_cdrom_param_tests(const char **machines)
11049d43a59SThomas Huth {
11149d43a59SThomas Huth     while (*machines) {
11249d43a59SThomas Huth         char *testname = g_strdup_printf("cdrom/param/%s", *machines);
11349d43a59SThomas Huth         qtest_add_data_func(testname, *machines, test_cdrom_param);
11449d43a59SThomas Huth         g_free(testname);
11549d43a59SThomas Huth         machines++;
11649d43a59SThomas Huth     }
11749d43a59SThomas Huth }
11849d43a59SThomas Huth 
11942f45505SThomas Huth static void test_cdboot(gconstpointer data)
12042f45505SThomas Huth {
12142f45505SThomas Huth     QTestState *qts;
12242f45505SThomas Huth 
1236f6e1698SPaolo Bonzini     qts = qtest_initf("-accel kvm -accel tcg -no-shutdown %s%s", (const char *)data,
12442f45505SThomas Huth                       isoimage);
12542f45505SThomas Huth     boot_sector_test(qts);
12642f45505SThomas Huth     qtest_quit(qts);
12742f45505SThomas Huth }
12842f45505SThomas Huth 
12942f45505SThomas Huth static void add_x86_tests(void)
13042f45505SThomas Huth {
13142f45505SThomas Huth     qtest_add_data_func("cdrom/boot/default", "-cdrom ", test_cdboot);
13242f45505SThomas Huth     qtest_add_data_func("cdrom/boot/virtio-scsi",
13342f45505SThomas Huth                         "-device virtio-scsi -device scsi-cd,drive=cdr "
13442f45505SThomas Huth                         "-blockdev file,node-name=cdr,filename=", test_cdboot);
1354300dadcSAlex Bennée     /*
1364300dadcSAlex Bennée      * Unstable CI test under load
1374300dadcSAlex Bennée      * See https://lists.gnu.org/archive/html/qemu-devel/2019-02/msg05509.html
1384300dadcSAlex Bennée      */
1394300dadcSAlex Bennée     if (g_test_slow()) {
14042f45505SThomas Huth         qtest_add_data_func("cdrom/boot/isapc", "-M isapc "
14142f45505SThomas Huth                             "-drive if=ide,media=cdrom,file=", test_cdboot);
1424300dadcSAlex Bennée     }
14342f45505SThomas Huth     qtest_add_data_func("cdrom/boot/am53c974",
14442f45505SThomas Huth                         "-device am53c974 -device scsi-cd,drive=cd1 "
14542f45505SThomas Huth                         "-drive if=none,id=cd1,format=raw,file=", test_cdboot);
14642f45505SThomas Huth     qtest_add_data_func("cdrom/boot/dc390",
14742f45505SThomas Huth                         "-device dc390 -device scsi-cd,drive=cd1 "
14842f45505SThomas Huth                         "-blockdev file,node-name=cd1,filename=", test_cdboot);
14942f45505SThomas Huth     qtest_add_data_func("cdrom/boot/lsi53c895a",
15042f45505SThomas Huth                         "-device lsi53c895a -device scsi-cd,drive=cd1 "
15142f45505SThomas Huth                         "-blockdev file,node-name=cd1,filename=", test_cdboot);
15242f45505SThomas Huth     qtest_add_data_func("cdrom/boot/megasas", "-M q35 "
15342f45505SThomas Huth                         "-device megasas -device scsi-cd,drive=cd1 "
15442f45505SThomas Huth                         "-blockdev file,node-name=cd1,filename=", test_cdboot);
15542f45505SThomas Huth     qtest_add_data_func("cdrom/boot/megasas-gen2", "-M q35 "
15642f45505SThomas Huth                         "-device megasas-gen2 -device scsi-cd,drive=cd1 "
15742f45505SThomas Huth                         "-blockdev file,node-name=cd1,filename=", test_cdboot);
15842f45505SThomas Huth }
15942f45505SThomas Huth 
16042f45505SThomas Huth static void add_s390x_tests(void)
16142f45505SThomas Huth {
16242f45505SThomas Huth     qtest_add_data_func("cdrom/boot/default", "-cdrom ", test_cdboot);
16342f45505SThomas Huth     qtest_add_data_func("cdrom/boot/virtio-scsi",
16442f45505SThomas Huth                         "-device virtio-scsi -device scsi-cd,drive=cdr "
16542f45505SThomas Huth                         "-blockdev file,node-name=cdr,filename=", test_cdboot);
16642f45505SThomas Huth }
16742f45505SThomas Huth 
16842f45505SThomas Huth int main(int argc, char **argv)
16942f45505SThomas Huth {
17042f45505SThomas Huth     int ret;
17142f45505SThomas Huth     const char *arch = qtest_get_arch();
17242f45505SThomas Huth     const char *genisocheck[] = { "genisoimage", "-version", NULL };
17342f45505SThomas Huth 
17442f45505SThomas Huth     g_test_init(&argc, &argv, NULL);
17542f45505SThomas Huth 
17642f45505SThomas Huth     if (exec_genisoimg(genisocheck)) {
17742f45505SThomas Huth         /* genisoimage not available - so can't run tests */
1784848cb3dSPaolo Bonzini         return g_test_run();
17942f45505SThomas Huth     }
18042f45505SThomas Huth 
18142f45505SThomas Huth     ret = prepare_image(arch, isoimage);
18242f45505SThomas Huth     if (ret) {
18342f45505SThomas Huth         return ret;
18442f45505SThomas Huth     }
18542f45505SThomas Huth 
18642f45505SThomas Huth     if (g_str_equal(arch, "i386") || g_str_equal(arch, "x86_64")) {
18742f45505SThomas Huth         add_x86_tests();
18842f45505SThomas Huth     } else if (g_str_equal(arch, "s390x")) {
18942f45505SThomas Huth         add_s390x_tests();
19049d43a59SThomas Huth     } else if (g_str_equal(arch, "ppc64")) {
19149d43a59SThomas Huth         const char *ppcmachines[] = {
192*b2ce76a0SThomas Huth             "pseries", "mac99", "g3beige", "40p", NULL
19349d43a59SThomas Huth         };
19449d43a59SThomas Huth         add_cdrom_param_tests(ppcmachines);
19549d43a59SThomas Huth     } else if (g_str_equal(arch, "sparc")) {
19649d43a59SThomas Huth         const char *sparcmachines[] = {
19749d43a59SThomas Huth             "LX", "SPARCClassic", "SPARCbook", "SS-10", "SS-20", "SS-4",
19849d43a59SThomas Huth             "SS-5", "SS-600MP", "Voyager", "leon3_generic", NULL
19949d43a59SThomas Huth         };
20049d43a59SThomas Huth         add_cdrom_param_tests(sparcmachines);
20149d43a59SThomas Huth     } else if (g_str_equal(arch, "sparc64")) {
20249d43a59SThomas Huth         const char *sparc64machines[] = {
20349d43a59SThomas Huth             "niagara", "sun4u", "sun4v", NULL
20449d43a59SThomas Huth         };
20549d43a59SThomas Huth         add_cdrom_param_tests(sparc64machines);
20649d43a59SThomas Huth     } else if (!strncmp(arch, "mips64", 6)) {
20749d43a59SThomas Huth         const char *mips64machines[] = {
20849d43a59SThomas Huth             "magnum", "malta", "mips", "pica61", NULL
20949d43a59SThomas Huth         };
21049d43a59SThomas Huth         add_cdrom_param_tests(mips64machines);
21149d43a59SThomas Huth     } else if (g_str_equal(arch, "arm") || g_str_equal(arch, "aarch64")) {
21249d43a59SThomas Huth         const char *armmachines[] = {
21349d43a59SThomas Huth             "realview-eb", "realview-eb-mpcore", "realview-pb-a8",
21449d43a59SThomas Huth             "realview-pbx-a9", "versatileab", "versatilepb", "vexpress-a15",
21549d43a59SThomas Huth             "vexpress-a9", "virt", NULL
21649d43a59SThomas Huth         };
21749d43a59SThomas Huth         add_cdrom_param_tests(armmachines);
21849d43a59SThomas Huth     } else {
21949d43a59SThomas Huth         const char *nonemachine[] = { "none", NULL };
22049d43a59SThomas Huth         add_cdrom_param_tests(nonemachine);
22142f45505SThomas Huth     }
22242f45505SThomas Huth 
22342f45505SThomas Huth     ret = g_test_run();
22442f45505SThomas Huth 
22542f45505SThomas Huth     unlink(isoimage);
22642f45505SThomas Huth 
22742f45505SThomas Huth     return ret;
22842f45505SThomas Huth }
229