test_class.c 10.4 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
 * 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
 *
 */

25 26
/**@ingroup test_msg
 * @file test_class.c
Pekka Pessi's avatar
Pekka Pessi committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
 * 
 * Message class for testing parser and transports.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * @date Created: Tue Mar  5 11:57:20 2002 ppessi
 */

#include "config.h"

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

#define TAG_NAMESPACE "tst"

45
#include <test_class.h>
46 47
#include <sofia-sip/msg_parser.h>
#include <sofia-sip/msg_mclass.h>
48
#include <test_protos.h>
49
#include <sofia-sip/msg_addr.h>
Pekka Pessi's avatar
Pekka Pessi committed
50 51 52 53 54 55 56 57

extern msg_mclass_t const msg_test_mclass[1];

extern msg_mclass_t const *msg_test_default(void)
{
  return msg_test_mclass;
}

58 59
#define msg_generic_update NULL

60 61
/**@ingroup test_msg
 * @defgroup msg_test_request Request Line for Testing
Pekka Pessi's avatar
Pekka Pessi committed
62 63 64 65 66 67 68
 */

static msg_xtra_f msg_request_dup_xtra;
static msg_dup_f msg_request_dup_one;

msg_hclass_t msg_request_class[] =
MSG_HEADER_CLASS(msg_, request, NULL, "", rq_common, 
69
		 single_critical, msg_request, msg_generic);
Pekka Pessi's avatar
Pekka Pessi committed
70 71

/** Decode a request line */
72
issize_t msg_request_d(su_home_t *home, msg_header_t *h, char *s, isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
73
{
74
  msg_request_t *rq = (msg_request_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
75 76 77 78 79 80 81 82 83 84 85 86 87
  char *uri, *version;

  if (msg_firstline_d(s, &uri, &version) < 0 || !uri ||
      url_d(rq->rq_url, uri) < 0)
    return -1;

  rq->rq_method_name = s;
  rq->rq_version = version;

  return 0;
}

/**Encode a request line. */
88
issize_t msg_request_e(char b[], isize_t bsiz, msg_header_t const *h, int flags)
Pekka Pessi's avatar
Pekka Pessi committed
89
{
90
  msg_request_t const *rq = (msg_request_t const *)h;
Pekka Pessi's avatar
Pekka Pessi committed
91 92 93 94 95 96 97

  return snprintf(b, bsiz, "%s " URL_FORMAT_STRING " %s" CRLF,
		  rq->rq_method_name,
		  URL_PRINT_ARGS(rq->rq_url),
		  rq->rq_version);
}

98
isize_t msg_request_dup_xtra(msg_header_t const *h, isize_t offset)
Pekka Pessi's avatar
Pekka Pessi committed
99
{
100
  isize_t rv = offset;
101
  msg_request_t const *rq = (msg_request_t const *)h;
Pekka Pessi's avatar
Pekka Pessi committed
102 103 104 105 106 107 108 109 110 111

  rv += url_xtra(rq->rq_url);
  rv += MSG_STRING_SIZE(rq->rq_method_name);
  rv += MSG_STRING_SIZE(rq->rq_version);

  return rv;
}

/** Duplicate one request header. */
char *msg_request_dup_one(msg_header_t *dst, msg_header_t const *src,
112
			  char *b, isize_t xtra)
Pekka Pessi's avatar
Pekka Pessi committed
113
{
114 115
  msg_request_t *rq = (msg_request_t *)dst;
  msg_request_t const *o = (msg_request_t const *)src;
Pekka Pessi's avatar
Pekka Pessi committed
116 117 118 119 120 121 122 123 124 125 126 127
  char *end = b + xtra;

  URL_DUP(b, end, rq->rq_url, o->rq_url);

  MSG_STRING_DUP(b, rq->rq_method_name, o->rq_method_name);
  MSG_STRING_DUP(b, rq->rq_version, o->rq_version);

  assert(b <= end);

  return b;
}

128 129
/**@ingroup test_msg
 * @defgroup msg_test_status Status Line for Testing
Pekka Pessi's avatar
Pekka Pessi committed
130 131 132 133 134 135 136
 */

static msg_xtra_f msg_status_dup_xtra;
static msg_dup_f msg_status_dup_one;

msg_hclass_t msg_status_class[1] =
MSG_HEADER_CLASS(msg_, status, NULL, "", st_common, 
137
		 single_critical, msg_status, msg_generic);
Pekka Pessi's avatar
Pekka Pessi committed
138 139

/** Parse status line */
140
issize_t msg_status_d(su_home_t *home, msg_header_t *h, char *s, isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
141
{
142
  msg_status_t *st = (msg_status_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156
  char *status, *phrase;
  unsigned long code;

  if (msg_firstline_d(s, &status, &phrase) < 0 ||
      (code = strtoul(status, &status, 10)) >= 1000 || *status)
    return -1;

  st->st_status = code;
  st->st_phrase = phrase;
  st->st_version = s;

  return 0;
}

157
issize_t msg_status_e(char b[], isize_t bsiz, msg_header_t const *h, int flags)
Pekka Pessi's avatar
Pekka Pessi committed
158
{
159 160
  msg_status_t const *st = (msg_status_t const *)h;
  int status = st->st_status;
Pekka Pessi's avatar
Pekka Pessi committed
161 162 163 164

  if (status > 999 || status < 100)
    status = 0;

165 166
  return snprintf(b, bsiz, "%s %03u %s" CRLF, 
		  st->st_version, status, st->st_phrase);
Pekka Pessi's avatar
Pekka Pessi committed
167 168 169
}

/** Extra size of a msg_status_t object. */
170
isize_t msg_status_dup_xtra(msg_header_t const *h, isize_t offset)
Pekka Pessi's avatar
Pekka Pessi committed
171
{
172
  isize_t rv = offset;
173
  msg_status_t const *st = (msg_status_t const *)h;
Pekka Pessi's avatar
Pekka Pessi committed
174 175 176 177 178 179 180
  rv += MSG_STRING_SIZE(st->st_version);
  rv += MSG_STRING_SIZE(st->st_phrase);
  return rv;
}

/** Duplicate one status header. */
char *msg_status_dup_one(msg_header_t *dst, msg_header_t const *src,
181
			 char *b, isize_t xtra)
Pekka Pessi's avatar
Pekka Pessi committed
182
{
183 184
  msg_status_t *st = (msg_status_t *)dst;
  msg_status_t const *o = (msg_status_t const *)src;
Pekka Pessi's avatar
Pekka Pessi committed
185 186 187 188 189 190
  char *end = b + xtra;

  MSG_STRING_DUP(b, st->st_version, o->st_version);
  st->st_status = o->st_status;
  MSG_STRING_DUP(b, st->st_phrase, o->st_phrase);

191
  assert(b <= end); (void)end;
Pekka Pessi's avatar
Pekka Pessi committed
192 193 194 195 196 197

  return b;
}

/** Extract the message body, including separator line. 
 *
198 199 200 201 202
 * @param[in,out] msg  message object 
 * @param[in,out] pub  public message structure 
 * @param[in]     b    buffer containing unparsed data 
 * @param[in]     bsiz buffer size 
 * @param[in]     eos  true if buffer contains whole message
Pekka Pessi's avatar
Pekka Pessi committed
203
 *
204 205 206
 * @retval -1     error
 * @retval 0      message is incomplete
 * @retval other  number of bytes extracted
Pekka Pessi's avatar
Pekka Pessi committed
207
 */
208 209
issize_t msg_test_extract_body(msg_t *msg, msg_pub_t *pub, 
			       char b[], isize_t bsiz, int eos)
Pekka Pessi's avatar
Pekka Pessi committed
210
{
211
  msg_test_t *tst = (msg_test_t *)pub;
212 213
  ssize_t m = 0;
  size_t body_len;
Pekka Pessi's avatar
Pekka Pessi committed
214 215 216

  if (!(tst->msg_flags & MSG_FLG_BODY)) {
    /* We are looking at a potential empty line */
217
    m = msg_extract_separator(msg, (msg_pub_t *)tst, b, bsiz, eos);
Pekka Pessi's avatar
Pekka Pessi committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
    if (m == 0 || m == -1)
      return m;
    tst->msg_flags |= MSG_FLG_BODY;
    b += m;
    bsiz -= m;
  }

  if (tst->msg_content_length)
    body_len = tst->msg_content_length->l_length;
  else if (MSG_IS_MAILBOX(tst->msg_flags)) /* message fragments */
    body_len = 0;
  else if (eos)
    body_len = bsiz;
  else
    return -1;

  if (body_len == 0) {
    tst->msg_flags |= MSG_FLG_COMPLETE;
    return m;
  }

  if (m)
    return m;

242
  if ((m = msg_extract_payload(msg, (msg_pub_t *)tst, NULL, body_len, b, bsiz, eos) ) == -1)
Pekka Pessi's avatar
Pekka Pessi committed
243 244 245 246 247 248 249 250 251 252 253
    return -1;
  
  tst->msg_flags |= MSG_FLG_FRAGS;
  if (bsiz >= body_len)
    tst->msg_flags |= MSG_FLG_COMPLETE;
  return m;
}

msg_href_t const msg_content_length_href[1] = 
  {{
    msg_content_length_class,
254
    offsetof(msg_test_t, msg_content_length)
Pekka Pessi's avatar
Pekka Pessi committed
255 256
  }};

257 258 259 260
#include <sofia-sip/su_tag_class.h>
#include <sofia-sip/su_tag_inline.h>
#include <sofia-sip/su_tagarg.h>
#include <sofia-sip/msg_tag_class.h>
Pekka Pessi's avatar
Pekka Pessi committed
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280

tagi_t *tsttag_filter(tagi_t *dst,
		      tagi_t const f[],
		      tagi_t const *src, 
		      void **bb);

/** Tag class for test header tags. @HIDE */
tag_class_t tsthdrtag_class[1] = 
  {{
    sizeof(tsthdrtag_class),
    /* tc_next */     NULL,
    /* tc_len */      NULL,
    /* tc_move */     NULL,
    /* tc_xtra */     msghdrtag_xtra,
    /* tc_dup */      msghdrtag_dup,
    /* tc_free */     NULL,
    /* tc_find */     NULL,
    /* tc_snprintf */ msghdrtag_snprintf,
    /* tc_filter */   tsttag_filter,
    /* tc_ref_set */  t_ptr_ref_set,
281
    /* tc_scan */     msghdrtag_scan,
Pekka Pessi's avatar
Pekka Pessi committed
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
  }};

/** Tag class for TST header string tags. @HIDE */
tag_class_t tststrtag_class[1] = 
  {{
    sizeof(tststrtag_class),
    /* tc_next */     NULL,
    /* tc_len */      NULL,
    /* tc_move */     NULL,
    /* tc_xtra */     t_str_xtra,
    /* tc_dup */      t_str_dup,
    /* tc_free */     NULL,
    /* tc_find */     NULL,
    /* tc_snprintf */ t_str_snprintf,
    /* tc_filter */   NULL /* msgtag_str_filter */,
    /* tc_ref_set */  t_ptr_ref_set,
298
    /* tc_scan */     t_str_scan
Pekka Pessi's avatar
Pekka Pessi committed
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 335 336 337 338 339
  }};

/** Tag class for TST message tags. @HIDE */
tag_class_t tstmsgtag_class[1] = 
  {{
    sizeof(tstmsgtag_class),
    /* tc_next */     NULL,
    /* tc_len */      NULL,
    /* tc_move */     NULL,
    /* tc_xtra */     msgobjtag_xtra,
    /* tc_dup */      msgobjtag_dup,
    /* tc_free */     NULL,
    /* tc_find */     NULL,
    /* tc_snprintf */ msgobjtag_snprintf,
    /* tc_filter */   NULL /* tsttag_tst_filter */,
    /* tc_ref_set */  t_ptr_ref_set,
  }};

tag_typedef_t tsttag_header = 
	{{ TAG_NAMESPACE, "header", tsthdrtag_class, 0 }};

tag_typedef_t tsttag_header_str = STRTAG_TYPEDEF(header_str);

/** Filter a TST header structure. */
tagi_t *tsttag_filter(tagi_t *dst,
		      tagi_t const f[],
		      tagi_t const *src, 
		      void **bb)
{
  tagi_t stub[2] = {{ NULL }};
  tag_type_t sctt, tt = f->t_tag;
  msg_hclass_t *hc = (msg_hclass_t *)tt->tt_magic;

  assert(src);

  sctt = src->t_tag;

  if (sctt && sctt->tt_class == tstmsgtag_class) {
    msg_test_t const *tst = (msg_test_t const *)src->t_value;
    msg_mclass_t *mc = (msg_mclass_t *)tst->msg_ident;
    msg_header_t const **hh = (msg_header_t const **)
340
      msg_hclass_offset(mc, (msg_pub_t *)tst, hc);
Pekka Pessi's avatar
Pekka Pessi committed
341 342 343
    msg_header_t const *h;

    if (tst == NULL || 
344 345
	(char *)hh >= ((char *)tst + tst->msg_size) ||
	(char *)hh < (char const *)&tst->msg_request)
Pekka Pessi's avatar
Pekka Pessi committed
346 347 348 349 350 351 352 353
      return dst;

    h = *hh;

    if (h == NULL)
      return dst;

    stub[0].t_tag = tt;
354
    stub[0].t_value = (tag_value_t)h;
Pekka Pessi's avatar
Pekka Pessi committed
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
    src = stub; sctt = tt;
  }

  if (tt != sctt)
    return dst;

  if (!src->t_value)
    return dst;
  else if (dst) {
    return t_dup(dst, src, bb);
  }
  else {
    *bb = (char *)*bb + t_xtra(src, (size_t)*bb);
    return dst + 1;
  }
}

/** Add duplicates of headers from taglist to the TST message. */
int tst_add_tl(msg_t *msg, msg_test_t *tst,
	       tag_type_t tag, tag_value_t value, ...)
{
  tagi_t const *t;
  ta_list ta;

  ta_start(ta, tag, value);

  for (t = ta_args(ta); t; t = tl_next(t)) {
    if (!(tag = t->t_tag) || !(value = t->t_value))
      continue;

    if (TSTTAG_P(tag)) {
      msg_hclass_t *hc = (msg_hclass_t *)tag->tt_magic;
      msg_header_t *h = (msg_header_t *)value, **hh;

      if (h == NULL)
	;
      else if (h == MSG_HEADER_NONE) {	/* Remove header */
392
	hh = msg_hclass_offset(msg_mclass(msg), (msg_pub_t *)tst, hc);
Pekka Pessi's avatar
Pekka Pessi committed
393
	while (hh && *hh)
394 395
	  msg_header_remove(msg, (msg_pub_t *)tst, *hh);
      } else if (msg_header_add_dup_as(msg, (msg_pub_t *)tst, hc, h) < 0)
Pekka Pessi's avatar
Pekka Pessi committed
396 397 398 399 400
	break;
    }
    else if (TSTTAG_STR_P(tag)) {
      msg_hclass_t *hc = (msg_hclass_t *)tag->tt_magic;
      char const *s = (char const *)value;
401
      if (s && msg_header_add_make(msg, (msg_pub_t *)tst, hc, s) < 0)
Pekka Pessi's avatar
Pekka Pessi committed
402 403 404
	break;
    }
    else if (tag == tsttag_header_str) {
405
      if (msg_header_add_str(msg, (msg_pub_t *)tst, (char const *)value) < 0)
Pekka Pessi's avatar
Pekka Pessi committed
406 407 408 409 410 411 412 413
	break;
    }
  }

  ta_end(ta);

  return t ? -1 : 0;
}