#include <stdio.h>
#include <string.h>
typedef struct {
const char* host;
const char* method;
const char* path;
const char* status;
const char* mime;
static const char*
rec_get(
const char* field,
void* ud) {
const REC* r = (
const REC*)ud;
if (strcmp(field, "host") == 0)
if (strcmp(field, "method") == 0)
if (strcmp(field, "path") == 0)
if (strcmp(field, "status") == 0)
if (strcmp(field, "mime") == 0)
return NULL;
}
static int run(
const char* expr,
const REC* r) {
char err[160] = "";
int m;
if (!q) {
return -1;
}
return m;
}
static void expect(
const char* label,
int got,
int want) {
if (got != want) {
n_log(
LOG_ERR,
"%s: got %d, expected %d", label, got, want);
}
}
char err[160] = "";
if (q) {
}
}
r.
host =
"api.example.com";
expect(
"eq",
run(
"host = api.example.com", &r), 1);
expect(
"eq ci",
run(
"host = API.EXAMPLE.COM", &r), 1);
expect(
"eq no",
run(
"host = other.test", &r), 0);
expect(
"contains",
run(
"host ~ example", &r), 1);
expect(
"contains path",
run(
"path ~ admin", &r), 1);
expect(
"not contains",
run(
"path !~ secret", &r), 1);
expect(
"ne",
run(
"mime != text/html", &r), 0);
expect(
"range",
run(
"status >= 400 AND status < 500", &r), 1);
expect(
"regex",
run(
"mime =~ ^text/", &r), 1);
expect(
"regex no",
run(
"mime =~ ^json", &r), 0);
expect(
"and",
run(
"method = GET AND status >= 400", &r), 1);
expect(
"or",
run(
"method = POST OR status = 403", &r), 1);
expect(
"not",
run(
"NOT (status = 200)", &r), 1);
expect(
"paren",
run(
"(method = POST OR method = GET) AND status = 403", &r), 1);
expect(
"prec",
run(
"method = POST AND status = 200 OR host ~ example", &r), 1);
expect(
"quoted",
run(
"path = \"/admin/login\"", &r), 1);
expect(
"unknown field",
run(
"bogus = x", &r), 0);
{
static const char* const KNOWN[] = {"host", "status", "mime"};
const size_t nknown = sizeof(KNOWN) / sizeof(KNOWN[0]);
char err[160] = "";
if (q || !strstr(err, "stauts")) {
n_log(
LOG_ERR,
"FAIL: a misspelled field was accepted (err=%s)", err);
} else {
}
err[0] = '\0';
q =
n_query_compile_ex(
"STATUS >= 400 AND host ~ example", KNOWN, nknown, err,
sizeof(err));
if (!q) {
n_log(
LOG_ERR,
"FAIL: a known field was refused (err=%s)", err);
} else {
}
err[0] = '\0';
if (!q) {
n_log(
LOG_ERR,
"FAIL: validation ran without a vocabulary (err=%s)", err);
} else {
n_log(
LOG_INFO,
"PASS: no vocabulary means no field validation");
}
}
return 1;
}
return 0;
}
static int expect(BITMAP *bmp, int x, int y, int r, int g, int b, const char *what)
static const char * rec_get(const char *field, void *ud)
static void expect_parse_error(const char *expr)
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_ERR
error conditions
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL ).
#define LOG_NOTICE
normal but significant condition
#define LOG_INFO
informational
int n_query_eval(const N_QUERY *query, N_QUERY_GET get, void *user_data)
Evaluate a compiled query against one record via get.
N_QUERY * n_query_compile(const char *expr, char *errbuf, size_t errlen)
Compile an expression into a query.
N_QUERY * n_query_compile_ex(const char *expr, const char *const *fields, size_t nfields, char *errbuf, size_t errlen)
Compile expr, rejecting any field not in fields.
void n_query_free(N_QUERY **query)
Free a compiled query and set the pointer to NULL.
Opaque compiled query (see n_query_compile / n_query_eval / n_query_free).
Small boolean query language over caller-named fields.