-fixes
[oweals/gnunet.git] / src / testing / helper.c
1 /*
2       This file is part of GNUnet
3       (C) 2012 Christian Grothoff (and other contributing authors)
4
5       GNUnet is free software; you can redistribute it and/or modify
6       it under the terms of the GNU General Public License as published
7       by the Free Software Foundation; either version 3, or (at your
8       option) any later version.
9
10       GNUnet is distributed in the hope that it will be useful, but
11       WITHOUT ANY WARRANTY; without even the implied warranty of
12       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13       General Public License for more details.
14
15       You should have received a copy of the GNU General Public License
16       along with GNUnet; see the file COPYING.  If not, write to the
17       Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18       Boston, MA 02111-1307, USA.
19  */
20
21 /**
22  * @file testing/helper.c
23  * @brief helper functions for testing
24  * @author Christian Grothoff
25  *
26  */
27 #include "platform.h"
28 #include "gnunet_testing_lib.h"
29
30
31
32
33 /**
34  * Obtain the peer identity of the peer with the given configuration
35  * handle.  This function reads the private key of the peer, obtains
36  * the public key and hashes it.
37  *
38  * @param cfg configuration of the peer
39  * @param pid where to store the peer identity
40  * @return GNUNET_OK on success, GNUNET_SYSERR on failure
41  */
42 int
43 GNUNET_TESTING_get_peer_identity (const struct GNUNET_CONFIGURATION_Handle *cfg,
44                                   struct GNUNET_PeerIdentity *pid)
45 {
46   char *keyfile;
47   struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
48   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
49
50   if (GNUNET_OK !=
51       GNUNET_CONFIGURATION_get_value_filename (cfg, "GNUNETD", "HOSTKEY",
52                                                &keyfile))
53   {
54     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
55                 _
56                 ("Peer is lacking HOSTKEY configuration setting.\n"));
57     return GNUNET_SYSERR;
58   }
59   my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
60   GNUNET_free (keyfile);
61   if (my_private_key == NULL)
62   {
63     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
64                 _("Could not access hostkey.\n"));
65     return GNUNET_SYSERR;
66   }
67   GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
68   GNUNET_CRYPTO_rsa_key_free (my_private_key);
69   GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
70                       &pid->hashPubKey);
71   return GNUNET_OK;
72 }
73
74
75 /* end of helper.c */