torture_bnf.c 12.2 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
8
 * This library is free software; you can redistribute it and/or
Pekka Pessi's avatar
Pekka Pessi committed
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
 * 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
 *
 */

/**@CFILE torture_bnf.c
 *
 * Torture tests for BNF functions.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * @date Created: Tue Aug 21 15:18:26 2001 ppessi
 */

#include "config.h"

#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stdio.h>
#include <assert.h>

43
#include "sofia-sip/bnf.h"
Pekka Pessi's avatar
Pekka Pessi committed
44 45 46 47

static int test_flags = 0;
#define TSTFLAGS test_flags

48
#include <sofia-sip/tstdef.h>
Pekka Pessi's avatar
Pekka Pessi committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

char const name[] = "torture_bnf";

int count_bnf(int bnf_flags)
{
  int i, n;

  for (i = 0, n = 0; i < 128; i++)
    if (_bnf_table[i] & bnf_flags)
      n++;

  return n;
}

int bnf_test(void)
{
  BEGIN();
  TEST_1(IS_TOKEN('a'));
  TEST_1(IS_TOKEN('b'));
68
  /* Fixed for 1.12.2: lws = [*wsp crlf] 1*wsp */
69 70 71 72 73 74 75 76
  TEST_SIZE(span_lws("  \r\n \r\nLoppuu"), 5);
  TEST_SIZE(span_lws("  \r\r \nLoppuu"), 2);
  TEST_SIZE(span_lws("  \n\r \nLoppuu"), 2);
  TEST_SIZE(span_lws("  \r \nLoppuu"), 4);
  TEST_SIZE(span_lws("  \n\t \nLoppuu"), 5);
  TEST_SIZE(span_lws("  \r\n\t \nLoppuu"), 6);
  TEST_SIZE(span_token(SIP_TOKEN), strlen(SIP_TOKEN));
  TEST_SIZE(count_bnf(bnf_token), strlen(SIP_TOKEN "$"));
Pekka Pessi's avatar
Pekka Pessi committed
77
  #define SIP_PARAM SIP_TOKEN "[:]/"
78 79
  TEST_SIZE(span_param(SIP_PARAM), strlen(SIP_PARAM));
  TEST_SIZE(count_bnf(bnf_param), strlen(SIP_PARAM "$"));
Pekka Pessi's avatar
Pekka Pessi committed
80

81 82
  TEST_SIZE(span_unreserved(URL_UNRESERVED URL_ESCAPED),
	    strlen(URL_UNRESERVED URL_ESCAPED));
Pekka Pessi's avatar
Pekka Pessi committed
83

84 85
  TEST_SIZE(count_bnf(bnf_unreserved),
	    strlen(URL_UNRESERVED URL_ESCAPED));
Pekka Pessi's avatar
Pekka Pessi committed
86 87 88

  {
    char word[] = ALPHA DIGIT "-.!%*_+`'~()<>:\\\"/[]?{}";
89
    TEST_SIZE(span_word(word), strlen(word));
Pekka Pessi's avatar
Pekka Pessi committed
90 91 92 93
  }

  END();
}
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

int ip_test(void)
{
  BEGIN();
  char *s;

  TEST(span_ip4_address("127.255.249.000,"), 15);
  TEST(span_ip4_address("0.00.000.000:,"), 12);

  /* Test error detection */
  TEST(span_ip4_address("256.00.000.000:,"), 0);
  TEST(span_ip4_address("255.00.000.0000,"), 0);
  TEST(span_ip4_address("255.00.000.199."), 0);

  {
    char ip0[] = "010.250.020.000,";
    char ip1[] = "0.00.000.000:,";
    char ip2[] = "256.00.000.000:,";
    char ip3[] = "255.00.000.0000,";
    char ip4[] = "255.00.000.199.";

    s = ip0;
    TEST(scan_ip4_address(&s), 15); TEST_S(s, ","); TEST_S(ip0, "10.250.20.0");

    s = ip1;
    TEST(scan_ip4_address(&s), 12); TEST_S(s, ":,"); TEST_S(ip1, "0.0.0.0");

    /* Test error detection */
    s = ip2; TEST(scan_ip4_address(&s), -1);
    s = ip3; TEST(scan_ip4_address(&s), -1);
    s = ip4; TEST(scan_ip4_address(&s), -1);
  }

  TEST(span_ip6_address("dead:beef:feed:ded:0:1:2:3"), 26);
  TEST(span_ip6_address("::beef:feed:ded:0:1:2:3"), 23);
  TEST(span_ip6_address("::255.0.0.0,"), 11);
  TEST(span_ip6_address("::,"), 2);

  TEST(span_ip_address("[dead:beef:feed:ded:0:1:2:3]"), 28);
  TEST(span_ip_address("::255.0.0.0,"), 11);
  TEST(span_ip_address("[::255.0.0.0]:"), 13);

  TEST(span_ip6_address("dead:beef:feed::0ded:0:1:2:3"), 28);
  TEST(span_ip6_address("dead:beef:feed::0ded:0000:0001:0002:0003"), 40);

  TEST(span_ip6_address("dead:beef:feed::0ded::0000:0001:0002:0003"), 0);
  TEST(span_ip6_address("::dead:beef:feed::0ded:0000:0001:0002:0003"), 0);
  TEST(span_ip6_address("dead:beef:feed:ded:0:1:2:3:4"), 0);
  TEST(span_ip6_address("dead:beef:feed:00ded:0:1:2:3"), 0);
  TEST(span_ip6_address("dead:beef:feed:ded:0:1:2:127.0.0.1"), 0);
  TEST(span_ip6_address(":255.0.0.0,"), 0);
  TEST(span_ip6_address("255.0.0.0,"), 0);

  /* Accept colon after IP4-quad */
  TEST(span_ip_address("::255.0.0.0:5060"), 11);

  /* This is a reference */
  TEST(span_ip6_address("[dead:beef:feed:ded:0:1:2:3]"), 0);
  TEST(span_ip6_reference("[dead:beef:feed:ded:0:1:2:3]:1"), 28);
  TEST(span_ip_address("[dead:beef:feed:ded:0:1:2:3]:1"), 28);
  TEST(span_ip_address("[127.0.0.1]:1"), 0);

  {
    char ip0[] = "dead:beef:feed:ded:0:1:2:3,";
    char ip1[] = "::beef:feed:ded:0:1:2:3;";
    char ip1b[] = "::beef:feed:ded:0:0:2:3;";
    char ip2[] = "::255.00.0.0,";
    char ip3[] = "::,";
    char ip4[] = "0:0:0:0:0:0:0:0,";
    char ip4b[] = "0:0:0:0:0:0:0.0.0.0,";
    char ip4c[] = "0:0:0:0:0:0:0.0.0.1,";
    char ip5[] = "dead:beef:feed::0ded:0:1:2:3";
    char ip6[] = "dead:beef:feed::0ded:0000:0001:0002:0003+";
    char ip7[] = "1:0:0:2:0:0:0:3,";
    char ip8[] = "1:0:0:2:0:0:3:4,";
    char ip9[] = "1::2:0:0:0:3,";

    s = ip0; TEST(scan_ip6_address(&s), 26); TEST_S(s, ",");
    TEST_S(ip0, "dead:beef:feed:ded::1:2:3");
    s = ip1; TEST(scan_ip6_address(&s), 23); TEST_S(s, ";");
    TEST_S(ip1, "::beef:feed:ded:0:1:2:3;");
    s = ip1b; TEST(scan_ip6_address(&s), 23); TEST_S(s, ";");
    TEST_S(ip1b, "0:beef:feed:ded::2:3");
    s = ip2; TEST(scan_ip6_address(&s), 12); TEST_S(s, ",");
    TEST_S(ip2, "::255.0.0.0");
    s = ip3; TEST(scan_ip6_address(&s), 2); TEST_S(s, ",");
    TEST_S(ip3, "::,");
    s = ip4; TEST(scan_ip6_address(&s), 15); TEST_S(s, ",");
    TEST_S(ip4, "::");
    s = ip4b; TEST(scan_ip6_address(&s), 19); TEST_S(s, ",");
    TEST_S(ip4b, "::");
    s = ip4c; TEST(scan_ip6_address(&s), 19); TEST_S(s, ",");
    TEST_S(ip4c, "::1");
187
    TEST_S(ip5, "dead:beef:feed::0ded:0:1:2:3");
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
    s = ip5; TEST(scan_ip6_address(&s), 28); TEST_S(s, "");
    TEST_S(ip5, "dead:beef:feed:ded::1:2:3");
    s = ip6; TEST(scan_ip6_address(&s), 40); TEST_S(s, "+");
    TEST_S(ip6, "dead:beef:feed:ded::1:2:3");
    s = ip7; TEST(scan_ip6_address(&s), 15); TEST_S(s, ",");
    TEST_S(ip7, "1:0:0:2::3");
    s = ip8; TEST(scan_ip6_address(&s), 15); TEST_S(s, ",");
    TEST_S(ip8, "1::2:0:0:3:4");
    s = ip9; TEST(scan_ip6_address(&s), 12); TEST_S(s, ",");
    TEST_S(ip9, "1:0:0:2::3");
  }

  {
    char err0[] = "dead:beef:feed::0ded::0000:0001:0002:0003";
    char err1[] = "::dead:beef:feed::0ded:0000:0001:0002:0003";
    char err2[] = "dead:beef:feed:ded:0:1:2:3:4";
    char err3[] = "dead:beef:feed:00ded:0:1:2:3";
    char err4[] = "dead:beef:feed:ded:0:1:2:127.0.0.1";
    char err5[] = ":255.0.0.0,";
    char err6[] = "255.0.0.0,";
    char err7[] = "dead:beef:feed:ded:0:1:2:3:4:5,";

    TEST(scan_ip6_address((s = err0, &s)), -1);
    TEST(scan_ip6_address((s = err1, &s)), -1);
    TEST(scan_ip6_address((s = err2, &s)), -1);
    TEST(scan_ip6_address((s = err3, &s)), -1);
    TEST(scan_ip6_address((s = err4, &s)), -1);
    TEST(scan_ip6_address((s = err5, &s)), -1);
    TEST(scan_ip6_address((s = err6, &s)), -1);
    TEST(scan_ip6_address((s = err7, &s)), -1);
  }

  {
    char err0[] = "[dead:beef:feed::0ded::0000:0001:0002:0003]:";
    char err1[] = "[::dead:beef:feed::0ded:0000:0001:0002:0003]+";
    char err2[] = "[dead:beef:feed:ded:0:1:2:3:4]+";
    char err3[] = "[dead:beef:feed:00ded:0:1:2:3]+";
    char err4[] = "[dead:beef:feed:ded:0:1:2:127.0.0.1]";
    char err5[] = "[:255.0.0.0],";
    char err6[] = "[255.0.0.0],";
    char err7[] = "[dead:beef:feed:ded:0:1:2:]";

    TEST(scan_ip6_reference((s = err0, &s)), -1);
    TEST(scan_ip6_reference((s = err1, &s)), -1);
    TEST(scan_ip6_reference((s = err2, &s)), -1);
    TEST(scan_ip6_reference((s = err3, &s)), -1);
    TEST(scan_ip6_reference((s = err4, &s)), -1);
    TEST(scan_ip6_reference((s = err5, &s)), -1);
    TEST(scan_ip6_reference((s = err6, &s)), -1);
    TEST(scan_ip6_reference((s = err7, &s)), -1);
  }

  END();
}

#define TEST_SCAN(scanner, input, canonic, output)			\
  do { char s0[] = input; char *s = s0;					\
245 246 247 248
    size_t n = sizeof(input) - sizeof(output);				\
    TEST_SIZE(scanner(&s), n);						\
    TEST_S(s, output);							\
    TEST_S(s0, canonic); } while(0)
249

250 251
#include <sofia-sip/hostdomain.h>

252 253 254 255
int host_test(void)
{
  BEGIN();

256 257 258 259 260 261 262 263
  TEST(host_is_ip4_address(NULL), 0);
  TEST(host_is_ip6_address(NULL), 0);
  TEST(host_ip6_reference(NULL), 0);
  TEST(host_is_ip_address(NULL), 0);
  TEST(host_is_domain(NULL), 0);
  TEST(host_is_valid(NULL), 0);
  TEST(host_has_domain_invalid(NULL), 0);

264 265 266 267 268 269 270 271 272 273
  TEST_SIZE(span_host("rama"), 4);
  TEST_SIZE(span_host("ra-ma.1-2.3-4.a4-9."), 19);
  TEST_SIZE(span_host("a.1.b"), 5);
  TEST_SIZE(span_host("127.255.249.000.a,"), 17);
  TEST_SIZE(span_host("127.255.249.000,"), 15);
  TEST_SIZE(span_host("0.00.000.000:,"), 12);
  TEST_SIZE(span_host("127.255.249.000,"), 15);
  TEST_SIZE(span_host("[dead:beef:feed:ded:0:1:2:3]:1"), 28);
  TEST_SIZE(span_host("[dead:beef:feed:ded::1:2:3]:1"), 27);
  TEST_SIZE(span_host("[::127.0.0.1]:1"), 13);
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288

  TEST_SCAN(scan_host, "rama", "rama", "");
  TEST_SCAN(scan_host, "rama.", "rama.", "");
  TEST_SCAN(scan_ip4_address, "127.255.249.000,", "127.255.249.0", ",");
  TEST_SCAN(scan_host, "127.255.249.000,", "127.255.249.0", ",");
  TEST_SCAN(scan_host, "a.1.b.", "a.1.b", "");
  TEST_SCAN(scan_host, "ra-ma.1-2.3-4.a4-9.", "ra-ma.1-2.3-4.a4-9", "");
  TEST_SCAN(scan_host, "127.255.249.000.a,", "127.255.249.000.a,", ",");
  TEST_SCAN(scan_host, "0.00.000.000:,", "0.0.0.0", ":,");
  TEST_SCAN(scan_host, "127.255.249.000,", "127.255.249.0", ",");
  TEST_SCAN(scan_host, "[dead:beef:feed:ded:0:1:2:3]:1", 
                       "[dead:beef:feed:ded::1:2:3]", ":1");
  TEST_SCAN(scan_host, "[::127.0.0.1]:1", "[::127.0.0.1]:1", ":1");

  /* Test error detection */
289 290 291 292 293 294 295 296 297
  TEST_SIZE(span_host("256.00.000.000:,"), 0);
  TEST_SIZE(span_host("255.00.000.0000,"), 0);
  TEST_SIZE(span_host("255.00.000.199."), 0);
  TEST_SIZE(span_host("[127.0.0.1]:1"), 0);
  TEST_SIZE(span_domain("rama.1"), 0);
  TEST_SIZE(span_domain("-ma.1-2.3-4.a4-9."), 0);
  TEST_SIZE(span_domain("a..b"), 0);
  TEST_SIZE(span_domain("a.b.-"), 0);
  TEST_SIZE(span_domain("a.b-"), 0);
298

299 300
  TEST(host_is_local("126.0.0.0"), 0);
  TEST(host_is_local("127.0.0.0"), 1);
301
  TEST(host_is_local("127.0.0.2"), 1);
302 303 304 305 306 307 308 309 310
  TEST(host_is_local("0.0.0.0"), 0);
  TEST(host_is_local("::1"), 1);
  TEST(host_is_local("::1"), 1);
  TEST(host_is_local("::1:3fff"), 0);
  TEST(host_is_local("[::1]"), 1);
  TEST(host_is_local("localdomain.domain.org"), 0);
  TEST(host_is_local("localhost.domain.org"), 0);
  TEST(host_is_local("localhost"), 1);
  TEST(host_is_local("localhost.localdomain"), 1);
311 312 313
  TEST(host_is_local("localhost."), 1);
  TEST(host_is_local("localhost.localdomain."), 1);
  TEST(host_is_local("localhost.localdomain.org"), 0);
314

315 316 317 318 319 320 321 322
  TEST(host_has_domain_invalid("invalid"), 1);
  TEST(host_has_domain_invalid("invalid."), 1);
  TEST(host_has_domain_invalid("1.invalid"), 1);
  TEST(host_has_domain_invalid("1.invalid."), 1);
  TEST(host_has_domain_invalid("1invalid"), 0);
  TEST(host_has_domain_invalid("valid."), 0);
  TEST(host_has_domain_invalid("1-.invalid."), 0);

Pekka Pessi's avatar
Pekka Pessi committed
323
  /* Invalid IP4 addresses (extra leading zeros) */
324
  TEST_1(!host_cmp("127.0.0.1", "127.0.0.01"));
Pekka Pessi's avatar
Pekka Pessi committed
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
  TEST_1(!host_cmp("[ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255]", 
		  "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
  /* Invalid reference (extra leading zeros) */
  TEST_1(host_cmp("[0ffff:0ffff:0ffff:0ffff:0ffff:0ffff:255.255.255.255]", 
		  "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
  TEST_1(!host_cmp("::1", "::001"));
  TEST_1(!host_cmp("[::1]", "::1"));
  TEST_1(!host_cmp("[::1]", "::0.0.0.1"));
  TEST_1(!host_cmp("::ffff:127.0.0.1", "127.0.0.1"));
  TEST_1(!host_cmp("::ffff:127.0.0.1", "::ffff:7f00:1"));
  TEST_1(!host_cmp("::ffff:127.0.0.1", "::ffff:7f00:1"));
  TEST_1(!host_cmp("[::ffff:127.0.0.1]", "[::7f00:1]"));
  TEST_1(host_cmp("::", "0.0.0.0"));
  TEST_1(host_cmp("::1", "0.0.0.1"));

340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
  END();
}

void usage(void)
{
  fprintf(stderr, "usage: %s [-v]\n", name);
}

int main(int argc, char *argv[])
{
  int retval = 0;
  int i;

  for (i = 1; argv[i]; i++) {
    if (strcmp(argv[i], "-v") == 0)
      test_flags |= tst_verbatim;
    else
      usage();
  }

  retval |= bnf_test(); fflush(stdout);
  retval |= ip_test(); fflush(stdout);
  retval |= host_test(); fflush(stdout);

  return retval;
}