summaryrefslogtreecommitdiff
path: root/test.c
blob: 42e6f780fb53ed5f30c6043ff976c656b50b9857 (plain)
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
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <fhp/fhp.h>

#define UNUSED(a) ((void) (a))

static void die(
  char *test,
  char *fn,
  fhp_err_t err
) {
  char buf[1024];

  if (fhp_strerror(err, buf, sizeof(buf)) == FHP_OK) {
    fprintf(stderr, "ERROR: %s(): %s(): %s\n", test, fn, buf);
  } else {
    fprintf(stderr, "ERROR: die(): fhp_strerror() failed\n");
  }

  /* exit app */
  exit(EXIT_FAILURE);
}

/**************/
/* basic test */
/**************/

static const char *
basic_str =
  "GET / HTTP/1.1\r\n"
  "Host: pablotron.org\r\n"
  "Connection: close\r\n"
  "Transfer-Encoding: deflate, chunked\r\n"
  "\r\n";

static bool
basic_cb(
  fhp_t *fhp,
  fhp_token_t token,
  uint8_t * const buf,
  size_t len
) {
  fhp_err_t err;

  UNUSED(fhp);
  UNUSED(buf);

  // get token name
  char token_buf[64];
  if ((err = fhp_strtoken(token, token_buf, sizeof(token_buf))) != FHP_OK)
    return false;

  // debug
  fprintf(stderr, "test_basic_cb(): token = %s, len = %lu\n", token_buf, len);

  // return success
  return true;
}

static void
test_basic(void) {
  fhp_err_t err;
  fhp_t fhp;

  // init parser
  if ((err = fhp_init(&fhp, NULL, basic_cb, NULL)) != FHP_OK) {
    die("test_basic", "fhp_init", err);
  }

  // parse data
  size_t len = strlen(basic_str);
  if ((err = fhp_push(&fhp, (uint8_t*) basic_str, len)) != FHP_OK) {
    die("test_basic", "fhp_push", err);
  }
}

/****************/
/* percent test */
/****************/

typedef struct {
  uint8_t buf[1024];
  size_t buf_len;
} percent_data;

static const char *
percent_str =
  "GET /foo%20bar HTTP/1.1\r\n"
  "Host: pablotron.org\r\n"
  "Connection: close\r\n"
  "\r\n";

static bool
percent_cb(
  fhp_t *fhp,
  fhp_token_t token,
  uint8_t * const buf,
  size_t len
) {
  percent_data *data = fhp_get_user_data(fhp);

  switch (token) {
  case FHP_TOKEN_URL_START:
    // clear buffer
    data->buf_len = 0;

    break;
  case FHP_TOKEN_URL_FRAGMENT:
    // buffer overflow, do not use in real code!!!
    memcpy(data->buf + data->buf_len, buf, len);
    data->buf_len += len;

    break;
  case FHP_TOKEN_URL_END:
    // terminate and print buffer
    data->buf[data->buf_len] = '\0';
    fprintf(stderr, "decoded URL: \"%s\"\n", data->buf);

    break;
  default:
    // do nothing
    NULL;
  }

  // return success
  return true;
}

static void
test_percent(void) {
  fhp_err_t err;
  fhp_t fhp;
  percent_data data;

  // init parser
  if ((err = fhp_init(&fhp, NULL, percent_cb, &data)) != FHP_OK) {
    die("test_percent", "fhp_init", err);
  }

  // parse data
  size_t len = strlen(percent_str);
  if ((err = fhp_push(&fhp, (uint8_t*) percent_str, len)) != FHP_OK) {
    die("test_percent", "fhp_push", err);
  }
}

/******************/
/* te parser test */
/******************/

static char *
te_parser_tests[] = {
  " ",
  "gzip",
  "deflate",
  "gzip,deflate,chunked",
  "gzip, deflate, chunked",
  "deflate , gzip , chunked  ",
  "  deflate , gzip , chunked  ",
  NULL
};

static void
test_te_parser(void) {
  for (size_t i = 0; te_parser_tests[i]; i++) {
    fhp_err_t err;

    // get test string
    char * const s = te_parser_tests[i];

    fprintf(stderr, "te testing: \"%s\"\n", s);

    // init parser
    fhp_te_parser_t p;
    fhp_te_parser_init(&p);

    // parse data
    if ((err = fhp_te_parser_push(&p, (uint8_t*) s, strlen(s))) != FHP_OK) {
      die("test_te_parser", "fhp_te_parser_push", err);
    }

    // finish parsing
    size_t num_tes;
    if ((err = fhp_te_parser_done(&p, &num_tes)) != FHP_OK) {
      die("test_te_parser", "fhp_te_parser_push", err);
    }

    fprintf(stderr, "te test: \"%s\", num_tes = %lu\n", s, num_tes);
  }
}

int main(int argc, char *argv[]) {
  UNUSED(argc);
  UNUSED(argv);

  test_basic();
  test_percent();
  test_te_parser();

  return EXIT_SUCCESS;
}