xref: /qemu/tests/unit/crypto-tls-psk-helpers.c (revision e8ad8b9987efdbac4116567e685e6fd8ec28ef48)
1e1a6dc91SRichard W.M. Jones /*
2e1a6dc91SRichard W.M. Jones  * Copyright (C) 2015-2018 Red Hat, Inc.
3e1a6dc91SRichard W.M. Jones  *
4e1a6dc91SRichard W.M. Jones  * This library is free software; you can redistribute it and/or
5e1a6dc91SRichard W.M. Jones  * modify it under the terms of the GNU Lesser General Public
6e1a6dc91SRichard W.M. Jones  * License as published by the Free Software Foundation; either
7e1a6dc91SRichard W.M. Jones  * version 2.1 of the License, or (at your option) any later version.
8e1a6dc91SRichard W.M. Jones  *
9e1a6dc91SRichard W.M. Jones  * This library is distributed in the hope that it will be useful,
10e1a6dc91SRichard W.M. Jones  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11e1a6dc91SRichard W.M. Jones  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12e1a6dc91SRichard W.M. Jones  * Lesser General Public License for more details.
13e1a6dc91SRichard W.M. Jones  *
14e1a6dc91SRichard W.M. Jones  * You should have received a copy of the GNU Lesser General Public
15e1a6dc91SRichard W.M. Jones  * License along with this library.  If not, see
16e1a6dc91SRichard W.M. Jones  * <http://www.gnu.org/licenses/>.
17e1a6dc91SRichard W.M. Jones  *
18e1a6dc91SRichard W.M. Jones  * Author: Richard W.M. Jones <rjones@redhat.com>
19e1a6dc91SRichard W.M. Jones  */
20e1a6dc91SRichard W.M. Jones 
21e1a6dc91SRichard W.M. Jones #include "qemu/osdep.h"
22e1a6dc91SRichard W.M. Jones 
23e1a6dc91SRichard W.M. Jones #include "crypto-tls-psk-helpers.h"
24e1a6dc91SRichard W.M. Jones #include "qemu/sockets.h"
25e1a6dc91SRichard W.M. Jones 
2658d25e97SDaniel P. Berrangé static void
test_tls_psk_init_common(const char * pskfile,const char * user,const char * key)2758d25e97SDaniel P. Berrangé test_tls_psk_init_common(const char *pskfile, const char *user, const char *key)
28e1a6dc91SRichard W.M. Jones {
29*f1018ea0SDaniel P. Berrangé     g_autoptr(GError) gerr = NULL;
30*f1018ea0SDaniel P. Berrangé     g_autofree char *line = g_strdup_printf("%s:%s\n", user, key);
31e1a6dc91SRichard W.M. Jones 
32*f1018ea0SDaniel P. Berrangé     g_file_set_contents(pskfile, line, strlen(line), &gerr);
33*f1018ea0SDaniel P. Berrangé     if (gerr != NULL) {
34*f1018ea0SDaniel P. Berrangé         g_critical("Failed to create pskfile %s: %s", pskfile, gerr->message);
35e1a6dc91SRichard W.M. Jones         abort();
36e1a6dc91SRichard W.M. Jones     }
37e1a6dc91SRichard W.M. Jones }
38e1a6dc91SRichard W.M. Jones 
test_tls_psk_init(const char * pskfile)3958d25e97SDaniel P. Berrangé void test_tls_psk_init(const char *pskfile)
4058d25e97SDaniel P. Berrangé {
4158d25e97SDaniel P. Berrangé     /* Don't hard code a key like this in real applications!  Use psktool. */
4258d25e97SDaniel P. Berrangé     test_tls_psk_init_common(pskfile, "qemu", "009d5638c40fde0c");
4358d25e97SDaniel P. Berrangé }
4458d25e97SDaniel P. Berrangé 
test_tls_psk_init_alt(const char * pskfile)4558d25e97SDaniel P. Berrangé void test_tls_psk_init_alt(const char *pskfile)
4658d25e97SDaniel P. Berrangé {
4758d25e97SDaniel P. Berrangé     /* Don't hard code a key like this in real applications!  Use psktool. */
4858d25e97SDaniel P. Berrangé     test_tls_psk_init_common(pskfile, "qemu", "10ffa6a2c42f0388");
4958d25e97SDaniel P. Berrangé }
5058d25e97SDaniel P. Berrangé 
test_tls_psk_cleanup(const char * pskfile)51e1a6dc91SRichard W.M. Jones void test_tls_psk_cleanup(const char *pskfile)
52e1a6dc91SRichard W.M. Jones {
53e1a6dc91SRichard W.M. Jones     unlink(pskfile);
54e1a6dc91SRichard W.M. Jones }
55