N_QUERY language regression (headless): operators, AND/OR/NOT, errors.
N_QUERY language regression (headless): operators, AND/OR/NOT, errors.
- Author
- Castagnier Mickael
- Version
- 1.0
#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);
return 1;
}
return 0;
}
static void expect(const char *label, int got, int want)
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
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.
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.