qemu

FORK: QEMU emulator
git clone https://git.neptards.moe/neptards/qemu.git
Log | Files | Refs | Submodules | LICENSE

tpm-tests.c (4034B)


      1 /*
      2  * QTest TPM commont test code
      3  *
      4  * Copyright (c) 2018 IBM Corporation
      5  * Copyright (c) 2018 Red Hat, Inc.
      6  *
      7  * Authors:
      8  *   Stefan Berger <stefanb@linux.vnet.ibm.com>
      9  *   Marc-André Lureau <marcandre.lureau@redhat.com>
     10  *
     11  * This work is licensed under the terms of the GNU GPL, version 2 or later.
     12  * See the COPYING file in the top-level directory.
     13  */
     14 
     15 #include "qemu/osdep.h"
     16 #include <glib/gstdio.h>
     17 
     18 #include "libqtest-single.h"
     19 #include "tpm-tests.h"
     20 
     21 static bool
     22 tpm_test_swtpm_skip(void)
     23 {
     24     if (!tpm_util_swtpm_has_tpm2()) {
     25         g_test_skip("swtpm not in PATH or missing --tpm2 support");
     26         return true;
     27     }
     28 
     29     return false;
     30 }
     31 
     32 void tpm_test_swtpm_test(const char *src_tpm_path, tx_func *tx,
     33                          const char *ifmodel, const char *machine_options)
     34 {
     35     char *args = NULL;
     36     QTestState *s;
     37     SocketAddress *addr = NULL;
     38     gboolean succ;
     39     GPid swtpm_pid;
     40     GError *error = NULL;
     41 
     42     if (tpm_test_swtpm_skip()) {
     43         return;
     44     }
     45 
     46     succ = tpm_util_swtpm_start(src_tpm_path, &swtpm_pid, &addr, &error);
     47     g_assert_true(succ);
     48 
     49     args = g_strdup_printf(
     50         "%s "
     51         "-chardev socket,id=chr,path=%s "
     52         "-tpmdev emulator,id=dev,chardev=chr "
     53         "-device %s,tpmdev=dev",
     54         machine_options ? : "", addr->u.q_unix.path, ifmodel);
     55 
     56     s = qtest_start(args);
     57     g_free(args);
     58 
     59     tpm_util_startup(s, tx);
     60     tpm_util_pcrextend(s, tx);
     61 
     62     static const unsigned char tpm_pcrread_resp[] =
     63         "\x80\x01\x00\x00\x00\x3e\x00\x00\x00\x00\x00\x00\x00\x16\x00\x00"
     64         "\x00\x01\x00\x0b\x03\x00\x04\x00\x00\x00\x00\x01\x00\x20\xf6\x85"
     65         "\x98\xe5\x86\x8d\xe6\x8b\x97\x29\x99\x60\xf2\x71\x7d\x17\x67\x89"
     66         "\xa4\x2f\x9a\xae\xa8\xc7\xb7\xaa\x79\xa8\x62\x56\xc1\xde";
     67     tpm_util_pcrread(s, tx, tpm_pcrread_resp,
     68                      sizeof(tpm_pcrread_resp));
     69 
     70     qtest_end();
     71     tpm_util_swtpm_kill(swtpm_pid);
     72 
     73     g_unlink(addr->u.q_unix.path);
     74     qapi_free_SocketAddress(addr);
     75 }
     76 
     77 void tpm_test_swtpm_migration_test(const char *src_tpm_path,
     78                                    const char *dst_tpm_path,
     79                                    const char *uri, tx_func *tx,
     80                                    const char *ifmodel,
     81                                    const char *machine_options)
     82 {
     83     gboolean succ;
     84     GPid src_tpm_pid, dst_tpm_pid;
     85     SocketAddress *src_tpm_addr = NULL, *dst_tpm_addr = NULL;
     86     GError *error = NULL;
     87     QTestState *src_qemu, *dst_qemu;
     88 
     89     if (tpm_test_swtpm_skip()) {
     90         return;
     91     }
     92 
     93     succ = tpm_util_swtpm_start(src_tpm_path, &src_tpm_pid,
     94                                 &src_tpm_addr, &error);
     95     g_assert_true(succ);
     96 
     97     succ = tpm_util_swtpm_start(dst_tpm_path, &dst_tpm_pid,
     98                                 &dst_tpm_addr, &error);
     99     g_assert_true(succ);
    100 
    101     tpm_util_migration_start_qemu(&src_qemu, &dst_qemu,
    102                                   src_tpm_addr, dst_tpm_addr, uri,
    103                                   ifmodel, machine_options);
    104 
    105     tpm_util_startup(src_qemu, tx);
    106     tpm_util_pcrextend(src_qemu, tx);
    107 
    108     static const unsigned char tpm_pcrread_resp[] =
    109         "\x80\x01\x00\x00\x00\x3e\x00\x00\x00\x00\x00\x00\x00\x16\x00\x00"
    110         "\x00\x01\x00\x0b\x03\x00\x04\x00\x00\x00\x00\x01\x00\x20\xf6\x85"
    111         "\x98\xe5\x86\x8d\xe6\x8b\x97\x29\x99\x60\xf2\x71\x7d\x17\x67\x89"
    112         "\xa4\x2f\x9a\xae\xa8\xc7\xb7\xaa\x79\xa8\x62\x56\xc1\xde";
    113     tpm_util_pcrread(src_qemu, tx, tpm_pcrread_resp,
    114                      sizeof(tpm_pcrread_resp));
    115 
    116     tpm_util_migrate(src_qemu, uri);
    117     tpm_util_wait_for_migration_complete(src_qemu);
    118 
    119     tpm_util_pcrread(dst_qemu, tx, tpm_pcrread_resp,
    120                      sizeof(tpm_pcrread_resp));
    121 
    122     qtest_quit(dst_qemu);
    123     qtest_quit(src_qemu);
    124 
    125     tpm_util_swtpm_kill(dst_tpm_pid);
    126     g_unlink(dst_tpm_addr->u.q_unix.path);
    127     qapi_free_SocketAddress(dst_tpm_addr);
    128 
    129     tpm_util_swtpm_kill(src_tpm_pid);
    130     g_unlink(src_tpm_addr->u.q_unix.path);
    131     qapi_free_SocketAddress(src_tpm_addr);
    132 }