xref: /qemu/tests/qtest/cdrom-test.c (revision 49d43a5978703d515393c70c5620ace8e8daf8cd)
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"
16*49d43a59SThomas 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 
93*49d43a59SThomas Huth /**
94*49d43a59SThomas Huth  * Check that at least the -cdrom parameter is basically working, i.e. we can
95*49d43a59SThomas Huth  * see the filename of the ISO image in the output of "info block" afterwards
96*49d43a59SThomas Huth  */
97*49d43a59SThomas Huth static void test_cdrom_param(gconstpointer data)
98*49d43a59SThomas Huth {
99*49d43a59SThomas Huth     QTestState *qts;
100*49d43a59SThomas Huth     char *resp;
101*49d43a59SThomas Huth 
102*49d43a59SThomas Huth     qts = qtest_startf("-M %s -cdrom %s", (const char *)data, isoimage);
103*49d43a59SThomas Huth     resp = qtest_hmp(qts, "info block");
104*49d43a59SThomas Huth     g_assert(strstr(resp, isoimage) != 0);
105*49d43a59SThomas Huth     g_free(resp);
106*49d43a59SThomas Huth     qtest_quit(qts);
107*49d43a59SThomas Huth }
108*49d43a59SThomas Huth 
109*49d43a59SThomas Huth static void add_cdrom_param_tests(const char **machines)
110*49d43a59SThomas Huth {
111*49d43a59SThomas Huth     while (*machines) {
112*49d43a59SThomas Huth         char *testname = g_strdup_printf("cdrom/param/%s", *machines);
113*49d43a59SThomas Huth         qtest_add_data_func(testname, *machines, test_cdrom_param);
114*49d43a59SThomas Huth         g_free(testname);
115*49d43a59SThomas Huth         machines++;
116*49d43a59SThomas Huth     }
117*49d43a59SThomas Huth }
118*49d43a59SThomas Huth 
11942f45505SThomas Huth static void test_cdboot(gconstpointer data)
12042f45505SThomas Huth {
12142f45505SThomas Huth     QTestState *qts;
12242f45505SThomas Huth 
12342f45505SThomas Huth     qts = qtest_startf("-accel kvm: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);
13542f45505SThomas Huth     qtest_add_data_func("cdrom/boot/isapc", "-M isapc "
13642f45505SThomas Huth                         "-drive if=ide,media=cdrom,file=", test_cdboot);
13742f45505SThomas Huth     qtest_add_data_func("cdrom/boot/am53c974",
13842f45505SThomas Huth                         "-device am53c974 -device scsi-cd,drive=cd1 "
13942f45505SThomas Huth                         "-drive if=none,id=cd1,format=raw,file=", test_cdboot);
14042f45505SThomas Huth     qtest_add_data_func("cdrom/boot/dc390",
14142f45505SThomas Huth                         "-device dc390 -device scsi-cd,drive=cd1 "
14242f45505SThomas Huth                         "-blockdev file,node-name=cd1,filename=", test_cdboot);
14342f45505SThomas Huth     qtest_add_data_func("cdrom/boot/lsi53c895a",
14442f45505SThomas Huth                         "-device lsi53c895a -device scsi-cd,drive=cd1 "
14542f45505SThomas Huth                         "-blockdev file,node-name=cd1,filename=", test_cdboot);
14642f45505SThomas Huth     qtest_add_data_func("cdrom/boot/megasas", "-M q35 "
14742f45505SThomas Huth                         "-device megasas -device scsi-cd,drive=cd1 "
14842f45505SThomas Huth                         "-blockdev file,node-name=cd1,filename=", test_cdboot);
14942f45505SThomas Huth     qtest_add_data_func("cdrom/boot/megasas-gen2", "-M q35 "
15042f45505SThomas Huth                         "-device megasas-gen2 -device scsi-cd,drive=cd1 "
15142f45505SThomas Huth                         "-blockdev file,node-name=cd1,filename=", test_cdboot);
15242f45505SThomas Huth }
15342f45505SThomas Huth 
15442f45505SThomas Huth static void add_s390x_tests(void)
15542f45505SThomas Huth {
15642f45505SThomas Huth     qtest_add_data_func("cdrom/boot/default", "-cdrom ", test_cdboot);
15742f45505SThomas Huth     qtest_add_data_func("cdrom/boot/virtio-scsi",
15842f45505SThomas Huth                         "-device virtio-scsi -device scsi-cd,drive=cdr "
15942f45505SThomas Huth                         "-blockdev file,node-name=cdr,filename=", test_cdboot);
16042f45505SThomas Huth }
16142f45505SThomas Huth 
16242f45505SThomas Huth int main(int argc, char **argv)
16342f45505SThomas Huth {
16442f45505SThomas Huth     int ret;
16542f45505SThomas Huth     const char *arch = qtest_get_arch();
16642f45505SThomas Huth     const char *genisocheck[] = { "genisoimage", "-version", NULL };
16742f45505SThomas Huth 
16842f45505SThomas Huth     g_test_init(&argc, &argv, NULL);
16942f45505SThomas Huth 
17042f45505SThomas Huth     if (exec_genisoimg(genisocheck)) {
17142f45505SThomas Huth         /* genisoimage not available - so can't run tests */
17242f45505SThomas Huth         return 0;
17342f45505SThomas Huth     }
17442f45505SThomas Huth 
17542f45505SThomas Huth     ret = prepare_image(arch, isoimage);
17642f45505SThomas Huth     if (ret) {
17742f45505SThomas Huth         return ret;
17842f45505SThomas Huth     }
17942f45505SThomas Huth 
18042f45505SThomas Huth     if (g_str_equal(arch, "i386") || g_str_equal(arch, "x86_64")) {
18142f45505SThomas Huth         add_x86_tests();
18242f45505SThomas Huth     } else if (g_str_equal(arch, "s390x")) {
18342f45505SThomas Huth         add_s390x_tests();
184*49d43a59SThomas Huth     } else if (g_str_equal(arch, "ppc64")) {
185*49d43a59SThomas Huth         const char *ppcmachines[] = {
186*49d43a59SThomas Huth             "pseries", "mac99", "g3beige", "40p", "prep", NULL
187*49d43a59SThomas Huth         };
188*49d43a59SThomas Huth         add_cdrom_param_tests(ppcmachines);
189*49d43a59SThomas Huth     } else if (g_str_equal(arch, "sparc")) {
190*49d43a59SThomas Huth         const char *sparcmachines[] = {
191*49d43a59SThomas Huth             "LX", "SPARCClassic", "SPARCbook", "SS-10", "SS-20", "SS-4",
192*49d43a59SThomas Huth             "SS-5", "SS-600MP", "Voyager", "leon3_generic", NULL
193*49d43a59SThomas Huth         };
194*49d43a59SThomas Huth         add_cdrom_param_tests(sparcmachines);
195*49d43a59SThomas Huth     } else if (g_str_equal(arch, "sparc64")) {
196*49d43a59SThomas Huth         const char *sparc64machines[] = {
197*49d43a59SThomas Huth             "niagara", "sun4u", "sun4v", NULL
198*49d43a59SThomas Huth         };
199*49d43a59SThomas Huth         add_cdrom_param_tests(sparc64machines);
200*49d43a59SThomas Huth     } else if (!strncmp(arch, "mips64", 6)) {
201*49d43a59SThomas Huth         const char *mips64machines[] = {
202*49d43a59SThomas Huth             "magnum", "malta", "mips", "pica61", NULL
203*49d43a59SThomas Huth         };
204*49d43a59SThomas Huth         add_cdrom_param_tests(mips64machines);
205*49d43a59SThomas Huth     } else if (g_str_equal(arch, "arm") || g_str_equal(arch, "aarch64")) {
206*49d43a59SThomas Huth         const char *armmachines[] = {
207*49d43a59SThomas Huth             "realview-eb", "realview-eb-mpcore", "realview-pb-a8",
208*49d43a59SThomas Huth             "realview-pbx-a9", "versatileab", "versatilepb", "vexpress-a15",
209*49d43a59SThomas Huth             "vexpress-a9", "virt", NULL
210*49d43a59SThomas Huth         };
211*49d43a59SThomas Huth         add_cdrom_param_tests(armmachines);
212*49d43a59SThomas Huth     } else {
213*49d43a59SThomas Huth         const char *nonemachine[] = { "none", NULL };
214*49d43a59SThomas Huth         add_cdrom_param_tests(nonemachine);
21542f45505SThomas Huth     }
21642f45505SThomas Huth 
21742f45505SThomas Huth     ret = g_test_run();
21842f45505SThomas Huth 
21942f45505SThomas Huth     unlink(isoimage);
22042f45505SThomas Huth 
22142f45505SThomas Huth     return ret;
22242f45505SThomas Huth }
223