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
|
/*
* list.c - Libteredo peer list tests
*/
/***********************************************************************
* Copyright © 2005 Rémi Denis-Courmont. *
* This program is free software; you can redistribute and/or modify *
* it under the terms of the GNU General Public License as published *
* by the Free Software Foundation; version 2 of the license, or (at *
* your option) any later version. *
* *
* This program 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 General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, you can get it from: *
* http://www.gnu.org/copyleft/gpl.html *
***********************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdbool.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h> // putenv()
#include <inttypes.h> /* for Mac OS X */
#include <sys/types.h>
#include <netinet/in.h>
#include <unistd.h>
#include "teredo.h"
#include "clock.h"
#include "peerlist.h"
static void wait (unsigned sec)
{
printf ("Waiting %d second%s...\n", sec, (sec != 1) ? "s" : "");
nanosleep (&(struct timespec){ sec, 0 }, NULL);
}
static teredo_peer *
lookup (teredo_peerlist *l, struct in6_addr *addr, bool *create)
{
teredo_peer *p = teredo_list_lookup (l, addr, create);
if (p != NULL)
teredo_list_release (l);
return p;
}
static bool try_lookup (teredo_peerlist *l, struct in6_addr *addr)
{
return lookup (l, addr, NULL) != NULL;
}
static bool try_insert (teredo_peerlist *l, struct in6_addr *addr)
{
bool created;
return (lookup (l, addr, &created) != NULL) && created;
}
static int test_list (teredo_peerlist *l)
{
struct in6_addr addr = { { } };
puts ("Initial insertion test...");
for (unsigned i = 0; i < 256; i++)
{
addr.s6_addr[12] = i;
if ((i & 1) ? !try_insert (l, &addr) : try_lookup (l, &addr))
return -1;
}
puts ("Initial lookup test...");
// lookup tests
for (unsigned i = 0; i < 256; i++)
{
addr.s6_addr[12] = i;
if ((i & 1) != try_lookup (l, &addr))
return -1;
}
wait (1);
addr.s6_addr[0] = 1;
puts ("Further insertion test...");
for (unsigned i = 0; i < 256; i++)
{
addr.s6_addr[12] = i;
if ((i & 1) ? ((i < 255) != try_insert (l, &addr))
: try_lookup (l, &addr))
// items 1, 3...253 should have been created
// items 255 should cause an overflow
// items 0, 2... 254 did not exist and should not have been found
return -1;
}
puts ("Lookup test...");
for (unsigned i = 0; i < 256; i++)
{
addr.s6_addr[0] = 0;
addr.s6_addr[12] = i;
if (((i & 3) == 3) && !try_lookup (l, &addr))
// item was created earlier
return -1;
addr.s6_addr[0] = 1;
if (((i & 1) && (i < 255)) != try_lookup (l, &addr))
return -1;
}
wait (2);
puts ("Further lookup test...");
for (unsigned i = 0; i < 256; i++)
{
addr.s6_addr[0] = 0;
addr.s6_addr[12] = i;
if ((i & 3) == 3)
{
if (!try_lookup (l, &addr))
return -1;
}
addr.s6_addr[0] = 1;
if (((i & 1) && (i != 255)) != try_lookup (l, &addr))
return -1;
}
wait (2);
addr.s6_addr[0] = 0;
puts ("Partial expiration test...");
for (unsigned i = 0; i < 256; i++)
{
addr.s6_addr[12] = i;
if ((i & 3) == 3)
continue;
if (try_lookup (l, &addr))
return -1;
}
wait (5);
puts ("Full expiration test...");
for (unsigned i = 0; i < 256; i++)
{
addr.s6_addr[12] = i;
if ((i & 1) ? !try_insert (l, &addr) : try_lookup (l, &addr))
return -1;
}
return 0;
}
int main (void)
{
struct in6_addr addr = { { } };
putenv ((char *)"MALLOC_CHECK_=2");
puts ("Basic empty list test...");
teredo_peerlist *l = teredo_list_create (0, 3);
if (l == NULL)
return -1;
else
{
bool create;
if (teredo_list_lookup (l, &addr, &create) != NULL)
return -1;
teredo_list_destroy (l);
}
puts ("Advanced empty list test...");
l = teredo_list_create (0, 3);
if (l == NULL)
return -1;
else
{
bool create;
if (teredo_list_lookup (l, &addr, &create) != NULL)
return -1;
teredo_list_reset (l, 1);
// should now be able to insert a single item
if (teredo_list_lookup (l, &addr, &create) == NULL)
return -1;
teredo_list_release (l);
addr.s6_addr[12] = 10;
if (teredo_list_lookup (l, &addr, &create) != NULL)
return -1;
teredo_list_reset (l, 1);
teredo_list_reset (l, 1);
teredo_list_destroy (l);
}
puts ("List creation test...");
l = teredo_list_create (255, 2);
if (l == NULL)
return -1;
if (test_list (l))
return 1;
wait (7);
if (test_list (l))
return 1;
puts ("Final list release...");
teredo_list_destroy (l);
puts ("Done.");
return 0;
}
|