summaryrefslogtreecommitdiff
path: root/test.c
blob: 280eab08e47d204ad6bc154cc8924a9097dcea2c (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
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
245
246
247
248
249
250
251
252
#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"
  "Content-Type: application/x-www-form-urlencoded\r\n"
  "Connection: close\r\n"
  "Content-Length: 15\r\n"
  "\r\n"
  "hello test test";

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

  UNUSED(ctx);
  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_ctx_t ctx;

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

  // parse data
  size_t len = strlen(basic_str);
  if ((err = fhp_ctx_push(&ctx, (uint8_t*) basic_str, len)) != FHP_OK) {
    die("test_basic", "fhp_ctx_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_ctx_t *ctx,
  fhp_token_t token,
  uint8_t * const buf,
  size_t len
) {
  percent_data *data = fhp_ctx_get_user_data(ctx);

  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_ctx_t ctx;
  percent_data data;

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

  // parse data
  size_t len = strlen(percent_str);
  if ((err = fhp_ctx_push(&ctx, (uint8_t*) percent_str, len)) != FHP_OK) {
    die("test_percent", "fhp_ctx_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);
  }
}

/******************/
/* cl parser test */
/******************/

static char *
cl_parser_tests[] = {
  " 12345 ",
  "100",
  "00120",
  "123123123123",
  "18446744073709551615", // UINT64_MAX
  // "18446744073709551616", // UINT64_MAX + 1
  // "184--46744073709551615", // test embedded garbage
  // "18446744073709551615  abc ", // test trailing garbage
  NULL
};

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

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

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

    // init parser
    fhp_cl_parser_t p;
    fhp_cl_parser_init(&p);

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

    // finish parsing
    uint64_t val;
    if ((err = fhp_cl_parser_done(&p, &val)) != FHP_OK) {
      die("test_cl_parser", "fhp_cl_parser_push", err);
    }

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

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

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

  return EXIT_SUCCESS;
}