test_sdp.c 2.98 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
 * 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
 *
 */

/**@internal
 *
27
 * @CFILE test_sdp.c
Pekka Pessi's avatar
Pekka Pessi committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
 *
 * Simple SDP tester
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * @date Created      : Fri Feb 18 10:25:08 2000 ppessi
 */

#include "config.h"

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

43
#include "sofia-sip/sdp.h"
Pekka Pessi's avatar
Pekka Pessi committed
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

int diff(const char *olds, const char *news, int *linep, int *pos)
{
  const char *o, *n;

  *linep = 0;

  for (o = olds, n = news; *o && *n && *o == *n ; o++, n++) {
    if (*o == '\n') ++*linep;
  }

  *pos = o - olds;

  return *o != *n;
}

int main(int argc, char *argv[])
{
  char buffer[2048];
  int  n;
  su_home_t *home = su_home_create();
  int exitcode = 1; 
  FILE *f;

  if (argv[1] && strcmp(argv[1], "-"))
    f = fopen(argv[1], "rb");
  else
    f = stdin;

  n = fread(buffer, 1, sizeof(buffer) - 1, f);
  buffer[n] = 0;

  if (feof(f)) {
    sdp_parser_t *p = sdp_parse(home, buffer, n + 1, 0);

    su_home_check(home);
    su_home_check((su_home_t *)p);

    if (sdp_session(p)) {
      sdp_session_t *sdp, *dup;
      sdp_printer_t *printer;

      sdp = sdp_session(p);
      dup = sdp_session_dup(home, sdp);
      su_home_check(home);

      sdp_parser_free(p), p = NULL;
      su_home_check(home);

      printer = sdp_print(home, dup, NULL, 0, 0);
      su_home_check(home);

      if (sdp_message(printer)) {
	int line, pos;

	if (diff(buffer, sdp_message(printer), &line, &pos)) {
100
	  fprintf(stdout, "test_sdp:%d: messages differ:\n", line);
Pekka Pessi's avatar
Pekka Pessi committed
101 102 103 104 105 106 107 108 109
	  fputs(buffer, stdout);
	  fprintf(stdout, ">>>new message:\n");
	  fputs(sdp_message(printer), stdout);
	}
	else {
	  exitcode = 0;
	}
      }
      else {
110
	fprintf(stderr, "test_sdp: %s\n", sdp_printing_error(printer));
Pekka Pessi's avatar
Pekka Pessi committed
111 112 113 114 115 116
      }
      sdp_printer_free(printer);

      su_home_check(home);
    }
    else {
117
      fprintf(stderr, "test_sdp: %s\n", sdp_parsing_error(p));
Pekka Pessi's avatar
Pekka Pessi committed
118 119 120 121 122 123
      sdp_parser_free(p);
      exit(1);
    }
  }
  else {
    if (ferror(f)) {
124
      perror("test_sdp");
Pekka Pessi's avatar
Pekka Pessi committed
125 126
    }
    else {
127
      fprintf(stderr, "test_sdp: maximum length of sdp messages is %u bytes\n", 
Pekka Pessi's avatar
Pekka Pessi committed
128 129 130 131 132
	      (unsigned)sizeof(buffer));
    }
    exit(1);
  }

133 134
  su_home_unref(home);

Pekka Pessi's avatar
Pekka Pessi committed
135 136
  return exitcode;
}