HomePort
header_parser_test.c
Go to the documentation of this file.
1 /*
2  * Copyright 2011 Aalborg University. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, are
5  * permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this list of
8  * conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11  * of conditions and the following disclaimer in the documentation and/or other materials
12  * provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY Aalborg University ''AS IS'' AND ANY EXPRESS OR IMPLIED
15  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Aalborg University OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  * The views and conclusions contained in the software and documentation are those of the
25  * authors and should not be interpreted as representing official policies, either expressed
26  */
27 
28 #include "unit_test.h"
29 #include "header_parser.c"
30 #include <stdio.h>
31 
32 struct data {
33  char *field;
34  char *value;
35  int count;
36  int errors;
37 };
38 
39 void on_field_value(void * _data,
40  const char* field, size_t field_length,
41  const char* value, size_t value_length)
42 {
43  int _errors = 0;
44  struct data *data = _data;
45  int i;
46  char *expect_field = data->field;
47  char *expect_value = data->value;
48  for (i = 0; i < data->count; i++) {
49  expect_field = &expect_field[strlen(expect_field)+1];
50  expect_value = &expect_value[strlen(expect_value)+1];
51  }
52  ASSERT_EQUAL(strncmp(field, expect_field, field_length), 0);
53  ASSERT_EQUAL(strncmp(value, expect_value, value_length), 0);
54  data->count++;
55  data->errors += _errors;
56 }
57 
58 TEST_START("header_parser.c")
59 
62 
63 TEST(Header parser test)
64 
65  struct data data;
66  data.field = "cat\0dog";
67  data.value = "yes\0yes, this is dog";
68  data.count = 0;
69  data.errors = 0;
70  settings.data = &data;
71 
72  struct hp *hp = hp_create(&settings);
73 
74  hp_on_header_field(hp, "cat",3);
75  hp_on_header_value(hp, "yes",3);
76 
77  hp_on_header_field(hp, "dog",3);
78  hp_on_header_value(hp, "yes, thi",8);
79  hp_on_header_value(hp, "s is dog",8);
80 
82 
83  hp_destroy(hp);
84 
85  _errors += data.errors;
86 
87  ASSERT_EQUAL(data.count, 2);
88 
89 TSET()
90 
91 TEST_END()
hp_destroy(hp)
char * value
data field
char * field
data value
#define HP_SETTINGS_DEFAULT
hp_on_header_value(hp,"yes", 3)
data errors
struct hp_settings settings
data count
TEST(CASE, sendf)
hpd_error_t hp_create(struct hp **instance, struct hp_settings *settings, const hpd_module_t *context)
settings on_field_value_pair
void on_field_value(void *_data, const char *field, size_t field_length, const char *value, size_t value_length)
hp_on_header_field(hp,"cat", 3)
ASSERT_EQUAL(data.count, 2)
hp_on_header_complete(hp)