summaryrefslogtreecommitdiff
path: root/test.c
blob: 792316ded62c5c2891d1035d8f6e74c9cd089350 (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
#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"
  "\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, 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_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, 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);
  }
}

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

  test_basic();
  test_percent();

  return EXIT_SUCCESS;
}