1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
|
/* Easy Publish and Consume Library
* Copyright (C) 2007, 2008 Openismus GmbH
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Authors:
* Mathias Hasselmann
*/
#include "framework.h"
#include "libepc/shell.h"
#include <avahi-client/client.h>
#include <avahi-common/error.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <unistd.h>
typedef struct _EpcIfTestStatus EpcIfTestStatus;
struct _EpcIfTestStatus
{
guint ifidx;
gchar *name;
gint mask;
};
static GSList *epc_test_service_browsers = NULL;
static GMainLoop *epc_test_loop = NULL;
static guint epc_test_timeout = 0;
static gint epc_test_result = 1;
gboolean
epc_test_init (gint test_count)
{
gint test_mask = (1 << test_count) - 1;
g_assert (test_mask <= EPC_TEST_MASK_USER);
epc_test_result = EPC_TEST_MASK_INIT | test_mask;
if (NULL == epc_test_loop)
epc_test_loop = g_main_loop_new (NULL, FALSE);
g_return_val_if_fail (NULL != epc_test_loop, FALSE);
return TRUE;
}
gint
_epc_test_quit (const gchar *strloc)
{
gint i;
if (epc_test_loop)
g_main_loop_quit (epc_test_loop);
g_slist_foreach (epc_test_service_browsers,
(GFunc) avahi_service_browser_free, NULL);
epc_test_service_browsers = NULL;
if (epc_test_loop)
{
g_main_loop_unref (epc_test_loop);
epc_test_loop = NULL;
}
for (i = 0; (1 << i) < EPC_TEST_MASK_USER; ++i)
if (epc_test_result & (1 << i))
g_print ("%s: Test #%d failed\n", strloc, i + 1);
return epc_test_result;
}
static gboolean
epc_test_timeout_cb (gpointer data G_GNUC_UNUSED)
{
GString *message = g_string_new (NULL);
gint i;
g_string_printf (message, "%s: Timeout reached. Aborting. Unsatisfied tests:", G_STRLOC);
for (i = 0; (1 << i) < EPC_TEST_MASK_USER; ++i)
if (epc_test_result & (1 << i))
g_string_append_printf (message, " %d", i + 1);
g_print ("%s\n", message->str);
g_string_free (message, TRUE);
g_return_val_if_fail (NULL != epc_test_loop, FALSE);
g_main_loop_quit (epc_test_loop);
return FALSE;
}
void
_epc_test_pass (const gchar *strloc,
gboolean once,
gint mask)
{
int i;
if (epc_test_timeout)
{
g_source_remove (epc_test_timeout);
epc_test_timeout = g_timeout_add (5000, epc_test_timeout_cb, NULL);
}
mask &= EPC_TEST_MASK_USER;
for (i = 0; (1 << i) < EPC_TEST_MASK_USER; ++i)
if (mask & (1 << i))
{
if (once && !(epc_test_result & (1 << i)))
g_error ("Test #%d passed a second time, which was not expected", i + 1);
else
g_print ("%s: Test #%d passed\n", strloc, i + 1);
}
epc_test_result &= ~mask;
if (0 == epc_test_result)
epc_test_quit ();
}
static EpcIfTestStatus*
epc_test_list_ifaces (void)
{
EpcIfTestStatus *ifaces = NULL;
gsize n_ifaces = 0, i, j;
struct ifconf ifc;
char buf[4096];
gint fd = -1;
fd = socket (AF_INET, SOCK_DGRAM, 0);
if (fd < 0)
{
g_warning ("%s: socket(AF_INET, SOCK_DGRAM): %s",
G_STRLOC, g_strerror (errno));
goto out;
}
ifc.ifc_buf = buf;
ifc.ifc_len = sizeof buf;
if (ioctl (fd, SIOCGIFCONF, &ifc) < 0)
{
g_warning ("%s: ioctl(SIOCGIFCONF): %s",
G_STRLOC, g_strerror (errno));
goto out;
}
n_ifaces = ifc.ifc_len / sizeof(struct ifreq);
ifaces = g_new0 (EpcIfTestStatus, n_ifaces + 1);
for (i = 0, j = 0; i < n_ifaces; ++i)
{
struct ifreq *req = &ifc.ifc_req[i];
ifaces[j].name = g_strdup (req->ifr_name);
if (ioctl (fd, SIOCGIFFLAGS, req) < 0)
{
g_warning ("%s: ioctl(SIOCGIFFLAGS): %s",
G_STRLOC, g_strerror (errno));
goto out;
}
if (req->ifr_flags & (IFF_LOOPBACK | IFF_POINTOPOINT) ||
g_str_has_suffix (ifaces[j].name, ":avahi"))
{
g_free (ifaces[j].name);
ifaces[j].name = NULL;
continue;
}
if (ioctl (fd, SIOCGIFINDEX, req) < 0)
{
g_warning ("%s: ioctl(SIOCGIFINDEX): %s",
G_STRLOC, g_strerror (errno));
goto out;
}
/* The following allows to fall back to ifr_index on kFreeBSD, where
ifr_ifindex does not exist. See bug #592474. */
#if !defined(ifr_ifindex) && defined(ifr_index)
#define ifr_ifindex ifr_index
#endif
ifaces[j].ifidx = req->ifr_ifindex;
ifaces[j].mask = epc_test_result;
g_print ("%s: name=%s, ifidx=%u, \n",
G_STRLOC, ifaces[j].name,
ifaces[j].ifidx);
++j;
}
out:
if (fd >= 0)
close (fd);
return ifaces;
}
static gboolean
_epc_test_match_any (const gchar *strloc,
const EpcIfTestStatus *status,
guint ifidx G_GNUC_UNUSED,
gint i)
{
const gint mask = (1 << i);
if (!(status->mask & mask))
return FALSE;
g_print ("%s: Test #%d passed for some network interface\n", strloc, i + 1);
return TRUE;
}
static gboolean
_epc_test_match_one (const gchar *strloc,
const EpcIfTestStatus *status,
guint ifidx,
gint i)
{
if (status->ifidx != ifidx)
return FALSE;
g_print ("%s: Test #%d passed for %s\n", strloc, i + 1, status->name);
return TRUE;
}
void
_epc_test_pass_once_per_iface (const gchar *strloc,
gint mask,
guint ifidx,
gboolean any)
{
gboolean (*match)(const gchar *strloc,
const EpcIfTestStatus *status,
guint ifidx,
gint i);
static EpcIfTestStatus *ifaces = NULL;
gint pending;
guint i, j;
if (!ifaces)
ifaces = epc_test_list_ifaces ();
g_assert (NULL != ifaces);
mask &= EPC_TEST_MASK_USER;
if (any)
match = _epc_test_match_any;
else
match = _epc_test_match_one;
for (i = 0; (1 << i) < EPC_TEST_MASK_USER; ++i)
{
if (0 == (mask & (1 << i)))
continue;
pending = 0;
for (j = 0; ifaces[j].name; ++j)
{
if (match (strloc, &ifaces[j], ifidx, i))
{
ifaces[j].mask &= ~(1 << i);
break;
}
}
for (j = 0; ifaces[j].name; ++j)
pending |= (ifaces[j].mask & (1 << i));
if (!pending)
_epc_test_pass (strloc, TRUE, 1 << i);
}
}
gboolean
epc_test_init_service_browser (const gchar *service,
AvahiServiceBrowserCallback callback,
gpointer data)
{
AvahiServiceBrowser *browser;
GError *error = NULL;
browser = epc_shell_create_service_browser (AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
service, NULL, AVAHI_LOOKUP_USE_MULTICAST,
callback, data, &error);
if (NULL == browser)
{
g_warning ("%s: %s (%d)", G_STRFUNC, error->message, error->code);
return FALSE;
}
epc_test_service_browsers = g_slist_prepend (epc_test_service_browsers, browser);
return TRUE;
}
gint
epc_test_run ()
{
epc_test_result &= ~EPC_TEST_MASK_INIT;
if (!epc_test_loop)
epc_test_loop = g_main_loop_new (NULL, FALSE);
epc_test_timeout = g_timeout_add (5000, epc_test_timeout_cb, NULL);
g_main_loop_run (epc_test_loop);
return epc_test_result;
}
|