fix test
[oweals/gnunet.git] / src / ats / test_ats_api_common.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010,2011 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  * @file ats/test_ats_api_common.c
22  * @brief shared functions for ats test
23  * @author Christian Grothoff
24  * @author Matthias Wachs
25  */
26
27 #include "test_ats_api_common.h"
28
29 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
30
31 #define PEERID0 "2AK99KD8RM9UA9LC3QKA0IQ5UBFC0FBB50EBGCFQT8448DGGACNAC4CJQDD1CPFS494O41U88DJD1FLIG8VA5CQR9IN4L96GP104MVO"
32 #define PEERID1 "5ED7I0AR3MSTAL7FQN04S22E0EQ3CR9RLASCDLVMM1BNFPUPTCT46DLKNJ4DACASJ6U0DR5J8S3R2UJL49682JS7MOVRAB8P8A4PJH0"
33
34 void
35 create_test_address (struct Test_Address *dest, char * plugin, void *session, void *addr, size_t addrlen)
36 {
37
38   dest->plugin = GNUNET_strdup (plugin);
39   dest->session = session;
40   if (addrlen > 0)
41   {
42     dest->addr = GNUNET_malloc (addrlen);
43     memcpy (dest->addr, addr, addrlen);
44   }
45   else
46       dest->addr = NULL;
47   dest->addr_len = addrlen;
48 }
49
50 void
51 free_test_address (struct Test_Address *dest)
52 {
53   GNUNET_free (dest->plugin);
54   if (NULL != dest->addr)
55     GNUNET_free (dest->addr);
56 }
57
58 int
59 compare_addresses (const struct GNUNET_HELLO_Address *address1, void *session1,
60                    const struct GNUNET_HELLO_Address *address2, void *session2)
61 {
62   if (0 != memcmp (&address1->peer, &address2->peer, sizeof (struct GNUNET_PeerIdentity)))
63   {
64       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid peer id'\n");
65       return GNUNET_SYSERR;
66   }
67   if (0 != strcmp (address1->transport_name, address2->transport_name))
68   {
69       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid plugin'\n");
70       return GNUNET_SYSERR;
71   }
72   if (address1->address_length != address2->address_length)
73   {
74       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid address length'\n");
75       return GNUNET_SYSERR;
76
77   }
78   else if (0 != memcmp (address1->address, address2->address, address2->address_length))
79   {
80       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid address'\n");
81       return GNUNET_SYSERR;
82   }
83   if (session1 != session2)
84   {
85       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid session1 %p vs session2 %p'\n",
86                   session1, session2);
87       return GNUNET_SYSERR;
88
89   }
90   return GNUNET_OK;
91 }
92
93
94 int
95 compare_ats (const struct GNUNET_ATS_Information *ats_is, uint32_t ats_count_is,
96              const struct GNUNET_ATS_Information *ats_should, uint32_t ats_count_should)
97 {
98   unsigned int c_o;
99   unsigned int c_i;
100   char *prop[] = GNUNET_ATS_PropertyStrings;
101   uint32_t type1;
102   uint32_t type2;
103   uint32_t val1;
104   uint32_t val2;
105   int res = GNUNET_OK;
106
107   for (c_o = 0; c_o < ats_count_is; c_o++)
108   {
109     for (c_i = 0; c_i < ats_count_should; c_i++)
110     {
111         type1 = ntohl(ats_is[c_o].type);
112         type2 = ntohl(ats_should[c_i].type);
113         if (type1 == type2)
114         {
115             GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS type `%s'\n",
116                         prop[type1]);
117             val1 = ntohl(ats_is[c_o].value);
118             val2 = ntohl(ats_should[c_i].value);
119             if (val1 != val2)
120             {
121                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ATS value `%s' not equal: %u != %u\n",
122                             prop[type1],
123                             val1, val2);
124                 res = GNUNET_SYSERR;
125             }
126             else
127             {
128               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS value `%s' equal: %u == %u\n",
129                           prop[type1],
130                           val1, val2);
131             }
132         }
133     }
134   }
135   return res;
136 }
137
138
139 /* end of file test_ats_api_common.c */